org.sakaiproject.entitybroker.util.http
Class EntityHttpServletRequest

java.lang.Object
  extended by org.sakaiproject.entitybroker.util.http.EntityHttpServletRequest
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

public class EntityHttpServletRequest
extends Object
implements javax.servlet.http.HttpServletRequest

Makes a copy of the data in a servlet request into a new request object

Author:
Aaron Zeckoski (azeckoski @ gmail.com)

Nested Class Summary
static class EntityHttpServletRequest.EntityHttpSession
          A fake session for testing, from the spring mock session
static class EntityHttpServletRequest.EntityRequestDispatcher
          A non-functional request dispatcher, based on the spring mock version
static class EntityHttpServletRequest.EntityServletContext
          For testing, based on the spring Mock version
 
Field Summary
 javax.servlet.http.HttpServletRequest copy
           
static String DEFAULT_PROTOCOL
          default protocol: HTTP/1.0
static String DEFAULT_REMOTE_ADDR
          default remote address: 127.0.0.1
static String DEFAULT_REMOTE_HOST
          The default remote host: 'localhost'.
static String DEFAULT_SCHEMA
          default schema: http
static String DEFAULT_SERVER_ADDR
          default server address: 127.0.0.1
static String DEFAULT_SERVER_NAME
          default server name: localhost
static int DEFAULT_SERVER_PORT
          default server port: 80
 Map<String,String[]> pathQueryParams
          This stores all the query params found when the request was created
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req)
          Create a new request from a given request
EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req, String pathString)
          Create a new request from a given request and modify it based on the path string
EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req, String method, String pathString)
          Create a new request from a given request and modify it based on the path string and method
EntityHttpServletRequest(String pathString)
          Create a request using the pathString
EntityHttpServletRequest(String method, String pathString)
          Create a request using the pathString and setting the method
EntityHttpServletRequest(String method, String pathString, String... params)
          Create a request using the pathString and setting the method and params
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Adds a new cookie to the request
 void addHeader(String name, String... content)
           
 void addParameter(String name, String value)
          Add a single parameter, this will append to an existing one
 void addParameter(String name, String[] values)
          Add an array of values for a parameter, these will append to existing ones
 void clearAttributes()
          Remove all attributes
 void clearCookies()
          Remove all cookies
 void clearHeaders()
          Remove all headers
 void clearParameters()
          Remove all parameters
 Object getAttribute(String name)
           
 Enumeration getAttributeNames()
           
 String getAuthType()
           
 String getCharacterEncoding()
           
 javax.servlet.ServletInputStream getContent()
          Gets the content data out of the request, ensures that the data can be retrieved somehow
 int getContentLength()
           
 String getContentType()
           
 String getContextPath()
           
 javax.servlet.http.Cookie[] getCookies()
           
 long getDateHeader(String name)
           
 String getHeader(String name)
           
 Enumeration getHeaderNames()
           
 Enumeration getHeaders(String name)
           
 javax.servlet.ServletInputStream getInputStream()
           
 int getIntHeader(String name)
           
 String getLocalAddr()
           
 Locale getLocale()
           
 Enumeration getLocales()
           
 String getLocalName()
           
 int getLocalPort()
           
 String getMethod()
           
 String getParameter(String name)
           
 Map getParameterMap()
           
 Enumeration getParameterNames()
           
 Map<String,String[]> getParameters()
           
 String[] getParameterValues(String name)
           
 String getPathInfo()
           
 String getPathTranslated()
           
 String getProtocol()
           
 String getQueryString()
           
 BufferedReader getReader()
           
 String getRealPath(String path)
           
 String getRemoteAddr()
           
 String getRemoteHost()
           
 int getRemotePort()
           
 String getRemoteUser()
           
 javax.servlet.RequestDispatcher getRequestDispatcher(String path)
           
 String getRequestedSessionId()
           
 String getRequestURI()
           
 StringBuffer getRequestURL()
           
 String getScheme()
           
 String getServerName()
           
 int getServerPort()
           
 String getServletPath()
           
 javax.servlet.http.HttpSession getSession()
           
 javax.servlet.http.HttpSession getSession(boolean create)
           
 Map<String,String> getStringParameters()
           
 Principal getUserPrincipal()
           
 boolean isRequestedSessionIdFromCookie()
           
 boolean isRequestedSessionIdFromUrl()
           
 boolean isRequestedSessionIdFromURL()
           
 boolean isRequestedSessionIdValid()
           
 boolean isSecure()
           
 boolean isUserInRole(String role)
           
 void removeAttribute(String name)
           
 void removeCookie(String name)
          Removes the cookie with the given name from the request
 void removeHeader(String name)
           
 void removeParameter(String name)
          Removes a parameter and all values for it
 void setAttribute(String name, Object o)
           
 void setCharacterEncoding(String env)
           
 void setContent(byte[] content)
          Allows control over the content data which is used in this request, all data should be UTF-8 encoded
 void setContent(InputStream contentStream, int contentLength)
          Allows control over the content data which is used in this request, all data should be UTF-8 encoded
 void setContent(String content)
          Allows control over the content data which is used in this request, all data should be UTF-8 encoded
 void setContentType(String contentType)
          Sets the content type for this request content
 void setContextPath(String contextPath)
           
 void setLocale(Locale locale)
           
 void setMethod(String method)
           
 void setParameter(String name, String[] values)
          Set a new parameter (overwrite an existing one)
 void setParameters(Map<String,String[]> params)
          Set a large number of params at once, replaces existing params
 void setPathInfo(String pathInfo)
           
 URLData setPathString(String pathString)
          This will set the given url/path string values into this request, this will override any values that are currently set
 void setQueryString(String queryString)
           
 void setRequestValues(javax.servlet.http.HttpServletRequest req)
          Set all the values from a request on this request object and set this request as the one which the values were copied from
 void setServletPath(String servletPath)
           
 void setUseRealDispatcher(boolean real)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SCHEMA

