Fixing UpgradeRequest / UpgradeResponse hierarchy.

This commit is contained in:
Joakim Erdfelt 2012-11-06 15:45:13 -07:00
parent 57c97505c8
commit 000cfe6986
6 changed files with 223 additions and 440 deletions

View File

@ -18,6 +18,9 @@
package org.eclipse.jetty.websocket.api;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -25,37 +28,139 @@ import javax.net.websocket.HandshakeRequest;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
public interface UpgradeRequest extends HandshakeRequest
public class UpgradeRequest implements HandshakeRequest
{
public void addExtensions(String... extConfigs);
private URI requestURI;
private List<String> subProtocols = new ArrayList<>();
private Object session;
public Map<String, String> getCookieMap();
protected UpgradeRequest()
{
/* anonymous, no requestURI, upgrade request */
}
public List<ExtensionConfig> getExtensions();
public UpgradeRequest(String requestURI)
{
this.requestURI = URI.create(requestURI);
}
public String getHeader(String name);
public UpgradeRequest(URI requestURI)
{
this.requestURI = requestURI;
}
public void addExtensions(String... extConfigs)
{
// TODO Auto-generated method stub
}
public List<ExtensionConfig> getExtensions()
{
// TODO Auto-generated method stub
return null;
}
public String getHeader(String name)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, List<String>> getHeaders();
public Map<String, List<String>> getHeaders()
{
// TODO Auto-generated method stub
return null;
}
public String getHost();
public String getHost()
{
return getHeader("Host");
}
public String getHttpVersion();
public String getHttpVersion()
{
// TODO Auto-generated method stub
return null;
}
public String getMethod();
public String getMethod()
{
// TODO Auto-generated method stub
return null;
}
public String getOrigin();
public String getOrigin()
{
return getHeader("Origin");
}
@Override
public String getQueryString();
public Map<String, String[]> getParameterMap()
{
// TODO Auto-generated method stub
return null;
}
public String getRemoteURI();
@Override
public String getQueryString()
{
return requestURI.getQuery();
}
public List<String> getSubProtocols();
@Override
public URI getRequestURI()
{
return requestURI;
}
public boolean hasSubProtocol(String test);
@Override
public Object getSession()
{
return session;
}
public boolean isOrigin(String test);
public List<String> getSubProtocols()
{
return subProtocols;
}
public void setSubProtocols(String protocols);
@Override
public Principal getUserPrincipal()
{
// TODO Auto-generated method stub
return null;
}
public boolean hasSubProtocol(String test)
{
return subProtocols.contains(test);
}
public boolean isOrigin(String test)
{
return test.equalsIgnoreCase(getOrigin());
}
@Override
public boolean isUserInRole(String role)
{
// TODO Auto-generated method stub
return false;
}
public void setSession(Object session)
{
this.session = session;
}
public void setSubProtocols(List<String> subProtocols)
{
this.subProtocols = subProtocols;
}
public void setSubProtocols(String protocols)
{
// TODO Auto-generated method stub
}
}

View File

