Search in sources :

Example 66 with ConfigurationContext

use of org.apache.axis2.context.ConfigurationContext in project wso2-synapse by wso2.

the class SALSessionsTest method testUpdateWithId.

/**
 * Test updating session with session id
 * @throws Exception
 */
@Test
public void testUpdateWithId() throws Exception {
    BasicConfigurator.configure();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    AxisConfiguration axisConfiguration = synapseConfiguration.getAxisConfiguration();
    ConfigurationContext cfgCtx = new ConfigurationContext(axisConfiguration);
    SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(cfgCtx, synapseConfiguration);
    Axis2MessageContext axis2MessageContext = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(), synapseConfiguration, synapseEnvironment);
    MessageContext messageContext = axis2MessageContext;
    Endpoint endpoint = new AddressEndpoint();
    List<Endpoint> endpoints = new ArrayList<>();
    endpoints.add(endpoint);
    messageContext.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST, endpoints);
    SALSessions salSessions = SALSessions.getInstance();
    salSessions.initialize(false, cfgCtx);
    salSessions.updateSession(messageContext, "testSession");
    SessionInformation sessionInformation = salSessions.getSession("testSession");
    Assert.assertEquals("Session not updated!", "testSession", sessionInformation.getId());
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ArrayList(java.util.ArrayList) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 67 with ConfigurationContext

use of org.apache.axis2.context.ConfigurationContext in project wso2-synapse by wso2.

the class JSONClient method executeClient.

public static void executeClient() throws Exception {
    Options options = new Options();
    ServiceClient serviceClient;
    ConfigurationContext configContext = null;
    String addUrl = getProperty("addurl", "http://localhost:8280/services/JSONProxy");
    String trpUrl = getProperty("trpurl", null);
    String prxUrl = getProperty("prxurl", null);
    String repo = getProperty("repository", "client_repo");
    String symbol = getProperty("symbol", "IBM");
    if (repo != null && !"null".equals(repo)) {
        configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repo, repo + File.separator + "conf" + File.separator + "axis2.xml");
        serviceClient = new ServiceClient(configContext, null);
    } else {
        serviceClient = new ServiceClient();
    }
    if (trpUrl != null && !"null".equals(trpUrl)) {
        options.setProperty(Constants.Configuration.TRANSPORT_URL, trpUrl);
    }
    if (prxUrl != null && !"null".equals(prxUrl)) {
        HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
        URL url = new URL(prxUrl);
        proxyProperties.setProxyName(url.getHost());
        proxyProperties.setProxyPort(url.getPort());
        proxyProperties.setUserName("");
        proxyProperties.setPassWord("");
        proxyProperties.setDomain("");
        options.setProperty(HTTPConstants.PROXY, proxyProperties);
    }
    serviceClient.engageModule("addressing");
    options.setTo(new EndpointReference(addUrl));
    options.setAction("urn:getQuote");
    options.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/json");
    serviceClient.setOptions(options);
    OMElement payload = AXIOMUtil.stringToOM("<getQuote><request><symbol>" + symbol + "</symbol>" + "</request></getQuote>");
    OMElement response = serviceClient.sendReceive(payload);
    if (response.getLocalName().equals("getQuoteResponse")) {
        OMElement last = response.getFirstElement().getFirstChildWithName(new QName("last"));
        System.out.println("Standard :: Stock price = $" + last.getText());
    } else {
        throw new Exception("Unexpected response : " + response);
    }
    Thread.sleep(1000);
    if (configContext != null) {
        configContext.terminate();
    }
}
Also used : Options(org.apache.axis2.client.Options) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) HttpTransportProperties(org.apache.axis2.transport.http.HttpTransportProperties) ServiceClient(org.apache.axis2.client.ServiceClient) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) URL(java.net.URL) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 68 with ConfigurationContext

use of org.apache.axis2.context.ConfigurationContext in project wso2-synapse by wso2.

the class LoadbalanceFailoverClient method sessionlessClient.

