Search in sources :

Example 1 with Axis2MessageContext

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

the class EnqueueMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog log = getLog(synCtx);
    if (log.isTraceOrDebugEnabled()) {
        log.traceOrDebug("Start: enqueue mediator");
    }
    assert executorName != null : "executor name shouldn't be null";
    PriorityExecutor executor = synCtx.getConfiguration().getPriorityExecutors().get(executorName);
    if (executor == null) {
        log.auditWarn("Cannot find executor " + executorName + ". Using existing thread for mediation");
        Mediator m = synCtx.getSequence(sequenceName);
        if (m != null && m instanceof SequenceMediator) {
            return m.mediate(synCtx);
        } else {
            handleException("Sequence cannot be found : " + sequenceName, synCtx);
            return false;
        }
    }
    Mediator m = synCtx.getSequence(sequenceName);
    if (m != null && m instanceof SequenceMediator) {
        MediatorWorker worker = new MediatorWorker(m, synCtx);
        try {
            // execute with the given priority
            executor.execute(worker, priority);
        } catch (RejectedExecutionException ex) {
            // if RejectedExecutionException, jump to fault handler
            handleException("Unable to process message in priority executor " + executorName + " with priority " + priority + ". Thread pool exhausted.", synCtx);
        }
        // with the nio transport, this causes the listener not to write a 202
        // Accepted response, as this implies that Synapse does not yet know if
        // a 202 or 200 response would be written back.
        ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
        if (log.isTraceOrDebugEnabled()) {
            log.traceOrDebug("End: enqueue mediator");
        }
        return true;
    } else {
        handleException("Sequence cannot be found : " + sequenceName, synCtx);
        return false;
    }
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) MediatorWorker(org.apache.synapse.mediators.MediatorWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with Axis2MessageContext

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

the class PropertyMediator method mediate.

/**
 * Sets a property into the current (local) Synapse Context or into the Axis Message Context
 * or into Transports Header and removes above properties from the corresponding locations.
 *
 * @param synCtx the message context
 * @return true always
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Property mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (action == ACTION_SET) {
        Object resultValue = getResultValue(synCtx);
        // choose part of it
        if (resultValue instanceof String && pattern != null) {
            resultValue = getMatchedValue((String) resultValue, synLog);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Setting property : " + name + " at scope : " + (scope == null ? "default" : scope) + " to : " + resultValue + " (i.e. " + (value != null ? "constant : " + value : "result of expression : " + expression) + ")");
        }
        if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
            // Setting property into the  Synapse Context
            if (resultValue != null && resultValue instanceof OMElement) {
                ((OMElement) resultValue).build();
            }
            synCtx.setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting property into the  Axis2 Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.setProperty(name, resultValue);
            MediatorPropertyUtils.handleSpecialProperties(name, resultValue, axis2MessageCtx);
        } else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting property into the  Axis2 Message Context client options
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.getOptions().setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting Transport Headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            /*
                 * if null is passed as header value at AbstractHTTPSender in Axis2 when header
                 * value is read causes a null-pointer issue
                 */
            if (resultValue == null) {
                resultValue = "";
            }
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                headersMap.put(name, resultValue);
            }
            if (headers == null) {
                Map headersMap = new HashMap();
                headersMap.put(name, resultValue);
                axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
            }
        } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting Transport Headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2smc.getAxis2MessageContext().getOperationContext().setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope) && synCtx instanceof Axis2MessageContext) {
            String[] args = name.split("@");
            String path = "";
            String propertyName = "";
            // with the property mentioned and the value as its value
            if (args.length == 1) {
                path = args[0];
            } else if (args.length == 2) {
                path = args[0];
                propertyName = args[1];
            }
            Registry registry = synCtx.getConfiguration().getRegistry();
            registry.newNonEmptyResource(path, false, CONTENT_TYPE, resultValue.toString(), propertyName);
        }
    } else {
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Removing property : " + name + " (scope:" + (scope == null ? "default" : scope) + ")");
        }
        if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
            // Removing property from the  Synapse Context
            Set pros = synCtx.getPropertyKeySet();
            if (pros != null) {
                pros.remove(name);
            }
        } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing property from the Axis2 Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.removeProperty(name);
        } else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing property from the Axis2-client Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            // Property value is set to null since axis2MessageCtx.getOptions()
            // does not have an option to remove properties
            axis2MessageCtx.getOptions().setProperty(name, null);
        } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing transport headers
            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;
                headersMap.remove(name);
            } else {
                synLog.traceOrDebug("No transport headers found for the message");
            }
        } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing operation scope headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            OperationContext axis2oc = axis2MessageCtx.getOperationContext();
            axis2oc.removeProperty(name);
        }
    }
    synLog.traceOrDebug("End : Property mediator");
    return true;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) Set(java.util.Set) HashMap(java.util.HashMap) OMElement(org.apache.axiom.om.OMElement) Registry(org.apache.synapse.registry.Registry) SynapseLog(org.apache.synapse.SynapseLog) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with Axis2MessageContext

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

the class RespondMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Respond Mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    synCtx.setTo(null);
    synCtx.setResponse(true);
    Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
    axis2MessageCtx.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
    Axis2Sender.sendBack(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("End : Respond Mediator");
    }
    return false;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 4 with Axis2MessageContext

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

the class Target method handleProperty.

