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 *
42 * Copyright 2001-2002 (C) Exoffice Technologies Inc. All Rights Reserved.
43 *
44 * $Id: HttpJmsServerServlet.java,v 1.7 2003/08/07 13:33:09 tanderson Exp $
45 *
46 * Date Author Changes
47 * Fri 21 Sep 2001 mourikis Created
48 *
49 */
50 package org.exolab.jms.server.http.servlet;
51
52 import java.io.BufferedInputStream;
53 import java.io.BufferedOutputStream;
54 import java.io.IOException;
55 import java.io.ObjectInputStream;
56 import java.io.ObjectOutputStream;
57 import java.util.Vector;
58
59 import javax.servlet.ServletConfig;
60 import javax.servlet.ServletException;
61 import javax.servlet.http.HttpServlet;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
64
65 import org.exolab.core.ipc.IpcIfc;
66 import org.exolab.core.mipc.DisconnectionEventListener;
67 import org.exolab.core.mipc.MultiplexConnection;
68 import org.exolab.core.mipc.MultiplexConnectionIfc;
69 import org.exolab.core.mipc.ObjectChannel;
70 import org.exolab.core.mipc.TcpConstants;
71 import org.exolab.jms.jndi.JndiConstants;
72
73
74 /***
75 * Servlet for handling OpenJMS server requests and message publishing.
76 *
77 * @version $Revision: 1.7 $ $Date: 2003/08/07 13:33:09 $
78 * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
79 */
80 public class HttpJmsServerServlet extends HttpServlet
81 implements DisconnectionEventListener {
82
83 // This is a reference to the server connection.
84 private static MultiplexConnectionIfc _mc = null;
85
86 private static IpcIfc _connection = null;
87
88 /***
89 * The server host name
90 */
91 private static String _host = TcpConstants.LOCAL_HOST;
92
93 /***
94 * The port number the server is listening to
95 */
96 private static int _port = TcpConstants.DEFAULT_PORT;
97
98 /***
99 * The content type of the data.
100 */
101 private static final String CONTENT_TYPE = "text/html";
102
103
104 /***
105 * Called by the servlet container to indicate to a servlet that the
106 * servlet is being placed into service.
107 */
108 public void init(ServletConfig config) throws ServletException {
109 super.init(config);
110
111 if (config != null) {
112 String host = config.getInitParameter(JndiConstants.HOST_PROPERTY);
113 String port = config.getInitParameter(
114 JndiConstants.PORT_NUMBER_PROPERTY);
115 if (host != null) {
116 _host = host;
117 }
118 if (port != null) {
119 _port = Integer.parseInt(port);
120 }
121 }
122 connect();
123
124 log("OpenJMS server servlet ready");
125 }
126
127 /***
128 * Handle HTTP POST requests
129 *
130 * @param request contains the request the client has made of the servlet
131 * @param response contains the response the servlet sends to the client
132 * @throws IOException if an I/O error is encountered
133 */
134 public void doPost(HttpServletRequest request,
135 HttpServletResponse response)
136 throws IOException {
137
138 ObjectInputStream input = new ObjectInputStream(
139 new BufferedInputStream(request.getInputStream()));
140 ObjectOutputStream output = new ObjectOutputStream(
141 new BufferedOutputStream(response.getOutputStream()));
142 response.setContentType(CONTENT_TYPE);
143
144 try {
145 if (_mc == null) {
146 connect();
147 }
148 Object object = input.readObject();
149 if (object instanceof Vector) {
150 synchronized (_connection) {
151 Vector message = (Vector) object;
152 _connection.send(message);
153
154 String type = request.getHeader("jms-response");
155 if (type != null && type.equals("yes")) {
156 message = (Vector) _connection.receive();
157 output.writeObject(message);
158 }
159 }
160 // send response
161 response.setStatus(HttpServletResponse.SC_OK);
162 } else {
163 response.setStatus
164 (HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
165 output.writeObject("Object is NULL, or UNEXPECTED type");
166 }
167 } catch (Exception exception) {
168 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
169 output.writeObject(exception.getMessage());
170 } finally {
171 output.flush();
172 }
173 }
174
175
176 /***Clean up resources*/
177 public void destroy() {
178 disconnected(null);
179 }
180
181 // implementation of DisconnectionEventListener.disconnected
182 public void disconnected(MultiplexConnectionIfc connection) {
183 if (_mc != null) {
184 try {
185 _mc.finish();
186 _connection.close();
187 } catch (Exception ignore) {
188 // just swallow
189 } finally {
190 _mc = null;
191 _connection = null;
192 }
193 }
194 }
195
196 /***
197 * Connect to the OpenJMS server.
198 */
199 protected void connect() throws ServletException {
200 try {
201 _mc = new MultiplexConnection(_host, _port);
202 } catch (Exception exception) {
203 String message = "Failed to connect using host=" + _host +
204 " and port=" + _port + " " + exception.getMessage();
205 log(message);
206 throw new ServletException(message);
207 }
208
209 _mc.setDisconnectionEventListener(this);
210 ((Thread) _mc).start();
211
212 _connection = new ObjectChannel("server", _mc);
213 }
214
215 } //-- HttpJmsServerServlet
This page was automatically generated by Maven