Thursday, February 09, 2017

Serialize and Deserialize Object in java

Sample Data Class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package sample.test;

import java.util.Vector;

public class MyXML implements java.io.Serializable {

 String name;
 String value;
 Vector subtree = new Vector();

 public MyXML() {
 }

 public void insert(MyXML x) {
  subtree.addElement(x);
 }

 static final long serialVersionUID = 1L;
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package sample.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeTest {
 public static void main1(String[] args)
 {
  try
  {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("empInfo.ser"));
      MyXML emp = new MyXML();
   emp.name = "test";
   emp.value = "1";
      //Serialize the object
      oos.writeObject(emp);
      oos.close();
  } catch (Exception e)
  {
      System.out.println(e);
  }

 }

 public static void main(String[] args) {
  try {
   ObjectInputStream ooi = new ObjectInputStream(new FileInputStream(
     "empInfo.ser"));
   // Read the object back
   MyXML readEmpInfo = (MyXML) ooi.readObject();
   System.out.println(readEmpInfo.name);
   System.out.println(readEmpInfo.value);
   ooi.close();
  } catch (Exception e) {
   System.out.println(e);
  }

 }
}

Thursday, June 16, 2016

How to get the referred/called url

I want to find the URL from which my page is called or from which my page is redirected. The example is given below.


Example
@InjectObject(value = "service:tapestry.globals.RequestGlobals")
public abstract RequestGlobals getRequestGlobals();

HttpServletRequest httpServletRequest = requestGlobals.getRequest();
String referrer =httpServletRequest.getHeader("referer");

Test

test

Call SOAP Webservice from javascript

Example


<html>
   <head>
      <title>SOAP Sample</title>
      <script type="text/javascript">
         function soapCall() {
             var xmlhttp = new XMLHttpRequest();
             xmlhttp.open('POST', 'soapurl', true);
             // build SOAP request
             var sr =
                 '<?xml version="1.0" encoding="utf-8"?>' +
                 '<soapenv:Envelope ' +
                     'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                     'xmlns:api="url" ' +
                     'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                     'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                     '<soapenv:Body>' +
                         '<api:parm1>' +
                             '<api:param>2014_DAY_339_2</api:param>' +
                         '</api:parm1>' +
                     '</soapenv:Body>' +
                 '</soapenv:Envelope>';
        
             xmlhttp.onreadystatechange = function () {
                 if (xmlhttp.readyState == 4) {
                     if (xmlhttp.status == 200) {
         alert('--'+xmlhttp.response);
                     }
                 }
             }
             // Send the POST request
             xmlhttp.setRequestHeader('Content-Type', 'text/xml');
             xmlhttp.send(sr);
         }
        
      </script>
   </head>
   <body>
      <script type="text/javascript">
         soapCall();
      </script>
   </body>
   <html>
</html>

Wednesday, July 22, 2015

Send a file as attachment from webservice to client using Axis2 Axioms

WebService
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public String executeWebServiceXML(OMElement element){
  FileDataSource fileDataSource=null;
  DataHandler fileDataHandler=null;
  MessageContext inMessageContext = MessageContext.getCurrentMessageContext();
  OperationContext operationContext = inMessageContext.getOperationContext();
  MessageContext outMessageContext = null;
  try {
    outMessageContext = operationContext
     .getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
  } catch (org.apache.axis2.AxisFault e) {
  }
  String paramCode = element.getFirstChildWithName(new QName("http://mywebservice.webservices.com","paramCode")).getText();
  String paramDate = element.getFirstChildWithName(new QName("http://mywebservice.webservices.com","paramDate")).getText();
  
   String folderPath="D://test":

  String filePath = folderPath +File.separator+ merchantCode+SplCharsConstants.UNDERSCORE+refundDate+ FileExtenConstants.XML;
  fileDataSource = new FileDataSource(filePath);
  fileDataHandler = new DataHandler(fileDataSource);
  String dataHandlerId = outMessageContext.addAttachment(fileDataHandler);
  createDownloadStatisticsElement(dataHandlerId);
  return dataHandlerId;
 }
  
 private OMElement createDownloadStatisticsElement(String dataHandlerId) {
  OMFactory factory = OMAbstractFactory.getOMFactory();
  OMNamespace omNs = factory.createOMNamespace("http://mywebservice.webservices.com", "swa");
  OMElement wrapperElement = factory.createOMElement("getStatsResponse", omNs);
  OMElement dataElement = factory.createOMElement("getElementResponse", omNs, wrapperElement);
  dataHandlerId = "cid:"+dataHandlerId;
  dataElement.addAttribute("href", dataHandlerId,null);
  return wrapperElement;
 }
 
Client
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static void main(String[] args) {
  final String ENDPOINT="http://localhost:8080/webservice/services/myWebService.MyWebServiceHttpSoap11Endpoint/";
  EndpointReference targetEPR = new EndpointReference(ENDPOINT);
  Options options = new Options();
  options.setTo(targetEPR);
  options.setAction("urn:executeWebService");
  options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
  ServiceClient sender=null;
  try {
   sender = new ServiceClient();
   sender.setOptions(options);
   OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
         MessageContext messageContext = new MessageContext();
         SOAPEnvelope env = createEnvelope();
         messageContext.setEnvelope(env);
   mepClient.addMessageContext(messageContext);
   mepClient.execute(true);
   MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
   SOAPBody body = response.getEnvelope().getBody();
   OMElement element1 = body.getFirstElement();
   String dataHandlerId =element1.getFirstElement().getText();
   DataHandler dataHandler= response.getAttachment(dataHandlerId);
         if (dataHandler!=null){
    File xMLFile = new File("c://temp/new.xml");
    FileOutputStream outputStream = new FileOutputStream(xMLFile);
    dataHandler.writeTo(outputStream);
    outputStream.flush();
         }
  } catch (AxisFault e) {
  } catch (FileNotFoundException e) {
  } catch (IOException e) {
  }
 }

 private static SOAPEnvelope createEnvelope() {
  SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
  SOAPEnvelope env = fac.getDefaultEnvelope();
  OMNamespace omNs = fac.createOMNamespace("http://mywebservice.webservices.com","swa");
  OMElement statsElement = fac.createOMElement("executeWebService",omNs);
  OMElement root = fac.createOMElement("root",omNs);
  OMElement nameEle = fac.createOMElement("paramCode", omNs);
  nameEle.setText("test");
  OMElement nameEle1 = fac.createOMElement("paramDate", omNs);
  nameEle1.setText("20150719");
  statsElement.addChild(root);
  root.addChild(nameEle);
  root.addChild(nameEle1);
  env.getBody().addChild(statsElement);
  return env;
 }
}