Search in sources :

Example 1 with OperationClient

use of org.apache.axis2.client.OperationClient in project wso2-axis2-transports by wso2.

the class TCPEchoRawXMLTest method testEchoXMLSyncMC.

public void testEchoXMLSyncMC() throws Exception {
    AxisOperation opdesc = new OutInAxisOperation(new QName("echoOMElement"));
    Options options = new Options();
    options.setTo(targetEPR);
    options.setAction(operationName.getLocalPart());
    options.setTransportInProtocol(Constants.TRANSPORT_TCP);
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
    OMElement method = fac.createOMElement("echoOMElement", omNs);
    OMElement value = fac.createOMElement("myValue", omNs);
    value.setText("Isaac Asimov, The Foundation Trilogy");
    method.addChild(value);
    SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope envelope = factory.getDefaultEnvelope();
    envelope.getBody().addChild(method);
    MessageContext requestContext = new MessageContext();
    requestContext.setConfigurationContext(configContext);
    requestContext.setAxisService(clientService);
    requestContext.setAxisOperation(opdesc);
    requestContext.setEnvelope(envelope);
    ServiceClient sender = new ServiceClient(configContext, clientService);
    sender.setOptions(options);
    OperationClient opClient = sender.createClient(new QName("echoOMElement"));
    opClient.addMessageContext(requestContext);
    opClient.setOptions(options);
    opClient.execute(true);
    MessageContext response = opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    SOAPEnvelope env = response.getEnvelope();
    assertNotNull(env);
    env.getBody().serialize(StAXUtils.createXMLStreamWriter(System.out));
    sender.cleanup();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) Options(org.apache.axis2.client.Options) OperationClient(org.apache.axis2.client.OperationClient) OMNamespace(org.apache.axiom.om.OMNamespace) OutInAxisOperation(org.apache.axis2.description.OutInAxisOperation) AxisOperation(org.apache.axis2.description.AxisOperation) QName(javax.xml.namespace.QName) ServiceClient(org.apache.axis2.client.ServiceClient) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) MessageContext(org.apache.axis2.context.MessageContext) OutInAxisOperation(org.apache.axis2.description.OutInAxisOperation) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 2 with OperationClient

use of org.apache.axis2.client.OperationClient in project wso2-axis2-transports by wso2.

the class AxisTestClient method send.

protected MessageContext send(ClientOptions options, AxisMessage message, QName operationQName, boolean block, String resultMessageLabel) throws Exception {
    OperationClient mepClient = serviceClient.createClient(operationQName);
    MessageContext mc = new MessageContext();
    mc.setProperty(Constants.Configuration.MESSAGE_TYPE, message.getMessageType());
    mc.setEnvelope(message.getEnvelope());
    Attachments attachments = message.getAttachments();
    if (attachments != null) {
        mc.setAttachmentMap(attachments);
        mc.setDoingSwA(true);
        mc.setProperty(Constants.Configuration.ENABLE_SWA, true);
    }
    for (AxisTestClientConfigurator configurator : configurators) {
        configurator.setupRequestMessageContext(mc);
    }
    mc.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, options.getCharset());
    mc.setServiceContext(serviceClient.getServiceContext());
    if (metrics != null) {
        mc.setProperty(BaseConstants.METRICS_COLLECTOR, metrics);
    }
    mepClient.addMessageContext(mc);
    mepClient.execute(block);
    // mepClient.complete(mc);
    return resultMessageLabel == null ? null : mepClient.getMessageContext(resultMessageLabel);
}
Also used : OperationClient(org.apache.axis2.client.OperationClient) MessageContext(org.apache.axis2.context.MessageContext) Attachments(org.apache.axiom.attachments.Attachments)

Example 3 with OperationClient

use of org.apache.axis2.client.OperationClient in project wso2-synapse by wso2.

the class BlockingMsgSender method sendReceive.

