Author: remy.maucherat(a)jboss.com
Date: 2012-03-27 19:20:28 -0400 (Tue, 27 Mar 2012)
New Revision: 2009
Added:
trunk/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
trunk/test/java/org/jboss/web/upgrade/
trunk/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
Modified:
trunk/java/org/apache/catalina/connector/LocalStrings.properties
trunk/java/org/apache/catalina/connector/Response.java
trunk/java/org/apache/catalina/connector/ResponseFacade.java
trunk/java/org/apache/coyote/http11/Http11AprProcessor.java
trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java
trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
trunk/test/java/org/apache/el/lang/TestELSupport.java
Log:
Add simpler connection upgrade, now using the existing HttpEventServlet API.
Modified: trunk/java/org/apache/catalina/connector/LocalStrings.properties
===================================================================
--- trunk/java/org/apache/catalina/connector/LocalStrings.properties 2012-03-27 16:31:59
UTC (rev 2008)
+++ trunk/java/org/apache/catalina/connector/LocalStrings.properties 2012-03-27 23:20:28
UTC (rev 2009)
@@ -41,6 +41,7 @@
coyoteResponse.sendFile.path=Invalid path
coyoteResponse.upgrade.ise=Cannot call sendUpgrade() after the response has been
committed
coyoteResponse.upgrade.noEvents=Cannot upgrade from HTTP/1.1 without IO events
+coyoteResponse.upgrade.noHttpEventServlet=Cannot upgrade from HTTP/1.1 is not using an
HttpEventServlet
#
# CoyoteRequest
Modified: trunk/java/org/apache/catalina/connector/Response.java
===================================================================
--- trunk/java/org/apache/catalina/connector/Response.java 2012-03-27 16:31:59 UTC (rev
2008)
+++ trunk/java/org/apache/catalina/connector/Response.java 2012-03-27 23:20:28 UTC (rev
2009)
@@ -1342,7 +1342,7 @@
}
- public void sendUpgrade(org.jboss.web.upgrade.ProtocolHandler protocolHandler)
+ public void sendUpgrade()
throws IOException {
if (isCommitted())
@@ -1353,12 +1353,24 @@
throw new IllegalStateException
(sm.getString("coyoteResponse.upgrade.noEvents"));
- request.getCoyoteRequest().action(ActionCode.UPGRADE, protocolHandler);
+ if (!request.isEventMode() || request.getAsyncContext() != null)
+ throw new IllegalStateException
+ (sm.getString("coyoteResponse.upgrade.noHttpEventServlet"));
+ // Ignore any call from an included servlet
+ if (included)
+ return;
+
+ // Clear any data content that has been buffered
+ resetBuffer();
+
// Output required by RFC2616. Protocol specific headers should have
// already been set.
setStatus(HttpServletResponse.SC_SWITCHING_PROTOCOLS);
+ outputBuffer.flush();
+ request.getCoyoteRequest().action(ActionCode.UPGRADE, null);
+
// Cause the response to be finished (from the application perspective)
setSuspended(true);
Modified: trunk/java/org/apache/catalina/connector/ResponseFacade.java
===================================================================
--- trunk/java/org/apache/catalina/connector/ResponseFacade.java 2012-03-27 16:31:59 UTC
(rev 2008)
+++ trunk/java/org/apache/catalina/connector/ResponseFacade.java 2012-03-27 23:20:28 UTC
(rev 2009)
@@ -34,6 +34,7 @@
import org.apache.catalina.Globals;
import org.apache.catalina.security.SecurityUtil;
import org.apache.catalina.util.StringManager;
+import org.jboss.servlet.http.UpgradableHttpServletResponse;
/**
* Facade class that wraps a Coyote response object.
@@ -45,7 +46,7 @@
*/
@SuppressWarnings("deprecation")
public class ResponseFacade
- implements HttpServletResponse {
+ implements HttpServletResponse, UpgradableHttpServletResponse {
// ----------------------------------------------------------- DoPrivileged
@@ -454,7 +455,7 @@
}
- public void sendUpgrade(org.jboss.web.upgrade.ProtocolHandler protocolHandler)
+ public void sendUpgrade()
throws IOException {
if (isCommitted())
@@ -463,7 +464,7 @@
response.setAppCommitted(true);
- response.sendUpgrade(protocolHandler);
+ response.sendUpgrade();
}
Modified: trunk/java/org/apache/coyote/http11/Http11AprProcessor.java
===================================================================
--- trunk/java/org/apache/coyote/http11/Http11AprProcessor.java 2012-03-27 16:31:59 UTC
(rev 2008)
+++ trunk/java/org/apache/coyote/http11/Http11AprProcessor.java 2012-03-27 23:20:28 UTC
(rev 2009)
@@ -1297,6 +1297,10 @@
writeNotification = true;
} else if (actionCode == ActionCode.ACTION_EVENT_TIMEOUT) {
timeout = ((Integer) param).intValue();
+ } else if (actionCode == ActionCode.UPGRADE) {
+ // Switch to raw bytes mode
+ inputBuffer.removeActiveFilters();
+ outputBuffer.removeActiveFilters();
}
}
@@ -1653,7 +1657,7 @@
}
int statusCode = response.getStatus();
- if ((statusCode == 204) || (statusCode == 205)
+ if ((statusCode == 101) || (statusCode == 204) || (statusCode == 205)
|| (statusCode == 304)) {
// No entity body
outputBuffer.addActiveFilter
Modified: trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java
===================================================================
--- trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-27 16:31:59
UTC (rev 2008)
+++ trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-27 23:20:28
UTC (rev 2009)
@@ -288,6 +288,11 @@
}
+ public void removeActiveFilters() {
+ lastActiveFilter = -1;
+ }
+
+
/**
* Set the swallow input flag.
*/
Modified: trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
===================================================================
--- trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-27 16:31:59
UTC (rev 2008)
+++ trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-27 23:20:28
UTC (rev 2009)
@@ -283,6 +283,11 @@
}
+ public void removeActiveFilters() {
+ lastActiveFilter = -1;
+ }
+
+
// --------------------------------------------------------- Public Methods
Added: trunk/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
===================================================================
--- trunk/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
(rev 0)
+++ trunk/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java 2012-03-27
23:20:28 UTC (rev 2009)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.servlet.http;
+
+import java.io.IOException;
+
+/**
+ * Upgradable HTTP Servlet response.
+ *
+ * @author remm
+ */
+public interface UpgradableHttpServletResponse {
+
+ public void sendUpgrade()
+ throws IOException;
+
+}
Modified: trunk/test/java/org/apache/el/lang/TestELSupport.java
===================================================================
--- trunk/test/java/org/apache/el/lang/TestELSupport.java 2012-03-27 16:31:59 UTC (rev
2008)
+++ trunk/test/java/org/apache/el/lang/TestELSupport.java 2012-03-27 23:20:28 UTC (rev
2009)
@@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
Added: trunk/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
===================================================================
--- trunk/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
(rev 0)
+++ trunk/test/java/org/jboss/web/upgrade/UpgradeServletTest.java 2012-03-27 23:20:28 UTC
(rev 2009)
@@ -0,0 +1,107 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+
+package org.jboss.web.upgrade;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletResponse;
+
+import org.jboss.servlet.http.HttpEvent;
+import org.jboss.servlet.http.HttpEventServlet;
+import org.jboss.servlet.http.UpgradableHttpServletResponse;
+
+@WebServlet("/upgrade")
+public class UpgradeServletTest extends HttpServlet implements HttpEventServlet {
+
+ int count = 0;
+
+ public void event(HttpEvent event) throws IOException, ServletException {
+ System.out.println("[" +
event.getHttpServletRequest().getSession(true).getId() + "] " +
event.getType());
+ switch (event.getType()) {
+ case BEGIN:
+ event.setTimeout(20000);
+ HttpServletResponse response = event.getHttpServletResponse();
+ if (response instanceof UpgradableHttpServletResponse) {
+ response.setHeader("Upgrade", "Foobar");
+ response.setHeader("Connection", "Upgrade");
+ ((UpgradableHttpServletResponse) response).sendUpgrade();
+ } else {
+ throw new IllegalStateException("Cannot upgrade connection");
+ }
+ break;
+ case END:
+ break;
+ case ERROR:
+ event.close();
+ break;
+ case EVENT:
+ ServletOutputStream os = event.getHttpServletResponse().getOutputStream();
+ // Using while (true): Not checking if the connection is available to writing
immediately
+ // will cause the write to be performed in blocking mode.
+ // boolean b = true;
+ // while (b) {
+ while (event.isWriteReady()) {
+ if (count % 100 == 0) {
+ os.println((count++) + " ");
+ } else {
+ os.print((count++) + " ");
+ }
+ }
+ //if (event.ready())
+ // os.flush();
+ break;
+ case READ:
+ ServletInputStream is = event.getHttpServletRequest().getInputStream();
+ // Using while (true): Not checking if input is available will trigger a
blocking
+ // read. No other event should be triggered (the current READ event will be
in progress
+ // until the read timeouts, which will trigger an ERROR event due to an
IOException).
+ // while (true) {
+ while (is.available() > 0) {
+ int c = is.read();
+ if (c > 0) {
+ System.out.print((char) c);
+ } else {
+ System.out.print(c);
+ break;
+ }
+ }
+ System.out.println();
+ break;
+ case TIMEOUT:
+ // This will cause a generic event to be sent to the servlet every time the
connection is idle for
+ // a while.
+ event.resume();
+ break;
+ case WRITE:
+ break;
+ }
+ }
+
+}