Search in sources :

Example 1 with FaultHandler

use of org.apache.synapse.FaultHandler in project wso2-synapse by wso2.

the class MessageHelper method cloneMessageContext.

/**
 * This method will simulate cloning the message context and creating an exact copy of the
 * passed message. One should use this method with care; that is because, inside the new MC,
 * most of the attributes of the MC like opCtx and so on are still kept as references inside
 * the axis2 MessageContext for performance improvements. (Note: U dont have to worrie
 * about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
 * @param synCtx - this will be cloned
 * @param cloneSoapEnvelope whether to clone the soap envelope
 * @return cloned Synapse MessageContext
 * @throws AxisFault if there is a failure in creating the new Synapse MC or in a failure in
 *          clonning the underlying axis2 MessageContext
 *
 * @see MessageHelper#cloneAxis2MessageContext
 */
public static MessageContext cloneMessageContext(MessageContext synCtx, boolean cloneSoapEnvelope) throws AxisFault {
    // creates the new MessageContext and clone the internal axis2 MessageContext
    // inside the synapse message context and place that in the new one
    MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
    Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
    axis2MC.setAxis2MessageContext(cloneAxis2MessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext(), cloneSoapEnvelope));
    newCtx.setConfiguration(synCtx.getConfiguration());
    newCtx.setEnvironment(synCtx.getEnvironment());
    newCtx.setContextEntries(synCtx.getContextEntries());
    // set the parent correlation details to the cloned MC -
    // for the use of aggregation like tasks
    newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
    // copying the core parameters of the synapse MC
    newCtx.setTo(synCtx.getTo());
    newCtx.setReplyTo(synCtx.getReplyTo());
    newCtx.setSoapAction(synCtx.getSoapAction());
    newCtx.setWSAAction(synCtx.getWSAAction());
    newCtx.setResponse(synCtx.isResponse());
    // copy all the synapse level properties to the newCtx
    for (Object o : synCtx.getPropertyKeySet()) {
        // If there are non String keyed properties neglect them rather than trow exception
        if (o instanceof String) {
            String strkey = (String) o;
            Object obj = synCtx.getProperty(strkey);
            if (obj instanceof String) {
            // No need to do anything since Strings are immutable
            } else if (obj instanceof ArrayList) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Started for  ArrayList property: " + strkey + ".");
                }
                // Call this method to deep clone ArrayList
                obj = cloneArrayList((ArrayList) obj);
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Ended for  ArrayList property: " + strkey + ".");
                }
            } else if (obj instanceof Stack && strkey.equals(SynapseConstants.SYNAPSE__FUNCTION__STACK)) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for Template function stack");
                }
                obj = getClonedTemplateStack((Stack<TemplateContext>) obj);
            } else if (obj instanceof OMElement) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for OMElement");
                }
                obj = (OMElement) ((OMElement) obj).cloneOMElement();
            } else if (obj instanceof ResponseState) {
            // do nothing and let the same reference to go to the cloned context
            } else {
                /**
                 * Need to add conditions according to type if found in
                 * future
                 */
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
                }
            }
            newCtx.setProperty(strkey, obj);
        }
    }
    // Make deep copy of fault stack so that parent will not be lost it's fault stack
    Stack<FaultHandler> faultStack = synCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                newCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
    if (functionStack != null) {
        newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
    }
    if (log.isDebugEnabled()) {
        log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
    }
    // Copy ContinuationStateStack from original MC to the new MC
    if (synCtx.isContinuationEnabled()) {
        Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
        newCtx.setContinuationEnabled(true);
        for (ContinuationState continuationState : continuationStates) {
            if (continuationState != null) {
                newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
            }
        }
    }
    newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
    return newCtx;
}
Also used : ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) ContinuationState(org.apache.synapse.ContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) ResponseState(org.apache.synapse.core.axis2.ResponseState) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) FaultHandler(org.apache.synapse.FaultHandler) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with FaultHandler

use of org.apache.synapse.FaultHandler in project wso2-synapse by wso2.

the class MessageHelper method cloneMessageContextForAggregateMediator.

