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.jetty; 023 024import java.io.IOException; 025 026import javax.servlet.ServletException; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.granite.gravity.*; 031import org.granite.logging.Logger; 032import org.mortbay.jetty.RetryRequest; 033import org.mortbay.util.ajax.Continuation; 034import org.mortbay.util.ajax.ContinuationSupport; 035 036import flex.messaging.messages.AsyncMessage; 037import flex.messaging.messages.Message; 038 039/** 040 * @author William DRAI 041 */ 042public class GravityJettyServlet extends AbstractGravityServlet { 043 044 private static final long serialVersionUID = 1L; 045 046 private static final Logger log = Logger.getLogger(GravityJettyServlet.class); 047 048 049 @Override 050 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 051 052 log.debug("doPost: from %s:%d", request.getRemoteAddr(), request.getRemotePort()); 053 054 rejectJMFContentType(request); 055 056 Gravity gravity = GravityManager.getGravity(getServletContext()); 057 ContinuationChannelFactory channelFactory = new ContinuationChannelFactory(gravity); 058 059 try { 060 // Setup context (thread local GraniteContext, etc.) 061 initializeRequest(gravity, request, response); 062 063 AsyncMessage connect = getConnectMessage(request); 064 065 // Resumed request (pending messages or timeout). 066 if (connect != null) { 067 String channelId = (String)connect.getClientId(); 068 ContinuationChannel channel = gravity.getChannel(channelFactory, channelId); 069 070 // Reset channel continuation instance and deliver pending messages. 071 synchronized (channel) { 072 channel.close(); 073 channel.runReceived(new AsyncHttpContext(request, response, connect)); 074 } 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 with a 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 ContinuationChannel 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 110 // No pending messages, wait for new ones or timeout. 111 setConnectMessage(request, amf3Request); 112 synchronized (channel) { 113 Continuation continuation = ContinuationSupport.getContinuation(request, channel); 114 channel.setContinuation(continuation); 115 116 // This call throws a RetryRequest exception, so we'll never reach return below... 117 continuation.suspend(getLongPollingTimeout()); 118 } 119 } 120 121 return; 122 } 123 124 if (amf3Responses == null) 125 amf3Responses = new Message[amf3Requests.length]; 126 amf3Responses[i] = amf3Response; 127 } 128 129 log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses); 130 131 serialize(gravity, response, amf3Responses); 132 } 133 catch (RetryRequest e) { 134 throw e; 135 } 136 catch (IOException e) { 137 log.error(e, "Gravity message error"); 138 throw e; 139 } 140 catch (Exception e) { 141 log.error(e, "Gravity message error"); 142 throw new ServletException(e); 143 } 144 finally { 145 // Cleanup context (thread local GraniteContext, etc.) 146 cleanupRequest(request); 147 } 148 } 149}