Search in sources :

Example 71 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class SynapseDebugManager method getAxis2Properties.

protected JSONObject getAxis2Properties() throws JSONException, IOException {
    JSONObject result = new JSONObject();
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_TO, synCtx.getTo() != null ? synCtx.getTo().getAddress() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_FROM, synCtx.getFrom() != null ? synCtx.getFrom().getAddress() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_WSACTION, synCtx.getWSAAction() != null ? synCtx.getWSAAction() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_SOAPACTION, synCtx.getSoapAction() != null ? synCtx.getSoapAction() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_REPLY_TO, synCtx.getReplyTo() != null ? synCtx.getReplyTo().getAddress() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_MESSAGE_ID, synCtx.getMessageID() != null ? synCtx.getMessageID() : "");
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_DIRECTION, synCtx.isResponse() ? "response" : "request");
    if (((String) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("messageType")).contains("json")) {
        InputStream jsonPayloadStream = JsonUtil.getJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext());
        if (jsonPayloadStream != null) {
            StringWriter writer = new StringWriter();
            String encoding = null;
            IOUtils.copy(jsonPayloadStream, writer, encoding);
            String jsonPayload = writer.toString();
            result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_ENVELOPE, jsonPayload != null ? jsonPayload : synCtx.getEnvelope().toString());
        } else {
            result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_ENVELOPE, synCtx.getEnvelope() != null ? synCtx.getEnvelope().toString() : "");
        }
    } else {
        result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_ENVELOPE, synCtx.getEnvelope() != null ? synCtx.getEnvelope().toString() : "");
    }
    String axis2MessageContextKey = getAxis2MessagePropertiesKey(((Axis2MessageContext) synCtx).getAxis2MessageContext());
    if (addedPropertyValuesMap.containsKey(axis2MessageContextKey)) {
        Map scopePropertyMap = (Map) addedPropertyValuesMap.get(axis2MessageContextKey);
        if (scopePropertyMap.containsKey(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2)) {
            Set<String> propertyKeySet = (Set<String>) scopePropertyMap.get(SynapseDebugCommandConstants.DEBUG_COMMAND_PROPERTY_CONTEXT_AXIS2);
            for (String key : propertyKeySet) {
                result.put(key, ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(key));
            }
        }
    }
    JSONObject soapHeader = new JSONObject();
    if (synCtx.getEnvelope() != null) {
        SOAPHeader header = synCtx.getEnvelope().getHeader();
        if (header != null) {
            for (Iterator iter = header.examineAllHeaderBlocks(); iter.hasNext(); ) {
                Object o = iter.next();
                if (o instanceof SOAPHeaderBlock) {
                    SOAPHeaderBlock headerBlk = (SOAPHeaderBlock) o;
                    soapHeader.put(headerBlk.getLocalName(), headerBlk.getText());
                } else if (o instanceof OMElement) {
                    OMElement headerElem = (OMElement) o;
                    soapHeader.put(headerElem.getLocalName(), headerElem.getText());
                }
            }
        }
    }
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_SOAPHEADER, soapHeader);
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_EXCESS_TRANSPORT_HEADERS, ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("EXCESS_TRANSPORT_HEADERS"));
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_MESSAGE_TYPE, ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("messageType"));
    result.put(SynapseDebugCommandConstants.AXIS2_PROPERTY_CONTENT_TYPE, ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty("ContentType"));
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) InputStream(java.io.InputStream) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) OMElement(org.apache.axiom.om.OMElement) JSONObject(org.codehaus.jettison.json.JSONObject) StringWriter(java.io.StringWriter) Iterator(java.util.Iterator) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) SOAPHeader(org.apache.axiom.soap.SOAPHeader) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 72 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class AbstractEndpoint method send.