public static MessageContext cloneMessageContextForAggregateMediator(MessageContext synCtx) throws AxisFault {
    // creates the new MessageContext and clone the internal axis2 MessageContext
    // inside the synapse message context and place that in the new one
    MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
    Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
    axis2MC.setAxis2MessageContext(cloneAxis2MessageContextForAggregate(((Axis2MessageContext) synCtx).getAxis2MessageContext()));
    newCtx.setConfiguration(synCtx.getConfiguration());
    newCtx.setEnvironment(synCtx.getEnvironment());
    newCtx.setContextEntries(synCtx.getContextEntries());
    // set the parent correlation details to the cloned MC -
    // for the use of aggregation like tasks
    newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
    // copying the core parameters of the synapse MC
    newCtx.setTo(synCtx.getTo());
    newCtx.setReplyTo(synCtx.getReplyTo());
    newCtx.setSoapAction(synCtx.getSoapAction());
    newCtx.setWSAAction(synCtx.getWSAAction());
    newCtx.setResponse(synCtx.isResponse());
    // copy all the synapse level properties to the newCtx
    for (Object o : synCtx.getPropertyKeySet()) {
        // throw exception
        if (o instanceof String) {
            /**
             * Clone the properties and add to new context
             * If not cloned can give errors in target configuration
             */
            String strkey = (String) o;
            Object obj = synCtx.getProperty(strkey);
            if (obj instanceof String) {
            // No need to do anything since Strings are immutable
            } else if (obj instanceof ArrayList) {
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone Started for  ArrayList property: " + strkey + ".");
                }
                // Call this method to deep clone ArrayList
                obj = cloneArrayList((ArrayList) obj);
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone Ended for  ArrayList property: " + strkey + ".");
                }
            } else {
                /**
                 * Need to add conditions according to type if found in
                 * future
                 */
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
                }
            }
            newCtx.setProperty(strkey, obj);
        }
    }
    // Make deep copy of fault stack so that parent will not be lost it's fault stack
    Stack<FaultHandler> faultStack = synCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                newCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
    if (functionStack != null) {
        newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
    }
    if (log.isDebugEnabled()) {
        log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
    }
    // Copy ContinuationStateStack from original MC to the new MC
    if (synCtx.isContinuationEnabled()) {
        Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
        newCtx.setContinuationEnabled(true);
        for (ContinuationState continuationState : continuationStates) {
            if (continuationState != null) {
                newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
            }
        }
    }
    newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
    return newCtx;
}
Also used : ArrayList(java.util.ArrayList) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) ContinuationState(org.apache.synapse.ContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) FaultHandler(org.apache.synapse.FaultHandler) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with FaultHandler

use of org.apache.synapse.FaultHandler in project wso2-synapse by wso2.

the class SynapseCallbackReceiver method handleMessage.

/**
 * Handle the response or error (during a failed send) message received for an outgoing request
 *
 * @param messageID        Request message ID
 * @param response         the Axis2 MessageContext that has been received and has to be handled
 * @param synapseOutMsgCtx the corresponding (outgoing) Synapse MessageContext for the above
 *                         Axis2 MC, that holds Synapse specific information such as the error
 *                         handler stack and local properties etc.
 * @throws AxisFault       if the message cannot be processed
 */
