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.jbossweb;
022
023 import java.io.IOException;
024 import java.io.InputStream;
025
026 import javax.servlet.ServletException;
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.http.HttpServletResponse;
029
030 import org.granite.gravity.AsyncHttpContext;
031 import org.granite.gravity.Gravity;
032 import org.granite.gravity.GravityManager;
033 import org.granite.gravity.tomcat.ByteArrayCometIO;
034 import org.granite.gravity.tomcat.CometIO;
035 import org.granite.logging.Logger;
036 import org.jboss.servlet.http.HttpEvent;
037
038 import flex.messaging.messages.Message;
039
040 /**
041 * @author Franck WOLFF
042 */
043 public class GravityJBossWebServlet extends AbstractHttpEventServlet {
044
045 private static final long serialVersionUID = 1L;
046
047 private static final Logger log = Logger.getLogger(GravityJBossWebServlet.class);
048
049
050 @Override
051 public CometIO createCometIO() {
052 return new ByteArrayCometIO();
053 }
054
055 @Override
056 public boolean handleRequest(HttpEvent event, InputStream content) throws IOException, ServletException {
057
058 Gravity gravity = GravityManager.getGravity(getServletContext());
059 JBossWebChannelFactory channelFactory = new JBossWebChannelFactory(gravity);
060
061 HttpServletRequest request = event.getHttpServletRequest();
062 HttpServletResponse response = event.getHttpServletResponse();
063
064 try {
065 initializeRequest(gravity, request, response);
066
067 Message[] amf3Requests = deserialize(gravity, request, content);
068
069 log.debug(">> [AMF3 REQUESTS] %s", (Object)amf3Requests);
070
071 Message[] amf3Responses = null;
072
073 boolean accessed = false;
074 for (int i = 0; i < amf3Requests.length; i++) {
075 Message amf3Request = amf3Requests[i];
076
077 // Ask gravity to create a specific response (will be null for connect request from tunnel).
078 Message amf3Response = gravity.handleMessage(channelFactory, amf3Request);
079 String channelId = (String)amf3Request.getClientId();
080
081 // Mark current channel (if any) as accessed.
082 if (!accessed)
083 accessed = gravity.access(channelId);
084
085 // (Re)Connect message from tunnel...
086 if (amf3Response == null) {
087 if (amf3Requests.length > 1)
088 throw new IllegalArgumentException("Only one request is allowed on tunnel.");
089
090 JBossWebChannel channel = gravity.getChannel(channelFactory, channelId);
091 if (channel == null)
092 throw new NullPointerException("No channel on tunnel connect");
093
094 // Try to send pending messages if any (using current container thread).
095 if (channel.runReceived(new AsyncHttpContext(request, response, amf3Request)))
096 return true; // Close http event.
097
098 // No pending messages, wait for new ones or timeout.
099 setConnectMessage(request, amf3Request);
100 channel.setHttpEvent(event);
101 return false; // Do not close http event.
102 }
103
104 if (amf3Responses == null)
105 amf3Responses = new Message[amf3Requests.length];
106 amf3Responses[i] = amf3Response;
107 }
108
109 log.debug("<< [AMF3 RESPONSES] %s", (Object)amf3Responses);
110
111 serialize(gravity, response, amf3Responses);
112 }
113 catch (IOException e) {
114 log.error(e, "Gravity message error");
115 throw e;
116 }
117 catch (Exception e) {
118 log.error(e, "Gravity message error");
119 throw new ServletException(e);
120 }
121 finally {
122 try {
123 if (content != null)
124 content.close();
125 }
126 finally {
127 cleanupRequest(request);
128 }
129 }
130
131 return true; // Close http event.
132 }
133
134 @Override
135 public boolean handleEnd(HttpEvent event) throws IOException, ServletException {
136 return true; // Close http event.
137 }
138
139 @Override
140 public boolean handleError(HttpEvent event) throws IOException {
141 if (EventUtil.isErrorButNotTimeout(event))
142 log.error("Got an error event: %s", EventUtil.toString(event));
143
144 try {
145 HttpServletRequest request = event.getHttpServletRequest();
146 Message connect = getConnectMessage(request);
147 if (connect != null) { // This should be a timeout.
148 Gravity gravity = GravityManager.getGravity(getServletContext());
149 String channelId = (String)connect.getClientId();
150 JBossWebChannelFactory channelFactory = new JBossWebChannelFactory(gravity);
151 JBossWebChannel channel = gravity.getChannel(channelFactory, channelId);
152
153 // Cancel channel's execution (timeout or other errors).
154 if (channel != null)
155 channel.setHttpEvent(null);
156 }
157 }
158 catch (Exception e) {
159 log.error(e, "Error while processing event: %s", EventUtil.toString(event));
160 }
161
162 return true; // Close http event.
163 }
164 }