public void send(MessageContext synCtx) {
    logSetter();
    Integer statisticReportingIndex = null;
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (isStatisticsEnabled) {
        statisticReportingIndex = OpenEventCollector.reportEntryEvent(synCtx, getReportingName(), definition.getAspectConfiguration(), ComponentType.ENDPOINT);
    }
    boolean traceOn = isTraceOn(synCtx);
    boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
    if (!initialized) {
        // can't send to a non-initialized endpoint. This is a program fault
        throw new IllegalStateException("not initialized, " + "endpoint must be in initialized state");
    }
    prepareForEndpointStatistics(synCtx);
    if (traceOrDebugOn) {
        String address = definition.getAddress();
        if (address == null && synCtx.getTo() != null && synCtx.getTo().getAddress() != null) {
            // compute address for the default endpoint only for logging purposes
            address = synCtx.getTo().getAddress();
        }
        traceOrDebug(traceOn, "Sending message through endpoint : " + getName() + " resolving to address = " + address);
        traceOrDebug(traceOn, "SOAPAction: " + (synCtx.getSoapAction() != null ? synCtx.getSoapAction() : "null"));
        traceOrDebug(traceOn, "WSA-Action: " + (synCtx.getWSAAction() != null ? synCtx.getWSAAction() : "null"));
        if (traceOn && trace.isTraceEnabled()) {
            trace.trace("Envelope : \n" + synCtx.getEnvelope());
        }
    }
    // push the errorHandler sequence into the current message as the fault handler
    if (errorHandler != null) {
        Mediator errorHandlerMediator = synCtx.getSequence(errorHandler);
        if (errorHandlerMediator != null) {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Setting the onError handler : " + errorHandler + " for the endpoint : " + endpointName);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
        } else {
            log.warn("onError handler sequence : " + errorHandler + " for : " + endpointName + " cannot be found");
        }
    }
    // register this as the immediate fault handler for this message.
    synCtx.pushFaultHandler(this);
    // add this as the last endpoint to process this message - used by statistics counting code
    synCtx.setProperty(SynapseConstants.LAST_ENDPOINT, this);
    // set message level metrics collector
    org.apache.axis2.context.MessageContext axis2Ctx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    axis2Ctx.setProperty(BaseConstants.METRICS_COLLECTOR, metricsMBean);
    if (contentAware) {
        try {
            RelayUtils.buildMessage(((Axis2MessageContext) synCtx).getAxis2MessageContext(), false);
            axis2Ctx.setProperty(RelayConstants.FORCE_RESPONSE_EARLY_BUILD, Boolean.TRUE);
            if (forceBuildMC) {
                ((Axis2MessageContext) synCtx).getAxis2MessageContext().getEnvelope().build();
            }
        } catch (Exception e) {
            handleException("Error while building message", e);
        }
    }
    evaluateProperties(synCtx);
    // if the envelope preserving set build the envelope
    MediatorProperty preserveEnv = getProperty(SynapseConstants.PRESERVE_ENVELOPE);
    if (preserveEnv != null && JavaUtils.isTrueExplicitly(preserveEnv.getValue() != null ? preserveEnv.getValue() : preserveEnv.getEvaluatedExpression(synCtx))) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Preserving the envelope by building it before " + "sending, since it is explicitly set");
        }
        synCtx.getEnvelope().build();
    }
    // Send the message through this endpoint
    synCtx.getEnvironment().send(definition, synCtx);
    if (isStatisticsEnabled) {
        CloseEventCollector.closeEntryEvent(synCtx, getReportingName(), ComponentType.ENDPOINT, statisticReportingIndex, false);
    }
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) Mediator(org.apache.synapse.Mediator) SynapseException(org.apache.synapse.SynapseException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 73 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class DynamicLoadbalanceEndpoint method setupTransportHeaders.

/**
 * Adding the X-Forwarded-For/X-Originating-IP headers to the outgoing message.
 *
 * @param synCtx Current message context
 */
protected void setupTransportHeaders(MessageContext synCtx) {
    Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    if (headers != null && headers instanceof Map) {
        Map headersMap = (Map) headers;
        String xForwardFor = (String) headersMap.get(NhttpConstants.HEADER_X_FORWARDED_FOR);
        String remoteHost = (String) axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.REMOTE_ADDR);
        if (xForwardFor != null && !"".equals(xForwardFor)) {
            StringBuilder xForwardedForString = new StringBuilder();
            xForwardedForString.append(xForwardFor);
            if (remoteHost != null && !"".equals(remoteHost)) {
                xForwardedForString.append(",").append(remoteHost);
            }
            headersMap.put(NhttpConstants.HEADER_X_FORWARDED_FOR, xForwardedForString.toString());
        } else {
            headersMap.put(NhttpConstants.HEADER_X_FORWARDED_FOR, remoteHost);
        }
        // Extracting information of X-Originating-IP
        if (headersMap.get(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_1) != null) {
            headersMap.put(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_1, headersMap.get(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_1));
        } else if (headersMap.get(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_2) != null) {
            headersMap.put(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_2, headersMap.get(NhttpConstants.HEADER_X_ORIGINATING_IP_FORM_2));
        }
    }
}
Also used : Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 74 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class ThrottleMediatorTest method createLightweightSynapseMessageContext.

public static MessageContext createLightweightSynapseMessageContext(String payload) throws Exception {
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    SynapseConfiguration config = new SynapseConfiguration();
    SynapseEnvironment env = new Axis2SynapseEnvironment(config);
    MessageContext synMc = new Axis2MessageContext(mc, config, env);
    SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
    OMDocument omDoc = OMAbstractFactory.getSOAP11Factory().createOMDocument();
    omDoc.addChild(envelope);
    envelope.getBody().addChild(createOMElement(payload));
    synMc.setEnvelope(envelope);
    return synMc;
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) OMDocument(org.apache.axiom.om.OMDocument)

Example 75 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class SynapseConfiguration method getTenantDomain.

private String getTenantDomain(SynapseEnvironment synapseEnvironment) {
    TenantInfoConfigurator configurator = synapseEnvironment.getTenantInfoConfigurator();
    if (configurator != null) {
        org.apache.axis2.context.MessageContext axisMessageContext = new org.apache.axis2.context.MessageContext();
        MessageContext messageContext = new Axis2MessageContext(axisMessageContext, this, synapseEnvironment);
        configurator.extractTenantInfo(messageContext);
        if (messageContext.getProperty("tenant.info.domain") != null) {
            return (String) messageContext.getProperty("tenant.info.domain");
        }
    }
    return null;
}
Also used : TenantInfoConfigurator(org.apache.synapse.carbonext.TenantInfoConfigurator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)92 MessageContext (org.apache.synapse.MessageContext)50 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 Map (java.util.Map)23 HashMap (java.util.HashMap)20 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)19 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 Test (org.junit.Test)16 OMElement (org.apache.axiom.om.OMElement)15 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)14 SynapseException (org.apache.synapse.SynapseException)14 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)13 ArrayList (java.util.ArrayList)12 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 EndpointReference (org.apache.axis2.addressing.EndpointReference)10 SynapseLog (org.apache.synapse.SynapseLog)9 Endpoint (org.apache.synapse.endpoints.Endpoint)9 OperationContext (org.apache.axis2.context.OperationContext)8 AxisFault (org.apache.axis2.AxisFault)7 Entry (org.apache.synapse.config.Entry)7