001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.gravity.jetty8;
022
023import java.io.IOException;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.eclipse.jetty.continuation.Continuation;
030import org.eclipse.jetty.continuation.ContinuationSupport;
031import org.eclipse.jetty.continuation.ContinuationThrowable;
032import org.granite.gravity.AbstractGravityServlet;
033import org.granite.gravity.AsyncHttpContext;
034import org.granite.gravity.Gravity;
035import org.granite.gravity.GravityManager;
036import org.granite.gravity.GravityServletUtil;
037import org.granite.logging.Logger;
038
039import flex.messaging.messages.AsyncMessage;
040import flex.messaging.messages.Message;
041
042/**
043 * @author William DRAI
044 */
045public class GravityJettyServlet extends AbstractGravityServlet {
046
047    private static final long serialVersionUID = 1L;
048
049    private static final Logger log = Logger.getLogger(GravityJettyServlet.class);
050
051
052    @Override
053    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
054        
055        log.debug("doPost: from %s:%d", request.getRemoteAddr(), request.getRemotePort());
056
057                Gravity gravity = GravityManager.getGravity(getServletContext());
058                ContinuationChannelFactory channelFactory = new ContinuationChannelFactory(gravity);
059                
060                try {
061                        // Setup context (thread local GraniteContext, etc.)
062                        initializeRequest(gravity, request, response);
063                        
064                        AsyncMessage connect = getConnectMessage(request);
065                        
066                        // Resumed request (pending messages or timeout).
067                        if (connect != null) {
068                                String channelId = (String)connect.getClientId();
069                                ContinuationChannel channel = gravity.getChannel(channelFactory, channelId);
070
071                                // Reset channel continuation instance and deliver pending messages.
072                                synchronized (channel) {
073                                        channel.close();
074                                        channel.runReceived(new AsyncHttpContext(request, response, connect));
075                                }
076                                
077                                return;
078                        }
079                        
080                        // New Request.
081                        Message[] amf3Requests = deserialize(gravity, request);
082
083            log.debug(">> [AMF3 REQUESTS] %s", (Object)amf3Requests);
084
085            Message[] amf3Responses = null;
086            
087            boolean accessed = false;
088            for (int i = 0; i < amf3Requests.length; i++) {
089                Message amf3Request = amf3Requests[i];
090                
091                // Ask gravity to create a specific response (will be null with a connect request from tunnel).
092                Message amf3Response = gravity.handleMessage(channelFactory, amf3Request);
093                String channelId = (String)amf3Request.getClientId();
094                
095                // Mark current channel (if any) as accessed.
096                if (!accessed)
097                        accessed = gravity.access(channelId);
098                
099                // (Re)Connect message from tunnel.
100                if (amf3Response == null) {
101                    if (amf3Requests.length > 1)
102                        throw new IllegalArgumentException("Only one request is allowed on tunnel.");
103
104                        ContinuationChannel channel = gravity.getChannel(channelFactory, channelId);
105                        if (channel == null)
106                                throw new NullPointerException("No channel on tunnel connect");
107                        
108                    // Try to send pending messages if any (using current container thread).
109                        if (!channel.runReceived(new AsyncHttpContext(request, response, amf3Request))) {
110                        
111                                // No pending messages, wait for new ones or timeout.
112                                synchronized (channel) {
113                                        Continuation continuation = ContinuationSupport.getContinuation(request);
114                                        continuation.setTimeout(getLongPollingTimeout());
115                                        continuation.setAttribute(GravityServletUtil.CONNECT_MESSAGE_KEY, amf3Request);
116                                        channel.setContinuation(continuation);
117                                        
118                                        // Suspend the current resquest/response processing and free the thread
119                                        continuation.suspend(response);
120                                }
121                        }
122                        
123                        return;
124                }
125
126                if (amf3Responses == null)
127                        amf3Responses = new Message[amf3Requests.length];
128                amf3Responses[i] = amf3Response;
129            }
130
131            log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses);
132
133            serialize(gravity, response, amf3Responses);
134                }
135        catch (ContinuationThrowable e) {
136            throw e;
137        }
138        catch (IOException e) {
139            log.error(e, "Gravity message error");
140            throw e;
141        }
142        catch (Exception e) {
143            log.error(e, "Gravity message error");
144            throw new ServletException(e);
145        }
146                finally {
147                        // Cleanup context (thread local GraniteContext, etc.)
148                        cleanupRequest(request);
149                }
150    }
151}