Class SiteRequest
- All Implemented Interfaces:
Request
- Direct Known Subclasses:
MockRequest
SiteRunner, in the same process and without a servlet container.
You provide the method, the headers and the body, while the path and
the query string are given to SiteRunner.handle(String, SiteRequest).
RIFE2 will then parse the parameters from the query string and from
application/x-www-form-urlencoded and multipart/form-data
bodies. The cookies are taken from the Cookie header and the
locales from the Accept-Language header.
A request can only be handled once.
- Since:
- 1.11.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newGETrequest.SiteRequest(RequestMethod method) Creates a new request with a particular method. -
Method Summary
Modifier and TypeMethodDescriptionaddDateHeader(String name, long value) Adds a request header with the given name and date-value.Adds a request header with the given name and value.addIntHeader(String name, int value) Adds a request header with the given name and integer value.voidAdds aLocaleto this request, which then takes precedence over theAccept-Languageheader.Sets this request's body as a string.bodyAsBytes(byte[] bytes) Sets this request's body as bytes.bodyAsStream(InputStream stream) Sets the body of this request as a stream.voidcancel()Cancels this request, for instance because the web view stopped loading it.characterEncoding(String encoding) Sets the character encoding of this request.booleancontainsHeader(String name) Checks whether a certain request header is present.contentType(String type) Sets the content type of this request.protected SiteResponseCreates the response of this request.dateHeader(String name, long value) Sets a request header with the given name and date-value.getAttribute(String name) getBody()Retrieves the body of this request as a string.byte[]Retrieves the body of this request as a byte array.Retrieves the character encoding of this request.Retrieves the content type of this request, which is itsContent-Typeheader.Retrieves a cookie.Cookie[]longgetDateHeader(String name) Retrieves an uploaded file.getFiles()Retrieves the files that were uploaded in this request.Retrieves all files that have been uploaded for a particular name.getHeaders(String name) Retrieves the underlying servlet request, which doesn't exist when a site is run by aSiteRunner.intgetIntHeader(String name) Retrieves the preferred locale of this request, from the locales that were added or otherwise from theAccept-Languageheader.Retrieves the parameters that were sent in this request.intgetServerRootUrl(int port) Returns the root URL of the server that is running this web applications.getSession(boolean create) booleanhasAttribute(String name) Checks if a request attribute exists.booleanChecks whether a cookie is present.booleanChecks if a particular file has been uploaded in this request.Sets a request header with the given name and value.Sets a request header with the given name and integer value.booleanIndicates whether this request has been cancelled.booleanisSecure()Indicates whether this request is secure.Adds aLocaleto this request.method(RequestMethod method) Sets the method of this request.protected voidparseParameters(String queryString) Parses the parameters of this request from the query string and from the body.Sets the protocol of this request.remoteAddr(String remoteAddr) Sets the remote address of this request.remoteHost(String remoteHost) Sets the remote host of this request.remoteUser(String remoteUser) Sets the remote user of this request.voidremoveAttribute(String name) voidremoveHeader(String name) Removes a request header with the given name.secure(boolean secure) Sets whether this request is secure.voidsetAttribute(String name, Object object) voidSets this request's body as a string, which will be converted to bytes with the character encoding of this request when the body is read.voidsetBodyAsBytes(byte[] bytes) Sets this request's body as bytes.voidsetBodyAsStream(InputStream stream) Sets the body of this request as a stream, which will be read when the body is needed.voidsetCharacterEncoding(String encoding) Sets the character encoding of this request.voidsetContentType(String type) Sets the content type of this request.voidsetDateHeader(String name, long value) Sets a request header with the given name and date-value.voidSets a request header with the given name and value.voidsetIntHeader(String name, int value) Sets a request header with the given name and integer value.voidsetMethod(RequestMethod method) Sets the method of this request.voidsetProtocol(String protocol) Sets the protocol of this request.voidsetRemoteAddr(String remoteAddr) Sets the remote address of this request.voidsetRemoteHost(String remoteHost) Sets the remote host of this request.voidsetRemoteUser(String remoteUser) Sets the remote user of this request.voidsetSecure(boolean secure) Sets whether this request is secure.protected voidsetUploadedFiles(Map<String, UploadedFile[]> files) Sets the uploaded files of this request, for extending classes that provide them themselves.
-
Constructor Details
-
SiteRequest
public SiteRequest()Creates a newGETrequest.- Since:
- 1.11.0
-
SiteRequest
Creates a new request with a particular method.- Parameters:
method- the method of the request- Since:
- 1.11.0
-
-
Method Details
-
createResponse
Creates the response of this request. Extending classes can override this to provide their own type of response.- Returns:
- a new response
- Since:
- 1.11.0
-
parseParameters
Parses the parameters of this request from the query string and from the body.The values of the query string come first. Urlencoded bodies are read for
POST,PUTandPATCH, while multipart bodies are read for any method. A request with a malformed escape, or with a form beyond the limits of the runner, is answered with400.- Parameters:
queryString- the raw query string, ornullwhen there's none- Since:
- 1.11.0
-
cancel
public void cancel()Cancels this request, for instance because the web view stopped loading it. This method can be called from any thread.No new call to the listener of the response will start, anything that the element still writes will fail like it does on a closed connection, and a detached event stream will be closed. The listener is told with
SiteResponseListener.cancelled()once no call of it is under way anymore.- Since:
- 1.11.0
- See Also:
-
isCancelled
public boolean isCancelled()Indicates whether this request has been cancelled.- Returns:
trueif the request was cancelled; orfalseotherwise- Since:
- 1.11.0
- See Also:
-
getMethod
Description copied from interface:Request -
setMethod
Sets the method of this request.The method defaults to
RequestMethod.GET.- Parameters:
method- the method that will be used by this request- Since:
- 1.11.0
- See Also:
-
method
Sets the method of this request.- Parameters:
method- the method that will be used by this request- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getParameters
Description copied from interface:RequestRetrieves the parameters that were sent in this request.- Specified by:
getParametersin interfaceRequest- Returns:
- a
Mapwith all the parameter names and values
-
setUploadedFiles
Sets the uploaded files of this request, for extending classes that provide them themselves.- Parameters:
files- the uploaded files by parameter name, ornullwhen there are none- Since:
- 1.11.0
-
setBody
Sets this request's body as a string, which will be converted to bytes with the character encoding of this request when the body is read.- Parameters:
body- the body as a string- Since:
- 1.11.0
- See Also:
-
body
Sets this request's body as a string.- Parameters:
body- the body as a string- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getBody
Description copied from interface:RequestRetrieves the body of this request as a string. -
setBodyAsBytes
public void setBodyAsBytes(byte[] bytes) Sets this request's body as bytes.- Parameters:
bytes- the body as bytes- Since:
- 1.11.0
- See Also:
-
bodyAsBytes
Sets this request's body as bytes.- Parameters:
bytes- the body as bytes- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
setBodyAsStream
Sets the body of this request as a stream, which will be read when the body is needed. A multipart body is read from the stream as it arrives, so that it isn't kept in memory.- Parameters:
stream- the stream with the body- Since:
- 1.11.0
- See Also:
-
bodyAsStream
Sets the body of this request as a stream.- Parameters:
stream- the stream with the body- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getBodyAsBytes
public byte[] getBodyAsBytes()Description copied from interface:RequestRetrieves the body of this request as a byte array.- Specified by:
getBodyAsBytesin interfaceRequest- Returns:
- the byte array of the request body
- See Also:
-
getFiles
Description copied from interface:RequestRetrieves the files that were uploaded in this request. -
hasFile
Description copied from interface:RequestChecks if a particular file has been uploaded in this request. -
getFile
Description copied from interface:RequestRetrieves an uploaded file. -
getFiles
Description copied from interface:RequestRetrieves all files that have been uploaded for a particular name. -
getServerRootUrl
Description copied from interface:RequestReturns the root URL of the server that is running this web applications.This includes the protocol, the server name and the server port, for example:
http://www.somehost.com:8080.- Specified by:
getServerRootUrlin interfaceRequest- Returns:
- the server's root url
-
hasCookie
Description copied from interface:RequestChecks whether a cookie is present. -
getCookie
Description copied from interface:RequestRetrieves a cookie. -
getCookies
Description copied from interface:Request- Specified by:
getCookiesin interfaceRequest
-
getAttribute
Description copied from interface:Request- Specified by:
getAttributein interfaceRequest
-
hasAttribute
Description copied from interface:RequestChecks if a request attribute exists.- Specified by:
hasAttributein interfaceRequest- Parameters:
name- aStringspecifying the name of the attributefalseotherwise
-
getAttributeNames
Description copied from interface:Request- Specified by:
getAttributeNamesin interfaceRequest
-
removeAttribute
Description copied from interface:Request- Specified by:
removeAttributein interfaceRequest
-
setAttribute
Description copied from interface:Request- Specified by:
setAttributein interfaceRequest
-
getCharacterEncoding
Retrieves the character encoding of this request.When none was set, this is the request encoding of the engine, which is what a deployed site uses too.
- Specified by:
getCharacterEncodingin interfaceRequest- Returns:
- the character encoding
- Since:
- 1.11.0
-
setCharacterEncoding
Sets the character encoding of this request.- Parameters:
encoding- the name of the character encoding- Since:
- 1.11.0
- See Also:
-
characterEncoding
Sets the character encoding of this request.- Parameters:
encoding- the name of the character encoding- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getContentType
Retrieves the content type of this request, which is itsContent-Typeheader.- Specified by:
getContentTypein interfaceRequest- Returns:
- the content type; or
nullif there's none - Since:
- 1.11.0
-
setContentType
Sets the content type of this request.- Parameters:
type- the content type- Since:
- 1.11.0
- See Also:
-
contentType
Sets the content type of this request.- Parameters:
type- the content type- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getDateHeader
Description copied from interface:Request- Specified by:
getDateHeaderin interfaceRequest
-
getHeader
Description copied from interface:Request -
getHeaderNames
Description copied from interface:Request- Specified by:
getHeaderNamesin interfaceRequest
-
getHeaders
Description copied from interface:Request- Specified by:
getHeadersin interfaceRequest
-
getIntHeader
Description copied from interface:Request- Specified by:
getIntHeaderin interfaceRequest
-
addHeader
Adds a request header with the given name and value. This method allows request headers to have multiple values. The header name is case-insensitive.- Parameters:
name- the name of the header to setvalue- the additional header value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
-
addDateHeader
Adds a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. This method allows request headers to have multiple values.- Parameters:
name- the name of the header to setvalue- the additional date value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
-
addIntHeader
Adds a request header with the given name and integer value. This method allows request headers to have multiple values.- Parameters:
name- the name of the header to setvalue- the additional integer value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
-
containsHeader
Checks whether a certain request header is present.- Parameters:
name- the name of the header to check- Returns:
trueif the header was present; orfalseotherwise- Since:
- 1.11.0
-
setDateHeader
Sets a request header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. If the header had already been set, the new value overwrites the previous one. ThecontainsHeader(java.lang.String)method can be used to test for the presence of a header before setting its value.- Parameters:
name- the name of the header to setvalue- the assigned date value- Since:
- 1.11.0
- See Also:
-
dateHeader
Sets a request header with the given name and date-value.- Parameters:
name- the name of the header to setvalue- the assigned date value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
setHeader
Sets a request header with the given name and value. If the header had already been set, the new value overwrites the previous one. ThecontainsHeader(java.lang.String)method can be used to test for the presence of a header before setting its value.- Parameters:
name- the name of the header to setvalue- the header value- Since:
- 1.11.0
- See Also:
-
header
Sets a request header with the given name and value.- Parameters:
name- the name of the header to setvalue- the header value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
setIntHeader
Sets a request header with the given name and integer value. If the header had already been set, the new value overwrites the previous one. ThecontainsHeader(java.lang.String)method can be used to test for the presence of a header before setting its value.- Parameters:
name- the name of the header to setvalue- the assigned integer value- Since:
- 1.11.0
- See Also:
-
intHeader
Sets a request header with the given name and integer value.- Parameters:
name- the name of the header to setvalue- the assigned integer value- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
removeHeader
Removes a request header with the given name.- Parameters:
name- the name of the header to remove- Since:
- 1.11.0
-
getLocale
Retrieves the preferred locale of this request, from the locales that were added or otherwise from theAccept-Languageheader. -
getLocales
Description copied from interface:Request- Specified by:
getLocalesin interfaceRequest
-
addLocale
Adds aLocaleto this request, which then takes precedence over theAccept-Languageheader.- Parameters:
locale- the locale to add- Since:
- 1.11.0
- See Also:
-
locale
Adds aLocaleto this request.- Parameters:
locale- the locale to add- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getProtocol
Description copied from interface:Request- Specified by:
getProtocolin interfaceRequest
-
setProtocol
Sets the protocol of this request.The default protocol is
"HTTP/1.1".- Parameters:
protocol- the protocol to set- Since:
- 1.11.0
- See Also:
-
protocol
Sets the protocol of this request.- Parameters:
protocol- the protocol to set- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getRemoteAddr
Description copied from interface:Request- Specified by:
getRemoteAddrin interfaceRequest
-
setRemoteAddr
Sets the remote address of this request.The default remote address is
"127.0.0.1".- Parameters:
remoteAddr- the remote address to set- Since:
- 1.11.0
- See Also:
-
remoteAddr
Sets the remote address of this request.- Parameters:
remoteAddr- the remote address to set- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getRemoteUser
Description copied from interface:Request- Specified by:
getRemoteUserin interfaceRequest
-
setRemoteUser
Sets the remote user of this request.The default remote user is
null.- Parameters:
remoteUser- the remote user to set- Since:
- 1.11.0
- See Also:
-
remoteUser
Sets the remote user of this request.- Parameters:
remoteUser- the remote user to set- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getRemoteHost
Description copied from interface:Request- Specified by:
getRemoteHostin interfaceRequest
-
setRemoteHost
Sets the remote host of this request.The default remote host is
"localhost".- Parameters:
remoteHost- the remote host to set- Since:
- 1.11.0
- See Also:
-
remoteHost
Sets the remote host of this request.- Parameters:
remoteHost- the remote host to set- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getRequestDispatcher
Description copied from interface:Request- Specified by:
getRequestDispatcherin interfaceRequest
-
getSession
Description copied from interface:Request- Specified by:
getSessionin interfaceRequest
-
getSession
Description copied from interface:Request- Specified by:
getSessionin interfaceRequest
-
getServerPort
public int getServerPort()Description copied from interface:Request- Specified by:
getServerPortin interfaceRequest
-
getScheme
Description copied from interface:Request -
getServerName
Description copied from interface:Request- Specified by:
getServerNamein interfaceRequest
-
getContextPath
Description copied from interface:Request- Specified by:
getContextPathin interfaceRequest
-
isSecure
public boolean isSecure()Indicates whether this request is secure. By default, this is the case when its scheme ishttpsor when the site runner was configured to be secure. -
setSecure
public void setSecure(boolean secure) Sets whether this request is secure.By default, a request is secure when its scheme is
httpsor when the site runner was configured to be secure.- Parameters:
secure-trueif this request is secure; orfalseotherwise- Since:
- 1.11.0
- See Also:
-
secure
Sets whether this request is secure.- Parameters:
secure-trueif this request is secure; orfalseotherwise- Returns:
- this
SiteRequestinstance - Since:
- 1.11.0
- See Also:
-
getHttpServletRequest
Retrieves the underlying servlet request, which doesn't exist when a site is run by aSiteRunner.- Specified by:
getHttpServletRequestin interfaceRequest- Returns:
null- Since:
- 1.11.0
-