public String sessionlessClient() throws AxisFault {
    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);
    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
        }
    }
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMElement value = fac.createOMElement("Value", null);
    value.setText("Sample string");
    Options options = new Options();
    options.setTo(new EndpointReference("http://localhost:" + synapsePort + "/services/LBService1"));
    options.setAction("urn:sampleOperation");
    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);
    long timeout = Integer.parseInt(getProperty("timeout", "10000000"));
    System.out.println("timeout=" + timeout);
    options.setTimeOutInMilliSeconds(timeout);
    // 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);
    String testString = "";
    long i = 0;
    while (i < iterations || infinite) {
        if (sleepTime != -1) {
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException ignored) {
            }
        }
        client.getOptions().setManageSession(true);
        OMElement responseElement = client.sendReceive(value);
        String response = responseElement.getText();
        i++;
        System.out.println("Request: " + i + " ==> " + response);
        testString = testString.concat(":" + i + ">" + response + ":");
    }
    return testString;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Options(org.apache.axis2.client.Options) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) HttpTransportProperties(org.apache.axis2.transport.http.HttpTransportProperties) MalformedURLException(java.net.MalformedURLException) OMElement(org.apache.axiom.om.OMElement) URL(java.net.URL) EndpointReference(org.apache.axis2.addressing.EndpointReference) OMFactory(org.apache.axiom.om.OMFactory) ServiceClient(org.apache.axis2.client.ServiceClient)

Example 69 with ConfigurationContext

use of org.apache.axis2.context.ConfigurationContext 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 70 with ConfigurationContext

use of org.apache.axis2.context.ConfigurationContext in project wso2-synapse by wso2.

the class FIXTransportListenerTest method testFIXTransportListenerInit.

@Test
public void testFIXTransportListenerInit() throws Exception {
    AxisService axisService = new AxisService("testFIXService");
    axisService.addParameter(new Parameter(FIXConstants.FIX_ACCEPTOR_CONFIG_URL_PARAM, "/sample/path/Mock.cfg"));
    axisService.addParameter(new Parameter(FIXConstants.FIX_INITIATOR_CONFIG_URL_PARAM, "/sample/path/Mock2.cfg"));
    ConfigurationContext cfgCtx = new ConfigurationContext(new AxisConfiguration());
    TransportDescriptionFactory transportDescriptionFactory = new TransportDescriptionFactory() {

        @Override
        public TransportOutDescription createTransportOutDescription() throws Exception {
            TransportOutDescription trpOutDesc = new TransportOutDescription("fix");
            trpOutDesc.setSender(new FIXTransportSender());
            return trpOutDesc;
        }

        @Override
        public TransportInDescription createTransportInDescription() throws Exception {
            TransportInDescription trpInDesc = new TransportInDescription("fix");
            trpInDesc.setReceiver(new FIXTransportListener());
            return trpInDesc;
        }
    };
    TransportInDescription transportInDescription = null;
    try {
        transportInDescription = transportDescriptionFactory.createTransportInDescription();
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Error occurred while creating transport in description");
    }
    FIXTransportListener listner = new FIXTransportListener();
    listner.init(cfgCtx, transportInDescription);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AxisService(org.apache.axis2.description.AxisService) Parameter(org.apache.axis2.description.Parameter) TransportInDescription(org.apache.axis2.description.TransportInDescription) TransportDescriptionFactory(org.apache.axis2.transport.testkit.axis2.TransportDescriptionFactory) TransportOutDescription(org.apache.axis2.description.TransportOutDescription) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ConfigurationContext (org.apache.axis2.context.ConfigurationContext)143 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)97 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)58 Parameter (org.apache.axis2.description.Parameter)52 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)52 Test (org.junit.Test)48 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)45 OMElement (org.apache.axiom.om.OMElement)42 AxisFault (org.apache.axis2.AxisFault)27 AxisService (org.apache.axis2.description.AxisService)25 EndpointReference (org.apache.axis2.addressing.EndpointReference)23 MessageContext (org.apache.synapse.MessageContext)22 Options (org.apache.axis2.client.Options)21 MessageContext (org.apache.axis2.context.MessageContext)21 ServiceContext (org.apache.axis2.context.ServiceContext)15 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 HashMap (java.util.HashMap)14 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)14 ArrayList (java.util.ArrayList)13