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