Search in sources :

Example 6 with OperationClient

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

the class StockQuoteSampleClient method statefulClient.

public SampleClientResult statefulClient(String addUrl, String trpUrl, int iterations) {
    boolean infinite = false;
    String session = null;
    SampleClientResult clientResult = new SampleClientResult();
    try {
        SOAPEnvelope env1 = buildSoapEnvelope("c1", "v1");
        SOAPEnvelope env2 = buildSoapEnvelope("c2", "v1");
        SOAPEnvelope env3 = buildSoapEnvelope("c3", "v1");
        SOAPEnvelope[] envelopes = { env1, env2, env3 };
        init(addUrl, trpUrl, null, null, 10000);
        serviceClient.getOptions().setAction("urn:sampleOperation");
        int i = 0;
        int sessionNumber;
        String[] cookies = new String[3];
        boolean httpSession = session != null && "http".equals(session);
        int cookieNumber;
        while (i < iterations || infinite) {
            i++;
            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 = serviceClient.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"));
                clientResult.incrementResponseCount();
                log.info("Request: " + i + " with Session ID: " + (httpSession ? cookie : sessionNumber) + " ---- " + "Response : with  " + (httpSession && receivedCookie != null ? (receivedSetCookie != null ? receivedSetCookie : receivedCookie) : " ") + " " + vElement.getText());
            } catch (AxisFault axisFault) {
                log.error("Request with session id " + (httpSession ? cookie : sessionNumber) + " " + "- Get a Fault : " + axisFault.getMessage(), axisFault);
            }
        }
    } catch (Exception e) {
        log.error("Error invoking service", e);
        clientResult.setException(e);
    } finally {
        terminate();
    }
    return clientResult;
}
Also used : AxisFault(org.apache.axis2.AxisFault) OperationClient(org.apache.axis2.client.OperationClient) QName(javax.xml.namespace.QName) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SampleClientResult(org.apache.synapse.samples.framework.SampleClientResult) MessageContext(org.apache.axis2.context.MessageContext)

Example 7 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)

Example 8 with OperationClient

use of org.apache.axis2.client.OperationClient in project carbon-business-process by wso2.

the class HTTPBindingHandler method invoke.

public HTTPBindingResponse invoke(final PartnerRoleMessageExchange partnerRoleMessageExchange, final BPELMessageContext bpelMessageContext) throws AxisFault {
    MessageContext messageContext;
    OperationClient operationClient = AxisServiceUtils.getOperationClient(bpelMessageContext, configurationContext);
    operationClient.getOptions().setAction("\"\"");
    operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);
    addPropertyToOperationClient(operationClient, WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR, "&");
    addPropertyToOperationClient(operationClient, Constants.Configuration.ENABLE_REST, true);
    addPropertyToOperationClient(operationClient, Constants.Configuration.HTTP_METHOD, getVerb().trim());
    addPropertyToOperationClient(operationClient, WSDL2Constants.ATTR_WHTTP_LOCATION, getHTTPLocation(partnerRoleMessageExchange.getOperationName()));
    addPropertyToOperationClient(operationClient, Constants.Configuration.CONTENT_TYPE, inferContentType(getVerb()));
    addPropertyToOperationClient(operationClient, Constants.Configuration.MESSAGE_TYPE, inferContentType(getVerb()));
    SOAPEnvelope soapEnvelope = getFactory(operationClient.getOptions().getSoapVersionURI()).getDefaultEnvelope();
    messageContext = new MessageContext();
    populateSOAPBody(soapEnvelope, partnerRoleMessageExchange);
    messageContext.setEnvelope(soapEnvelope);
    operationClient.addMessageContext(messageContext);
    String mexEndpointUrl = ((MutableEndpoint) partnerRoleMessageExchange.getEndpointReference()).getUrl();
    if (!mexEndpointUrl.equals(getServiceLocation())) {
        operationClient.getOptions().setTo(new EndpointReference(mexEndpointUrl));
    }
    operationClient.getOptions().setTo(bpelMessageContext.getUep());
    operationClient.execute(true);
    MessageContext responseMessageContext = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    MessageContext faultMessageContext = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_FAULT_VALUE);
    return new HTTPBindingResponse(responseMessageContext, faultMessageContext);
}
Also used : OperationClient(org.apache.axis2.client.OperationClient) MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) MutableEndpoint(org.apache.ode.bpel.epr.MutableEndpoint) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 9 with OperationClient

