KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > tcp > TCPTransportSender


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.tcp;
17
18 import org.apache.axis2.addressing.EndpointReference;
19 import org.apache.axis2.context.MessageContext;
20 import org.apache.axis2.engine.AxisFault;
21 import org.apache.axis2.transport.AbstractTransportSender;
22 import org.apache.axis2.util.URL;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.net.InetSocketAddress JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.Socket JavaDoc;
31 import java.net.SocketAddress JavaDoc;
32
33 /**
34  * Class HTTPTransportSender
35  */

36 public class TCPTransportSender extends AbstractTransportSender {
37     /**
38      * Field out
39      */

40     protected Writer JavaDoc out;
41
42     /**
43      * Field socket
44      */

45     private Socket JavaDoc socket;
46     private ByteArrayOutputStream JavaDoc outputStream;
47
48     /**
49      * Method writeTransportHeaders
50      *
51      * @param out
52      * @param url
53      * @param msgContext
54      * @throws IOException
55      */

56     protected void writeTransportHeaders(
57         Writer JavaDoc out,
58         URL url,
59         MessageContext msgContext,
60         int contentLength)
61         throws IOException JavaDoc {
62         //TCP no headers :)
63
}
64
65     public void finalizeSendWithOutputStreamFromIncomingConnection(MessageContext msgContext,OutputStream JavaDoc out) {
66     }
67
68     public void finalizeSendWithToAddress(MessageContext msgContext,OutputStream JavaDoc out) throws AxisFault {
69         try {
70             socket.shutdownOutput();
71             msgContext.setProperty(
72                 MessageContext.TRANSPORT_IN,socket.getInputStream());
73         } catch (IOException JavaDoc e) {
74             throw new AxisFault(e);
75         }
76     }
77
78     protected OutputStream JavaDoc openTheConnection(EndpointReference toURL,MessageContext msgContext) throws AxisFault {
79         if (toURL != null) {
80             try {
81                 URL url = new URL(toURL.getAddress());
82                 SocketAddress JavaDoc add =
83                     new InetSocketAddress JavaDoc(url.getHost(), url.getPort() == -1 ? 80 : url.getPort());
84                 socket = new Socket JavaDoc();
85                 socket.connect(add);
86                 return socket.getOutputStream();
87             } catch (MalformedURLException JavaDoc e) {
88                 throw new AxisFault(e.getMessage(), e);
89             } catch (IOException JavaDoc e) {
90                 throw new AxisFault(e.getMessage(), e);
91             }
92         } else {
93             throw new AxisFault("to EPR must be specified");
94         }
95     }
96
97     public OutputStream JavaDoc startSendWithOutputStreamFromIncomingConnection(
98         MessageContext msgContext,
99         OutputStream JavaDoc out)
100         throws AxisFault {
101             return out;
102     }
103
104     public OutputStream JavaDoc startSendWithToAddress(MessageContext msgContext, OutputStream JavaDoc out) {
105         return out;
106     }
107
108     public void cleanUp(MessageContext msgContext) throws AxisFault {
109         try {
110             if (socket != null) {
111                 socket.close();
112                 socket = null;
113             }
114
115         } catch (IOException JavaDoc e) {
116         }
117
118     }
119
120 }
121
Popular Tags