private void handleMessage(String messageID, MessageContext response, org.apache.synapse.MessageContext synapseOutMsgCtx, AsyncCallback callback) throws AxisFault {
    // apply the tenant information to the out message context
    TenantInfoConfigurator configurator = synapseOutMsgCtx.getEnvironment().getTenantInfoConfigurator();
    if (configurator != null) {
        configurator.applyTenantInfo(synapseOutMsgCtx);
    }
    Boolean isConcurrencyThrottleEnabled = (Boolean) synapseOutMsgCtx.getProperty(SynapseConstants.SYNAPSE_CONCURRENCY_THROTTLE);
    if (isConcurrencyThrottleEnabled != null && isConcurrencyThrottleEnabled) {
        ConcurrentAccessController concurrentAccessController = (ConcurrentAccessController) synapseOutMsgCtx.getProperty(SynapseConstants.SYNAPSE_CONCURRENT_ACCESS_CONTROLLER);
        int available = concurrentAccessController.incrementAndGet();
        int concurrentLimit = concurrentAccessController.getLimit();
        if (log.isDebugEnabled()) {
            log.debug("Concurrency Throttle : Connection returned" + " :: " + available + " of available of " + concurrentLimit + " connections");
        }
        ConcurrentAccessReplicator concurrentAccessReplicator = (ConcurrentAccessReplicator) synapseOutMsgCtx.getProperty(SynapseConstants.SYNAPSE_CONCURRENT_ACCESS_REPLICATOR);
        String throttleKey = (String) synapseOutMsgCtx.getProperty(SynapseConstants.SYNAPSE_CONCURRENCY_THROTTLE_KEY);
        if (concurrentAccessReplicator != null) {
            concurrentAccessReplicator.replicate(throttleKey, true);
        }
    }
    Object o = response.getProperty(SynapseConstants.SENDING_FAULT);
    if (o != null && Boolean.TRUE.equals(o)) {
        // This path hits with a fault. Sequence mediator threads should not remove faultSequence.
        // SynapseCallbackReceiver thread should handle the faultStack.
        Pipe pipe = (Pipe) ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext().getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
        if (pipe != null && pipe.isSerializationComplete()) {
            NHttpServerConnection conn = (NHttpServerConnection) ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext().getProperty("pass-through.Source-Connection");
            SourceConfiguration sourceConfiguration = (SourceConfiguration) ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext().getProperty("PASS_THROUGH_SOURCE_CONFIGURATION");
            Pipe newPipe = new Pipe(conn, sourceConfiguration.getBufferFactory().getBuffer(), "source", sourceConfiguration);
            ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext().setProperty(PassThroughConstants.PASS_THROUGH_PIPE, newPipe);
        }
        // there is a sending fault. propagate the fault to fault handlers.
        Stack faultStack = synapseOutMsgCtx.getFaultStack();
        if (faultStack != null && !faultStack.isEmpty()) {
            // fault envelope
            try {
                synapseOutMsgCtx.getEnvelope().build();
            } catch (Exception x) {
                synapseOutMsgCtx.setEnvelope(response.getEnvelope());
            }
            Exception e = (Exception) response.getProperty(SynapseConstants.ERROR_EXCEPTION);
            synapseOutMsgCtx.setProperty(SynapseConstants.SENDING_FAULT, Boolean.TRUE);
            synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_CODE, response.getProperty(SynapseConstants.ERROR_CODE));
            synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_MESSAGE, response.getProperty(SynapseConstants.ERROR_MESSAGE));
            synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_DETAIL, response.getProperty(SynapseConstants.ERROR_DETAIL));
            synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_EXCEPTION, e);
            if (synapseOutMsgCtx.getEnvironment().isContinuationEnabled()) {
                synapseOutMsgCtx.setContinuationEnabled(true);
            }
            if (log.isDebugEnabled()) {
                log.debug("[Failed Request Message ID : " + messageID + "]" + " [New to be Retried Request Message ID : " + synapseOutMsgCtx.getMessageID() + "]");
            }
            int errorCode = (Integer) response.getProperty(SynapseConstants.ERROR_CODE);
            // If a timeout has occured and the timeout action of the callback is to discard the message
            if (errorCode == SynapseConstants.NHTTP_CONNECTION_TIMEOUT && callback.getTimeOutAction() == SynapseConstants.DISCARD) {
                // Do not execute any fault sequences. Discard message
                if (log.isWarnEnabled()) {
                    log.warn("Synapse timed out for the request with Message ID : " + messageID + ". Ignoring fault handlers since the timeout action is DISCARD");
                }
                faultStack.removeAllElements();
            } else {
                ((FaultHandler) faultStack.pop()).handleFault(synapseOutMsgCtx, null);
            }
        }
    } else {
        // there can always be only one instance of an Endpoint in the faultStack of a message
        // if the send was successful, so remove it before we proceed any further
        Stack faultStack = synapseOutMsgCtx.getFaultStack();
        Endpoint successfulEndpoint = null;
        if (faultStack != null && !faultStack.isEmpty() && faultStack.peek() instanceof Endpoint) {
            successfulEndpoint = (Endpoint) faultStack.pop();
        }
        if (log.isDebugEnabled()) {
            log.debug("Synapse received an asynchronous response message");
            log.debug("Received To: " + (response.getTo() != null ? response.getTo().getAddress() : "null"));
            log.debug("SOAPAction: " + (response.getSoapAction() != null ? response.getSoapAction() : "null"));
            log.debug("WSA-Action: " + (response.getWSAAction() != null ? response.getWSAAction() : "null"));
            String[] cids = null;
            try {
                cids = response.getAttachmentMap().getAllContentIDs();
            } catch (Exception ex) {
                // partially read stream could lead to corrupted attachment map and hence this exception
                // corrupted attachment map leads to inconsistent runtime exceptions and behavior
                // discard the attachment map for the fault handler invocation
                // ensure the successful completion for fault handler flow
                response.setAttachmentMap(null);
                log.error("Synapse encountered an exception when reading attachments from bytes stream. " + "Hence Attachments map is dropped from the message context.", ex);
            }
            if (cids != null && cids.length > 0) {
                for (String cid : cids) {
                    log.debug("Attachment : " + cid);
                }
            }
            log.debug("Body : \n" + response.getEnvelope());
        }
        MessageContext axisOutMsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
        // Processes 'Accept-Encoding'
        ResponseAcceptEncodingProcessor.process(response, axisOutMsgCtx);
        response.setServiceContext(null);
        response.setOperationContext(axisOutMsgCtx.getOperationContext());
        response.setAxisMessage(axisOutMsgCtx.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE));
        // set properties on response
        response.setServerSide(true);
        response.setProperty(SynapseConstants.ISRESPONSE_PROPERTY, Boolean.TRUE);
        response.setProperty(MessageContext.TRANSPORT_OUT, axisOutMsgCtx.getProperty(MessageContext.TRANSPORT_OUT));
        response.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, axisOutMsgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
        response.setTransportIn(axisOutMsgCtx.getTransportIn());
        response.setTransportOut(axisOutMsgCtx.getTransportOut());
        // response.setDoingREST(axisOutMsgCtx.isDoingREST()); This information already present, hence removing
        if (axisOutMsgCtx.isDoingMTOM() && (axisOutMsgCtx.getProperty(org.apache.axis2.Constants.Configuration.ENABLE_MTOM) == null || Boolean.getBoolean((String) axisOutMsgCtx.getProperty(org.apache.axis2.Constants.Configuration.ENABLE_MTOM)) == true)) {
            response.setDoingMTOM(true);
            response.setProperty(org.apache.axis2.Constants.Configuration.ENABLE_MTOM, org.apache.axis2.Constants.VALUE_TRUE);
        }
        if (axisOutMsgCtx.isDoingSwA()) {
            response.setDoingSwA(true);
            response.setProperty(org.apache.axis2.Constants.Configuration.ENABLE_SWA, org.apache.axis2.Constants.VALUE_TRUE);
        }
        // property state to original state.
        if (axisOutMsgCtx.getProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES) != null) {
            response.setProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES, axisOutMsgCtx.getProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES));
        } else {
            response.removeProperty(AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES);
        }
        Object messageType = axisOutMsgCtx.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE);
        if (!HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(messageType)) {
            // copy the message type property that's used by the out message to the
            // response message
            response.setProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE, messageType);
        }
        if (axisOutMsgCtx.getMessageID() != null) {
            response.setRelationships(new RelatesTo[] { new RelatesTo(axisOutMsgCtx.getMessageID()) });
        }
        response.setReplyTo(axisOutMsgCtx.getReplyTo());
        response.setFaultTo(axisOutMsgCtx.getFaultTo());
        if (axisOutMsgCtx.isPropertyTrue(NhttpConstants.IGNORE_SC_ACCEPTED)) {
            response.setProperty(NhttpConstants.FORCE_SC_ACCEPTED, Constants.VALUE_TRUE);
        }
        // axis2 client options still contains properties such as policy files used in
        // outgoing request. Need to remove those.
        removeUnwantedClientOptions(response);
        // create the synapse message context for the response
        Axis2MessageContext synapseInMessageContext = new Axis2MessageContext(response, synapseOutMsgCtx.getConfiguration(), synapseOutMsgCtx.getEnvironment());
        synapseInMessageContext.setResponse(true);
        Object obj = synapseOutMsgCtx.getProperty(SynapseConstants.FORCE_ERROR_PROPERTY);
        String errorOnSOAPFault = (String) obj;
        if (Constants.VALUE_TRUE.equals(errorOnSOAPFault) && successfulEndpoint != null) {
            if (log.isDebugEnabled()) {
                log.debug("FORCE_ERROR_ON_SOAP_FAULT is true, checking for SOAPFault");
            }
            try {
                RelayUtils.buildMessage(((Axis2MessageContext) synapseInMessageContext).getAxis2MessageContext(), true);
            } catch (Exception e) {
            // handleException("Error while building message", e, synapseInMessageContext);
            }
            if ((synapseInMessageContext.getEnvelope() != null) && synapseInMessageContext.getEnvelope().hasFault()) {
                if (log.isDebugEnabled()) {
                    log.debug("SOAPFault found in response message, forcing endpoint " + successfulEndpoint.getName() + " to fail");
                }
                // setup new pipe configuration..if failure happens (this will be setup as the source writer and during the TargetContext
                // clean up operation the writer will be reset and pull to the buffer
                MessageContext axis2OUTMC = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
                NHttpServerConnection conn = (NHttpServerConnection) axis2OUTMC.getProperty("pass-through.Source-Connection");
                if (conn != null) {
                    SourceConfiguration sourceConfiguration = (SourceConfiguration) axis2OUTMC.getProperty("PASS_THROUGH_SOURCE_CONFIGURATION");
                    Pipe pipe = new Pipe(conn, sourceConfiguration.getBufferFactory().getBuffer(), "source", sourceConfiguration);
                    axis2OUTMC.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe);
                }
                synapseOutMsgCtx.setProperty(SynapseConstants.SENDING_FAULT, Boolean.TRUE);
                synapseOutMsgCtx.setProperty(SynapseConstants.ERROR_CODE, SynapseConstants.ENDPOINT_CUSTOM_ERROR);
                boolean failOver = false;
                if (successfulEndpoint instanceof AbstractEndpoint) {
                    Endpoint endpoint = ((AbstractEndpoint) successfulEndpoint).getParentEndpoint();
                    if (endpoint != null && (endpoint instanceof FailoverEndpoint)) {
                        failOver = true;
                    }
                }
                for (Object key : synapseOutMsgCtx.getPropertyKeySet()) {
                    synapseInMessageContext.setProperty((String) key, synapseOutMsgCtx.getProperty((String) key));
                }
                if (failOver) {
                    // we may required to handle same message for failover cases only other than that
                    // should treat based on the incoming message
                    ((FaultHandler) successfulEndpoint).handleFault(synapseOutMsgCtx, null);
                } else {
                    faultStack = synapseOutMsgCtx.getFaultStack();
                    if (faultStack != null) {
                        synapseInMessageContext.getFaultStack().addAll(faultStack);
                        ((FaultHandler) successfulEndpoint).handleFault(synapseInMessageContext, null);
                    }
                }
                return;
            } else {
                successfulEndpoint.onSuccess();
            }
        } else if (successfulEndpoint != null) {
            successfulEndpoint.onSuccess();
        }
        synapseInMessageContext.setTo(new EndpointReference(AddressingConstants.Final.WSA_ANONYMOUS_URL));
        synapseInMessageContext.setTracingState(synapseOutMsgCtx.getTracingState());
        synapseInMessageContext.setMessageFlowTracingState(synapseOutMsgCtx.getMessageFlowTracingState());
        for (Object key : synapseOutMsgCtx.getPropertyKeySet()) {
            synapseInMessageContext.setProperty((String) key, synapseOutMsgCtx.getProperty((String) key));
        }
        // Copy SequenceCallStack from original MC to the new MC
        Boolean isContinuationCall = (Boolean) synapseOutMsgCtx.getProperty(SynapseConstants.CONTINUATION_CALL);
        if (isContinuationCall != null && isContinuationCall) {
            // Set the message direction
            if (!synapseOutMsgCtx.isResponse()) {
                synapseInMessageContext.setResponse(false);
            }
            Stack<ContinuationState> seqContinuationStates = synapseOutMsgCtx.getContinuationStateStack();
            for (int i = 0; i < seqContinuationStates.size(); i++) {
                synapseInMessageContext.pushContinuationState(seqContinuationStates.get(i));
            }
        }
        // If this response is related to session affinity endpoints -Server initiated session
        Dispatcher dispatcher = (Dispatcher) synapseOutMsgCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER);
        if (dispatcher != null && dispatcher.isServerInitiatedSession()) {
            dispatcher.updateSession(synapseInMessageContext);
        }
        // send the response message through the synapse mediation flow
        try {
            synapseOutMsgCtx.getEnvironment().injectMessage(synapseInMessageContext);
        } catch (Exception syne) {
            // introduced to handle runtime exceptions which are occurred inside Synapse handlers
            // partially read stream could lead to corrupted attachment map and hence this exception
            // corrupted attachment map leads to inconsistent runtime exceptions and behavior
            // discard the attachment map for the fault handler invocation
            // ensure the successful completion for fault handler flow
            // even we drop attachment map for both cases messages which have attachment /
            // messages which do not have attachments it would still not be any impact.
            // However setting attachment map to null for messages which do not have attachments is not required.
            // introduced due to the fact conflicts between Axiom exceptions for attachment/ non attachments cases
            // and performance impact that could cause of regular expression matching of exceptional stack traces.
            Axis2MessageContext axis2smc = (Axis2MessageContext) synapseInMessageContext;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            // Set correct status code
            axis2MessageCtx.setProperty(PassThroughConstants.HTTP_SC, HttpStatus.SC_INTERNAL_SERVER_ERROR);
            axis2MessageCtx.setAttachmentMap(null);
            Stack stack = synapseInMessageContext.getFaultStack();
            if (stack != null && stack.isEmpty()) {
                registerFaultHandler(synapseInMessageContext);
            }
            if (stack != null && !stack.isEmpty()) {
                ((FaultHandler) stack.pop()).handleFault(synapseInMessageContext, syne);
            } else {
                log.error("Synapse encountered an exception, " + "No error handlers found - [Message Dropped]\n" + syne.getMessage());
            }
        }
    }
}
Also used : Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher) FailoverEndpoint(org.apache.synapse.endpoints.FailoverEndpoint) RelatesTo(org.apache.axis2.addressing.RelatesTo) NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) Endpoint(org.apache.synapse.endpoints.Endpoint) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) FailoverEndpoint(org.apache.synapse.endpoints.FailoverEndpoint) ConcurrentAccessController(org.apache.synapse.commons.throttle.core.ConcurrentAccessController) MessageContext(org.apache.axis2.context.MessageContext) FaultHandler(org.apache.synapse.FaultHandler) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) Pipe(org.apache.synapse.transport.passthru.Pipe) Endpoint(org.apache.synapse.endpoints.Endpoint) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) FailoverEndpoint(org.apache.synapse.endpoints.FailoverEndpoint) ConcurrentAccessReplicator(org.apache.synapse.commons.throttle.core.ConcurrentAccessReplicator) Stack(java.util.Stack) EndpointReference(org.apache.axis2.addressing.EndpointReference) ContinuationState(org.apache.synapse.ContinuationState) TenantInfoConfigurator(org.apache.synapse.carbonext.TenantInfoConfigurator) SourceConfiguration(org.apache.synapse.transport.passthru.config.SourceConfiguration)

