Search in sources :

Example 71 with URL

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

the class URLRewriteMediator method mediate.

public boolean mediate(MessageContext messageContext) {
    if (messageContext.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(messageContext)) {
            return true;
        }
    }
    URIFragments fragments;
    URI inputURI = getInputAddress(messageContext);
    if (inputURI != null) {
        /*try {
                URL url = new URL(inputURI.toString());
            } catch (MalformedURLException e) {
                handleException("Malformed URL when processing " + inputURI, e, messageContext);
            }*/
        fragments = new URIFragments(inputURI);
    } else {
        fragments = new URIFragments();
    }
    try {
        for (RewriteRule r : rules) {
            r.rewrite(fragments, messageContext);
        }
        if (outputProperty != null) {
            messageContext.setProperty(outputProperty, fragments.toURIString());
        } else {
            messageContext.setTo(new EndpointReference(fragments.toURIString()));
        }
        if (log.isDebugEnabled()) {
            log.debug("URL Rewrite Mediator has rewritten the address url : \n " + messageContext.getEnvelope());
        }
    } catch (URISyntaxException e) {
        handleException("Error while constructing a URI from the fragments", e, messageContext);
    }
    return true;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 72 with URL

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

the class AxisOperationClient method sendCustomPayload.

/**
 * Send custom payload
 *
 * <ns:getQuote>
 *    <ns:request>
 *        <ns:symbols>
 *            <ns:company></ns:company>
 *        </ns:symbols>
 *    </ns:request>
 * </ns:getQuote>
 * @param trpUrl Transport url
 * @param addUrl WS-Addressing EPR url
 * @param symbol Stock quote symbol
 * @param action Soap action
 * @return
 * @throws AxisFault
 */
public OMElement sendCustomPayload(String trpUrl, String addUrl, String symbol, String action) throws AxisFault {
    init();
    OMElement payload = createCustomPayload(symbol);
    operationClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
    setMessageContext(addUrl, trpUrl, action);
    outMsgCtx.setEnvelope(createSOAPEnvelope(payload));
    operationClient.addMessageContext(outMsgCtx);
    operationClient.execute(true);
    MessageContext inMsgtCtx = operationClient.getMessageContext("In");
    SOAPEnvelope response = inMsgtCtx.getEnvelope();
    return response;
}
Also used : OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 73 with URL

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

the class AxisOperationClient method createMultipleQuoteRequest.

/**
 * Creating the multiple quote request
 *
 * @param trpUrl Transport URL
 * @param addUrl WS-Addressing EPR url
 * @param symbol Stock quote symbol
 * @param iterations No of iterations
 * @return Response from the backend
 * @throws IOException
 */