public static final String DEFAULT_SCHEMA
default schema: http

See Also:
Constant Field Values

DEFAULT_PROTOCOL

public static final String DEFAULT_PROTOCOL
default protocol: HTTP/1.0

See Also:
Constant Field Values

DEFAULT_SERVER_ADDR

public static final String DEFAULT_SERVER_ADDR
default server address: 127.0.0.1

See Also:
Constant Field Values

DEFAULT_SERVER_NAME

public static final String DEFAULT_SERVER_NAME
default server name: localhost

See Also:
Constant Field Values

DEFAULT_SERVER_PORT

public static final int DEFAULT_SERVER_PORT
default server port: 80

See Also:
Constant Field Values

DEFAULT_REMOTE_ADDR

public static final String DEFAULT_REMOTE_ADDR
default remote address: 127.0.0.1

See Also:
Constant Field Values

DEFAULT_REMOTE_HOST

public static final String DEFAULT_REMOTE_HOST
The default remote host: 'localhost'.

See Also:
Constant Field Values

copy

public javax.servlet.http.HttpServletRequest copy

pathQueryParams

public Map<String,String[]> pathQueryParams
This stores all the query params found when the request was created

Constructor Detail

EntityHttpServletRequest

public EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req)
Create a new request from a given request

Parameters:
req - any request

EntityHttpServletRequest

public EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req,
                                String pathString)
Create a new request from a given request and modify it based on the path string

Parameters:
req - any request
pathString - any full path or URL (/direct/prefix/id.xml)

EntityHttpServletRequest

public EntityHttpServletRequest(javax.servlet.http.HttpServletRequest req,
                                String method,
                                String pathString)
Create a new request from a given request and modify it based on the path string and method

Parameters:
req - any request
method - GET, POST, PUT, DELETE (PUT and DELETE not supported by browsers), this will be set to POST if null or unset
pathString - any full path or URL (/direct/prefix/id.xml)

EntityHttpServletRequest

public EntityHttpServletRequest(String pathString)
Create a request using the pathString

Parameters:
pathString - any path or URL

EntityHttpServletRequest

public EntityHttpServletRequest(String method,
                                String pathString)
Create a request using the pathString and setting the method

Parameters:
pathString - any path or URL
method - GET, POST, PUT, DELETE (PUT and DELETE not supported by browsers), this will be set to POST if null or unset

EntityHttpServletRequest

public EntityHttpServletRequest(String method,
                                String pathString,
                                String... params)
Create a request using the pathString and setting the method and params

Parameters:
method - GET, POST, PUT, DELETE (PUT and DELETE not supported by browsers), this will be set to POST if null or unset
pathString - any path or URL
params - alternating keys and values (starting with keys) to place into the request parameters
Method Detail

toString

public String toString()
Overrides:
toString in class Object

setRequestValues

public void setRequestValues(javax.servlet.http.HttpServletRequest req)
Set all the values from a request on this request object and set this request as the one which the values were copied from

Parameters:
req - any request

setPathString

public URLData setPathString(String pathString)
This will set the given url/path string values into this request, this will override any values that are currently set

Parameters:
pathString - any url or path string
Returns:
the url data object based on the input object OR null if the string is null or empty

