424735 - WebSocket / Make ServletUpgradeRequest expose its HttpServletRequest

+ Added ServletUpgradeRequest.getHttpServletRequest() to access the
  underlying HttpServletRequest.
+ Wrapping underlying post-upgraded HttpServletRequest object with
  many disabled methods that are no longer relevant at this
  post-upgraded state of the connection.
This commit is contained in:
Joakim Erdfelt 2014-01-07 14:47:10 -07:00
parent a22cfbd885
commit fb8d55e46e
2 changed files with 207 additions and 3 deletions

View File

@ -0,0 +1,190 @@
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import javax.servlet.AsyncContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.Part;
public class PostUpgradedHttpServletRequest extends HttpServletRequestWrapper
{
private static final String UNSUPPORTED_WITH_WEBSOCKET_UPGRADE = "Feature unsupported with a Upgraded to WebSocket HttpServletRequest";
public PostUpgradedHttpServletRequest(HttpServletRequest request)
{
super(request);
}
@Override
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public String changeSessionId()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public AsyncContext getAsyncContext()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public String getCharacterEncoding()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public int getContentLength()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public long getContentLengthLong()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public String getContentType()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public ServletInputStream getInputStream() throws IOException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public Part getPart(String name) throws IOException, ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public Collection<Part> getParts() throws IOException, ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public BufferedReader getReader() throws IOException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public ServletRequest getRequest()
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public RequestDispatcher getRequestDispatcher(String path)
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public boolean isAsyncStarted()
{
return false;
}
@Override
public boolean isAsyncSupported()
{
return false;
}
@Override
public boolean isWrapperFor(Class<?> wrappedType)
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public boolean isWrapperFor(ServletRequest wrapped)
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public void login(String username, String password) throws ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public void logout() throws ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public void setRequest(ServletRequest request)
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public AsyncContext startAsync() throws IllegalStateException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
@Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
{
throw new UnsupportedOperationException(UNSUPPORTED_WITH_WEBSOCKET_UPGRADE);
}
}

View File

@ -34,6 +34,7 @@ import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
@ -44,12 +45,12 @@ import org.eclipse.jetty.websocket.api.util.WSURI;
*/
public class ServletUpgradeRequest extends UpgradeRequest
{
private HttpServletRequest req;
private final HttpServletRequest req;
public ServletUpgradeRequest(HttpServletRequest request) throws URISyntaxException
{
super(WSURI.toWebsocket(request.getRequestURL(),request.getQueryString()));
this.req = request;
this.req = new PostUpgradedHttpServletRequest(request);
// Copy Request Line Details
setMethod(request.getMethod());
@ -112,6 +113,19 @@ public class ServletUpgradeRequest extends UpgradeRequest
{
return (X509Certificate[])req.getAttribute("javax.servlet.request.X509Certificate");
}
/**
* Return the underlying HttpServletRequest that existed at Upgrade time.
* <p>
* Note: many features of the HttpServletRequest are invalid when upgraded,
* especially ones that deal with body content, streams, readers, and responses.
*
* @return a limited version of the underlying HttpServletRequest
*/
public HttpServletRequest getHttpServletRequest()
{
return req;
}
/**
* Equivalent to {@link HttpServletRequest#getLocalAddr()}
@ -264,7 +278,7 @@ public class ServletUpgradeRequest extends UpgradeRequest
* Note: this is equivalent to {@link HttpServletRequest#getSession()} and will not create a new HttpSession.
*/
@Override
public Object getSession()
public HttpSession getSession()
{
return this.req.getSession(false);
}