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;
 }
}

Send a File from server to client using Axis2 Web Service

Web Service
1
2
3
4
5
6
7
8
9
public DataHandler executeWebService(String param1,String param2){
  DataHandler actualDH=null;
  try {
    actualDH = new DataHandler(new URL("file:///D:\\test\\NEW.xml"));
  } catch (MalformedURLException e) {
  }
  return actualDH;
  
 }
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 public static void main(String args[]) throws java.lang.Exception {
             RPCServiceClient serviceClient = null;;
         try
         {
             serviceClient = new RPCServiceClient();
         }
         catch (AxisFault e1)
         {
             e1.printStackTrace();
         }

         Options options = serviceClient.getOptions();
         EndpointReference targetEPR = new EndpointReference(
            "http://localhost:8080/webservice/services/myWebService.MyWebServiceHttpSoap11Endpoint/");
         options.setProperty(Constants.Configuration.ENABLE_MTOM,
                 Constants.VALUE_TRUE);
         options.setAction("urn:executeWebService");
         options.setTo(targetEPR);
         QName opName = new QName("http://my.webservices.com", "executeWebService");
         Object[] opSetArgs = new Object[0];
          Class[] returnTypes = new Class[] { DataHandler.class };

          try
          {
              Object[] response = serviceClient.invokeBlocking(opName, opSetArgs,
                      returnTypes);
              DataHandler actualDH;
              actualDH = (DataHandler) response[0];// get the binary data
              System.out.println(actualDH);

              //Received file will be available in this location.
              File graphFile = new File("D://test//NEW1.xml");
              if (!graphFile.exists())
              {
                  try
                  {
                      graphFile.createNewFile();
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
                  FileOutputStream outputStream = null;
                  try
                  {
                      outputStream = new FileOutputStream(graphFile);
                  }
                  catch (FileNotFoundException e)
                  {
                      e.printStackTrace();
                  }
                  try
                  {
                      actualDH.writeTo(outputStream);
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
           
          } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } 
             }

Tuesday, April 28, 2015

Set page dynamically in tapestry using IPage

Below is sample on how to set page dynamically in tapestry 4.1 using ipage

public IPage doCallMethodSave(IRequestCycle cycle) {
IPage ipage = null;
ipage = cycle.getPage("pageName");
return ipage;
}

Thursday, April 16, 2015

Postgres Bring row values of a column comma separated to primary key

Employee Master
id bigint
name text
department_id bigint


Department Master
id bigint
name text

Query
select userid,string_agg(gr.roledescription,',') from employee_master employee
INNER JOIN department_master dept ONemployee.department_id=dept.id