Search in sources :

Example 6 with MediatorFaultHandler

use of org.apache.synapse.mediators.MediatorFaultHandler in project wso2-synapse by wso2.

the class ContinuationStackManager method pushRootFaultHandlerForSequence.

/**
 * Find the correct root fault handler for named sequences.
 *
 * If the message is initiated from a proxy, we need to assign the proxy fault sequence.
 * If the message is initiated from a API Resource, we need to assign the resource fault sequence.
 *
 * @param synCtx message context
 */
private static void pushRootFaultHandlerForSequence(MessageContext synCtx) {
    // For Proxy services
    String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
    if (proxyName != null && !"".equals(proxyName)) {
        ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
        if (proxyService != null) {
            proxyService.registerFaultHandler(synCtx);
        } else {
            handleException("Proxy service : " + proxyName + " not found");
        }
        return;
    }
    // For APIs
    String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
    if (apiName != null && !"".equals(apiName)) {
        API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
        if (api != null) {
            String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
            Resource resource = api.getResource(resourceName);
            if (resource != null) {
                resource.registerFaultHandler(synCtx);
            } else {
                handleException("Resource : " + resourceName + " not found");
            }
        } else {
            handleException("REST API : " + apiName + " not found");
        }
        return;
    }
    // For main sequence/MessageInjector etc, push the default fault handler
    synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) ProxyService(org.apache.synapse.core.axis2.ProxyService) Resource(org.apache.synapse.rest.Resource) API(org.apache.synapse.rest.API)

Example 7 with MediatorFaultHandler

use of org.apache.synapse.mediators.MediatorFaultHandler in project wso2-synapse by wso2.

the class MessageInjector method execute.

/**
 * This will be invoked by the scheduler to inject the message
 * in to the SynapseEnvironment
 */