private org.apache.axis2.context.MessageContext sendReceive(org.apache.axis2.context.MessageContext axisOutMsgCtx, Options clientOptions, AxisService anonymousService, ServiceContext serviceCtx, MessageContext synapseInMsgCtx) throws AxisFault {
    AxisOperation axisAnonymousOperation = anonymousService.getOperation(new QName(AnonymousServiceFactory.OUT_IN_OPERATION));
    OperationClient operationClient = axisAnonymousOperation.createClient(serviceCtx, clientOptions);
    operationClient.addMessageContext(axisOutMsgCtx);
    axisOutMsgCtx.setAxisMessage(axisAnonymousOperation.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE));
    this.invokeHandlers(synapseInMsgCtx, false);
    operationClient.execute(true);
    org.apache.axis2.context.MessageContext resultMsgCtx = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    org.apache.axis2.context.MessageContext returnMsgCtx = new org.apache.axis2.context.MessageContext();
    if (resultMsgCtx.getEnvelope() != null) {
        returnMsgCtx.setEnvelope(MessageHelper.cloneSOAPEnvelope(resultMsgCtx.getEnvelope()));
        if (JsonUtil.hasAJsonPayload(resultMsgCtx)) {
            JsonUtil.cloneJsonPayload(resultMsgCtx, returnMsgCtx);
        }
    } else {
        if (axisOutMsgCtx.isSOAP11()) {
            returnMsgCtx.setEnvelope(OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope());
        } else {
            returnMsgCtx.setEnvelope(OMAbstractFactory.getSOAP12Factory().getDefaultEnvelope());
        }
    }
    returnMsgCtx.setProperty(SynapseConstants.HTTP_SENDER_STATUSCODE, resultMsgCtx.getProperty(SynapseConstants.HTTP_SENDER_STATUSCODE));
    axisOutMsgCtx.getTransportOut().getSender().cleanup(axisOutMsgCtx);
    returnMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, resultMsgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
    this.invokeHandlers(synapseInMsgCtx, true);
    return returnMsgCtx;
}
Also used : OperationClient(org.apache.axis2.client.OperationClient) AxisOperation(org.apache.axis2.description.AxisOperation) QName(javax.xml.namespace.QName) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 4 with OperationClient

use of org.apache.axis2.client.OperationClient in project wso2-synapse by wso2.

the class BlockingMsgSender method sendRobust.

private void sendRobust(org.apache.axis2.context.MessageContext axisOutMsgCtx, Options clientOptions, AxisService anonymousService, ServiceContext serviceCtx, MessageContext synapseInMsgCtx) throws AxisFault {
    this.invokeHandlers(synapseInMsgCtx, false);
    AxisOperation axisAnonymousOperation = anonymousService.getOperation(new QName(AnonymousServiceFactory.OUT_ONLY_OPERATION));
    OperationClient operationClient = axisAnonymousOperation.createClient(serviceCtx, clientOptions);
    operationClient.addMessageContext(axisOutMsgCtx);
    axisOutMsgCtx.setAxisMessage(axisAnonymousOperation.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE));
    operationClient.execute(true);
    axisOutMsgCtx.getTransportOut().getSender().cleanup(axisOutMsgCtx);
}
Also used : OperationClient(org.apache.axis2.client.OperationClient) AxisOperation(org.apache.axis2.description.AxisOperation) QName(javax.xml.namespace.QName)

Example 5 with OperationClient

use of org.apache.axis2.client.OperationClient in project wso2-synapse by wso2.

the class LoadbalanceFailoverClient method sessionfullClient.

/**
 * This method creates 3 soap envelopes for 3 different client based sessions. Then it randomly
 * choose one envelope for each iteration and send it to the ESB. ESB should be configured with
 * session affinity load balancer and the SampleClientInitiatedSession dispatcher. This will
 * output request number, session number and the server ID for each iteration. So it can be
 * observed that one session number always associated with one server ID.
 */
