Sunday, September 6, 2015

Calling Direct API

/** * */
package net.webservicex.www;
import java.rmi.RemoteException;
import org.apache.axiom.om.*;
import org.apache.axis2.client.*;
import org.apache.axis2.addressing.*;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.OperationClient;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.context.MessageContext;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.wsdl.WSDLConstants; /** * @author Bala * */
public class TestWS { /** * @param args * @throws AxisFault */
    public static void main(String[] args) throws AxisFault {

        // TODO Auto-generated method stub
        // String END_POINT = "http://localhost:8080/axis2/services/MyService";

     /*   StockQuoteStub stub = new StockQuoteStub();
        StockQuoteStub.GetQuote getQuote = new StockQuoteStub.GetQuote();
        getQuote.setSymbol("10");
        try {
            StockQuoteStub.GetQuoteResponse resp = stub.getQuote(getQuote);
            System.out.println("result :: " + resp);
        } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); }
            String wsdlDocument = "http://www.webservicex.net/stockquote.asmx?WSDL";
            EndpointReference targetEPR = new EndpointReference(wsdlDocument);
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace("http://www.webserviceX.NET/", "ns1");
            OMElement method = fac.createOMElement("GetQuote", omNs);
            OMElement paramOne = fac.createOMElement("symbol", omNs);
            paramOne.setText("12");
            method.addChild(paramOne);
            ServiceClient client = new ServiceClient();
            Options opts = new Options();
            opts.setTo(targetEPR);
            client.setOptions(opts);
            System.out.println("req " + method);
            OMElement res = client.sendReceive(method);
            String result = res.getFirstElement().getText();
            System.out.println("Result: " + result);*/


            QName ANON_OUT_IN_OP = new QName("http://ws.apache.org/namespaces/axis2", "anonOutInOp", "axis2");
            EndpointReference targetEPR = new EndpointReference("http://www.webservicex.net/stockquote.asmx?WSDL");
            SOAPFactory soapFac = OMAbstractFactory.getSOAP12Factory();
            ServiceClient sender = new ServiceClient();
            Options options = new Options();
            OMNamespace omNs = soapFac.createOMNamespace("http://www.webserviceX.NET/", "ns1");
            OMElement method = soapFac.createOMElement("GetQuote", omNs);
            OMElement value = soapFac.createOMElement("symbol", omNs);
            value.addChild(soapFac.createOMText(value, "10"));
            method.addChild(value);
            System.out.println("WEBSERVICE METHOD PARAMETER - " + method);
            options.setTo(targetEPR);
            sender.setOptions(options);
            OMElement result = sendSOAPReceive(ANON_OUT_IN_OP, method, sender, soapFac);
            System.out.println("WEBSERVICE RESULT - " + result);
        }
        public static OMElement sendSOAPReceive(QName operationQName, OMElement xmlPayload, ServiceClient service, SOAPFactory soapFac) throws AxisFault {
            MessageContext messageContext = new MessageContext();
            SOAPEnvelope envelope = soapFac.getDefaultEnvelope();
            if (xmlPayload != null) {
                envelope.getBody().addChild(xmlPayload);
            }
            messageContext.setEnvelope(envelope);
            System.out.println("SOAP REQUEST MESSAGE - " + messageContext.getEnvelope());
            OperationClient operationClient = service.createClient(operationQName);
            operationClient.addMessageContext(messageContext);
            operationClient.execute(true);
            MessageContext response = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
            return response.getEnvelope().getBody().getFirstElement();
        }
    }

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...