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