public void execute() {
    if (log.isDebugEnabled()) {
        log.debug("execute");
    }
    if (synapseEnvironment == null) {
        handleError("Synapse Environment not set");
        return;
    }
    if (synapseEnvironment.getTaskManager() != null && !synapseEnvironment.getTaskManager().isInitialized()) {
        log.warn("Task Manager not initialized. Not executing the cycle");
        return;
    }
    if (message == null && registryKey == null) {
        handleError("message or registry-key not set");
        return;
    }
    if (INJECT_TO_PROXY.equalsIgnoreCase(injectTo)) {
        if (proxyName == null || proxyName.equals("")) {
            handleError("Proxy service name not specified");
        }
        // Prepare axis2 message context
        org.apache.axis2.context.MessageContext axis2MsgCtx = new org.apache.axis2.context.MessageContext();
        ConfigurationContext configurationContext = ((Axis2SynapseEnvironment) synapseEnvironment).getAxis2ConfigurationContext();
        axis2MsgCtx.setConfigurationContext(configurationContext);
        axis2MsgCtx.setIncomingTransportName(Constants.TRANSPORT_LOCAL);
        axis2MsgCtx.setServerSide(true);
        axis2MsgCtx.setMessageID(UIDGenerator.generateURNString());
        try {
            AxisService axisService = configurationContext.getAxisConfiguration().getService(proxyName);
            if (axisService == null) {
                handleError("Proxy Service: " + proxyName + " not found");
            }
            axis2MsgCtx.setAxisService(axisService);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while attempting to find the Proxy Service");
        }
        if (to != null) {
            axis2MsgCtx.setTo(new EndpointReference(to));
        }
        SOAPEnvelope envelope = null;
        if (format == null) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope();
        } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (POX_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
        } else if (GET_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
            axis2MsgCtx.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
        } else {
            handleError("incorrect format specified");
        }
        try {
            PayloadHelper.setXMLPayload(envelope, message.cloneOMElement());
            axis2MsgCtx.setEnvelope(envelope);
        } catch (AxisFault axisFault) {
            handleError("Error in setting the message payload : " + message);
        }
        if (soapAction != null) {
            axis2MsgCtx.setSoapAction(soapAction);
        }
        try {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to proxy service : " + proxyName);
            }
            AxisEngine.receive(axis2MsgCtx);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while invoking proxy service : " + proxyName);
        }
    } else {
        MessageContext mc = synapseEnvironment.createMessageContext();
        mc.setMessageID(UIDGenerator.generateURNString());
        mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
        if (to != null) {
            mc.setTo(new EndpointReference(to));
        }
        if (registryKey == null) {
            if (format == null) {
                PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
            } else {
                try {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
                } catch (AxisFault axisFault) {
                    handleError("Error in setting the message payload : " + message);
                }
            }
        } else {
            Object entry = mc.getEntry(registryKey);
            if (entry == null) {
                handleError("Key " + registryKey + " not found ");
            }
            String text = "";
            if (entry instanceof OMElement) {
                OMElement e = (OMElement) entry;
                removeIndentations(e);
                text = e.toString();
            } else if (entry instanceof OMText) {
                text = ((OMText) entry).getText();
            } else if (entry instanceof String) {
                text = (String) entry;
            }
            OMElement omXML = null;
            try {
                omXML = AXIOMUtil.stringToOM(text);
                if (format == null) {
                    PayloadHelper.setXMLPayload(mc, omXML);
                } else {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, omXML);
                }
            } catch (XMLStreamException e) {
                handleError("Error parsing XML for JSON conversion, please check your property values return valid XML");
            } catch (AxisFault axisFault) {
                handleError("Error in setting the message payload : " + omXML);
            }
        }
        if (soapAction != null) {
            mc.setSoapAction(soapAction);
        }
        // Adding runtime properties to SynapseMessageContext, if exists
        if (runtimeProperties != null && runtimeProperties.size() > 0) {
            for (Map.Entry<String, Object> entry : runtimeProperties.entrySet()) {
                mc.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (INJECT_TO_SEQUENCE.equalsIgnoreCase(injectTo)) {
            if (sequenceName == null || sequenceName.equals("")) {
                handleError("Sequence name not specified");
            }
            SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(sequenceName);
            if (seq != null) {
                if (log.isDebugEnabled()) {
                    log.debug("injecting message to sequence : " + sequenceName);
                }
                mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
                synapseEnvironment.injectAsync(mc, seq);
            } else {
                handleError("Sequence: " + sequenceName + " not found");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to main sequence");
            }
            synapseEnvironment.injectMessage(mc);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) AxisService(org.apache.axis2.description.AxisService) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) XMLStreamException(javax.xml.stream.XMLStreamException) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with MediatorFaultHandler

use of org.apache.synapse.mediators.MediatorFaultHandler in project wso2-synapse by wso2.

the class ProxyService method registerFaultHandler.

/**
 * Register the fault handler for the message context
 *
 * @param synCtx Message Context
 */
public void registerFaultHandler(MessageContext synCtx) {
    boolean traceOn = trace();
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    if (targetFaultSequence != null) {
        Mediator faultSequence = synCtx.getSequence(targetFaultSequence);
        if (faultSequence != null) {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Setting the fault-sequence to : " + faultSequence);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getSequence(targetFaultSequence)));
        } else {
            // fault sequence and the message mediation can still continue
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "Unable to find fault-sequence : " + targetFaultSequence + "; using default fault sequence");
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
        }
    } else if (targetInLineFaultSequence != null) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Setting specified anonymous fault-sequence for proxy");
        }
        synCtx.pushFaultHandler(new MediatorFaultHandler(targetInLineFaultSequence));
    } else {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Setting default fault-sequence for proxy");
        }
        synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
    }
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 9 with MediatorFaultHandler

use of org.apache.synapse.mediators.MediatorFaultHandler in project wso2-synapse by wso2.

the class SynapseCallbackReceiver method registerFaultHandler.

private void registerFaultHandler(org.apache.synapse.MessageContext synCtx) {
    String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
    if (proxyName == null || "".equals(proxyName)) {
        synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
    }
    ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
    if (proxyService != null) {
        proxyService.registerFaultHandler(synCtx);
    }
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler)

Example 10 with MediatorFaultHandler

use of org.apache.synapse.mediators.MediatorFaultHandler 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)

Aggregations

MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)10 Mediator (org.apache.synapse.Mediator)5 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)4 MessageContext (org.apache.synapse.MessageContext)3 Stack (java.util.Stack)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 SynapseLog (org.apache.synapse.SynapseLog)2 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)2 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMElement (org.apache.axiom.om.OMElement)1 OMText (org.apache.axiom.om.OMText)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 AxisFault (org.apache.axis2.AxisFault)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 AxisService (org.apache.axis2.description.AxisService)1 TransportInDescription (org.apache.axis2.description.TransportInDescription)1