001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.handler;
021:
022: import org.apache.axis2.handlers.AbstractHandler;
023: import org.apache.axis2.context.MessageContext;
024: import org.apache.axis2.AxisFault;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.synapse.core.axis2.MessageContextCreatorForAxis2;
028: import org.apache.synapse.SynapseException;
029: import org.apache.synapse.FaultHandler;
030: import org.apache.synapse.SynapseConstants;
031: import org.apache.synapse.util.HandlerUtil;
032:
033: /**
034: * This is the InHandler which injects the Axis2MC in to Synapse after converting the message
035: * context to a SynapseMC
036: */
037: public class SynapseInHandler extends AbstractHandler {
038:
039: /**
040: * Log variable which will be used for logging
041: */
042: private static final Log log = LogFactory
043: .getLog(SynapseInHandler.class);
044:
045: /**
046: * This method will inject the message into Synapse after creating the SynapseMC from the
047: * Axis2MC and after the mediation if synapse lets the message to flow through this will let
048: * the message to flow and if not aborts the message
049: *
050: * @param messageContext - Axis2MC to be mediated using Synapse
051: * @return InvocationResponse.CONTINUE if Synapse lets the message to flow and
052: * InvocationResponse.ABORT if not
053: * @throws AxisFault - incase of a failure in mediation of initiation of the mediation
054: */
055: public InvocationResponse invoke(MessageContext messageContext)
056: throws AxisFault {
057:
058: HandlerUtil.doHandlerLogging(log, messageContext);
059:
060: org.apache.synapse.MessageContext synCtx = MessageContextCreatorForAxis2
061: .getSynapseMessageContext(messageContext);
062:
063: // handles the incoming and outgoing behaviours in client and server sides
064: if (!messageContext.isServerSide()) {
065: synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
066: synCtx.setResponse(true);
067: } else {
068: synCtx
069: .setProperty(SynapseConstants.RESPONSE,
070: Boolean.FALSE);
071: synCtx.setResponse(false);
072: }
073:
074: try {
075: // if synapse says ok let the message to flow through
076: if (synCtx.getEnvironment().injectMessage(synCtx)) {
077: return InvocationResponse.CONTINUE;
078: } else {
079: // if not abort the further processings
080: log.debug("Synapse has decided to abort the message:\n"
081: + synCtx.getEnvelope());
082: return InvocationResponse.ABORT;
083: }
084: } catch (SynapseException syne) {
085: if (!synCtx.getFaultStack().isEmpty()) {
086: ((FaultHandler) synCtx.getFaultStack().pop())
087: .handleFault(synCtx, syne);
088: } else {
089: log.error("Synapse encountered an exception, "
090: + "No error handlers found.\n"
091: + syne.getMessage());
092: throw new AxisFault("Synapse encountered an error."
093: + syne);
094: }
095: }
096:
097: // general case should let the message flow
098: return InvocationResponse.CONTINUE;
099: }
100: }
|