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    
021    package org.granite.gravity.servlet3;
022    
023    import java.io.IOException;
024    
025    import javax.servlet.AsyncContext;
026    import javax.servlet.ServletConfig;
027    import javax.servlet.ServletException;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    import org.granite.gravity.AbstractGravityServlet;
032    import org.granite.gravity.AsyncHttpContext;
033    import org.granite.gravity.Gravity;
034    import org.granite.gravity.GravityManager;
035    import org.granite.logging.Logger;
036    
037    import flex.messaging.messages.Message;
038    
039    /**
040     * @author Franck WOLFF
041     */
042    public class GravityAsyncServlet extends AbstractGravityServlet {
043    
044            private static final long serialVersionUID = 1L;
045    
046            private static final Logger log = Logger.getLogger(GravityAsyncServlet.class);
047        
048        @Override
049        public void init(ServletConfig config) throws ServletException {
050            super.init(config, new AsyncChannelFactory());
051        }
052    
053            @Override
054            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
055                    
056                    if (!request.isAsyncSupported())
057                            throw new ServletException("Asynchronous requests are not supported with this servlet. Please check your web.xml");
058    
059                    if (request.isAsyncStarted())
060                            throw new ServletException("Gravity Servlet3 implementation doesn't support dispatch(...) mode");
061    
062                    Gravity gravity = GravityManager.getGravity(getServletContext());
063            
064                    try {
065                initializeRequest(gravity, request, response);
066    
067                Message[] amf3Requests = deserialize(gravity, request);
068    
069                log.debug(">> [AMF3 REQUESTS] %s", (Object)amf3Requests);
070    
071                Message[] amf3Responses = null;
072                
073                boolean accessed = false;
074                for (int i = 0; i < amf3Requests.length; i++) {
075                    Message amf3Request = amf3Requests[i];
076    
077                    // Ask gravity to create a specific response (will be null for connect request from tunnel).
078                    Message amf3Response = gravity.handleMessage(amf3Request);
079                    String channelId = (String)amf3Request.getClientId();
080                    
081                    // Mark current channel (if any) as accessed.
082                    if (!accessed)
083                            accessed = gravity.access(channelId);
084                    
085                    // (Re)Connect message from tunnel...
086                    if (amf3Response == null) {
087                        if (amf3Requests.length > 1)
088                            throw new IllegalArgumentException("Only one connect request is allowed on tunnel.");
089    
090                        AsyncChannel channel = (AsyncChannel)gravity.getChannel(channelId);
091                        if (channel == null)
092                                    throw new NullPointerException("No channel on tunnel connect");
093                        
094                        // Try to send pending messages if any (using current container thread).
095                        if (!channel.runReceived(new AsyncHttpContext(request, response, amf3Request))) {
096                                // No pending messages, wait for new ones or timeout.
097                                setConnectMessage(request, amf3Request);
098                                    AsyncContext asyncContext = request.startAsync();
099                                    asyncContext.setTimeout(getLongPollingTimeout());
100                                    try {
101                                            asyncContext.addListener(new AsyncRequestListener(channel));
102                                            channel.setAsyncContext(asyncContext);
103                                    }
104                                    catch (Exception e) {
105                                            log.error(e, "Error while setting async context. Closing context...");
106                                            asyncContext.complete();
107                                    }
108                        }
109                        return;
110                    }
111    
112                    if (amf3Responses == null)
113                            amf3Responses = new Message[amf3Requests.length];
114                    amf3Responses[i] = amf3Response;
115                }
116    
117                log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses);
118    
119                serialize(gravity, response, amf3Responses);
120            }
121            catch (IOException e) {
122                log.error(e, "Gravity message error");
123                throw e;
124            }
125            catch (Exception e) {
126                log.error(e, "Gravity message error");
127                throw new ServletException(e);
128            }
129            finally {
130                    cleanupRequest(request);
131            }
132            }
133    }