KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > tunnel > TunnelServlet


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: TunnelServlet.java,v 1.1 2004/11/26 01:51:17 tanderson Exp $
44  */

45 package org.exolab.jms.net.tunnel;
46
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.InterruptedIOException JavaDoc;
50 import java.io.OutputStream JavaDoc;
51 import java.io.PrintWriter JavaDoc;
52 import java.net.Socket JavaDoc;
53 import javax.servlet.ServletException JavaDoc;
54 import javax.servlet.http.HttpServlet JavaDoc;
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57
58
59 /**
60  * HTTP Tunnel servlet
61  *
62  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
63  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:17 $
64  */

65 public class TunnelServlet extends HttpServlet JavaDoc {
66
67     /**
68      * The host that the server is running on
69      */

70     private String JavaDoc _host;
71
72     /**
73      * The port that the connector is listening on
74      */

75     private int _port;
76
77     /**
78      * The connection manager
79      */

80     private static final EndpointManager _manager = new EndpointManager();
81
82     /**
83      * Initialisation property name for the host that the server is running on.
84      */

85     private static final String JavaDoc SERVER_HOST = "host";
86
87     /**
88      * Initialisation property name for the port that the connector is
89      * listening on.
90      */

91     private static final String JavaDoc SERVER_PORT = "port";
92
93
94     /**
95      * Initialise the servlet
96      *
97      * @throws ServletException if the servlet can't be initialised
98      */

99     public void init() throws ServletException JavaDoc {
100         String JavaDoc host = getInitParameter(SERVER_HOST);
101         String JavaDoc port = getInitParameter(SERVER_PORT);
102         if (host == null) {
103             throw new ServletException JavaDoc("Property not defined: " + SERVER_HOST);
104         }
105         if (port == null) {
106             throw new ServletException JavaDoc("Property not defined: " + SERVER_PORT);
107         }
108         _host = host;
109         try {
110             _port = Integer.parseInt(port);
111         } catch (NumberFormatException JavaDoc exception) {
112             throw new ServletException JavaDoc("Invalid port: " + port);
113         }
114         log("OpenJMS tunnel accepting requests");
115     }
116
117     /**
118      * Handle a GET request. This method always sets the response code to
119      * <code>HttpServletResponse.SC_BAD_REQUEST</code>
120      *
121      * @param request the client request
122      * @param response the response to the request
123      * @throws IOException for any I/O error
124      */

125     protected void doGet(HttpServletRequest JavaDoc request,
126                          HttpServletResponse JavaDoc response) throws IOException JavaDoc {
127         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
128     }
129
130     /**
131      * Handle a POST request.
132      *
133      * @param request the client request
134      * @param response the response to the request
135      * @throws IOException for any I/O error
136      */

137     protected void doPost(HttpServletRequest JavaDoc request,
138                           HttpServletResponse JavaDoc response) throws IOException JavaDoc {
139
140         String JavaDoc action = request.getHeader("action");
141
142         if (action == null) {
143             response.sendError(HttpServletResponse.SC_BAD_REQUEST,
144                                "Invalid action");
145         } else if (action.equals("open")) {
146             open(request, response);
147         } else {
148             String JavaDoc id = request.getHeader("id");
149             if (id == null) {
150                 response.sendError(HttpServletResponse.SC_BAD_REQUEST,
151                                    "Invalid connection");
152             } else if (action.equals("read")) {
153                 read(id, response);
154             } else if (action.equals("write")) {
155                 write(id, request, response);
156             } else if (action.equals("close")) {
157                 close(id, response);
158             } else {
159                 response.sendError(HttpServletResponse.SC_BAD_REQUEST,
160                                    "Invalid action");
161             }
162         }
163     }
164
165     /**
166      * Handle an open request. A connection is established to the server and the
167      * identifier written to the client.
168      *
169      * @param request the client request
170      * @param response the response to the request
171      * @throws IOException for any I/O error
172      */

173     private void open(HttpServletRequest JavaDoc request,
174                       HttpServletResponse JavaDoc response) throws IOException JavaDoc {
175 // if (_log.isDebugEnabled()) {
176
// _log.debug("open");
177
// }
178

179         response.setContentType("text/plain");
180         PrintWriter JavaDoc out = new PrintWriter JavaDoc(response.getWriter());
181
182         try {
183             String JavaDoc id = _manager.open(_host, _port);
184             out.println("OPEN " + id);
185             response.setStatus(HttpServletResponse.SC_OK);
186         } catch (Exception JavaDoc exception) {
187             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
188                                exception.getMessage());
189             log("open failed", exception);
190         }
191     }
192
193     /**
194      * Handles a read request. Data is read from the endpoint and written to the
195      * client.
196      *
197      * @param id the endpoint identifier
198      * @param response the response to the client
199      * @throws IOException for any I/O error
200      */