private OMElement createMultipleQuoteRequest(String trpUrl, String addUrl, String symbol, int iterations) throws IOException {
    init();
    operationClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
    setMessageContext(addUrl, trpUrl, null);
    outMsgCtx.setEnvelope(createSOAPEnvelope(symbol, iterations));
    operationClient.addMessageContext(outMsgCtx);
    operationClient.execute(true);
    MessageContext inMsgtCtx = operationClient.getMessageContext("In");
    SOAPEnvelope response = inMsgtCtx.getEnvelope();
    return response;
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 74 with URL

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

the class SynapseXPathVariableContext method getVariableValue.

/**
 * Gets the variable values resolved from the context. This includes the
 * <dl>
 *   <dt><tt>body</tt></dt>
 *   <dd>The SOAP 1.1 or 1.2 body element.</dd>
 *   <dt><tt>header</tt></dt>
 *   <dd>The SOAP 1.1 or 1.2 header element.</dd>
 * </dl>
 * and the following variable prefixes
 * <dl>
 *   <dt><tt>ctx</tt></dt>
 *   <dd>Prefix for Synapse MessageContext properties</dd>
 *   <dt><tt>axis2</tt></dt>
 *   <dd>Prefix for Axis2 MessageContext properties</dd>
 *   <dt><tt>trp</tt></dt>
 *   <dd>Prefix for the transport headers</dd>
 * </dl>
 * If the variable is unknown, this method attempts to resolve it using
 * the parent variable context.
 *
 * @param namespaceURI namespaces for the variable resolution
 * @param prefix string prefix for the variable resolution
 * @param localName string local name for the variable resolution
 * @return Resolved variable value
 * @throws UnresolvableException if the variable specified does not found
 */
public Object getVariableValue(String namespaceURI, String prefix, String localName) throws UnresolvableException {
    if (namespaceURI == null) {
        if (env != null) {
            if (SynapseXPathConstants.SOAP_BODY_VARIABLE.equals(localName)) {
                return env.getBody();
            } else if (SynapseXPathConstants.SOAP_HEADER_VARIABLE.equals(localName)) {
                return env.getHeader();
            } else if (SynapseXPathConstants.SOAP_ENVELOPE_VARIABLE.equals(localName)) {
                return env;
            }
        }
        if (prefix != null && !"".equals(prefix) && synCtx != null) {
            if (SynapseXPathConstants.MESSAGE_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
                return synCtx.getProperty(localName);
            } else if (SynapseXPathConstants.AXIS2_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
                return ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(localName);
            } else if (SynapseXPathConstants.FUNC_CONTEXT_VARIABLE_PREFIX.equals(prefix)) {
                Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
                TemplateContext topCtxt = functionStack.peek();
                if (topCtxt != null) {
                    Object result = topCtxt.getParameterValue(localName);
                    if (result != null && result instanceof SynapseXPath && env != null) {
                        SynapseXPath expression = (SynapseXPath) topCtxt.getParameterValue(localName);
                        try {
                            return expression.evaluate(env);
                        } catch (JaxenException e) {
                            return null;
                        }
                    } else {
                        return result;
                    }
                }
            } else if (SynapseXPathConstants.TRANSPORT_VARIABLE_PREFIX.equals(prefix)) {
                org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                if (headers != null && headers instanceof Map) {
                    Map headersMap = (Map) headers;
                    return headersMap.get(localName);
                } else {
                    return null;
                }
            } else if (SynapseXPathConstants.URL_VARIABLE_PREFIX.equals(prefix)) {
                EndpointReference toEPR = synCtx.getTo();
                if (toEPR != null) {
                    String completeURL = toEPR.getAddress();
                    AxisBindingOperation axisBindingOperation = (AxisBindingOperation) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(Constants.AXIS_BINDING_OPERATION);
                    String queryParameterSeparator = null;
                    if (axisBindingOperation != null) {
                        queryParameterSeparator = (String) axisBindingOperation.getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
                    }
                    if (queryParameterSeparator == null) {
                        queryParameterSeparator = WSDL20DefaultValueHolder.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR_DEFAULT;
                    }
                    int i = completeURL.indexOf("?");
                    if (i > -1) {
                        String queryString = completeURL.substring(i + 1);
                        if (queryString != null && !queryString.equals("")) {
                            String[] params = queryString.split(queryParameterSeparator);
                            if (params == null || params.length == 0) {
                                return "";
                            }
                            for (String param : params) {
                                String[] temp = param.split("=");
                                if (temp != null && temp.length >= 1) {
                                    if (temp[0].equalsIgnoreCase(localName)) {
                                        try {
                                            return temp.length > 1 ? URIEncoderDecoder.decode(temp[1]) : "";
                                        } catch (UnsupportedEncodingException e) {
                                            String msg = "Couldn't decode the URL parameter " + "value " + temp[1] + " with name " + localName;
                                            log.error(msg, e);
                                            throw new UnresolvableException(msg + e.getMessage());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return "";
            } else if (SynapseXPathConstants.OPERATION_SCOPE_VARIABLE_PREFIX.equals(prefix)) {
                Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
                return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(localName);
            } else if (SynapseXPathConstants.SYSTEM_SCOPE_VARIABLE_PREFIX.equals(prefix)) {
                String propVal = System.getProperty(localName);
                if (propVal != null) {
                    return propVal;
                } else {
                    return "";
                }
            } else {
                Object o = synCtx.getProperty(prefix);
                if (o instanceof Map) {
                    Object valueObject = ((Map) o).get(localName);
                    if (valueObject != null) {
                        return valueObject.toString();
                    }
                }
            }
        }
    }
    // try resolving using available custom extensions
    Object obj = XpathExtensionUtil.resolveVariableContext(synCtx, namespaceURI, prefix, localName);
    if (obj != null) {
        return obj;
    }
    return parent.getVariableValue(namespaceURI, prefix, localName);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) AxisBindingOperation(org.apache.axis2.description.AxisBindingOperation) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) EndpointReference(org.apache.axis2.addressing.EndpointReference) JaxenException(org.jaxen.JaxenException) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) UnresolvableException(org.jaxen.UnresolvableException) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 75 with URL

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

the class DefaultHttpGetProcessor method generateXsd.

/**
 * Generates Schema.
 *
 * @param response    HttpResponse
 * @param conn        NHttpServerConnection
 * @param os          OutputStream
 * @param serviceName service name
 * @param parameters  url parameters
 */
protected void generateXsd(HttpRequest request, HttpResponse response, MessageContext messageCtx, NHttpServerConnection conn, OutputStream os, String serviceName, Map<String, String> parameters, boolean isRestDispatching) {
    if (parameters.get("xsd") == null || "".equals(parameters.get("xsd"))) {
        AxisService service = cfgCtx.getAxisConfiguration().getServices().get(serviceName);
        if (service != null) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                service.printSchema(baos);
                response.addHeader(CONTENT_TYPE, TEXT_XML);
                serverHandler.commitResponseHideExceptions(conn, response);
                os.write(baos.toByteArray());
                closeOutputStream(os);
            } catch (Exception e) {
                handleBrowserException(response, conn, os, "Error generating ?xsd output for service : " + serviceName, e);
            }
        } else {
            processGetAndDelete(request, response, messageCtx, conn, os, serviceName, isRestDispatching);
        }
    } else {
        // cater for named xsds - check for the xsd name
        String schemaName = parameters.get("xsd");
        AxisService service = cfgCtx.getAxisConfiguration().getServices().get(serviceName);
        if (service != null) {
            // run the population logic just to be sure
            service.populateSchemaMappings();
            // write out the correct schema
            Map schemaTable = service.getSchemaMappingTable();
            XmlSchema schema = (XmlSchema) schemaTable.get(schemaName);
            if (schema == null) {
                int dotIndex = schemaName.indexOf('.');
                if (dotIndex > 0) {
                    String schemaKey = schemaName.substring(0, dotIndex);
                    schema = (XmlSchema) schemaTable.get(schemaKey);
                }
            }
            // schema found - write it to the stream
            if (schema != null) {
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    schema.write(baos);
                    response.addHeader(CONTENT_TYPE, TEXT_XML);
                    serverHandler.commitResponseHideExceptions(conn, response);
                    os.write(baos.toByteArray());
                    closeOutputStream(os);
                } catch (Exception e) {
                    handleBrowserException(response, conn, os, "Error generating named ?xsd output for service : " + serviceName, e);
                }
            } else {
                // no schema available by that name  - send 404
                response.setStatusCode(HttpStatus.SC_NOT_FOUND);
                closeOutputStream(os);
            }
        } else {
            processGetAndDelete(request, response, messageCtx, conn, os, serviceName, isRestDispatching);
        }
    }
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) AxisService(org.apache.axis2.description.AxisService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SocketException(java.net.SocketException) IOException(java.io.IOException)

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