com.mortbay.HTTP
Class HttpRequest

java.lang.Object
  |
  +--com.mortbay.HTTP.HttpHeader
        |
        +--com.mortbay.HTTP.HttpRequest

public class HttpRequest
extends HttpHeader
implements javax.servlet.http.HttpServletRequest

A received HTTP request. Implements and extends the javax.servlet.http.HttpServletRequest interface. The extensions are for HttpHandler instances that need to modify the request or have better access to the IO.

Usage

The method comments present examples based on a request to the URL:
 http://localhost:1234/Servlet/Path/Foo/Bar?aaa=123&bbb=456
 

Version:
$Id: HttpRequest.java,v 2.54 2000/11/12 20:13:35 gregwilkins Exp $
Author:
Greg Wilkins
See Also:
HttpServer

Field Summary
static byte[] Continue
           
static java.lang.String DELETE
           
static java.lang.String GET
           
static java.lang.String HEAD
           
static java.lang.String MOVE
           
static java.lang.String OPTIONS
           
static java.lang.String POST
           
static java.lang.String PUT
           
static java.lang.String SESSIONID_COOKIE
           
static java.lang.String SESSIONID_NONE
           
static java.lang.String SESSIONID_NOT_CHECKED
          For decoding session ids etc.
static java.lang.String SESSIONID_URL
           
 
Fields inherited from class com.mortbay.HTTP.HttpHeader
__COLON, __CRLF, __dateReceive, __dateSend, __singleValuedMap, Authorization, Chunked, Close, COLON, Connection, ContentLength, ContentType, Cookie, CRLF, Date, Expires, Host, HTTP_1_0, HTTP_1_1, IfModifiedSince, IfUnmodifiedSince, LastModified, Referer, SetCookie, SingleValued, TransferEncoding, UserAgent, WwwAuthenticate, WwwFormUrlEncode
 
Constructor Summary
HttpRequest(HttpServer httpServer, java.net.Socket connection, InetAddrPort address)
          Construct received request.
HttpRequest(HttpServer server, java.lang.String method, java.lang.String uri)
          Construct request to send
HttpRequest(HttpServer server, java.lang.String method, URI uri)
          Construct request to send
 