/**
 * Handles enrichment of properties when defined as a custom type
 *
 * @param xpath          expression to get property
 * @param synContext     messageContext used in the mediation
 * @param sourceNodeList node list which used to change the target
 * @param synLog         the Synapse log to use
 */
private void handleProperty(SynapseXPath xpath, MessageContext synContext, ArrayList<OMNode> sourceNodeList, SynapseLog synLog) {
    String scope = XMLConfigConstants.SCOPE_DEFAULT;
    Pattern p = Pattern.compile(XPATH_PROPERTY_PATTERN);
    Matcher m = p.matcher(xpath.getExpression());
    List<String> propList = new ArrayList();
    while (m.find()) {
        propList.add(StringUtils.substringBetween(m.group(), "\'", "\'"));
    }
    if (propList.size() > 1) {
        property = propList.get(1);
        scope = propList.get(0);
    } else {
        property = propList.get(0);
    }
    OMElement documentElement = null;
    Object propertyObj = null;
    Axis2MessageContext axis2smc = (Axis2MessageContext) synContext;
    if (action != null && property != null) {
        if (XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
            propertyObj = synContext.getProperty(property);
        } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope)) {
            propertyObj = axis2smc.getAxis2MessageContext().getProperty(property);
        } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope)) {
            propertyObj = axis2smc.getAxis2MessageContext().getOperationContext().getProperty(property);
        }
        if (propertyObj != null && propertyObj instanceof OMElement && action.equals(ACTION_ADD_CHILD)) {
            documentElement = (OMElement) propertyObj;
            documentElement = documentElement.cloneOMElement();
            // logic should valid only when adding child elements, and other cases
            // such as sibling and replacement using the else condition
            insertElement(sourceNodeList, documentElement, synLog);
            this.setProperty(scope, synContext, documentElement);
        } else {
            this.setProperty(scope, synContext, sourceNodeList);
        }
    } else {
        this.setProperty(scope, synContext, sourceNodeList);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 5 with Axis2MessageContext

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

the class FailoverForwardingService method dispatch.

/**
 * Sends the message to a given message store.
 *
 * @param messageContext synapse {@link MessageContext} to be sent
 */
public void dispatch(MessageContext messageContext) {
    if (log.isDebugEnabled()) {
        log.debug("Sending the message to client with message processor [" + messageProcessor.getName() + "]");
    }
    SOAPEnvelope originalEnvelop = messageContext.getEnvelope();
    if (targetMessageStoreName != null) {
        try {
            // Send message to the client
            while (!isSuccessful && !isTerminated) {
                try {
                    // For each retry we need to have a fresh copy of the
                    // actual message. otherwise retry may not
                    // work as expected.
                    messageContext.setEnvelope(MessageHelper.cloneSOAPEnvelope(originalEnvelop));
                    // 
                    OMElement firstChild = null;
                    org.apache.axis2.context.MessageContext origAxis2Ctx = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
                    if (JsonUtil.hasAJsonPayload(origAxis2Ctx)) {
                        firstChild = origAxis2Ctx.getEnvelope().getBody().getFirstElement();
                    }
                    // OMSourcedElemImpl correctly.
                    if (JsonUtil.hasAJsonPayload(firstChild)) {
                        // 
                        OMElement clonedFirstElement = messageContext.getEnvelope().getBody().getFirstElement();
                        if (clonedFirstElement != null) {
                            clonedFirstElement.detach();
                            messageContext.getEnvelope().getBody().addChild(firstChild);
                        }
                    }
                    if (messageConsumer != null && messageConsumer.isAlive() && targetMessageStoreName != null) {
                        targetMessageProducer = synapseEnvironment.getSynapseConfiguration().getMessageStore(targetMessageStoreName).getProducer();
                        if (targetMessageProducer != null) {
                            isSuccessful = targetMessageProducer.storeMessage(messageContext);
                        } else {
                            isSuccessful = false;
                        }
                    }
                } catch (Exception e) {
                    log.error("Message store messageSender of message processor [" + this.messageProcessor.getName() + "] failed to send message to the target message store");
                    sendThroughFaultSeq(messageContext);
                }
                if (isSuccessful) {
                    messageConsumer.ack();
                    attemptCount = 0;
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully sent the message to message store [" + targetMessageStoreName + "]" + " with message processor [" + messageProcessor.getName() + "]");
                    }
                    if (messageProcessor.isPaused()) {
                        this.messageProcessor.resumeService();
                        log.info("Resuming the service of message processor [" + messageProcessor.getName() + "]");
                    }
                } else {
                    // Then we have to retry sending the message to the target
                    // store.
                    prepareToRetry(messageContext);
                }
            }
        } catch (Exception e) {
            log.error("Message processor [" + messageProcessor.getName() + "] failed to send the message to target store", e);
        }
    } else {
        /*
			 * No Target message store defined for the Message So we do not have a
			 * place to deliver.
			 * Here we log a warning and remove the message
			 * this by implementing a target inferring
			 * mechanism.
			 */
        log.warn("Property " + FailoverForwardingProcessorConstants.TARGET_MESSAGE_STORE + " not found in the message context , Hence removing the message ");
        messageConsumer.ack();
    }
    return;
}
Also used : OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseException(org.apache.synapse.SynapseException) StoreForwardException(org.apache.synapse.message.StoreForwardException) 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