@ -21,41 +21,84 @@ package org.eclipse.jetty.websocket.api;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.websocket.HandshakeResponse;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
public interface UpgradeResponse extends HandshakeResponse
public class UpgradeResponse implements HandshakeResponse
{
public void addHeader(String name, String value);
public void addHeader(String name, String value)
{
// TODO Auto-generated method stub
}
/**
* Get the accepted WebSocket protocol.
*
* @return the accepted WebSocket protocol.
*/
public String getAcceptedSubProtocol();
public String getAcceptedSubProtocol()
{
// TODO Auto-generated method stub
return null;
}
/**
* Get the list of extensions that should be used for the websocket.
*
* @return the list of negotiated extensions to use.
*/
public List<ExtensionConfig> getExtensions();
public List<ExtensionConfig> getExtensions()
{
// TODO Auto-generated method stub
return null;
}
public Set<String> getHeaderNamesSet();
public Set<String> getHeaderNamesSet()
{
// TODO Auto-generated method stub
return null;
}
public String getHeaderValue(String name);
@Override
public Map<String, List<String>> getHeaders()
{
// TODO Auto-generated method stub
return null;
}
public Iterator<String> getHeaderValues(String name);
public String getHeaderValue(String name)
{
// TODO Auto-generated method stub
return null;
}
public int getStatusCode();
public Iterator<String> getHeaderValues(String name)
{
// TODO Auto-generated method stub
return null;
}
public String getStatusReason();
public int getStatusCode()
{
// TODO Auto-generated method stub
return -1;
}
public boolean isSuccess();
public String getStatusReason()
{
// TODO Auto-generated method stub
return null;
}
public boolean isSuccess()
{
// TODO Auto-generated method stub
return false;
}
/**
* Issue a forbidden upgrade response.
@ -68,7 +111,9 @@ public interface UpgradeResponse extends HandshakeResponse
* the short 1 line detail message about the forbidden response
* @throws IOException
*/
public void sendForbidden(String message) throws IOException;
public void sendForbidden(String message) throws IOException
{
}
/**
* Set the accepted WebSocket Protocol.
@ -76,7 +121,10 @@ public interface UpgradeResponse extends HandshakeResponse
* @param protocol
* the protocol to list as accepted
*/
public void setAcceptedSubProtocol(String protocol);
public void setAcceptedSubProtocol(String protocol)
{
// TODO Auto-generated method stub
}
/**
* Set the list of extensions that are approved for use with this websocket.
@ -93,9 +141,18 @@ public interface UpgradeResponse extends HandshakeResponse
* @param extensions
* the list of extensions to use.
*/
public void setExtensions(List<ExtensionConfig> extensions);
public void setExtensions(List<ExtensionConfig> extensions)
{
// TODO Auto-generated method stub
}
public void setHeader(String name, String value);
public void setHeader(String name, String value)
{
// TODO Auto-generated method stub
}
public void validateWebSocketHash(String expectedHash) throws UpgradeException;
public void validateWebSocketHash(String expectedHash) throws UpgradeException
{
// TODO Auto-generated method stub
}
}

View File

@ -18,19 +18,11 @@
package org.eclipse.jetty.websocket.common.extensions.mux;
import java.net.URI;
import java.nio.ByteBuffer;
import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
import org.eclipse.jetty.websocket.common.RequestedExtensionConfig;
public class MuxRequest implements UpgradeRequest
public class MuxRequest extends UpgradeRequest
{
public static final String HEADER_VALUE_DELIM="\"\\\n\r\t\f\b%+ ;=";
@ -38,8 +30,6 @@ public class MuxRequest implements UpgradeRequest
{
MuxRequest req = new MuxRequest(baseReq);
req.method = overlay(deltaReq.getMethod(),req.getMethod());
// TODO: finish
return req;
@ -61,186 +51,13 @@ public class MuxRequest implements UpgradeRequest
return req;
}
private String method;
private String httpVersion;
private String remoteURI;
private String queryString;
private List<String> subProtocols;
private Map<String, String> cookies;
private List<ExtensionConfig> extensions;
private Map<String, List<String>> headers;
private Map<String, String[]> parameterMap;
public MuxRequest()
{
// TODO Auto-generated constructor stub
super();
}
public MuxRequest(UpgradeRequest copy)
{
// TODO Auto-generated constructor stub
}
@Override
public void addExtensions(String... extConfigs)
{
for (String extConfig : extConfigs)
{
extensions.add(RequestedExtensionConfig.parse(extConfig));
}
}
@Override
public Map<String, String> getCookieMap()
{
return cookies;
}
@Override
public List<ExtensionConfig> getExtensions()
{
return extensions;
}
@Override
public String getHeader(String name)
{
List<String> values = headers.get(name);
// not set
if ((values == null) || (values.isEmpty()))
{
return null;
}
// only 1 value (most common scenario)
if (values.size() == 1)
{
return values.get(0);
}
// merge multiple values together
StringBuilder ret = new StringBuilder();
boolean delim = false;
for (String value : values)
{
if (delim)
{
ret.append(", ");
}
QuotedStringTokenizer.quoteIfNeeded(ret,value,HEADER_VALUE_DELIM);
delim = true;
}
return ret.toString();
}
@Override
public Map<String, List<String>> getHeaders()
{
return headers;
}
@Override
public String getHost()
{
return getHeader("Host");
}
@Override
public String getHttpVersion()
{
return httpVersion;
}
@Override
public String getMethod()
{
return method;
}
@Override
public String getOrigin()
{
return getHeader("Origin");
}
@Override
public Map<String, String[]> getParameterMap()
{
return parameterMap;
}
@Override
public String getQueryString()
{
return queryString;
}
@Override
public String getRemoteURI()
{
return remoteURI;
}
@Override
public URI getRequestURI()
{
// TODO Auto-generated method stub
return null;
}
@Override
public Object getSession()
{
// TODO Auto-generated method stub
return null;
}
@Override
public List<String> getSubProtocols()
{
return subProtocols;
}
@Override
public Principal getUserPrincipal()
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasSubProtocol(String test)
{
for (String protocol : subProtocols)
{
if (protocol.equalsIgnoreCase(test))
{
return true;
}
}
return false;
}
@Override
public boolean isOrigin(String test)
{
return test.equalsIgnoreCase(getOrigin());
}
@Override
public boolean isUserInRole(String role)
{
// TODO Auto-generated method stub
return false;
}
@Override
public void setSubProtocols(String protocols)
{
this.subProtocols.clear();
if (StringUtil.isBlank(protocols))
{
return;
}
for (String protocol : protocols.split("\\s*,\\s*"))
{
this.subProtocols.add(protocol);
}
super();
}
}