setContent

public void setContent(InputStream contentStream,
                       int contentLength)
Allows control over the content data which is used in this request, all data should be UTF-8 encoded

Parameters:
content - any IS content, UTF-8 encoded, replaces existing content

setContent

public void setContent(byte[] content)
Allows control over the content data which is used in this request, all data should be UTF-8 encoded

Parameters:
content - any byte[] content, UTF-8 encoded, replaces existing content

setContent

public void setContent(String content)
Allows control over the content data which is used in this request, all data should be UTF-8 encoded

Parameters:
content - any string content, UTF-8 encoded, replaces existing content

getContent

public javax.servlet.ServletInputStream getContent()
                                            throws IOException
Gets the content data out of the request, ensures that the data can be retrieved somehow

Returns:
the content data from the request
Throws:
IOException - if there is a failure

getAttribute

public Object getAttribute(String name)
Specified by:
getAttribute in interface javax.servlet.ServletRequest

getAttributeNames

public Enumeration getAttributeNames()
Specified by:
getAttributeNames in interface javax.servlet.ServletRequest

removeAttribute

public void removeAttribute(String name)
Specified by:
removeAttribute in interface javax.servlet.ServletRequest

setAttribute

public void setAttribute(String name,
                         Object o)
Specified by:
setAttribute in interface javax.servlet.ServletRequest

clearAttributes

public void clearAttributes()
Remove all attributes


setCharacterEncoding

public void setCharacterEncoding(String env)
                          throws UnsupportedEncodingException
Specified by:
setCharacterEncoding in interface javax.servlet.ServletRequest
Throws:
UnsupportedEncodingException

getCharacterEncoding

public String getCharacterEncoding()
Specified by:
getCharacterEncoding in interface javax.servlet.ServletRequest

setContentType

public void setContentType(String contentType)
Sets the content type for this request content

Parameters:
contentType -

getContentType

public String getContentType()
Specified by:
getContentType in interface javax.servlet.ServletRequest

getContentLength

public int getContentLength()
Specified by:
getContentLength in interface javax.servlet.ServletRequest

getInputStream

public javax.servlet.ServletInputStream getInputStream()
                                                throws IOException
Specified by:
getInputStream in interface javax.servlet.ServletRequest
Throws:
IOException

getReader

public BufferedReader getReader()
                         throws IOException
Specified by:
getReader in interface javax.servlet.ServletRequest
Throws:
IOException

setLocale

public void setLocale(Locale locale)

getLocale

public Locale getLocale()
Specified by:
getLocale in interface javax.servlet.ServletRequest

setMethod

public void setMethod(String method)

getMethod

public String getMethod()
Specified by:
getMethod in interface javax.servlet.http.HttpServletRequest

setContextPath

public void setContextPath(String contextPath)

getContextPath

public String getContextPath()
Specified by:
getContextPath in interface javax.servlet.http.HttpServletRequest

setPathInfo

public void setPathInfo(String pathInfo)

getPathInfo

public String getPathInfo()
Specified by:
getPathInfo in interface javax.servlet.http.HttpServletRequest

getPathTranslated

public String getPathTranslated()
Specified by:
getPathTranslated in interface javax.servlet.http.HttpServletRequest

setQueryString

public void setQueryString(String queryString)

getQueryString

public String getQueryString()
Specified by:
getQueryString in interface javax.servlet.http.HttpServletRequest

getRequestURI

public String getRequestURI()
Specified by:
getRequestURI in interface javax.servlet.http.HttpServletRequest

getRequestURL

public StringBuffer getRequestURL()
Specified by:
getRequestURL in interface javax.servlet.http.HttpServletRequest

setServletPath

public void setServletPath(String servletPath)

getServletPath

public String getServletPath()
Specified by:
getServletPath in interface javax.servlet.http.HttpServletRequest

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Adds a new cookie to the request

Parameters:
cookie - a cookie to add, will displace existing cookies with the same name

removeCookie

public void removeCookie(String name)
Removes the cookie with the given name from the request

Parameters:
name - the name of a cookie

getCookies

public javax.servlet.http.Cookie[] getCookies()
Specified by:
getCookies in interface javax.servlet.http.HttpServletRequest

clearCookies

public void clearCookies()
Remove all cookies


addHeader

public void addHeader(String name,
                      String... content)

removeHeader

public void removeHeader(String name)

getHeader

public String getHeader(String name)
Specified by:
getHeader in interface javax.servlet.http.HttpServletRequest

getHeaderNames

