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.generic; 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.AbstractGravityServlet; 031import org.granite.gravity.AsyncHttpContext; 032import org.granite.gravity.Gravity; 033import org.granite.gravity.GravityManager; 034import org.granite.logging.Logger; 035 036import flex.messaging.messages.AsyncMessage; 037import flex.messaging.messages.Message; 038 039/** 040 * @author William DRAI 041 */ 042public class GravityGenericServlet extends AbstractGravityServlet { 043 044 private static final long serialVersionUID = 1L; 045 046 private static final Logger log = Logger.getLogger(GravityGenericServlet.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 Gravity gravity = GravityManager.getGravity(getServletContext()); 055 GenericChannelFactory channelFactory = new GenericChannelFactory(gravity); 056 057 try { 058 // Setup context (thread local GraniteContext, etc.) 059 initializeRequest(gravity, request, response); 060 061 AsyncMessage connect = getConnectMessage(request); 062 063 // Resumed request (new received messages or timeout). 064 if (connect != null) { 065 try { 066 String channelId = (String)connect.getClientId(); 067 GenericChannel channel = gravity.getChannel(channelFactory, channelId); 068 // Reset channel continuation instance and deliver pending messages. 069 synchronized (channel) { 070 channel.close(); 071 channel.runReceived(new AsyncHttpContext(request, response, connect)); 072 } 073 } 074 finally { 075 removeConnectMessage(request); 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 for 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 GenericChannel 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 // No pending messages, wait for new ones or timeout. 111 setConnectMessage(request, amf3Request); 112 synchronized (channel) { 113 WaitingContinuation continuation = new WaitingContinuation(channel); 114 channel.setContinuation(continuation); 115 continuation.suspend(getLongPollingTimeout()); 116 } 117 } 118 119 return; 120 } 121 122 if (amf3Responses == null) 123 amf3Responses = new Message[amf3Requests.length]; 124 amf3Responses[i] = amf3Response; 125 } 126 127 log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses); 128 129 serialize(gravity, response, amf3Responses); 130 } 131 catch (IOException e) { 132 log.error(e, "Gravity message error"); 133 throw e; 134 } 135 catch (ClassNotFoundException e) { 136 log.error(e, "Gravity message error"); 137 throw new ServletException("Gravity message error", e); 138 } 139 finally { 140 // Cleanup context (thread local GraniteContext, etc.) 141 cleanupRequest(request); 142 } 143 144 removeConnectMessage(request); 145 } 146}