201     private void read(String JavaDoc id, HttpServletResponse JavaDoc response)
202             throws IOException JavaDoc {
203         Socket JavaDoc endpoint = _manager.getEndpoint(id);
204         if (endpoint == null) {
205             log("Connection not found, id=" + id);
206             response.sendError(HttpServletResponse.SC_BAD_REQUEST,
207                                "Connection not found");
208         } else {
209             byte[] data = new byte[1024];
210             try {
211                 endpoint.setSoTimeout(1000);
212                 InputStream JavaDoc in = endpoint.getInputStream();
213                 int count = 0;
214                 try {
215                     count = in.read(data);
216                 } catch (InterruptedIOException JavaDoc ignore) {
217                 }
218                 // log("read(id=" + id + "), [length=" + count + "]");
219
if (count != -1) {
220                     response.setContentLength(count);
221                     response.setStatus(HttpServletResponse.SC_OK);
222                     OutputStream JavaDoc out = response.getOutputStream();
223                     out.write(data, 0, count);
224                     out.flush();
225                 } else {
226                     remove(id);
227                     response.setStatus(
228                             HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
229                 }
230             } catch (IOException JavaDoc exception) {
231                 String JavaDoc message = exception.getMessage();
232                 log("read failed", exception);
233                 remove(id);
234                 response.sendError(
235                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
236                         exception.getMessage());
237             }
238         }
239     }
240
241     /**
242      * Handles a write request. Data from the client is written to the endpoint
243      *
244      * @param id the endpoint identifier
245      * @param request the client request
246      * @param response the response to the client
247      * @throws IOException for any I/O error
248      */

249     private void write(String JavaDoc id, HttpServletRequest JavaDoc request,
250                        HttpServletResponse JavaDoc response) throws IOException JavaDoc {
251         Socket JavaDoc endpoint = _manager.getEndpoint(id);
252         if (endpoint == null) {
253             response.sendError(HttpServletResponse.SC_BAD_REQUEST,
254                                "Connection not found");
255         } else {
256             try {
257                 // log("write(id=" + id + "), [length="
258
// + request.getContentLength()
259
// + "]");
260
InputStream JavaDoc in = request.getInputStream();
261                 OutputStream JavaDoc out = endpoint.getOutputStream();
262                 byte[] data = new byte[1024];
263                 int count = 0;
264                 while (count != -1) {
265                     count = in.read(data);
266                     if (count > 0) {
267                         out.write(data, 0, count);
268                     }
269                 }
270                 in.close();
271                 out.flush();
272                 response.setStatus(HttpServletResponse.SC_OK);
273             } catch (IOException JavaDoc exception) {
274                 String JavaDoc message = exception.getMessage();
275                 log("write failed", exception);
276                 remove(id);
277                 response.sendError(
278                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
279                         exception.getMessage());
280             }
281         }
282     }
283
284     /**
285      * Handle a close request.
286      *
287      * @param id the endpoint identifier
288      * @param response the response to the client
289      * @throws IOException for any I/O error
290      */

291     private void close(String JavaDoc id, HttpServletResponse JavaDoc response)
292             throws IOException JavaDoc {
293
294         try {
295             log("close(id=" + id + ")");
296             _manager.close(id);
297             response.setStatus(HttpServletResponse.SC_OK);
298         } catch (IOException JavaDoc exception) {
299             log("close failed", exception);
300             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
301                                exception.getMessage());
302         }
303     }
304
305     /**
306      * Removes an endpoint
307      *
308      * @param id the endpoint identifier
309      */

310     private void remove(String JavaDoc id) {
311         try {
312             _manager.close(id);
313         } catch (IOException JavaDoc ignore) {
314         }
315     }
316
317 }
318
Popular Tags