Method Summary
 void cookiesAsParameters()
          Decode Cookies If includeAsParameters is true, cookies may be fetch via the getParameter() method
 void decodeFormParameters()
          decode Form Parameters After this call, form parameters may be fetch via the getParameter() method.
 void destroy()
          Destroy the request.
 java.lang.Object getAttribute(java.lang.String name)
          Returns the value of the named attribute as an Object.
 java.util.Enumeration getAttributeNames()
          Returns an Enumeration containing the names of the attributes available to this request.
 java.lang.String getAuthType()
          Get the authentication method (normally BASIC)
 java.lang.String getCharacterEncoding()
          Returns the character set encoding for the input of this request.
 int getContentLength()
          Get the length of any content in this request or -1 if not known.
 java.lang.String getContentType()
          Return the MIME encoding type of any (form) content in this request or null if not known.
 javax.servlet.http.Cookie[] getCookies()
          Returns an array containing all of the Cookie objects the browser sent with this request.
 HttpInputStream getHttpInputStream()
          Get the HttpInputStream of the request.
 HttpResponse getHttpResponse()
          Get associated response
 HttpServer getHttpServer()
          Get the HttpServer of the request.
 javax.servlet.ServletInputStream getInputStream()
          Get request input stream
 java.lang.String getMethod()
          Get the HTTP method for this request.
 java.lang.String getParameter(java.lang.String name)
          Deprecated. use getParameterValues
 java.util.Enumeration getParameterNames()
          Returns an enumeration of strings representing the parameter names for this request.
 java.lang.String[] getParameterValues(java.lang.String name)
          Returns the multi-values of the specified parameter for the request.
 java.lang.String getPathInfo()
          Get the URI segment that is more than the servlet path
 java.lang.String getPathTranslated()
          Return the actual URI after translation.
 java.lang.String getProtocol()
          Get the actual protocol and version.
 java.lang.String getQueryString()
          Get the query string of the request
 java.io.BufferedReader getReader()
          Returns the body of the request as a BufferedReader that translates character set encodings.
 java.lang.String getRealPath(java.lang.String path)
          Deprecated.  
 java.lang.String getRemoteAddr()
          Get the remote IP address of the system that sent the request.
 java.lang.String getRemoteHost()
          Get the hostname of the system that sent the request.
 java.lang.String getRemoteUser()
          Get the remote user name decoded from authentication headers
 java.lang.String getRequestedSessionId()
          Returns the session ID specified by the client.
 java.lang.String getRequestLine()
          Return the HTTP request line as it was received
 java.lang.String getRequestPath()
          Get the URI path minus any query strings with translations applied.
 java.lang.String getRequestURI()
          Get the full URI.
 java.lang.String getResourcePath()
          Get the resource path.
 java.lang.String getScheme()
          Returns the scheme of the URL used in this request, for example "http", "https"
 java.lang.String getServerName()
          Get the host name of the server that received the request.
 int getServerPort()
          Get the port number used in the request
 java.lang.String getServletPath()
          Get the URI sub path that matched to the servlet.
 javax.servlet.http.HttpSession getSession()
          Returns the current session associated with this request, or if the request does not have a session, creates one.
 javax.servlet.http.HttpSession getSession(boolean create)
          Returns the current HttpSession associated with this request or, if necessary, creates a new session for the request.
 java.lang.String getVersion()
          Get the protocol version
 java.io.InputStream handleRequestLocally()
          Handle a request locally.
 boolean isLocalRequest()
          Get local request status
 boolean isRequestedSessionIdFromCookie()
          Checks whether the session ID this request submitted came in as a cookie, rather than from the getSession() method.
 boolean isRequestedSessionIdFromUrl()
          Deprecated.  
 boolean isRequestedSessionIdFromURL()
          Checks whether the session ID this request submitted came in as part of the request URL, rather than from the getSession() method.
 boolean isRequestedSessionIdValid()
          Checks whether this request has a valid session in the current session context (which is a HttpSessionContext).
 void putParameter(java.lang.String name, java.lang.String value)
          Put a parameter into the request.
 void putParameterValues(java.lang.String name, java.lang.String[] values)
          Put a multi-valued parameter into the request.
 void setAttribute(java.lang.String name, java.lang.Object o)
          Stores an attribute in the context of this request.
 void setHttpResponse(HttpResponse response)
          Set associated response
 void setPathTranslated(java.lang.String pathTranslated)
          Set the translated path
 void setRemoteUser(java.lang.String authType, java.lang.String name)
          Set the remoteUser from authentication headers
 void setRequestPath(java.lang.String path)
          Set the URI path and redirect params
 void setResourcePath(java.lang.String path)
          Set the resource path.
 void setResourcePath(URI newuri)
          Set the resource path.
 void setScheme(java.lang.String scheme)
          Sets the scheme of the URL used in this request, for example "http", "https"
 void setServletPath(java.lang.String servletPath)
          Set the servlet path getServletPath and getPathInfo are not valid until this has been called.
static void setSessionMaxInactiveInterval(int defaultTime)
          Set the default session timeout.
 void setVersion(java.lang.String version)
          Set the request version
 void translateAddress(java.lang.String pathSpec, java.lang.String newPath, boolean translateURI)
          Translate the URI Apply any translation rules to the URI.
 void write(java.io.OutputStream outstream)
          Write the request header to an output stream
 
Methods inherited from class com.mortbay.HTTP.HttpHeader
dump, getDateHeader, getHeader, getHeaderNames, getIntHeader, read, setDateHeader, setHeader, setIntHeader, toString, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

GET

public static final java.lang.String GET

POST

public static final java.lang.String POST

PUT

public static final java.lang.String PUT

HEAD

public static final java.lang.String HEAD

DELETE

public static final java.lang.String DELETE

MOVE

public static final java.lang.String MOVE

OPTIONS

public static final java.lang.String OPTIONS

Continue

public static final byte[] Continue

SESSIONID_NOT_CHECKED

public static final java.lang.String SESSIONID_NOT_CHECKED
For decoding session ids etc.

SESSIONID_URL

public static final java.lang.String SESSIONID_URL

SESSIONID_COOKIE

public static final java.lang.String SESSIONID_COOKIE

SESSIONID_NONE

public static final java.lang.String SESSIONID_NONE
Constructor Detail

HttpRequest

public HttpRequest(HttpServer httpServer,
                   java.net.Socket connection,
                   InetAddrPort address)
            throws java.io.IOException
Construct received request.
Parameters:
httpServer - The server for this request.
connection - The socket the request was received over.
address - the IP address that was listened on for the request.
Throws:
java.io.IOException - Problem reading the request header

HttpRequest

public HttpRequest(HttpServer server,
                   java.lang.String method,
                   java.lang.String uri)
Construct request to send
Parameters:
httpServer - The server for this request.
method - The method for this request
uri - The uri for this request.

HttpRequest