View File

@ -18,120 +18,9 @@
package org.eclipse.jetty.websocket.common.extensions.mux;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jetty.websocket.api.UpgradeException;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
public class MuxResponse implements UpgradeResponse
public class MuxResponse extends UpgradeResponse
{
@Override
public void addHeader(String name, String value)
{
// TODO Auto-generated method stub
}
@Override
public String getAcceptedSubProtocol()
{
// TODO Auto-generated method stub
return null;
}
@Override
public List<ExtensionConfig> getExtensions()
{
// TODO Auto-generated method stub
return null;
}
@Override
public Set<String> getHeaderNamesSet()
{
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, List<String>> getHeaders()
{
// TODO Auto-generated method stub
return null;
}
@Override
public String getHeaderValue(String name)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator<String> getHeaderValues(String name)
{
// TODO Auto-generated method stub
return null;
}
@Override
public int getStatusCode()
{
// TODO Auto-generated method stub
return 0;
}
@Override
public String getStatusReason()
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isSuccess()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void sendForbidden(String message) throws IOException
{
// TODO Auto-generated method stub
}
@Override
public void setAcceptedSubProtocol(String protocol)
{
// TODO Auto-generated method stub
}
@Override
public void setExtensions(List<ExtensionConfig> extensions)
{
// TODO Auto-generated method stub
}
@Override
public void setHeader(String name, String value)
{
// TODO Auto-generated method stub
}
@Override
public void validateWebSocketHash(String expectedHash) throws UpgradeException
{
// TODO Auto-generated method stub
}
}

View File

@ -26,23 +26,30 @@ import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.websocket.core.api.Extension;
import org.eclipse.jetty.websocket.core.api.UpgradeRequest;
import org.eclipse.jetty.websocket.core.protocol.ExtensionConfig;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.extensions.Extension;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
public class ServletWebSocketRequest extends HttpServletRequestWrapper implements UpgradeRequest
public class ServletWebSocketRequest extends UpgradeRequest
{
private List<String> subProtocols = new ArrayList<>();
private List<ExtensionConfig> extensions;
private Map<String, String> cookieMap;
public ServletWebSocketRequest(HttpServletRequest request)
{
super(request);
super(request.getRequestURI());
// TODO: copy values over
cookieMap = new HashMap<String, String>();
for (Cookie cookie : request.getCookies())
{
cookieMap.put(cookie.getName(),cookie.getValue());
}
Enumeration<String> protocols = request.getHeaders("Sec-WebSocket-Protocol");
List<String> subProtocols = new ArrayList<>();
String protocol = null;
while ((protocol == null) && (protocols != null) && protocols.hasMoreElements())
{
@ -52,6 +59,7 @@ public class ServletWebSocketRequest extends HttpServletRequestWrapper implement
subProtocols.add(p);
}
}
setSubProtocols(subProtocols);
extensions = new ArrayList<>();
Enumeration<String> e = request.getHeaders("Sec-WebSocket-Extensions");
@ -74,15 +82,9 @@ public class ServletWebSocketRequest extends HttpServletRequestWrapper implement
}
}
@Override
public Map<String, String> getCookieMap()
{
Map<String, String> ret = new HashMap<String, String>();
for (Cookie cookie : super.getCookies())
{
ret.put(cookie.getName(),cookie.getValue());
}
return ret;
return cookieMap;
}
@Override
@ -91,61 +93,6 @@ public class ServletWebSocketRequest extends HttpServletRequestWrapper implement
return extensions;
}
@Override
public Map<String, List<String>> getHeaders()
{
// TODO Auto-generated method stub
return null;
}
@Override
public String getHost()
{
return getHeader("Host");
}
@Override
public String getHttpVersion()
{
// TODO Auto-generated method stub
return null;
}
@Override
public String getOrigin()
{
return getHeader("Origin");
}
/**
* Get the endpoint of the WebSocket connection.
* <p>
* Per the <a href="https://tools.ietf.org/html/rfc6455#section-1.3">Opening Handshake (RFC 6455)</a>
*/
@Override
public String getRemoteURI()
{
return getRequestURI();
}
@Override
public List<String> getSubProtocols()
{
return subProtocols;
}
@Override
public boolean hasSubProtocol(String test)
{
return subProtocols.contains(test);
}
@Override
public boolean isOrigin(String test)
{
return test.equalsIgnoreCase(getOrigin());
}
protected String[] parseProtocols(String protocol)
{
if (protocol == null)
@ -165,17 +112,6 @@ public class ServletWebSocketRequest extends HttpServletRequestWrapper implement
return protocols;
}
/**
* Not implemented (not relevant) on server side.
*
* @see org.eclipse.jetty.websocket.core.api.UpgradeRequest#setSubProtocols(java.lang.String)
*/
@Override
public void setSubProtocols(String protocol)
{
/* not relevant for server side/servlet work */
}
public void setValidExtensions(List<Extension> valid)
{
if (this.extensions != null)

View File

@ -20,27 +20,25 @@ package org.eclipse.jetty.websocket.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.websocket.core.api.UpgradeException;
import org.eclipse.jetty.websocket.core.api.UpgradeResponse;
import org.eclipse.jetty.websocket.core.protocol.ExtensionConfig;
import org.eclipse.jetty.websocket.api.UpgradeException;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
public class ServletWebSocketResponse extends HttpServletResponseWrapper implements UpgradeResponse
public class ServletWebSocketResponse extends UpgradeResponse
{
private String acceptedProtocol;
private List<ExtensionConfig> extensions = new ArrayList<>();
private boolean success = true;
private HttpServletResponse resp;
public ServletWebSocketResponse(HttpServletResponse resp)
{
super(resp);
super();
this.resp = resp;
}
@Override
@ -61,25 +59,6 @@ public class ServletWebSocketResponse extends HttpServletResponseWrapper impleme
return this.extensions;
}
@Override
public Set<String> getHeaderNamesSet()
{
Collection<String> names = getHeaderNames();
return new HashSet<String>(names);
}
@Override
public String getHeaderValue(String name)
{
return super.getHeader(name);
}
@Override
public Iterator<String> getHeaderValues(String name)
{
return super.getHeaders(name).iterator();
}
@Override
public int getStatusCode()
{
@ -102,7 +81,7 @@ public class ServletWebSocketResponse extends HttpServletResponseWrapper impleme
public void sendForbidden(String message) throws IOException
{
success = false;
sendError(HttpServletResponse.SC_FORBIDDEN,message);
resp.sendError(HttpServletResponse.SC_FORBIDDEN,message);
}
@Override