Author: remy.maucherat(a)jboss.com
Date: 2012-03-28 08:51:24 -0400 (Wed, 28 Mar 2012)
New Revision: 2011
Added:
branches/7.0.x/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
branches/7.0.x/test/java/org/jboss/web/upgrade/
branches/7.0.x/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
Modified:
branches/7.0.x/java/org/apache/catalina/connector/LocalStrings.properties
branches/7.0.x/java/org/apache/catalina/connector/Response.java
branches/7.0.x/java/org/apache/catalina/connector/ResponseFacade.java
branches/7.0.x/java/org/apache/coyote/ActionCode.java
branches/7.0.x/java/org/apache/coyote/http11/Http11AprProcessor.java
branches/7.0.x/java/org/apache/coyote/http11/InternalAprInputBuffer.java
branches/7.0.x/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
branches/7.0.x/webapps/docs/changelog.xml
trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java
trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
Log:
- Port upgrade API, since it is very simple and integrated with the existing event API.
- Add recycling of the filters, just in case.
Modified: branches/7.0.x/java/org/apache/catalina/connector/LocalStrings.properties
===================================================================
--- branches/7.0.x/java/org/apache/catalina/connector/LocalStrings.properties 2012-03-28
08:18:47 UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/catalina/connector/LocalStrings.properties 2012-03-28
12:51:24 UTC (rev 2011)
@@ -39,6 +39,9 @@
coyoteResponse.sendFile.ise=Cannot call sendFile() after the response has been committed
coyoteResponse.sendFile.no=Sendfile is disabled
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: branches/7.0.x/java/org/apache/catalina/connector/Response.java
===================================================================
--- branches/7.0.x/java/org/apache/catalina/connector/Response.java 2012-03-28 08:18:47
UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/catalina/connector/Response.java 2012-03-28 12:51:24
UTC (rev 2011)
@@ -47,6 +47,7 @@
import org.apache.catalina.util.CharsetMapper;
import org.apache.catalina.util.DateTool;
import org.apache.catalina.util.StringManager;
+import org.apache.coyote.ActionCode;
import org.apache.naming.resources.CacheEntry;
import org.apache.naming.resources.ProxyDirContext;
import org.apache.tomcat.util.buf.CharChunk;
@@ -1341,6 +1342,38 @@
}
+ public void sendUpgrade()
+ throws IOException {
+
+ if (isCommitted())
+ throw new IllegalStateException
+ (sm.getString("coyoteResponse.upgrade.ise"));
+
+ if (!connector.hasIoEvents())
+ throw new IllegalStateException
+ (sm.getString("coyoteResponse.upgrade.noEvents"));
+
+ 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);
+
+ }
+
+
public void sendFile(String path, String absolutePath, long start, long end) {
if (isCommitted())
Modified: branches/7.0.x/java/org/apache/catalina/connector/ResponseFacade.java
===================================================================
--- branches/7.0.x/java/org/apache/catalina/connector/ResponseFacade.java 2012-03-28
08:18:47 UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/catalina/connector/ResponseFacade.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -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,6 +455,19 @@
}
+ public void sendUpgrade()
+ throws IOException {
+
+ if (isCommitted())
+ throw new IllegalStateException
+ (/*sm.getString("responseBase.reset.ise")*/);
+
+ response.setAppCommitted(true);
+
+ response.sendUpgrade();
+
+ }
+
public void setDateHeader(String name, long date) {
if (isCommitted())
Modified: branches/7.0.x/java/org/apache/coyote/ActionCode.java
===================================================================
--- branches/7.0.x/java/org/apache/coyote/ActionCode.java 2012-03-28 08:18:47 UTC (rev
2010)
+++ branches/7.0.x/java/org/apache/coyote/ActionCode.java 2012-03-28 12:51:24 UTC (rev
2011)
@@ -171,6 +171,11 @@
*/
public static final ActionCode ACTION_EVENT_WRITE = new ActionCode(27);
+ /**
+ * Ask for a protocol upgrade
+ */
+ public static final ActionCode UPGRADE = new ActionCode(28);
+
// ----------------------------------------------------------- Constructors
int code;
Modified: branches/7.0.x/java/org/apache/coyote/http11/Http11AprProcessor.java
===================================================================
--- branches/7.0.x/java/org/apache/coyote/http11/Http11AprProcessor.java 2012-03-28
08:18:47 UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/coyote/http11/Http11AprProcessor.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -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: branches/7.0.x/java/org/apache/coyote/http11/InternalAprInputBuffer.java
===================================================================
--- branches/7.0.x/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-28
08:18:47 UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -288,6 +288,15 @@
}
+ public void removeActiveFilters() {
+ // Recycle filters
+ for (int i = 0; i <= lastActiveFilter; i++) {
+ activeFilters[i].recycle();
+ }
+ lastActiveFilter = -1;
+ }
+
+
/**
* Set the swallow input flag.
*/
Modified: branches/7.0.x/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
===================================================================
--- branches/7.0.x/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-28
08:18:47 UTC (rev 2010)
+++ branches/7.0.x/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -283,6 +283,15 @@
}
+ public void removeActiveFilters() {
+ // Recycle filters
+ for (int i = 0; i <= lastActiveFilter; i++) {
+ activeFilters[i].recycle();
+ }
+ lastActiveFilter = -1;
+ }
+
+
// --------------------------------------------------------- Public Methods
Added: branches/7.0.x/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
===================================================================
--- branches/7.0.x/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java
(rev 0)
+++
branches/7.0.x/java/org/jboss/servlet/http/UpgradableHttpServletResponse.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -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;
+
+}
Added: branches/7.0.x/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
===================================================================
--- branches/7.0.x/test/java/org/jboss/web/upgrade/UpgradeServletTest.java
(rev 0)
+++ branches/7.0.x/test/java/org/jboss/web/upgrade/UpgradeServletTest.java 2012-03-28
12:51:24 UTC (rev 2011)
@@ -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;
+ }
+ }
+
+}
Modified: branches/7.0.x/webapps/docs/changelog.xml
===================================================================
--- branches/7.0.x/webapps/docs/changelog.xml 2012-03-28 08:18:47 UTC (rev 2010)
+++ branches/7.0.x/webapps/docs/changelog.xml 2012-03-28 12:51:24 UTC (rev 2011)
@@ -24,6 +24,9 @@
<fix>
<jboss-jira>JBPAPP-8505</jboss-jira>: Add back the multi files logic
for Windows. (jfclere/mturk)
</fix>
+ <add>
+ Protocol upgrade API. (remm)
+ </add>
</changelog>
</subsection>
</section>
Modified: trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java
===================================================================
--- trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-28 08:18:47
UTC (rev 2010)
+++ trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java 2012-03-28 12:51:24
UTC (rev 2011)
@@ -289,6 +289,10 @@
public void removeActiveFilters() {
+ // Recycle filters
+ for (int i = 0; i <= lastActiveFilter; i++) {
+ activeFilters[i].recycle();
+ }
lastActiveFilter = -1;
}
Modified: trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
===================================================================
--- trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-28 08:18:47
UTC (rev 2010)
+++ trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java 2012-03-28 12:51:24
UTC (rev 2011)
@@ -284,6 +284,10 @@
public void removeActiveFilters() {
+ // Recycle filters
+ for (int i = 0; i <= lastActiveFilter; i++) {
+ activeFilters[i].recycle();
+ }
lastActiveFilter = -1;
}