public HttpRequest(HttpServer server,
                   java.lang.String method,
                   URI uri)
Construct request to send
Parameters:
httpServer - The server for this request.
method - The method for this request
uri - The uri for this request.
Method Detail

isLocalRequest

public boolean isLocalRequest()
Get local request status
Returns:
true if this is a synthetic local request

setHttpResponse

public void setHttpResponse(HttpResponse response)
Set associated response

getHttpResponse

public HttpResponse getHttpResponse()
Get associated response
Returns:
response

getRequestPath

public java.lang.String getRequestPath()
Get the URI path minus any query strings with translations applied.
Returns:
For the given example, this would return
 /Servlet/Path/Foo/Bar
 

setRequestPath

public void setRequestPath(java.lang.String path)
Set the URI path and redirect params

getResourcePath

public java.lang.String getResourcePath()
Get the resource path. If set, the resource path is the path used by Jetty Handlers to locate the resource that will handle the request. If not set, the RequestPath is returned. A resource path is required to implement the RequestDispatcher.include method, which leaves the requestPath unmodified when calling a new resource for content. HttpHandlers should use the resource path to determine which entity should handle the request. The RequestPath should be used for any actually handling. Note that this just shows how much the RequestDispatcher API sux and is not really generic.
Returns:
the resource path or the request path.

setResourcePath

public void setResourcePath(java.lang.String path)
Set the resource path. The resource path is the path used by Jetty Handlers to locate the resource that will handle the request. A resource path is required to implement the RequestDispatcher.include method, which leaves the requestPath unmodified when calling a new resource for content.
Parameters:
path -  

setResourcePath

public void setResourcePath(URI newuri)
Set the resource path. The resource path is the path used by Jetty Handlers to locate the resource that will handle the request. A resource path is required to implement the RequestDispatcher.include method, which leaves the requestPath unmodified when calling a new resource for content.
Parameters:
path -  

getRequestLine

public java.lang.String getRequestLine()
Return the HTTP request line as it was received

getVersion

public java.lang.String getVersion()
Get the protocol version
Returns:
return the version.

setPathTranslated

public void setPathTranslated(java.lang.String pathTranslated)
Set the translated path
Parameters:
pathTranslated - Translated Path.

setServletPath

public void setServletPath(java.lang.String servletPath)
                    throws java.net.MalformedURLException
Set the servlet path getServletPath and getPathInfo are not valid until this has been called.

setVersion

public void setVersion(java.lang.String version)
Set the request version
Parameters:
version -  

translateAddress

public void translateAddress(java.lang.String pathSpec,
                             java.lang.String newPath,
                             boolean translateURI)
Translate the URI Apply any translation rules to the URI. If setServletPath has been called, it's results are nulled
Parameters:
pathSpec - The path segment to be translated. This may end with the special characters defined in PathMap.
newPath - The path segment to replace path with.
translateURI - If true, the URI of the request is translated, otherwise only the resource path is affected.

setRemoteUser

public void setRemoteUser(java.lang.String authType,
                          java.lang.String name)
Set the remoteUser from authentication headers

decodeFormParameters

public void decodeFormParameters()
                          throws java.io.IOException
decode Form Parameters After this call, form parameters may be fetch via the getParameter() method.

cookiesAsParameters

public void cookiesAsParameters()
                         throws java.io.IOException
Decode Cookies If includeAsParameters is true, cookies may be fetch via the getParameter() method

write

public void write(java.io.OutputStream outstream)
           throws java.io.IOException
Write the request header to an output stream
Overrides:
write in class HttpHeader

putParameter

public void putParameter(java.lang.String name,
                         java.lang.String value)
Put a parameter into the request. Placed in uri query parameters for forwarded requests
Parameters:
deprecated - Use putParameterValues

putParameterValues

public void putParameterValues(java.lang.String name,
                               java.lang.String[] values)
Put a multi-valued parameter into the request. Placed in uri query parameters for forwarded requests

getHttpServer

public HttpServer getHttpServer()
Get the HttpServer of the request.

getHttpInputStream

public HttpInputStream getHttpInputStream()
Get the HttpInputStream of the request. It is dangerous to use this unless you know what you are doing.

handleRequestLocally

public java.io.InputStream handleRequestLocally()
                                         throws java.io.IOException
Handle a request locally. This methods dispatches this request to the local server. It is used by the ServletContext.getResourceAsStream method.
Returns:
InputStream containing content.
Throws:
java.io.IOException -  

getContentLength

public int getContentLength()
Get the length of any content in this request or -1 if not known.
Returns:
For the given example, this would return
 -1
 