use of org.apache.axis2.client.OperationClient in project carbon-business-process by wso2.

the class ServiceUtils method getOperationClient.

private static OperationClient getOperationClient(MessageContext partnerMessageContext, ConfigurationContext clientConfigCtx) throws AxisFault {
    AxisService anonymousService = AnonymousServiceFactory.getAnonymousService(Constants.registrationService, Constants.REGISTRATION_PORT, clientConfigCtx.getAxisConfiguration(), Constants.HUMANTASK_COORDINATION_MODULE_NAME);
    anonymousService.getParent().addParameter("hiddenService", "true");
    ServiceGroupContext sgc = new ServiceGroupContext(clientConfigCtx, (AxisServiceGroup) anonymousService.getParent());
    ServiceContext serviceCtx = sgc.getServiceContext(anonymousService);
    AxisOperation axisAnonymousOperation = anonymousService.getOperation(ServiceClient.ANON_OUT_IN_OP);
    Options clientOptions = cloneOptions(partnerMessageContext.getOptions());
    clientOptions.setExceptionToBeThrownOnSOAPFault(false);
    /* This value doesn't overrideend point config. */
    clientOptions.setTimeOutInMilliSeconds(60000);
    return axisAnonymousOperation.createClient(serviceCtx, clientOptions);
}
Also used : Options(org.apache.axis2.client.Options) AxisOperation(org.apache.axis2.description.AxisOperation) ServiceGroupContext(org.apache.axis2.context.ServiceGroupContext) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService)

Example 10 with OperationClient

use of org.apache.axis2.client.OperationClient in project carbon-business-process by wso2.

the class AxisServiceUtils method getOperationClient.

public static OperationClient getOperationClient(BPELMessageContext partnerMessageContext, ConfigurationContext clientConfigCtx) throws AxisFault {
    AxisService anonymousService = AnonymousServiceFactory.getAnonymousService(partnerMessageContext.getService(), partnerMessageContext.getPort(), clientConfigCtx.getAxisConfiguration(), partnerMessageContext.getCaller());
    anonymousService.engageModule(clientConfigCtx.getAxisConfiguration().getModule("UEPModule"));
    anonymousService.getParent().addParameter(BPELConstants.HIDDEN_SERVICE_PARAM, "true");
    ServiceGroupContext sgc = new ServiceGroupContext(clientConfigCtx, (AxisServiceGroup) anonymousService.getParent());
    ServiceContext serviceCtx = sgc.getServiceContext(anonymousService);
    // get a reference to the DYNAMIC operation of the Anonymous Axis2 service
    AxisOperation axisAnonymousOperation = anonymousService.getOperation(partnerMessageContext.isTwoWay() ? ServiceClient.ANON_OUT_IN_OP : ServiceClient.ANON_OUT_ONLY_OP);
    Options clientOptions = cloneOptions(partnerMessageContext.getInMessageContext().getOptions());
    clientOptions.setExceptionToBeThrownOnSOAPFault(false);
    /* This value doesn't overrideend point config. */
    clientOptions.setTimeOutInMilliSeconds(60000);
    return axisAnonymousOperation.createClient(serviceCtx, clientOptions);
}
Also used : Options(org.apache.axis2.client.Options) AxisOperation(org.apache.axis2.description.AxisOperation) ServiceGroupContext(org.apache.axis2.context.ServiceGroupContext) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService)

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