private void sessionfullClient() {
    String synapsePort = "8280";
    int iterations = 100;
    boolean infinite = true;
    String pPort = getProperty("port", synapsePort);
    String pIterations = getProperty("i", null);
    String addUrl = getProperty("addurl", null);
    String trpUrl = getProperty("trpurl", null);
    String prxUrl = getProperty("prxurl", null);
    String sleep = getProperty("sleep", null);
    String session = getProperty("session", null);
    long sleepTime = -1;
    if (sleep != null) {
        try {
            sleepTime = Long.parseLong(sleep);
        } catch (NumberFormatException ignored) {
        }
    }
    if (pPort != null) {
        try {
            Integer.parseInt(pPort);
            synapsePort = pPort;
        } catch (NumberFormatException e) {
        // run with default value
        }
    }
    if (pIterations != null) {
        try {
            iterations = Integer.parseInt(pIterations);
            if (iterations != -1) {
                infinite = false;
            }
        } catch (NumberFormatException e) {
        // run with default values
        }
    }
    Options options = new Options();
    options.setTo(new EndpointReference("http://localhost:" + synapsePort + "/services/LBService1"));
    options.setAction("urn:sampleOperation");
    options.setTimeOutInMilliSeconds(10000000);
    try {
        SOAPEnvelope env1 = buildSoapEnvelope("c1", "v1");
        SOAPEnvelope env2 = buildSoapEnvelope("c2", "v1");
        SOAPEnvelope env3 = buildSoapEnvelope("c3", "v1");
        SOAPEnvelope[] envelopes = { env1, env2, env3 };
        String repoLocationProperty = System.getProperty("repository");
        String repo = repoLocationProperty != null ? repoLocationProperty : DEFAULT_CLIENT_REPO;
        ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repo, repo + File.separator + "conf" + File.separator + "axis2.xml");
        ServiceClient client = new ServiceClient(configContext, null);
        // set addressing, transport and proxy url
        if (addUrl != null && !"null".equals(addUrl)) {
            client.engageModule("addressing");
            options.setTo(new EndpointReference(addUrl));
        }
        if (trpUrl != null && !"null".equals(trpUrl)) {
            options.setProperty(Constants.Configuration.TRANSPORT_URL, trpUrl);
        } else {
            client.engageModule("addressing");
        }
        if (prxUrl != null && !"null".equals(prxUrl)) {
            HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
            try {
                URL url = new URL(prxUrl);
                proxyProperties.setProxyName(url.getHost());
                proxyProperties.setProxyPort(url.getPort());
                proxyProperties.setUserName("");
                proxyProperties.setPassWord("");
                proxyProperties.setDomain("");
                options.setProperty(HTTPConstants.PROXY, proxyProperties);
            } catch (MalformedURLException e) {
                throw new AxisFault("Error creating proxy URL", e);
            }
        }
        client.setOptions(options);
        int i = 0;
        int sessionNumber;
        String[] cookies = new String[3];
        boolean httpSession = session != null && "http".equals(session);
        int cookieNumber;
        while (i < iterations || infinite) {
            i++;
            if (sleepTime != -1) {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException ignored) {
                }
            }
            MessageContext messageContext = new MessageContext();
            sessionNumber = getSessionTurn(envelopes.length);
            messageContext.setEnvelope(envelopes[sessionNumber]);
            cookieNumber = getSessionTurn(cookies.length);
            String cookie = cookies[cookieNumber];
            if (httpSession) {
                setSessionID(messageContext, cookie);
            }
            try {
                OperationClient op = client.createClient(ServiceClient.ANON_OUT_IN_OP);
                op.addMessageContext(messageContext);
                op.execute(true);
                MessageContext responseContext = op.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
                String receivedCookie = extractSessionID(responseContext);
                String receivedSetCookie = getSetCookieHeader(responseContext);
                if (httpSession) {
                    if (receivedSetCookie != null && !"".equals(receivedSetCookie)) {
                        cookies[cookieNumber] = receivedCookie;
                    }
                }
                SOAPEnvelope responseEnvelope = responseContext.getEnvelope();
                OMElement vElement = responseEnvelope.getBody().getFirstChildWithName(new QName("Value"));
                System.out.println("Request: " + i + " with Session ID: " + (httpSession ? cookie : sessionNumber) + " ---- " + "Response : with  " + (httpSession && receivedCookie != null ? (receivedSetCookie != null ? receivedSetCookie : receivedCookie) : " ") + " " + vElement.getText());
            } catch (AxisFault axisFault) {
                System.out.println("Request with session id " + (httpSession ? cookie : sessionNumber) + " " + "- Get a Fault : " + axisFault.getMessage());
            }
        }
    } catch (AxisFault axisFault) {
        System.out.println(axisFault.getMessage());
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Options(org.apache.axis2.client.Options) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) OperationClient(org.apache.axis2.client.OperationClient) HttpTransportProperties(org.apache.axis2.transport.http.HttpTransportProperties) MalformedURLException(java.net.MalformedURLException) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) URL(java.net.URL) EndpointReference(org.apache.axis2.addressing.EndpointReference) ServiceClient(org.apache.axis2.client.ServiceClient) MessageContext(org.apache.axis2.context.MessageContext)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)14 OperationClient (org.apache.axis2.client.OperationClient)13 Options (org.apache.axis2.client.Options)11 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)9 QName (javax.xml.namespace.QName)8 AxisOperation (org.apache.axis2.description.AxisOperation)7 EndpointReference (org.apache.axis2.addressing.EndpointReference)6 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 ServiceClient (org.apache.axis2.client.ServiceClient)4 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)4 ServiceContext (org.apache.axis2.context.ServiceContext)4 ServiceGroupContext (org.apache.axis2.context.ServiceGroupContext)4 AxisService (org.apache.axis2.description.AxisService)4 Attachments (org.apache.axiom.attachments.Attachments)3 OMElement (org.apache.axiom.om.OMElement)3 DataHandler (javax.activation.DataHandler)2 FileDataSource (javax.activation.FileDataSource)2 SOAPBody (org.apache.axiom.soap.SOAPBody)2 AxisFault (org.apache.axis2.AxisFault)2 HttpTransportProperties (org.apache.axis2.transport.http.HttpTransportProperties)2