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.generic;
022
023import java.io.IOException;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.granite.gravity.AbstractGravityServlet;
030import org.granite.gravity.AsyncHttpContext;
031import org.granite.gravity.Gravity;
032import org.granite.gravity.GravityManager;
033import org.granite.logging.Logger;
034
035import flex.messaging.messages.AsyncMessage;
036import flex.messaging.messages.Message;
037
038/**
039 * @author William DRAI
040 */
041public class GravityGenericServlet extends AbstractGravityServlet {
042
043    private static final long serialVersionUID = 1L;
044
045    private static final Logger log = Logger.getLogger(GravityGenericServlet.class);
046
047
048    @Override
049    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
050        
051        log.debug("doPost: from %s:%d", request.getRemoteAddr(), request.getRemotePort());
052
053                Gravity gravity = GravityManager.getGravity(getServletContext());
054                GenericChannelFactory channelFactory = new GenericChannelFactory(gravity);
055                
056                try {
057                        // Setup context (thread local GraniteContext, etc.)
058                        initializeRequest(gravity, request, response);
059                        
060                        AsyncMessage connect = getConnectMessage(request);
061                        
062                        // Resumed request (new received messages or timeout).
063                        if (connect != null) {
064                                try {
065                                        String channelId = (String)connect.getClientId();
066                                        GenericChannel channel = gravity.getChannel(channelFactory, channelId);
067                                        // Reset channel continuation instance and deliver pending messages.
068                                        synchronized (channel) {
069                                                channel.close();
070                                                channel.runReceived(new AsyncHttpContext(request, response, connect));
071                                        }
072                                }
073                                finally {
074                                        removeConnectMessage(request);
075                                }
076                                return;
077                        }
078                        
079                        // New Request.
080                        Message[] amf3Requests = deserialize(gravity, request);
081
082            log.debug(">> [AMF3 REQUESTS] %s", (Object)amf3Requests);
083
084            Message[] amf3Responses = null;
085            
086            boolean accessed = false;
087            for (int i = 0; i < amf3Requests.length; i++) {
088                Message amf3Request = amf3Requests[i];
089                
090                // Ask gravity to create a specific response (will be null for connect request from tunnel).
091                Message amf3Response = gravity.handleMessage(channelFactory, amf3Request);
092                String channelId = (String)amf3Request.getClientId();
093                
094                // Mark current channel (if any) as accessed.
095                if (!accessed)
096                        accessed = gravity.access(channelId);
097                
098                // (Re)Connect message from tunnel.
099                if (amf3Response == null) {
100                    if (amf3Requests.length > 1)
101                        throw new IllegalArgumentException("Only one request is allowed on tunnel.");
102
103                        GenericChannel channel = gravity.getChannel(channelFactory, channelId);
104                        if (channel == null)
105                                throw new NullPointerException("No channel on tunnel connect");
106
107                    // Try to send pending messages if any (using current container thread).
108                        if (!channel.runReceived(new AsyncHttpContext(request, response, amf3Request))) {
109                        // No pending messages, wait for new ones or timeout.
110                            setConnectMessage(request, amf3Request);
111                                synchronized (channel) {
112                                        WaitingContinuation continuation = new WaitingContinuation(channel);
113                                        channel.setContinuation(continuation);
114                                        continuation.suspend(getLongPollingTimeout());
115                                }
116                        }
117                        
118                        return;
119                }
120
121                if (amf3Responses == null)
122                        amf3Responses = new Message[amf3Requests.length];
123                amf3Responses[i] = amf3Response;
124            }
125
126            log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses);
127
128            serialize(gravity, response, amf3Responses);
129                }
130        catch (IOException e) {
131            log.error(e, "Gravity message error");
132            throw e;
133        }
134        catch (ClassNotFoundException e) {
135            log.error(e, "Gravity message error");
136            throw new ServletException("Gravity message error", e);
137        }
138                finally {
139                        // Cleanup context (thread local GraniteContext, etc.)
140                        cleanupRequest(request);
141                }
142
143        removeConnectMessage(request);
144    }
145}