Package org.wso2.carbon.transport.relay

Source Code of org.wso2.carbon.transport.relay.TargetErrorHandler

/**
*  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.wso2.carbon.transport.relay;

import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.MessageReceiver;
import org.apache.axis2.util.MessageContextBuilder;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.transport.relay.config.TargetConfiguration;

public class TargetErrorHandler {
    private Log log = LogFactory.getLog(TargetErrorHandler.class);

    private TargetConfiguration targetConfiguration = null;

    public TargetErrorHandler(TargetConfiguration targetConfiguration) {
        this.targetConfiguration = targetConfiguration;
    }

    /**
     * Mark request to send failed with error
     *
     * @param mc the failed message context
     * @param errorCode the error code to raise
     * @param errorMessage the text for an error message to be returned to the MR on failure
     * @param exceptionToRaise an Exception to be returned to the MR on failure
     * @param state state of the connection
     */
    protected void handleError(final MessageContext mc,
                               final int errorCode,
                               final String errorMessage,
                               final Exception exceptionToRaise,
                               final ProtocolState state) {

        if (errorCode == -1 && errorMessage == null && exceptionToRaise == null) {
            return;
        }

        if (mc.getAxisOperation() == null ||
                mc.getAxisOperation().getMessageReceiver() == null) {
            return;
        }

        targetConfiguration.getWorkerPool().execute(new Runnable() {
            public void run() {
                MessageReceiver mr = mc.getAxisOperation().getMessageReceiver();
                try {
                    AxisFault axisFault = (exceptionToRaise != null ?
                            new AxisFault(errorMessage, exceptionToRaise) :
                            new AxisFault(errorMessage));

                    MessageContext faultMessageContext =
                            MessageContextBuilder.createFaultMessageContext(mc, axisFault);

                    SOAPEnvelope envelope = faultMessageContext.getEnvelope();

                    if (log.isDebugEnabled()) {
                        log.debug("Sending Fault for Request with Message ID : "
                                + mc.getMessageID());
                    }

                    faultMessageContext.setTo(null);
                    faultMessageContext.removeProperty(RelayConstants.RELAY_PIPE);

                    // copy the important properties from the original message context
                    faultMessageContext.setProperty(RelayConstants.RELAY_SOURCE_CONNECTION,
                            mc.getProperty(RelayConstants.RELAY_SOURCE_CONNECTION));
                    faultMessageContext.setProperty(RelayConstants.RELAY_SOURCE_CONFIGURATION,
                            mc.getProperty(RelayConstants.RELAY_SOURCE_CONFIGURATION));

                    faultMessageContext.setServerSide(true);
                    faultMessageContext.setDoingREST(mc.isDoingREST());
                    faultMessageContext.setProperty(MessageContext.TRANSPORT_IN, mc
                            .getProperty(MessageContext.TRANSPORT_IN));
                    faultMessageContext.setTransportIn(mc.getTransportIn());
                    faultMessageContext.setTransportOut(mc.getTransportOut());


                    faultMessageContext.setAxisMessage(
                            mc.getOperationContext().getAxisOperation().getMessage(
                                    WSDLConstants.MESSAGE_LABEL_IN_VALUE));
                   
                    faultMessageContext.setOperationContext(mc.getOperationContext());
                    faultMessageContext.setConfigurationContext(mc.getConfigurationContext());
                    faultMessageContext.setTo(null);

                    faultMessageContext.setProperty(
                            RelayConstants.SENDING_FAULT, Boolean.TRUE);
                    faultMessageContext.setProperty(
                            RelayConstants.ERROR_MESSAGE, errorMessage);
                    if (errorCode != -1) {
                        faultMessageContext.setProperty(
                                RelayConstants.ERROR_CODE, getErrorCode(errorCode, state));
                    }
                    if (exceptionToRaise != null) {
                        faultMessageContext.setProperty(
                                RelayConstants.ERROR_DETAIL, exceptionToRaise.toString());
                        faultMessageContext.setProperty(
                                RelayConstants.ERROR_EXCEPTION, exceptionToRaise);
                        envelope.getBody().getFault().getDetail().setText(
                                exceptionToRaise.toString());
                    } else {
                        faultMessageContext.setProperty(
                                RelayConstants.ERROR_DETAIL, errorMessage);
                        envelope.getBody().getFault().getDetail().setText(errorMessage);
                    }

                    faultMessageContext.setProperty(RelayConstants.NO_ENTITY_BODY, true);

                    mr.receive(faultMessageContext);

                } catch (AxisFault af) {
                    log.error("Unable to report back failure to the message receiver", af);
                }
            }
        });
    }

    private int getErrorCode(int errorCode, ProtocolState state) {
        return errorCode + state.ordinal();
    }

}
TOP

Related Classes of org.wso2.carbon.transport.relay.TargetErrorHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.