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.gae;
022
023 import java.io.IOException;
024 import java.util.List;
025
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.Gravity;
033 import org.granite.gravity.GravityManager;
034 import org.granite.logging.Logger;
035
036 import com.google.apphosting.api.DeadlineExceededException;
037
038 import flex.messaging.messages.AsyncMessage;
039 import flex.messaging.messages.Message;
040
041
042 /**
043 * @author William DRAI
044 */
045 public class GravityGAEServlet extends AbstractGravityServlet {
046
047 private static final long serialVersionUID = 1L;
048
049 private static final Logger log = Logger.getLogger(GravityGAEServlet.class);
050
051
052 private static long GAE_TIMEOUT = 20000L;
053 private static long GAE_POLLING_INTERVAL = 500L;
054
055
056 @Override
057 public void init(ServletConfig config) throws ServletException {
058 super.init(config, new GAEChannelFactory());
059 }
060
061
062 @Override
063 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
064 doPost(req,resp);
065 }
066
067 @Override
068 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
069
070 Gravity gravity = GravityManager.getGravity(getServletContext());
071
072 try {
073 // Setup context (thread local GraniteContext, etc.)
074 initializeRequest(gravity, request, response);
075
076 // New Request.
077 Message[] amf3Requests = deserialize(gravity, request);
078
079 log.debug(">> [AMF3 REQUESTS] %s", (Object)amf3Requests);
080
081 Message[] amf3Responses = null;
082
083 boolean accessed = false;
084 for (int i = 0; i < amf3Requests.length; i++) {
085 Message amf3Request = amf3Requests[i];
086
087 // Ask gravity to create a specific response (will be null for connect request from tunnel).
088 Message amf3Response = gravity.handleMessage(amf3Request);
089 String channelId = (String)amf3Request.getClientId();
090
091 // Mark current channel (if any) as accessed.
092 if (!accessed)
093 accessed = gravity.access(channelId);
094
095 // (Re)Connect message from tunnel.
096 if (amf3Response == null) {
097 if (amf3Requests.length > 1)
098 throw new IllegalArgumentException("Only one request is allowed on tunnel.");
099
100 GAEChannel channel = (GAEChannel)gravity.getChannel(channelId);
101 if (channel == null)
102 throw new NullPointerException("No channel on connect");
103
104 long pollingInterval = gravity.getGravityConfig().getExtra().get("gae/@polling-interval", Long.TYPE, GAE_POLLING_INTERVAL);
105
106 long initialTime = System.currentTimeMillis();
107 do {
108 // Get messages or wait
109 List<Message> messages = null;
110 synchronized (channel) {
111 // Get pending messages from client queue
112 messages = channel.takeMessages();
113 }
114
115 // Send the messages
116 if (messages != null) {
117 amf3Responses = messages.toArray(new Message[0]);
118 ((AsyncMessage)amf3Responses[i]).setCorrelationId(amf3Requests[i].getMessageId());
119 break;
120 }
121
122 try {
123 Thread.sleep(pollingInterval);
124 }
125 catch (InterruptedException e) {
126 break;
127 }
128 catch (DeadlineExceededException e) {
129 break;
130 }
131 }
132 while (System.currentTimeMillis()-initialTime < GAE_TIMEOUT);
133
134 if (amf3Responses == null)
135 amf3Responses = new Message[0];
136 }
137 else {
138 if (amf3Responses == null)
139 amf3Responses = new Message[amf3Requests.length];
140 amf3Responses[i] = amf3Response;
141 }
142 }
143
144 log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses);
145
146 serialize(gravity, response, amf3Responses);
147 }
148 catch (IOException e) {
149 log.error(e, "Gravity message error");
150 throw e;
151 }
152 catch (Exception e) {
153 log.error(e, "Gravity message error");
154 throw new ServletException(e);
155 }
156 finally {
157 // Cleanup context (thread local GraniteContext, etc.)
158 cleanupRequest(request);
159 }
160 }
161 }