KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > AbstractTransportSender


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.transport;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.addressing.AddressingConstants;
20 import org.apache.axis2.addressing.EndpointReference;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.context.MessageContext;
23 import org.apache.axis2.description.HandlerDescription;
24 import org.apache.axis2.description.TransportOutDescription;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.handlers.AbstractHandler;
27 import org.apache.axis2.om.OMElement;
28 import org.apache.axis2.om.OMOutput;
29 import org.apache.axis2.soap.SOAPEnvelope;
30 import org.apache.axis2.transport.http.HTTPTransportUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import javax.xml.namespace.QName JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 /**
38  * By the time this Class is invoked either the To EPR on the MessageContext
39  * should be set or TRANSPORT_WRITER property set in the message Context with a
40  * Writer. This Class would write the SOAPMessage using either of the methods in
41  * the order To then Writer.
42  */

43 public abstract class AbstractTransportSender extends AbstractHandler implements
44         TransportSender {
45     /**
46      * Field log
47      */

48     private Log log = LogFactory.getLog(getClass());
49
50
51
52     /**
53      * Field NAME
54      */

55     public static final QName JavaDoc NAME = new QName JavaDoc("http://axis.ws.apache.org",
56             "TransportSender");
57
58     /**
59      * Constructor AbstractTransportSender
60      */

61     public AbstractTransportSender() {
62         init(new HandlerDescription(NAME));
63     }
64
65     public void init(ConfigurationContext confContext,
66             TransportOutDescription transportOut) throws AxisFault {
67
68     }
69
70     /**
71      * Method invoke
72      *
73      * @param msgContext
74      * @throws AxisFault
75      */

76     public void invoke(MessageContext msgContext) throws AxisFault {
77         //Check for the REST behaviour, if you desire rest beahaviour
78
//put a <parameter name="doREST" value="true"/> at the axis2.xml
79
msgContext.setDoingMTOM(HTTPTransportUtils.doWriteMTOM(msgContext));
80
81         OutputStream JavaDoc out = null;
82
83         EndpointReference epr = null;
84
85         if (msgContext.getTo() != null
86                 && !AddressingConstants.Submission.WSA_ANONYMOUS_URL
87                         .equals(msgContext.getTo().getAddress())
88                 && !AddressingConstants.Final.WSA_ANONYMOUS_URL
89                         .equals(msgContext.getTo().getAddress())) {
90             epr = msgContext.getTo();
91         }
92
93         if (epr != null) {
94             out = openTheConnection(epr, msgContext);
95             OutputStream JavaDoc newOut = startSendWithToAddress(msgContext, out);
96             if (newOut != null) {
97                 out = newOut;
98             }
99             writeMessage(msgContext, out);
100             finalizeSendWithToAddress(msgContext, out);
101         } else {
102             out = (OutputStream JavaDoc) msgContext
103                     .getProperty(MessageContext.TRANSPORT_OUT);
104             if (out != null) {
105                 startSendWithOutputStreamFromIncomingConnection(msgContext, out);
106                 writeMessage(msgContext, out);
107                 finalizeSendWithOutputStreamFromIncomingConnection(msgContext,
108                         out);
109             } else {
110                 throw new AxisFault(
111                         "Both the TO and Property MessageContext.TRANSPORT_WRITER is Null, No where to send");
112             }
113         }
114         //TODO fix this, we do not set the value if the operation context is
115
// not avalible
116
if (msgContext.getOperationContext() != null) {
117             msgContext.getOperationContext().setProperty(
118                     Constants.RESPONSE_WRITTEN, Constants.VALUE_TRUE);
119         }
120     }
121
122     public void writeMessage(MessageContext msgContext, OutputStream JavaDoc out)
123             throws AxisFault {
124         SOAPEnvelope envelope = msgContext.getEnvelope();
125         OMElement outputMessage = envelope;
126
127         if (envelope != null && msgContext.isDoingREST()) {
128             outputMessage = envelope.getBody().getFirstElement();
129         }
130
131         if (outputMessage != null) {
132             OMOutput omOutput = null;
133
134             try {
135                 if (msgContext.isDoingMTOM()) {
136                     omOutput = new OMOutput(out, true);
137                     outputMessage.serialize(omOutput);
138                     omOutput.flush();
139                     omOutput.complete();
140                     out.flush();
141                 } else {
142                     omOutput = new OMOutput(out, false);
143                     outputMessage.serialize(omOutput);
144                     omOutput.flush();
145                     out.flush();
146                 }
147             } catch (Exception JavaDoc e) {
148                 throw new AxisFault("Stream error", e);
149             }
150         } else {
151             throw new AxisFault("the OUTPUT message is Null, nothing to write");
152         }
153     }
154
155     public abstract OutputStream JavaDoc startSendWithToAddress(
156             MessageContext msgContext, OutputStream JavaDoc out) throws AxisFault;
157
158     public abstract void finalizeSendWithToAddress(MessageContext msgContext,
159             OutputStream JavaDoc out) throws AxisFault;
160
161     public abstract OutputStream JavaDoc startSendWithOutputStreamFromIncomingConnection(
162             MessageContext msgContext, OutputStream JavaDoc out) throws AxisFault;
163
164     public abstract void finalizeSendWithOutputStreamFromIncomingConnection(
165             MessageContext msgContext, OutputStream JavaDoc out) throws AxisFault;
166
167     protected abstract OutputStream JavaDoc openTheConnection(EndpointReference epr,
168             MessageContext msgctx) throws AxisFault;
169 }
Popular Tags