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.axis2.transport.local;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.addressing.EndpointReference;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.context.MessageContext;
027: import org.apache.axis2.description.TransportOutDescription;
028: import org.apache.axis2.handlers.AbstractHandler;
029: import org.apache.axis2.transport.TransportSender;
030: import org.apache.axis2.transport.TransportUtils;
031: import org.apache.axis2.transport.http.HTTPTransportUtils;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.io.ByteArrayInputStream;
036: import java.io.ByteArrayOutputStream;
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040:
041: public class LocalTransportSender extends AbstractHandler implements
042: TransportSender {
043: protected static final Log log = LogFactory
044: .getLog(LocalTransportSender.class);
045:
046: private ByteArrayOutputStream response;
047:
048: public void init(ConfigurationContext confContext,
049: TransportOutDescription transportOut) throws AxisFault {
050: }
051:
052: public void stop() {
053: }
054:
055: public void cleanup(MessageContext msgContext) throws AxisFault {
056: }
057:
058: protected OutputStream getResponse() {
059: return response;
060: }
061:
062: /**
063: * Method invoke
064: *
065: * @param msgContext the current MessageContext
066: * @throws AxisFault
067: */
068: public InvocationResponse invoke(MessageContext msgContext)
069: throws AxisFault {
070:
071: // Check for the REST behaviour, if you desire rest beahaviour
072: // put a <parameter name="doREST" value="true"/> at the axis2.xml
073: msgContext.setDoingMTOM(HTTPTransportUtils
074: .doWriteMTOM(msgContext));
075: msgContext.setDoingSwA(HTTPTransportUtils
076: .doWriteSwA(msgContext));
077:
078: OutputStream out;
079: EndpointReference epr = msgContext.getTo();
080:
081: if (log.isDebugEnabled()) {
082: ByteArrayOutputStream os = new ByteArrayOutputStream();
083: TransportUtils.writeMessage(msgContext, os);
084: log.debug("Sending - " + new String(os.toByteArray()));
085: }
086:
087: if (epr != null) {
088: if (!epr.hasNoneAddress()) {
089: out = new ByteArrayOutputStream();
090: TransportUtils.writeMessage(msgContext, out);
091: finalizeSendWithToAddress(msgContext,
092: (ByteArrayOutputStream) out);
093: }
094: } else {
095: out = (OutputStream) msgContext
096: .getProperty(MessageContext.TRANSPORT_OUT);
097:
098: if (out != null) {
099: TransportUtils.writeMessage(msgContext, out);
100: } else {
101: throw new AxisFault(
102: "Both the TO and Property MessageContext.TRANSPORT_OUT is Null, No where to send");
103: }
104: }
105:
106: TransportUtils.setResponseWritten(msgContext, true);
107:
108: return InvocationResponse.CONTINUE;
109: }
110:
111: public void finalizeSendWithToAddress(MessageContext msgContext,
112: ByteArrayOutputStream out) throws AxisFault {
113: try {
114: InputStream in = new ByteArrayInputStream(out.toByteArray());
115: response = new ByteArrayOutputStream();
116:
117: LocalTransportReceiver localTransportReceiver = new LocalTransportReceiver(
118: this );
119: localTransportReceiver.processMessage(in, msgContext
120: .getTo(), msgContext.getOptions().getAction());
121: in.close();
122: out.close();
123: in = new ByteArrayInputStream(response.toByteArray());
124: msgContext.setProperty(MessageContext.TRANSPORT_IN, in);
125: } catch (IOException e) {
126: throw AxisFault.makeFault(e);
127: }
128: }
129: }
|