Package org.codehaus.activemq.transport.http

Source Code of org.codehaus.activemq.transport.http.HttpTunnelServlet

/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.transport.http;

import EDU.oswego.cs.dl.util.concurrent.BoundedLinkedQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.Packet;
import org.codehaus.activemq.message.TextWireFormat;
import org.codehaus.activemq.transport.TransportChannelListener;
import org.codehaus.activemq.util.JMSExceptionHelper;

import javax.jms.JMSException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @version $Revision: 1.2 $
*/
public class HttpTunnelServlet extends HttpServlet {

    private static final Log log = LogFactory.getLog(HttpTunnelServlet.class);

    private TransportChannelListener listener;
    private TextWireFormat wireFormat;
    private Map clients = new HashMap();
    private long requestTimeout = 30000L;

    public void init() throws ServletException {
        super.init();
        listener = (TransportChannelListener) getServletContext().getAttribute("transportChannelListener");
        if (listener == null) {
            throw new ServletException("No such attribute 'transportChannelListener' available in the ServletContext");
        }
        wireFormat = (TextWireFormat) getServletContext().getAttribute("wireFormat");
        if (wireFormat == null) {
            throw new ServletException("No such attribute 'wireFormat' available in the ServletContext");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // lets return the next response
        Packet packet = null;
        try {
            HttpServerTransportChannel transportChannel = getTransportChannel(request);
            if (transportChannel == null) {
                return;
            }
            packet = (Packet) transportChannel.getChannel().poll(requestTimeout);
        }
        catch (InterruptedException e) {
            // ignore
        }
        if (packet == null) {
            response.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
        }
        else {
            try {
                wireFormat.writePacket(packet, new DataOutputStream(response.getOutputStream()));
            }
            catch (JMSException e) {
                throw JMSExceptionHelper.newIOException(e);
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpServerTransportChannel transportChannel = getTransportChannel(request);
        if (transportChannel == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
        else {
            try {
                Packet packet = wireFormat.fromString(readRequestBody(request));
                transportChannel.getPacketListener().consume(packet);
            }
            catch (IOException e) {
                log.error("Caught: " + e, e);
            }
            catch (JMSException e) {
                throw JMSExceptionHelper.newIOException(e);
            }
        }
    }

    protected String readRequestBody(HttpServletRequest request) throws IOException {
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = request.getReader();
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            else {
                buffer.append(line);
                buffer.append("\n");
            }
        }
        return buffer.toString();
    }

    protected HttpServerTransportChannel getTransportChannel(HttpServletRequest request) {
        String clientID = request.getHeader("clientID");
        if (clientID == null) {
            clientID = request.getParameter("clientID");
        }
        if (clientID == null) {
            log.warn("No clientID header so ignoring request");
            return null;
        }
        synchronized (this) {
            HttpServerTransportChannel answer = (HttpServerTransportChannel) clients.get(clientID);
            if (answer == null) {
                answer = createTransportChannel();
                clients.put(clientID, answer);
                listener.addClient(answer);
            }
            else {
                // this lookup should keep the client alive, otherwise we need to discard it
                keepAlivePing(answer);
            }
            return answer;
        }
    }

    /**
     * Disable this channel from being auto-disconnected after a timeout period
     */
    protected void keepAlivePing(HttpServerTransportChannel channel) {
        /** TODO */
    }

    protected HttpServerTransportChannel createTransportChannel() {
        return new HttpServerTransportChannel(new BoundedLinkedQueue(10));
    }
}
TOP

Related Classes of org.codehaus.activemq.transport.http.HttpTunnelServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.