Search in sources :

Example 11 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to false.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeEnabled() throws Exception {
    MessageContext synCtx = new TestMessageContext();
    SynapseConfiguration synCfg = new SynapseConfiguration();
    synCtx.setConfiguration(synCfg);
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeEnabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://invalid-host/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", true);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    Assert.assertNull("Axis service built with an unreachable wsdl endpoint be null", proxyService.buildAxisService(synCfg, axisCfg));
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) TestMessageContext(org.apache.synapse.TestMessageContext) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) MessageContext(org.apache.synapse.MessageContext) TestMessageContext(org.apache.synapse.TestMessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 12 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class ProxyServiceTest method testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled.

/**
 * Tests building a proxy service with an unreachable endpoint specified as the published wsdl url with the
 * 'enablePublishWSDLSafeMode' set to true.
 *
 * @throws Exception if an error occurs when converting the URI string to a URI
 */
public void testBuildAxisServiceWithUnreachableWsdlEndpointWithPublishWSDLSafeModeDisabled() throws Exception {
    SynapseConfiguration synCfg = new SynapseConfiguration();
    AxisConfiguration axisCfg = new AxisConfiguration();
    ProxyService proxyService = new ProxyService("faultyPublishWsdlEndpointProxyWithPublishWSDLSafeModeDisabled");
    proxyService.setPublishWSDLEndpoint("wsdlEndPoint");
    AddressEndpoint wsdlEndpoint = new AddressEndpoint();
    EndpointDefinition endpointDefinition = new EndpointDefinition();
    endpointDefinition.setAddress((new URI("http://invalid-host/SimpleStockService.wsdl")).toString());
    wsdlEndpoint.setDefinition(endpointDefinition);
    proxyService.addParameter("enablePublishWSDLSafeMode", false);
    synCfg.addEndpoint("wsdlEndPoint", wsdlEndpoint);
    try {
        proxyService.buildAxisService(synCfg, axisCfg);
        Assert.fail("Axis service built with an unreachable wsdl endpoint should throw fault");
    } catch (SynapseException e) {
        Assert.assertTrue("Unexpected exception thrown: " + e, e.getMessage().contains("Error reading from wsdl URI"));
    }
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) SynapseException(org.apache.synapse.SynapseException) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URI(java.net.URI)

Example 13 with URL

use of org.apache.axis2.util.URL in project wso2-synapse by wso2.

the class StockQuoteSampleClient method init.

private void init(String addUrl, String trpUrl, String prxUrl, String policyKey, long timeout) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Initializing sample Axis2 client");
    }
    configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(clientConfig.getClientRepo(), clientConfig.getAxis2Xml());
    serviceClient = new ServiceClient(configContext, null);
    Options options = new Options();
    if (addUrl != null && !"".equals(addUrl)) {
        serviceClient.engageModule("addressing");
        options.setTo(new EndpointReference(addUrl));
    }
    if (trpUrl != null && !"".equals(trpUrl)) {
        options.setProperty(Constants.Configuration.TRANSPORT_URL, trpUrl);
    }
    if (prxUrl != 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);
    }
    if (policyKey != null && !"".equals(policyKey)) {
        log.info("Using WS-Security");
        serviceClient.engageModule("addressing");
        serviceClient.engageModule("rampart");
        OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(new FileInputStream(policyKey));
        Policy policy = PolicyEngine.getPolicy(builder.getDocumentElement());
        options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy);
    }
    if (timeout > 0) {
        log.info("setting client timeout to: " + timeout);
        options.setTimeOutInMilliSeconds(timeout);
    }
    options.setProperty(HTTPConstants.HTTP_HEADERS, httpHeaders);
    serviceClient.setOptions(options);
}
Also used : Policy(org.apache.neethi.Policy) Options(org.apache.axis2.client.Options) HttpTransportProperties(org.apache.axis2.transport.http.HttpTransportProperties) ServiceClient(org.apache.axis2.client.ServiceClient) URL(java.net.URL) FileInputStream(java.io.FileInputStream) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 14 with URL

use of org.apache.axis2.util.URL 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 15 with URL

use of org.apache.axis2.util.URL 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

IOException (java.io.IOException)31 EndpointReference (org.apache.axis2.addressing.EndpointReference)29 AxisFault (org.apache.axis2.AxisFault)27 URL (java.net.URL)21 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)19 URL (org.apache.axis2.util.URL)19 HttpClient (org.apache.http.client.HttpClient)19 Options (org.apache.axis2.client.Options)18 MalformedURLException (java.net.MalformedURLException)17 OMElement (org.apache.axiom.om.OMElement)17 MessageContext (org.apache.axis2.context.MessageContext)16 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 ServiceClient (org.apache.axis2.client.ServiceClient)13 SynapseException (org.apache.synapse.SynapseException)13 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)12 AxisService (org.apache.axis2.description.AxisService)10 StringEntity (org.apache.http.entity.StringEntity)9 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)9 Map (java.util.Map)7 JSONObject (org.json.JSONObject)7