getContentType

public java.lang.String getContentType()
Return the MIME encoding type of any (form) content in this request or null if not known.
Returns:
For the given example, this would return
 null
 

getProtocol

public java.lang.String getProtocol()
Get the actual protocol and version. Get protocol and version used in the request as a string of the form <protocol>/<major version>.<minor version>.
Returns:
For the given example, this would return
 HTTP/1.0
 

getServerName

public java.lang.String getServerName()
Get the host name of the server that received the request.
Returns:
For the given example, this would return
 localhost
 

getServerPort

public int getServerPort()
Get the port number used in the request
Returns:
For the given example, this would return
 1234
 

getRemoteAddr

public java.lang.String getRemoteAddr()
Get the remote IP address of the system that sent the request.
Returns:
For the given example, this would return
 127.0.0.1
 

getRemoteHost

public java.lang.String getRemoteHost()
Get the hostname of the system that sent the request.
Returns:
For the given example, this would return
 localhost
 

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Deprecated.  

Applies alias rules to the specified virtual path and returns the corresponding real path. getRealPath("/") is the document root. It returns null if the translation cannot be performed.
Parameters:
path - the path to be translated

getInputStream

public javax.servlet.ServletInputStream getInputStream()
Get request input stream
Returns:
an input stream for reading the request body.

getParameter

public java.lang.String getParameter(java.lang.String name)
Deprecated. use getParameterValues

Returns the value of the specified parameter for the request. For example, in an HTTP servlet this would return the value of the specified query string parameter.
Parameters:
name - the parameter name
Returns:
For the given example, getParameter("aaa") would return "123".

getParameterValues

public java.lang.String[] getParameterValues(java.lang.String name)
Returns the multi-values of the specified parameter for the request.
Tags copied from interface: javax.servlet.ServletRequest
Parameters:
name - a String containing the name of the parameter whose value is requested
Returns:
an array of String objects containing the parameter's values
See Also:
ServletRequest.getParameter(java.lang.String)

getParameterNames

public java.util.Enumeration getParameterNames()
Returns an enumeration of strings representing the parameter names for this request.
Tags copied from interface: javax.servlet.ServletRequest
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters

getScheme

public java.lang.String getScheme()
Returns the scheme of the URL used in this request, for example "http", "https"
Tags copied from interface: javax.servlet.ServletRequest
Returns:
a String containing the name of the scheme used to make this request

setScheme

public void setScheme(java.lang.String scheme)
Sets the scheme of the URL used in this request, for example "http", "https"

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Description copied from interface: javax.servlet.ServletRequest
Returns the value of the named attribute as an Object. This method allows the servlet engine to give the servlet custom information about a request. This method returns null if no attribute of the given name exists.

Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.

Parameters:
name - the attribute name
Returns:
the value of the attribute, or null if not defined

getAttributeNames

public java.util.Enumeration getAttributeNames()
Description copied from interface: javax.servlet.ServletRequest
Returns an Enumeration containing the names of the attributes available to this request. This method returns an empty Enumeration if the request has no attributes available to it.
Tags copied from interface: javax.servlet.ServletRequest
Returns:
an Enumeration of strings containing the names of the request's attributes

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object o)
Description copied from interface: javax.servlet.ServletRequest
Stores an attribute in the context of this request. Attributes are reset between requests.

Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and com.sun.*, are reserved for use by Sun Microsystems.

Tags copied from interface: javax.servlet.ServletRequest
Parameters:
key - a String specifying the name of the attribute
o - an Object containing the context of the request
Throws:
java.lang.IllegalStateException - if the specified attribute already has a value

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the character set encoding for the input of this request. Checks the Content-Type header for a charset parameter and return its value if found or ISO8859_1 otherwise.
Returns:
Character Encoding.

getMethod

public java.lang.String getMethod()
Get the HTTP method for this request. Returns the method with which the request was made. The returned value can be "GET", "HEAD", "POST", or an extension method. Same as the CGI variable REQUEST_METHOD.
Specified by:
getMethod in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 GET
 

getRequestURI

public java.lang.String getRequestURI()
Get the full URI.
Specified by:
getRequestURI in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 /Servlet/Path/Foo/Bar
 
If the browser uses the abs_path URI, if it uses absoulteURI it will return
 http://localhost:1234/Servlet/Path/Foo/Bar
 

getServletPath

public java.lang.String getServletPath()
Get the URI sub path that matched to the servlet.
Specified by:
getServletPath in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 /Servlet/Path
 

getPathInfo

