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: package org.apache.synapse.transport.base;
020:
021: import org.apache.axis2.context.MessageContext;
022: import org.apache.axis2.context.ConfigurationContext;
023: import org.apache.axis2.AxisFault;
024: import org.apache.axis2.Constants;
025: import org.apache.axis2.wsdl.WSDLConstants;
026: import org.apache.axis2.util.MessageContextBuilder;
027: import org.apache.axis2.handlers.AbstractHandler;
028: import org.apache.axis2.engine.AxisEngine;
029: import org.apache.axis2.transport.TransportSender;
030: import org.apache.axis2.transport.OutTransportInfo;
031: import org.apache.axis2.description.TransportOutDescription;
032: import org.apache.axis2.description.TransportInDescription;
033: import org.apache.axis2.description.WSDL2Constants;
034: import org.apache.commons.logging.Log;
035: import org.apache.axiom.om.util.UUIDGenerator;
036:
037: import java.util.Map;
038:
039: public abstract class AbstractTransportSender extends AbstractHandler
040: implements TransportSender {
041:
042: /** the reference to the actual commons logger to be used for log messages */
043: protected Log log = null;
044:
045: /** the name of the transport */
046: protected String transportName = null;
047: /** the axis2 configuration context */
048: protected ConfigurationContext cfgCtx = null;
049: /** an axis2 engine over the above configuration context to process messages */
050: protected AxisEngine engine = null;
051: /** transport in description */
052: private TransportInDescription transportIn = null;
053: /** transport out description */
054: private TransportOutDescription transportOut = null;
055: /** is this transport started? */
056: protected boolean started = false;
057:
058: /**
059: * Initialize the generic transport sender.
060: *
061: * @param cfgCtx the axis configuration context
062: * @param transportOut the transport-out description
063: * @throws AxisFault on error
064: */
065: public void init(ConfigurationContext cfgCtx,
066: TransportOutDescription transportOut) throws AxisFault {
067: this .cfgCtx = cfgCtx;
068: this .engine = new AxisEngine(cfgCtx);
069: this .transportIn = cfgCtx.getAxisConfiguration()
070: .getTransportIn(transportName);
071: this .transportOut = transportOut;
072: }
073:
074: public void stop() {
075: if (started) {
076: started = false;
077: }
078: }
079:
080: public void cleanup(MessageContext msgContext) throws AxisFault {
081: }
082:
083: public abstract void sendMessage(MessageContext msgCtx,
084: String targetEPR, OutTransportInfo outTransportInfo)
085: throws AxisFault;
086:
087: public InvocationResponse invoke(MessageContext msgContext)
088: throws AxisFault {
089:
090: // is there a transport url which may be different to the WS-A To but has higher precedence
091: String targetAddress = (String) msgContext
092: .getProperty(Constants.Configuration.TRANSPORT_URL);
093:
094: if (targetAddress != null) {
095: sendMessage(msgContext, targetAddress, null);
096: } else if (msgContext.getTo() != null
097: && !msgContext.getTo().hasAnonymousAddress()) {
098: targetAddress = msgContext.getTo().getAddress();
099:
100: if (!msgContext.getTo().hasNoneAddress()) {
101: sendMessage(msgContext, targetAddress, null);
102: } else {
103: //Don't send the message.
104: return InvocationResponse.CONTINUE;
105: }
106: } else if (msgContext.isServerSide()) {
107: // get the out transport info for server side when target EPR is unknown
108: sendMessage(msgContext, null, (OutTransportInfo) msgContext
109: .getProperty(Constants.OUT_TRANSPORT_INFO));
110: }
111:
112: return InvocationResponse.CONTINUE;
113: }
114:
115: /**
116: * Process a new incoming message (Response) through the axis engine
117: * @param msgCtx the axis MessageContext
118: * @param trpHeaders the map containing transport level message headers
119: * @param soapAction the optional soap action or null
120: * @param contentType the optional content-type for the message
121: */
122: public void handleIncomingMessage(MessageContext msgCtx,
123: Map trpHeaders, String soapAction, String contentType) {
124:
125: // set the soapaction if one is available via a transport header
126: if (soapAction != null) {
127: msgCtx.setSoapAction(soapAction);
128: }
129:
130: // set the transport headers to the message context
131: msgCtx
132: .setProperty(MessageContext.TRANSPORT_HEADERS,
133: trpHeaders);
134:
135: // send the message context through the axis engine
136: try {
137: try {
138: engine.receive(msgCtx);
139: } catch (AxisFault e) {
140: if (log.isDebugEnabled()) {
141: log.debug("Error receiving message", e);
142: }
143: if (msgCtx.isServerSide()) {
144: engine.sendFault(MessageContextBuilder
145: .createFaultMessageContext(msgCtx, e));
146: }
147: }
148: } catch (AxisFault axisFault) {
149: logException("Error processing response message", axisFault);
150: }
151: }
152:
153: /**
154: * Create a new axis MessageContext for an incoming response message
155: * through this transport, for the given outgoing message
156: *
157: * @param outMsgCtx the outgoing message
158: * @return the newly created message context
159: */
160: public MessageContext createResponseMessageContext(
161: MessageContext outMsgCtx) {
162:
163: MessageContext responseMsgCtx = null;
164: try {
165: responseMsgCtx = outMsgCtx.getOperationContext()
166: .getMessageContext(WSDL2Constants.MESSAGE_LABEL_IN);
167: } catch (AxisFault af) {
168: log
169: .error(
170: "Error getting IN message context from the operation context",
171: af);
172: }
173:
174: if (responseMsgCtx == null) {
175: responseMsgCtx = new MessageContext();
176: responseMsgCtx.setOperationContext(outMsgCtx
177: .getOperationContext());
178: }
179:
180: responseMsgCtx.setIncomingTransportName(transportName);
181: responseMsgCtx.setTransportOut(transportOut);
182: responseMsgCtx.setTransportIn(transportIn);
183:
184: responseMsgCtx.setMessageID(UUIDGenerator.getUUID());
185:
186: responseMsgCtx.setDoingREST(outMsgCtx.isDoingREST());
187: responseMsgCtx.setProperty(MessageContext.TRANSPORT_IN,
188: outMsgCtx.getProperty(MessageContext.TRANSPORT_IN));
189: responseMsgCtx.setAxisMessage(outMsgCtx.getOperationContext()
190: .getAxisOperation().getMessage(
191: WSDLConstants.MESSAGE_LABEL_IN_VALUE));
192: responseMsgCtx.setTo(null);
193: //msgCtx.setProperty(MessageContext.TRANSPORT_NON_BLOCKING, isNonBlocking);
194:
195: // are these relevant?
196: //msgCtx.setServiceGroupContextId(UUIDGenerator.getUUID());
197: // this is required to support Sandesha 2
198: //msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL,
199: // new HttpCoreRequestResponseTransport(msgContext));
200:
201: return responseMsgCtx;
202: }
203:
204: /**
205: * Should the transport sender wait for a synchronous response to be received?
206: * @param msgCtx the outgoing message context
207: * @return true if a sync response is expected
208: */
209: protected boolean waitForSynchronousResponse(MessageContext msgCtx) {
210: return msgCtx.getOperationContext() != null
211: && WSDL2Constants.MEP_URI_OUT_IN.equals(msgCtx
212: .getOperationContext().getAxisOperation()
213: .getMessageExchangePattern());
214: }
215:
216: public String getTransportName() {
217: return transportName;
218: }
219:
220: public void setTransportName(String transportName) {
221: this .transportName = transportName;
222: }
223:
224: protected void handleException(String msg, Exception e)
225: throws AxisFault {
226: log.error(msg, e);
227: throw new AxisFault(msg, e);
228: }
229:
230: protected void handleException(String msg) throws AxisFault {
231: log.error(msg);
232: throw new AxisFault(msg);
233: }
234:
235: protected void logException(String msg, Exception e) {
236: log.error(msg, e);
237: }
238: }
|