Search in sources :

Example 21 with URL

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

the class RESTUtil method getURI.

/**
 * This method will return the URI part for the GET HTTPRequest by converting
 * the SOAP infoset to the URL-encoded GET format
 *
 * @param messageContext - from which the SOAP infoset will be extracted to encode
 * @param address        - address of the actual service
 * @return uri       - ERI of the GET request
 * @throws AxisFault - if the SOAP infoset cannot be converted in to the GET URL-encoded format
 */
public static String getURI(MessageContext messageContext, String address) throws AxisFault {
    OMElement firstElement;
    address = address.substring(address.indexOf("//") + 2);
    address = address.substring(address.indexOf("/"));
    String queryParameterSeparator = (String) messageContext.getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
    if (queryParameterSeparator == null) {
        queryParameterSeparator = WSDL20DefaultValueHolder.getDefaultValue(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
    }
    firstElement = messageContext.getEnvelope().getBody().getFirstElement();
    String params = "";
    if (firstElement != null) {
        // first element corresponds to the operation name
        address = address + "/" + firstElement.getLocalName();
    } else {
        firstElement = messageContext.getEnvelope().getBody();
    }
    Iterator iter = firstElement.getChildElements();
    String legalCharacters = WSDL2Constants.LEGAL_CHARACTERS_IN_QUERY.replaceAll(queryParameterSeparator, "");
    StringBuffer buff = new StringBuffer(params);
    // iterate through the child elements and find the request parameters
    while (iter.hasNext()) {
        OMElement element = (OMElement) iter.next();
        try {
            buff.append(URIEncoderDecoder.quoteIllegal(element.getLocalName(), legalCharacters)).append("=").append(URIEncoderDecoder.quoteIllegal(element.getText(), legalCharacters)).append(queryParameterSeparator);
        } catch (UnsupportedEncodingException e) {
            throw new AxisFault("URI Encoding error : " + element.getLocalName() + "=" + element.getText(), e);
        }
    }
    params = buff.toString();
    if (params.trim().length() != 0) {
        int index = address.indexOf("?");
        if (index == -1) {
            address = address + "?" + params.substring(0, params.length() - 1);
        } else if (index == address.length() - 1) {
            address = address + params.substring(0, params.length() - 1);
        } else {
            address = address + queryParameterSeparator + params.substring(0, params.length() - 1);
        }
    }
    return address;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OMElement(org.apache.axiom.om.OMElement) AxisEndpoint(org.apache.axis2.description.AxisEndpoint)

Example 22 with URL

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

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

the class Axis2HttpTransportSender method invoke.

@Override
public InvocationResponse invoke(MessageContext msgCtx) throws AxisFault {
    if (AddressingHelper.isReplyRedirected(msgCtx)) {
        msgCtx.setProperty(BridgeConstants.IGNORE_SC_ACCEPTED, BridgeConstants.VALUE_TRUE);
    }
    EndpointReference destinationEPR = RequestResponseUtils.getDestinationEPR(msgCtx);
    if (isRequestToBackend(destinationEPR)) {
        try {
            URL destinationURL = new URL(destinationEPR.getAddress());
            sendRequestToBackendService(msgCtx, destinationURL);
        } catch (MalformedURLException e) {
            handleException("Malformed URL in the target EPR", e);
        } catch (IOException e) {
            handleException("Error while sending the request to the backend service " + destinationEPR.getAddress(), e);
        }
    } else {
        HttpCarbonMessage clientRequest = (HttpCarbonMessage) msgCtx.getProperty(BridgeConstants.HTTP_CLIENT_REQUEST_CARBON_MESSAGE);
        if (clientRequest == null) {
            LOG.warn("Unable to find the original client request to send the response");
            return InvocationResponse.ABORT;
        }
        try {
            sendResponseToClient(msgCtx, clientRequest);
            if (msgCtx.getOperationContext() != null) {
                msgCtx.getOperationContext().setProperty(Constants.RESPONSE_WRITTEN, Constants.VALUE_TRUE);
            }
        } catch (IOException e) {
            handleException("Error occurred while sending a response to the client", e);
        }
    }
    return InvocationResponse.CONTINUE;
}
Also used : HttpCarbonMessage(org.wso2.transport.http.netty.message.HttpCarbonMessage) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 24 with URL

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

the class TargetRequestHandler method createHttpClient.

public static HttpClientConnector createHttpClient(URL url, MessageContext msgContext, HttpWsConnectorFactory httpWsConnectorFactory, ConnectionManager connectionManager, BootstrapConfiguration bootstrapConfiguration, TargetConfiguration targetConfiguration) throws AxisFault {
    try {
        SenderConfiguration senderConfiguration = new SenderConfiguration();
        populateSenderConfigurations(msgContext, senderConfiguration, targetConfiguration, url);
        return httpWsConnectorFactory.createHttpClientConnector(bootstrapConfiguration, senderConfiguration, connectionManager);
    } catch (Exception ex) {
        throw new AxisFault("Error while creating the HTTP Client Connector. ", ex);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) SenderConfiguration(org.wso2.transport.http.netty.contract.config.SenderConfiguration) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 25 with URL

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

the class TargetRequestHandler method getOutboundReqPath.

private static String getOutboundReqPath(URL url, MessageContext msgCtx) throws IOException {
    if (HttpUtils.isGETRequest(msgCtx) || (RelayUtils.isDeleteRequestWithoutPayload(msgCtx))) {
        MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
        OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgCtx);
        if (formatter != null) {
            URL targetURL = formatter.getTargetAddress(msgCtx, format, url);
            if (targetURL != null && !targetURL.toString().isEmpty()) {
                if (msgCtx.isPropertyTrue(BridgeConstants.POST_TO_URI)) {
                    return targetURL.toString();
                } else {
                    return targetURL.getPath() + ((targetURL.getQuery() != null && !targetURL.getQuery().isEmpty()) ? ("?" + targetURL.getQuery()) : "");
                }
            }
        }
    }
    if (msgCtx.isPropertyTrue(BridgeConstants.POST_TO_URI)) {
        return url.toString();
    }
    // TODO: need to check "(route.getProxyHost() != null && !route.isTunnelled())" as well
    return msgCtx.isPropertyTrue(BridgeConstants.FULL_URI) ? url.toString() : url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : "");
}
Also used : MessageFormatter(org.apache.axis2.transport.MessageFormatter) SOAPMessageFormatter(org.apache.axis2.transport.http.SOAPMessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) URL(java.net.URL)

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