public java.lang.String getPathInfo()
Get the URI segment that is more than the servlet path
Specified by:
getPathInfo in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 /Foo/Bar
 

getPathTranslated

public java.lang.String getPathTranslated()
Return the actual URI after translation. Not implemented
Specified by:
getPathTranslated in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 null
 

getQueryString

public java.lang.String getQueryString()
Get the query string of the request
Specified by:
getQueryString in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 aaa=123&bbb=456
 

getRemoteUser

public java.lang.String getRemoteUser()
Get the remote user name decoded from authentication headers
Specified by:
getRemoteUser in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 null
 

getAuthType

public java.lang.String getAuthType()
Get the authentication method (normally BASIC)
Specified by:
getAuthType in interface javax.servlet.http.HttpServletRequest
Returns:
For the given example, this would return
 null
 

getCookies

public javax.servlet.http.Cookie[] getCookies()
Description copied from interface: javax.servlet.http.HttpServletRequest
Returns an array containing all of the Cookie objects the browser sent with this request. This method returns null if the browser did not send any cookies.
Specified by:
getCookies in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
an array of all the Cookies included with this request, or null if the request has no cookies

getRequestedSessionId

public java.lang.String getRequestedSessionId()
Description copied from interface: javax.servlet.http.HttpServletRequest
Returns the session ID specified by the client. This may not be the same as the ID of the actual session in use. For example, if the request specified an old (expired) session ID and the server has started a new session, this method gets a new session with a new ID. If the request did not specify a session ID, this method returns null.
Specified by:
getRequestedSessionId in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
a String specifying the session ID, or null if the request did not specify a session ID
See Also:
HttpServletRequest.isRequestedSessionIdValid()

getSession

public javax.servlet.http.HttpSession getSession()
Description copied from interface: javax.servlet.http.HttpServletRequest
Returns the current session associated with this request, or if the request does not have a session, creates one.
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
the HttpSession associated with this request

getSession

public javax.servlet.http.HttpSession getSession(boolean create)
Description copied from interface: javax.servlet.http.HttpServletRequest
Returns the current HttpSession associated with this request or, if necessary, creates a new session for the request. Use true for create to create a new session, or false to return the current HttpSession.

If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method at least once before you write any output to the response. Newly created sessions (that is, sessions for which HttpSession.isNew returns true) do not have any application-specific state.

Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Parameters:
true - to create a new session for this request; false to return the current session
Returns:
the HttpSession associated with this request or null if create is false and the request has no valid session

isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Description copied from interface: javax.servlet.http.HttpServletRequest
Checks whether the session ID this request submitted came in as a cookie, rather than from the HttpServletRequest.getSession(boolean) method.
Specified by:
isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
true if the session ID came in as a cookie; otherwise, false
See Also:
HttpServletRequest.getSession(boolean)

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Deprecated.  

Specified by:
isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Description copied from interface: javax.servlet.http.HttpServletRequest
Checks whether the session ID this request submitted came in as part of the request URL, rather than from the HttpServletRequest.getSession(boolean) method.
Specified by:
isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
true if the session ID came in as part of an URL; otherwise, false
See Also:
HttpServletRequest.getSession(boolean)

isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Description copied from interface: javax.servlet.http.HttpServletRequest
Checks whether this request has a valid session in the current session context (which is a HttpSessionContext). If the session is not valid, the HttpServletRequest.getSession(boolean) method never returns it.
Specified by:
isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest
Tags copied from interface: javax.servlet.http.HttpServletRequest
Returns:
true if this request has a valid session in the current session context; otherwise, false
See Also:
HttpServletRequest.getRequestedSessionId(), HttpServletRequest.getSession(boolean), HttpSessionContext

getReader

public java.io.BufferedReader getReader()
Description copied from interface: javax.servlet.ServletRequest
Returns the body of the request as a BufferedReader that translates character set encodings.
Tags copied from interface: javax.servlet.ServletRequest
Returns:
a BufferedReader containing the body of the request
Throws:
java.io.UnsupportedEncodingException - if the character set encoding used is not supported and the text cannot be decoded
java.lang.IllegalStateException - if ServletRequest.getInputStream() method has been called on this request
java.io.IOException - if an input or output exception occurred
See Also:
ServletRequest.getInputStream()

destroy

public void destroy()
Destroy the request. Help the garbage collector by null everything that we can.
Overrides:
destroy in class HttpHeader

setSessionMaxInactiveInterval

public static void setSessionMaxInactiveInterval(int defaultTime)
Set the default session timeout.
Parameters:
default - The default timeout in seconds