Example 4 with FaultHandler

use of org.apache.synapse.FaultHandler in project wso2-synapse by wso2.

the class TimeoutHandler method processCallbacks.

private void processCallbacks() {
    // clear all the expired sessions
    SALSessions.getInstance().clearSessions();
    // concurrently by the SynapseCallbackReceiver.
    synchronized (callbackStore) {
        if (callbackStore.size() > 0) {
            long currentTime = currentTime();
            List toRemove = new ArrayList();
            for (Object key : callbackStore.keySet()) {
                AsyncCallback callback = (AsyncCallback) callbackStore.get(key);
                if (callback == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("There is no callback for key :" + key);
                    }
                    continue;
                }
                if (callback.getTimeOutOn() <= currentTime) {
                    synchronized (callback) {
                        if (callback.isMarkedForRemoval()) {
                            return;
                        }
                        callback.setMarkedForRemoval();
                        toRemove.add(key);
                    }
                    if (callback.getTimeOutAction() != SynapseConstants.NONE) {
                        // activate the fault sequence of the current sequence mediator
                        MessageContext msgContext = callback.getSynapseOutMsgCtx();
                        /* Clear the pipe to prevent release of the associated writer buffer
                               to the buffer factory.
                               This is to prevent same buffer is getting released to both source
                               and target buffer factories. Otherwise when a late response arrives,
                               buffer is released to both factories and makes system unstable
                            */
                        ((Axis2MessageContext) msgContext).getAxis2MessageContext().removeProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                        // add an error code to the message context, so that error sequences
                        // can identify the cause of error
                        msgContext.setProperty(SynapseConstants.ERROR_CODE, SynapseConstants.HANDLER_TIME_OUT);
                        msgContext.setProperty(SynapseConstants.ERROR_MESSAGE, SEND_TIMEOUT_MESSAGE);
                        SOAPEnvelope soapEnvelope;
                        if (msgContext.isSOAP11()) {
                            soapEnvelope = OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope();
                            soapEnvelope.addChild(OMAbstractFactory.getSOAP11Factory().createSOAPBody());
                        } else {
                            soapEnvelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
                            soapEnvelope.addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
                        }
                        try {
                            msgContext.setEnvelope(soapEnvelope);
                        } catch (Throwable ex) {
                            log.error("Exception or Error occurred resetting SOAP Envelope", ex);
                            continue;
                        }
                        Stack<FaultHandler> faultStack = msgContext.getFaultStack();
                        if (!faultStack.isEmpty()) {
                            FaultHandler faultHandler = faultStack.pop();
                            if (faultHandler != null) {
                                try {
                                    faultHandler.handleFault(msgContext);
                                } catch (Throwable ex) {
                                    log.warn("Exception or Error occurred while " + "executing the fault handler", ex);
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
            for (Object key : toRemove) {
                AsyncCallback callback = (AsyncCallback) callbackStore.get(key);
                if (callback == null) {
                    // we will get here if we get a response from the Backend while clearing callbacks
                    continue;
                }
                if (!"true".equals(callback.getSynapseOutMsgCtx().getProperty(SynapseConstants.OUT_ONLY))) {
                    log.warn("Expiring message ID : " + key + "; dropping message after " + callback.getTimeoutType().toString() + " of : " + (callback.getTimeoutDuration() / 1000) + " seconds for " + getEndpointLogMessage(callback.getSynapseOutMsgCtx(), callback.getAxis2OutMsgCtx()) + ", " + getServiceLogMessage(callback.getSynapseOutMsgCtx()));
                }
                org.apache.synapse.MessageContext synapseOutMsgCtx = callback.getSynapseOutMsgCtx();
                ConcurrencyThrottlingUtils.decrementConcurrencyThrottleAccessController(synapseOutMsgCtx);
                callbackStore.remove(key);
                if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                    CallbackStatisticCollector.callbackCompletionEvent(callback.getSynapseOutMsgCtx(), (String) key);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) ArrayList(java.util.ArrayList) List(java.util.List) MessageContext(org.apache.synapse.MessageContext) MessageContext(org.apache.synapse.MessageContext) FaultHandler(org.apache.synapse.FaultHandler)

Example 5 with FaultHandler

use of org.apache.synapse.FaultHandler in project wso2-synapse by wso2.

the class IterateMediator method copyFaultyIteratedMessage.

/**
 * Copy fault stack and properties of the iteratedMsgCtx to synCtx
 *
 * @param synCtx         Original Synapse Message Context
 * @param iteratedMsgCtx cloned Message Context used for the iteration
 */
private void copyFaultyIteratedMessage(MessageContext synCtx, MessageContext iteratedMsgCtx) {
    // remove original fault stack
    synCtx.getFaultStack().clear();
    Stack<FaultHandler> faultStack = iteratedMsgCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                synCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    // copy all the String keyed synapse level properties to the Original synCtx
    for (Object keyObject : iteratedMsgCtx.getPropertyKeySet()) {
        /*
             * There can be properties added while executing the iterated sequential flow and
             * these may be accessed in the fault sequence, so updating string valued properties
             */
        if (keyObject instanceof String) {
            String stringKey = (String) keyObject;
            synCtx.setProperty(stringKey, iteratedMsgCtx.getProperty(stringKey));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FaultHandler(org.apache.synapse.FaultHandler)

Aggregations

FaultHandler (org.apache.synapse.FaultHandler)7 ArrayList (java.util.ArrayList)4 Stack (java.util.Stack)3 ContinuationState (org.apache.synapse.ContinuationState)3 MessageContext (org.apache.synapse.MessageContext)3 EndpointReference (org.apache.axis2.addressing.EndpointReference)2 SynapseException (org.apache.synapse.SynapseException)2 SeqContinuationState (org.apache.synapse.continuation.SeqContinuationState)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 TemplateContext (org.apache.synapse.mediators.template.TemplateContext)2 List (java.util.List)1 OMElement (org.apache.axiom.om.OMElement)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 RelatesTo (org.apache.axis2.addressing.RelatesTo)1 MessageContext (org.apache.axis2.context.MessageContext)1 NHttpServerConnection (org.apache.http.nio.NHttpServerConnection)1 TenantInfoConfigurator (org.apache.synapse.carbonext.TenantInfoConfigurator)1 ConcurrentAccessController (org.apache.synapse.commons.throttle.core.ConcurrentAccessController)1 ConcurrentAccessReplicator (org.apache.synapse.commons.throttle.core.ConcurrentAccessReplicator)1