public Enumeration getHeaderNames()
Specified by:
getHeaderNames in interface javax.servlet.http.HttpServletRequest

getHeaders

public Enumeration getHeaders(String name)
Specified by:
getHeaders in interface javax.servlet.http.HttpServletRequest

getDateHeader

public long getDateHeader(String name)
Specified by:
getDateHeader in interface javax.servlet.http.HttpServletRequest

getIntHeader

public int getIntHeader(String name)
Specified by:
getIntHeader in interface javax.servlet.http.HttpServletRequest

clearHeaders

public void clearHeaders()
Remove all headers


getAuthType

public String getAuthType()
Specified by:
getAuthType in interface javax.servlet.http.HttpServletRequest

getLocalAddr

public String getLocalAddr()
Specified by:
getLocalAddr in interface javax.servlet.ServletRequest

getLocales

public Enumeration getLocales()
Specified by:
getLocales in interface javax.servlet.ServletRequest

getLocalName

public String getLocalName()
Specified by:
getLocalName in interface javax.servlet.ServletRequest

getLocalPort

public int getLocalPort()
Specified by:
getLocalPort in interface javax.servlet.ServletRequest

getParameters

public Map<String,String[]> getParameters()
Returns:
all parameters in this request

getStringParameters

public Map<String,String> getStringParameters()
Returns:
all parameters in this request as single strings

getParameter

public String getParameter(String name)
Specified by:
getParameter in interface javax.servlet.ServletRequest

getParameterMap

public Map getParameterMap()
Specified by:
getParameterMap in interface javax.servlet.ServletRequest

getParameterNames

public Enumeration getParameterNames()
Specified by:
getParameterNames in interface javax.servlet.ServletRequest

getParameterValues

public String[] getParameterValues(String name)
Specified by:
getParameterValues in interface javax.servlet.ServletRequest

addParameter

public void addParameter(String name,
                         String value)
Add a single parameter, this will append to an existing one


addParameter

public void addParameter(String name,
                         String[] values)
Add an array of values for a parameter, these will append to existing ones


setParameter

public void setParameter(String name,
                         String[] values)
Set a new parameter (overwrite an existing one)


setParameters

public void setParameters(Map<String,String[]> params)
Set a large number of params at once, replaces existing params

Parameters:
params - map of params

removeParameter

public void removeParameter(String name)
Removes a parameter and all values for it

Parameters:
name -

clearParameters

public void clearParameters()
Remove all parameters


getProtocol

public String getProtocol()
Specified by:
getProtocol in interface javax.servlet.ServletRequest

getRealPath

public String getRealPath(String path)
Specified by:
getRealPath in interface javax.servlet.ServletRequest

getRemoteAddr

public String getRemoteAddr()
Specified by:
getRemoteAddr in interface javax.servlet.ServletRequest

getRemoteHost

public String getRemoteHost()
Specified by:
getRemoteHost in interface javax.servlet.ServletRequest

getRemotePort

public int getRemotePort()
Specified by:
getRemotePort in interface javax.servlet.ServletRequest

getRemoteUser

public String getRemoteUser()
Specified by:
getRemoteUser in interface javax.servlet.http.HttpServletRequest

setUseRealDispatcher

public void setUseRealDispatcher(boolean real)
Parameters:
real - if true and there is a real RequestDispatcher available then use it, otherwise just emulate a forward/include call using the fake one (will always use the fake one if no real one is found)

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
Specified by:
getRequestDispatcher in interface javax.servlet.ServletRequest

getScheme

public String getScheme()
Specified by:
getScheme in interface javax.servlet.ServletRequest

getServerName

public String getServerName()
Specified by:
getServerName in interface javax.servlet.ServletRequest

getServerPort

public int getServerPort()
Specified by:
getServerPort in interface javax.servlet.ServletRequest

getRequestedSessionId

public String getRequestedSessionId()
Specified by:
getRequestedSessionId in interface javax.servlet.http.HttpServletRequest

getSession

public javax.servlet.http.HttpSession getSession()
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest

getSession

public javax.servlet.http.HttpSession getSession(boolean create)
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest

getUserPrincipal

public Principal getUserPrincipal()
Specified by:
getUserPrincipal in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Specified by:
isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Specified by:
isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Specified by:
isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Specified by:
isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest

isSecure

public boolean isSecure()
Specified by:
isSecure in interface javax.servlet.ServletRequest

isUserInRole

public boolean isUserInRole(String role)
Specified by:
isUserInRole in interface javax.servlet.http.HttpServletRequest


Copyright © 2007-2012 CARET, University of Cambridge. All Rights Reserved.