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.OutputStream;
036: import java.io.ByteArrayOutputStream;
037:
038: /**
039: * LocalResponder
040: */
041: public class LocalResponder extends AbstractHandler implements
042: TransportSender {
043: protected static final Log log = LogFactory
044: .getLog(LocalResponder.class);
045:
046: LocalTransportSender sender;
047:
048: public LocalResponder(LocalTransportSender sender) {
049: this .sender = sender;
050: }
051:
052: public void init(ConfigurationContext confContext,
053: TransportOutDescription transportOut) throws AxisFault {
054: }
055:
056: public void stop() {
057: }
058:
059: public void cleanup(MessageContext msgContext) throws AxisFault {
060: }
061:
062: /**
063: * Method invoke
064: *
065: * @param msgContext the active 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 = null;
080:
081: if (msgContext.getTo() != null
082: && !msgContext.getTo().hasAnonymousAddress()) {
083: epr = msgContext.getTo();
084: }
085:
086: try {
087: if (log.isDebugEnabled()) {
088: ByteArrayOutputStream os = new ByteArrayOutputStream();
089: TransportUtils.writeMessage(msgContext, os);
090: log.debug("Response - " + new String(os.toByteArray()));
091: }
092:
093: if (epr != null) {
094: if (!epr.hasNoneAddress()) {
095: out = sender.getResponse();
096: TransportUtils.writeMessage(msgContext, out);
097: }
098: } else {
099: out = (OutputStream) msgContext
100: .getProperty(MessageContext.TRANSPORT_OUT);
101:
102: if (out != null) {
103: TransportUtils.writeMessage(msgContext, out);
104: } else {
105: throw new AxisFault(
106: "Both the TO and Property MessageContext.TRANSPORT_OUT is Null, No where to send");
107: }
108: }
109: } catch (AxisFault axisFault) {
110: // At this point all we can do is log this error, since it happened while
111: // we were sending the response!
112: log.error("Error sending response", axisFault);
113: }
114:
115: TransportUtils.setResponseWritten(msgContext, true);
116:
117: return InvocationResponse.CONTINUE;
118: }
119: }
|