/** 
 * 
 * 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.activemq.transport.http;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.activemq.io.TextWireFormat;
import org.activemq.message.Packet;
import org.activemq.message.WireFormatInfo;
import org.activemq.transport.TransportChannelListener;
import org.activemq.transport.xstream.XStreamWireFormat;
import org.activemq.util.JMSExceptionHelper;

import EDU.oswego.cs.dl.util.concurrent.BoundedLinkedQueue;

/**
 * A servlet which handles server side HTTP transport, delegaging to the ActiveMQ broker.
 * This servlet is designed for being embedded inside an ActiveMQ Broker using an embedded
 * Jetty or Tomcat instance.
 *
 * @version $Revision$
 */
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) {
            wireFormat = createWireFormat();
        }
    }

    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 {
        try {
            Packet packet = wireFormat.fromString(readRequestBody(request));
            
            if( packet.getPacketType() == Packet.WIRE_FORMAT_INFO ) {
                
                // Can we handle the requested wire format?
                WireFormatInfo info = (WireFormatInfo) packet;
                if (!canProcessWireFormatVersion(info.getVersion())) {
	                response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot process wire format of version: " + info.getVersion());
                }
                
            } else {            
	            HttpServerTransportChannel transportChannel = getTransportChannel(request);
	            if (transportChannel == null) {
	                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
	            }	            
	            transportChannel.getPacketListener().consume(packet);
            }
        }
        catch (IOException e) {
            log.error("Caught: " + e, e);
        }
        catch (JMSException e) {
            throw JMSExceptionHelper.newIOException(e);
        }
    }

    /**
     * @param version
     * @return
     */
    private boolean canProcessWireFormatVersion(int version) {
        // TODO: 
        return true;
    }

    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));
    }

    protected TextWireFormat createWireFormat() {
        return new XStreamWireFormat();
    }
}
