📄 httpjmsclientservlet.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* Copyright 2001-2002 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: HttpJmsClientServlet.java,v 1.7 2003/08/30 08:00:53 tanderson Exp $
*
* Date Author Changes
* Fri 21 Sep 2001 mourikis Created
*
*/
package org.exolab.jms.client.http.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.exolab.core.ipc.Client;
/**
* This servlet forwards messages from the OpenJMS server to clients
*
* @version $Revision: 1.7 $ $Date: 2003/08/30 08:00:53 $
* @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
* @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
*/
public class HttpJmsClientServlet extends HttpServlet {
/**
* The list of active client connections, keyed on host and port.
*/
private HashMap _connections = new HashMap();
/**
* The content type of the data.
*/
private static final String CONTENT_TYPE = "text/html";
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being placed into service.
*/
public void init() {
log("OpenJMS client servlet ready");
}
/**
* Handle HTTP POST requests
*
* @param request contains the request the client has made of the servlet
* @param response contains the response the servlet sends to the client
* @throws IOException if an I/O error is encountered
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ObjectInputStream input = new ObjectInputStream(
new BufferedInputStream(request.getInputStream()));
ObjectOutputStream output = new ObjectOutputStream(
new BufferedOutputStream(response.getOutputStream()));
response.setContentType(CONTENT_TYPE);
String host = request.getHeader("jms-host");
int port = request.getIntHeader("jms-port");
try {
Object object = input.readObject();
if (object instanceof Vector) {
Vector message = (Vector) object;
Client connection = getConnection(host, port);
synchronized (connection) {
connection.send(message);
String type = request.getHeader("jms-response");
if (type != null && type.equals("yes")) {
message = (Vector) connection.receive();
output.writeObject(message);
}
}
response.setStatus(HttpServletResponse.SC_OK);
} else if (object instanceof String &&
"close".equals(object)) {
removeConnection(host, port);
} else {
log("Received invalid request");
response.setStatus(
HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
output.writeObject("Object is NULL, or UNEXPECTED type");
}
} catch (Exception exception) {
log("Failed to process request", exception);
removeConnection(host, port);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
output.writeObject(exception.getMessage());
} finally {
output.flush();
}
}
/**
* Clean up client connections
*/
public synchronized void destroy() {
Iterator iterator = _connections.values().iterator();
while (iterator.hasNext()) {
Client client = (Client) iterator.next();
try {
client.close();
} catch (IOException ignore) {
}
}
}
/**
* Returns a cached TCP connection for the specified host name and
* port number, creating one if none exists.
*
* @param host the host name of the client
* @param port the TCP port number
* @throws IOException if a connection cannot be establised
*/
private synchronized Client getConnection(String host, int port)
throws IOException {
String key = host + ":" + port;
Client client = (Client) _connections.get(key);
if (client == null) {
try {
client = new Client(host, port);
} catch (IOException exception) {
throw new IOException("Failed to connect to host=" + host
+ ", port=" + port + ": "
+ exception.getMessage());
}
_connections.put(key, client);
}
return client;
}
/**
* Remove a cached TCP connection.
*
* @param host the host name of the client
* @param port the TCP port number
*/
private synchronized void removeConnection(String host, int port) {
String key = host + ":" + port;
Client client = (Client) _connections.remove(key);
if (client != null) {
try {
client.close();
} catch (IOException ignore) {
// may already be closed.
}
}
}
} //-- HttpJmsClientServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -