Search in sources :

Example 91 with SynapseException

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

the class ForwardingService method tryToDispatchToEndpoint.

/**
 * Try to dispatch message to the given endpoint
 *
 * @param messageToDispatch MessageContext containing message to forward
 * @param endpoint                endpoint to forward message to
 */
private void tryToDispatchToEndpoint(MessageContext messageToDispatch, Endpoint endpoint) {
    isSuccessful = false;
    MessageContext outCtx = null;
    try {
        // For each retry we need to have a fresh copy of the original message
        messageToDispatch.setEnvelope(MessageHelper.cloneSOAPEnvelope(messageToDispatch.getEnvelope()));
        setSoapHeaderBlock(messageToDispatch);
        updateAxis2MessageContext(messageToDispatch);
        if (messageConsumer != null && messageConsumer.isAlive()) {
            outCtx = sender.send(endpoint, messageToDispatch);
        }
        // For Protocols like JMS etc no need of validating response
        if (isResponseValidationNotRequired) {
            isSuccessful = true;
            onForwardSuccess(endpoint);
        }
        // there is no response
        if (!isResponseValidationNotRequired && outCtx == null) {
            isSuccessful = true;
            onForwardSuccess(endpoint);
        }
        // there is a response (In message context) but failed to send with no exception thrown
        if (!isResponseValidationNotRequired && outCtx != null && "true".equals(outCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
            log.error("Blocking Sender Error " + outCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
            isSuccessful = false;
            onForwardFailure();
            // invoke fault sequence of MP
            sendThroughFaultSeq(outCtx);
            return;
        }
        // there is a response so check on status codes
        if (!isResponseValidationNotRequired && outCtx != null && validateResponse(outCtx)) {
            isSuccessful = true;
            sendThroughReplySeq(outCtx);
            onForwardSuccess(endpoint);
        }
    } catch (Exception e) {
        log.error("[ " + messageProcessor.getName() + " ] Error while forwarding message to endpoint " + targetEndpoint + ".", e);
        if (!isResponseValidationNotRequired && outCtx != null && e instanceof SynapseException && validateResponse(outCtx)) {
            isSuccessful = true;
            onForwardSuccess(endpoint);
        }
        if (!isResponseValidationNotRequired && outCtx != null && "true".equals(outCtx.getProperty(ForwardingProcessorConstants.BLOCKING_SENDER_ERROR))) {
            log.error("Blocking Sender Error " + outCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
            isSuccessful = false;
            onForwardFailure();
            // invoke fault sequence of MP
            sendThroughFaultSeq(outCtx);
        }
        if (!isResponseValidationNotRequired && outCtx != null && validateResponse(outCtx)) {
            isSuccessful = false;
            onForwardFailure();
            // invoke fault sequence of MP
            sendThroughFaultSeq(outCtx);
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) StoreForwardException(org.apache.synapse.message.StoreForwardException) SynapseException(org.apache.synapse.SynapseException) OMException(org.apache.axiom.om.OMException)

Example 92 with SynapseException

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

the class ForwardingService method init.

public void init(SynapseEnvironment se) throws SynapseException {
    // Setting up the JMS consumer here.
    try {
        setMessageConsumer();
    } catch (StoreForwardException e) {
        throw new SynapseException("Error while initializing consumer " + messageProcessor.getName(), e);
    }
    // Defaults to -1.
    Map<String, Object> parametersMap = messageProcessor.getParameters();
    if (parametersMap.get(MessageProcessorConstants.MAX_DELIVER_ATTEMPTS) != null) {
        maxDeliverAttempts = Integer.parseInt((String) parametersMap.get(MessageProcessorConstants.MAX_DELIVER_ATTEMPTS));
    }
    if (parametersMap.get(MessageProcessorConstants.RETRY_INTERVAL) != null) {
        retryInterval = Integer.parseInt((String) parametersMap.get(MessageProcessorConstants.RETRY_INTERVAL));
    }
    String maxConnectionAttemptsToStore = (String) parametersMap.get(MessageProcessorConstants.MAX_STORE_CONNECT_ATTEMPTS);
    String storeConnectionAttemptDelay = (String) parametersMap.get(MessageProcessorConstants.STORE_CONNECTION_RETRY_INTERVAL);
    if (null != maxConnectionAttemptsToStore) {
        this.maxConnectionAttemptsToStore = Integer.parseInt(maxConnectionAttemptsToStore);
    }
    if (null != storeConnectionAttemptDelay) {
        this.storeConnectionAttemptDelay = Integer.parseInt(storeConnectionAttemptDelay);
    }
    replySeq = (String) parametersMap.get(ForwardingProcessorConstants.REPLY_SEQUENCE);
    faultSeq = (String) parametersMap.get(ForwardingProcessorConstants.FAULT_SEQUENCE);
    deactivateSeq = (String) parametersMap.get(ForwardingProcessorConstants.DEACTIVATE_SEQUENCE);
    targetEndpoint = (String) parametersMap.get(ForwardingProcessorConstants.TARGET_ENDPOINT);
    String failMessageStoreName = (String) parametersMap.get(ForwardingProcessorConstants.FAIL_MESSAGES_STORE);
    if (null != failMessageStoreName && !failMessageStoreName.isEmpty()) {
        failMessageStore = se.createMessageContext().getConfiguration().getMessageStore(failMessageStoreName);
    }
    // Default value should be true.
    if (parametersMap.get(ForwardingProcessorConstants.THROTTLE) != null) {
        isThrottling = Boolean.parseBoolean((String) parametersMap.get(ForwardingProcessorConstants.THROTTLE));
    }
    if (parametersMap.get(ForwardingProcessorConstants.CRON_EXPRESSION) != null) {
        cronExpression = String.valueOf(parametersMap.get(ForwardingProcessorConstants.CRON_EXPRESSION));
    }
    // Default Value should be -1.
    if (cronExpression != null && parametersMap.get(ForwardingProcessorConstants.THROTTLE_INTERVAL) != null) {
        throttlingInterval = Long.parseLong((String) parametersMap.get(ForwardingProcessorConstants.THROTTLE_INTERVAL));
    }
    nonRetryStatusCodes = (String[]) parametersMap.get(ForwardingProcessorConstants.NON_RETRY_STATUS_CODES);
    // Default to FALSE.
    if (parametersMap.get(ForwardingProcessorConstants.MAX_DELIVERY_DROP) != null && parametersMap.get(ForwardingProcessorConstants.MAX_DELIVERY_DROP).toString().equals("Enabled") && maxDeliverAttempts > 0) {
        isMaxDeliveryAttemptDropEnabled = true;
    }
    // Setting the interval value.
    interval = Long.parseLong((String) parametersMap.get(MessageProcessorConstants.INTERVAL));
    /*
		 * Make sure to set the isInitialized flag to TRUE in order to avoid
		 * re-initialization.
		 */
    initialized = true;
}
Also used : SynapseException(org.apache.synapse.SynapseException) StoreForwardException(org.apache.synapse.message.StoreForwardException)

Example 93 with SynapseException

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

the class TransactionMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    // UserTransaction tx = null;
    final SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Transaction mediator (" + action + ")");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    initContext(synCtx);
    try {
        // tx = (UserTransaction) txContext.lookup(USER_TX_LOOKUP_STR);
        TranscationManger.lookUp(txContext);
    } catch (Exception e) {
        handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
    }
    if (action.equals(ACTION_COMMIT)) {
        try {
            // tx.commit();
            long key = Thread.currentThread().getId();
            TranscationManger.endTransaction(true, key);
        } catch (Exception e) {
            handleException("Unable to commit transaction", e, synCtx);
        }
    } else if (action.equals(ACTION_ROLLBACK)) {
        try {
            // tx.rollback();
            long key = Thread.currentThread().getId();
            TranscationManger.rollbackTransaction(true, key);
        } catch (Exception e) {
            handleException("Unable to rollback transaction", e, synCtx);
        }
    } else if (action.equals(ACTION_NEW)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (!(status == Status.STATUS_NO_TRANSACTION || status == Status.STATUS_UNKNOWN)) {
            throw new SynapseException("Require to begin a new transaction, " + "but a transaction already exist");
        }
        beginNewTransaction(synCtx);
    } else if (action.equals(ACTION_USE_EXISTING_OR_NEW)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (status == Status.STATUS_NO_TRANSACTION) {
            beginNewTransaction(synCtx);
        }
    } else if (action.equals(ACTION_FAULT_IF_NO_TX)) {
        int status = Status.STATUS_UNKNOWN;
        try {
            // status = tx.getStatus();
            status = TranscationManger.getStatus();
        } catch (Exception e) {
            handleException("Unable to query transaction status", e, synCtx);
        }
        if (status != Status.STATUS_ACTIVE)
            throw new SynapseException("No active transaction. Require an active transaction");
    } else {
        handleException("Invalid transaction mediator action : " + action, synCtx);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("End : Transaction mediator");
    }
    return true;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException) NamingException(javax.naming.NamingException)

Example 94 with SynapseException

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

the class SamplingService method dispatch.

/**
 * Sends the message to a given sequence.
 *
 * @param messageContext
 *            message to be injected.
 */
public void dispatch(final MessageContext messageContext) {
    setSoapHeaderBlock(messageContext);
    final ExecutorService executor = messageContext.getEnvironment().getExecutorService();
    executor.submit(new Runnable() {

        public void run() {
            try {
                Mediator processingSequence = messageContext.getSequence(sequence);
                if (processingSequence != null) {
                    processingSequence.mediate(messageContext);
                }
            } catch (SynapseException syne) {
                if (!messageContext.getFaultStack().isEmpty()) {
                    (messageContext.getFaultStack().pop()).handleFault(messageContext, syne);
                }
                log.error("Error occurred while executing the message", syne);
            } catch (Throwable t) {
                log.error("Error occurred while executing the message", t);
            }
        }
    });
}
Also used : SynapseException(org.apache.synapse.SynapseException) ExecutorService(java.util.concurrent.ExecutorService) Mediator(org.apache.synapse.Mediator)

Example 95 with SynapseException

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

the class RegistryResourceFetcher method init.

public void init(SynapseEnvironment se) {
    if (items == null) {
        String msg = "resources configuration is required";
        log.error(msg);
        throw new SynapseException(msg);
    }
    Iterator it = items.getChildrenWithName(new QName("resource"));
    while (it.hasNext()) {
        OMElement resourceElement = (OMElement) it.next();
        String path = resourceElement.getText();
        String type = "xml";
        OMAttribute typeAttribute = resourceElement.getAttribute(new QName("type"));
        if (typeAttribute != null) {
            type = typeAttribute.getAttributeValue();
        }
        registryResources.add(new RegistryResourceEntry(path, type));
    }
    this.synapseConfiguration = se.getSynapseConfiguration();
    this.registry = (AbstractRegistry) se.getSynapseConfiguration().getRegistry();
    this.synapseEnvironment = se;
    this.view = new RegistryResourceFetcherView(this);
    MBeanRegistrar.getInstance().registerMBean(view, "ESB-Registry", "RegistryResourceFetcher");
    this.state = State.ACTIVE;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

SynapseException (org.apache.synapse.SynapseException)136 OMElement (org.apache.axiom.om.OMElement)31 OMAttribute (org.apache.axiom.om.OMAttribute)23 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)16 QName (javax.xml.namespace.QName)15 Iterator (java.util.Iterator)14 JaxenException (org.jaxen.JaxenException)14 XMLStreamException (javax.xml.stream.XMLStreamException)13 AxisFault (org.apache.axis2.AxisFault)13 Map (java.util.Map)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)8 OMNode (org.apache.axiom.om.OMNode)7 Mediator (org.apache.synapse.Mediator)7 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)7