public class HttpSupport extends Object implements RequestAccess
Modifier and Type | Class and Description |
---|---|
class |
HttpSupport.HttpBuilder |
class |
HttpSupport.RenderBuilder |
LOGGER
Constructor and Description |
---|
HttpSupport() |
Modifier and Type | Method and Description |
---|---|
protected void |
assign(String name,
Object value)
Assigns a value for a view.
|
protected boolean |
blank(String... names)
Returns true if any named request parameter is blank.
|
protected void |
encoding(String encoding)
Synonym for
setEncoding(String) |
protected void |
flash(Map values)
Flash method to display multiple flash messages.
|
protected void |
flash(Object... values)
Flash method to display multiple flash messages.
|
protected void |
flash(String name)
Sets a flash name for a flash with a body.
|
protected void |
flash(String name,
Object value)
Sends value to flash.
|
protected String |
getEncoding()
Controllers can override this method to return encoding they require.
|
protected FileItem |
getFile(String fieldName,
List<FormItem> formItems)
Convenience method to get file content from
multipart/form-data request. |
protected javax.servlet.http.HttpServletRequest |
getHttpServletRequest()
Direct access to current
HttpServletRequest for low level operations. |
protected javax.servlet.http.HttpServletResponse |
getHttpServletResponse()
Direct access to current
HttpServletResponse for low level operations. |
Map<String,String> |
getMap(String hashName)
Returns a map parsed from a request if parameter names have a "hash" syntax:
|
Map<String,String> |
getMap(String hashName,
List<FormItem> formItems)
Convenience method to get parameter map in case
multipart/form-data request was used. |
protected String |
getRealPath(String path)
Returns a String containing the real path for a given virtual path.
|
protected byte[] |
getRequestBytes()
Reads entire request data as byte array.
|
protected InputStream |
getRequestInputStream()
Returns InputStream of the request.
|
protected InputStream |
getRequestStream()
Alias to
getRequestInputStream() . |
protected String |
getRequestString()
Reads entire request data as String.
|
Map<String,String> |
getResponseHeaders()
Returns response headers
|
protected void |
header(String name,
Object value)
Adds a header to response.
|
protected void |
header(String name,
String value)
Adds a header to response.
|
protected List |
jsonList()
Converts posted JSON array to a Java List.
|
protected Map |
jsonMap()
Converts posted JSON map to a Java Map.
|
protected Map[] |
jsonMaps()
Converts posted JSON maps to a Java Maps array.
|
protected void |
locale(Locale locale)
Same as
setLocale(java.util.Locale) |
protected void |
logDebug(String info) |
protected void |
logError(String info) |
protected void |
logError(String info,
Throwable e) |
protected void |
logError(Throwable e) |
protected void |
logInfo(String info) |
protected void |
logWarning(String info) |
protected void |
logWarning(String info,
Throwable e) |
protected String |
merge(String template,
Map values)
Will merge a template and return resulting string.
|
protected MultipartForm |
multipartForm() |
protected MultipartForm |
multipartForm(String encoding,
long maxUploadSize) |
protected List<FormItem> |
multipartFormItems()
Convenience method, calls
multipartFormItems(String) . |
protected List<FormItem> |
multipartFormItems(String encoding)
Returns a collection of uploaded files and form fields from a multi-part request.
|
protected List<FormItem> |
multipartFormItems(String encoding,
long maxUploadSize)
Returns a collection of uploaded files and form fields from a multi-part request.
|
protected OutputStream |
outputStream()
Use to send raw data to HTTP client.
|
protected OutputStream |
outputStream(String contentType)
Use to send raw data to HTTP client.
|
protected OutputStream |
outputStream(String contentType,
Map headers,
int status)
Use to send raw data to HTTP client.
|
protected HttpSupport.HttpBuilder |
redirect()
Redirects to the same controller, and action "index".
|
protected <T extends AppController> |
redirect(Class<T> controllerClass)
Redirects to given controller, action "index" without any parameters.
|
protected <T extends AppController> |
redirect(Class<T> controllerClass,
Map params)
Redirects to a controller, generates appropriate redirect path.
|
protected <T extends AppController> |
redirect(Class<T> controllerClass,
Object id)
Convenience method for
redirect(Class, java.util.Map) . |
protected <T extends AppController> |
redirect(Class<T> controllerClass,
String action)
Convenience method for
redirect(Class, java.util.Map) . |
protected <T extends AppController> |
redirect(Class<T> controllerClass,
String action,
Object id)
Convenience method for
redirect(Class, java.util.Map) . |
protected HttpSupport.HttpBuilder |
redirect(String path)
Redirects to a an action of this controller, or an action of a different controller.
|
protected HttpSupport.HttpBuilder |
redirect(URL url)
Redirects to another URL (usually another site).
|
protected HttpSupport.HttpBuilder |
redirectToReferrer()
Redirects to referrer if one exists.
|
protected HttpSupport.HttpBuilder |
redirectToReferrer(String defaultReference)
Redirects to referrer if one exists.
|
protected HttpSupport.RenderBuilder |
render(String template,
Map values)
Renders results with a template.
|
protected HttpSupport.HttpBuilder |
respond(String text)
This method will send the text to a client verbatim.
|
protected String |
sanitize(String unsafeContent)
Cleans HTML from harmful tags, making XSS impossible.
|
void |
sendCookie(Cookie cookie)
Sends cookie to browse with response.
|
void |
sendCookie(String name,
String value)
Sends cookie to browse with response.
|
protected HttpSupport.HttpBuilder |
sendFile(File file)
Convenience method for downloading files.
|
protected HttpSupport.HttpBuilder |
sendFile(File file,
boolean delete)
Convenience method for downloading files.
|
void |
sendPermanentCookie(String name,
String value)
Sends long to live cookie to browse with response.
|
protected SessionFacade |
session()
Returns reference to a current session.
|
protected void |
session(String name,
Serializable value)
Convenience method, sets an object on a session.
|
protected Boolean |
sessionBoolean(String name)
Convenience method, returns object from session, equivalent of:
|
protected Double |
sessionDouble(String name)
Convenience method, returns object from session, equivalent of:
|
protected Float |
sessionFloat(String name)
Convenience method, returns object from session, equivalent of:
|
protected boolean |
sessionHas(String name)
Returns true if session has named object, false if not.
|
protected Integer |
sessionInteger(String name)
Convenience method, returns object from session, equivalent of:
|
protected Long |
sessionLong(String name)
Convenience method, returns object from session, equivalent of:
|
protected Object |
sessionObject(String name)
Convenience method, returns object from session, equivalent of:
|
protected String |
sessionString(String name)
Convenience method, returns object from session, equivalent of:
|
protected void |
setContentLength(int length)
Sets content length of response.
|
protected void |
setEncoding(String encoding)
Sets character encoding on the response.
|
protected void |
setLocale(Locale locale)
Sets locale on response.
|
protected void |
setRequestEncoding(String encoding)
Sets character encoding for request.
|
protected void |
setResponseEncoding(String encoding)
Sets character encoding for response.
|
protected int |
status()
Returns status code from current response.
|
protected HttpSupport.HttpBuilder |
streamOut(InputStream in)
Streams content of the
reader to the HTTP client. |
protected Iterator<FormItem> |
uploadedFiles()
Returns a collection of uploaded files from a multi-part port request.
|
protected Iterator<FormItem> |
uploadedFiles(String encoding)
Returns a collection of uploaded files from a multi-part port request.
|
protected Iterator<FormItem> |
uploadedFiles(String encoding,
long maxFileSize)
Returns a collection of uploaded files from a multi-part port request.
|
protected Map |
values()
Returns a mutable Map with all the values from the current request context.
|
protected void |
view(Map values)
Convenience method, calls
assign(String, Object) internally. |
protected void |
view(Object... values)
Convenience method to pass multiple names and corresponding values to a view.
|
protected void |
view(String name,
Object value)
Alias to
assign(String, Object) . |
protected PrintWriter |
writer()
Produces a writer for sending raw data to HTTP clients.
|
protected PrintWriter |
writer(String contentType,
Map headers,
int status)
Produces a writer for sending raw data to HTTP clients.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
appContext, context, cookie, cookies, cookieValue, exists, format, getId, getLocale, getRequestHost, getRequestPort, getRequestProperties, getRequestProtocol, getRoute, header, headers, host, ipAddress, ipForwardedFor, isDelete, isGet, isHead, isMethod, isMultipartContent, isPost, isPut, isXhr, locale, method, param, param, params, params, params, params1st, params1st, path, port, protocol, queryString, remoteAddress, remoteHost, requestHas, servletPath, session, session, uri, url, userAgent, xhr
protected void logInfo(String info)
protected void logDebug(String info)
protected void logWarning(String info)
protected void logError(String info)
protected void logError(Throwable e)
protected void assign(String name, Object value)
name
- name of valuevalue
- value.protected void view(String name, Object value)
assign(String, Object)
.name
- name of object to be passed to viewvalue
- object to be passed to viewprotected int status()
protected void view(Map values)
assign(String, Object)
internally.
The keys in teh map are converted to String values.values
- map with values to pass to view.protected void view(Object... values)
values
- - pairs of names and values. such as: name1, value1, name2, value2, etc. Number of arguments must be even.protected void flash(Map values)
values
- values to flash.flash(String, Object)
protected void flash(Object... values)
values
- values to flash.flash(String, Object)
protected void flash(String name)
<@flash name="warning"> <div class="warning">${message}</div> </@flash>If body refers to variables (as in this example), then such variables need to be passed in to the template as usual using the
view(String, Object)
method.name
- name of a flashprotected void flash(String name, Object value)
name
- name of value to flashvalue
- value to live for one more request in current session.protected HttpSupport.RenderBuilder render(String template, Map values)
template
- - template name, must be "absolute", starting with slash,
such as: "/controller_dir/action_template".values
- map with values to pass to view.HttpSupport.RenderBuilder
, which is used to provide additional parameters.protected HttpSupport.HttpBuilder redirect(String path)
path
- - expected to be a path within the application.HttpSupport.HttpBuilder
to accept additional information.protected HttpSupport.HttpBuilder redirect(URL url)
url
- absolute URL: http://domain/path...
.HttpSupport.HttpBuilder
, to accept additional information.protected HttpSupport.HttpBuilder redirectToReferrer(String defaultReference)
defaultReference
.defaultReference
- where to redirect - can be absolute or relative; this will be used in case
the request does not provide a "Referrer" header.HttpSupport.HttpBuilder
, to accept additional information.protected HttpSupport.HttpBuilder redirectToReferrer()
HttpSupport.HttpBuilder
, to accept additional information.protected <T extends AppController> HttpSupport.HttpBuilder redirect(Class<T> controllerClass, String action, Object id)
redirect(Class, java.util.Map)
.controllerClass
- controller class where to send redirect.action
- action to redirect to.id
- id to redirect to.HttpSupport.HttpBuilder
, to accept additional information.protected <T extends AppController> HttpSupport.HttpBuilder redirect(Class<T> controllerClass, Object id)
redirect(Class, java.util.Map)
.controllerClass
- controller class where to send redirect.id
- id to redirect to.HttpSupport.HttpBuilder
, to accept additional information.protected <T extends AppController> HttpSupport.HttpBuilder redirect(Class<T> controllerClass, String action)
redirect(Class, java.util.Map)
.controllerClass
- controller class where to send redirect.action
- action to redirect to.HttpSupport.HttpBuilder
, to accept additional information.protected HttpSupport.HttpBuilder redirect()
redirect(BooksController.class);given that the current controller is
BooksController
.HttpSupport.HttpBuilder
, to accept additional information.protected <T extends AppController> HttpSupport.HttpBuilder redirect(Class<T> controllerClass)
controllerClass
- controller class where to send redirect.HttpSupport.HttpBuilder
, to accept additional information.protected <T extends AppController> HttpSupport.HttpBuilder redirect(Class<T> controllerClass, Map params)
/controller/action/id
for regular controllers, and:
/controller/id/action
for RESTful controllers. For RESTful controllers, the action names are limited to those described in
RESTful
and allowed on a GET URLs, which are: "edit_form" and "new_form".
The map may contain any number of other key/value pairs, which will be converted to a query string for
the redirect URI. Example:
Method:
redirect(app.controllers.PersonController.class, org.javalite.common.Collections.map("action", "show", "id", 123, "format", "json", "restrict", "true"));
will generate the following URI:
/person/show/123?format=json&restrict=true
This method will also perform URL - encoding of special characters if necessary.controllerClass
- controller classparams
- map with request parameters.HttpSupport.HttpBuilder
, to accept additional information.protected HttpSupport.HttpBuilder respond(String text)
text
- text of response.HttpSupport.HttpBuilder
, to accept additional information.protected HttpSupport.HttpBuilder sendFile(File file, boolean delete)
file
- file to download.delete
- true to delete the file after processingprotected HttpSupport.HttpBuilder sendFile(File file)
file
- file to download.protected FileItem getFile(String fieldName, List<FormItem> formItems)
multipart/form-data
request. If more than one files with the same
name are submitted, only one is returned.fieldName
- name of form field from the multipart/form-data
request corresponding to the uploaded file.formItems
- form items retrieved from multipart/form-data
request.InputStream
from which to read content of uploaded file or null if FileItem with this name is not found.protected javax.servlet.http.HttpServletRequest getHttpServletRequest()
HttpServletRequest
for low level operations.HttpServletRequest
.protected javax.servlet.http.HttpServletResponse getHttpServletResponse()
HttpServletResponse
for low level operations.HttpServletResponse
.protected Iterator<FormItem> uploadedFiles()
protected Iterator<FormItem> uploadedFiles(String encoding)
encoding
- specifies the character encoding to be used when reading the headers of individual part.
When not specified, or null, the request encoding is used. If that is also not specified, or null,
the platform default encoding is used.protected Iterator<FormItem> uploadedFiles(String encoding, long maxFileSize)
encoding
- specifies the character encoding to be used when reading the headers of individual part.
When not specified, or null, the request encoding is used. If that is also not specified, or null,
the platform default encoding is used.maxFileSize
- maximum file size in the upload in bytes. -1 indicates no limit.protected List<FormItem> multipartFormItems()
multipartFormItems(String)
. Does not set encoding before reading request.multipartFormItems(String)
protected List<FormItem> multipartFormItems(String encoding)
<listener> <listener-class> org.apache.commons.fileupload.servlet.FileCleanerCleanup </listener-class> </listener>For more information, see: Using FileUpload The size of upload defaults to max of 20mb. Files greater than that will be rejected. If you want to accept larger files, create a file called
activeweb.properties
,
add it to your classpath and place this property to the file:
#max upload size maxUploadSize = 20000000Alternatively, just call this method and pass a per-request parameter for the size:
multipartFormItems(String, long)
.encoding
- specifies the character encoding to be used when reading the headers of individual part.
When not specified, or null, the request encoding is used. If that is also not specified, or null,
the platform default encoding is used.protected MultipartForm multipartForm()
MultipartForm
object for convenience.protected MultipartForm multipartForm(String encoding, long maxUploadSize)
encoding
- encoding to use to read values from requestmaxUploadSize
- set max upload sizeMultipartForm
object for convenience.protected List<FormItem> multipartFormItems(String encoding, long maxUploadSize)
<listener> <listener-class> org.apache.commons.fileupload.servlet.FileCleanerCleanup </listener-class> </listener>For more information, see: Using FileUpload
encoding
- specifies the character encoding to be used when reading the headers of individual part.
When not specified, or null, the request encoding is used. If that is also not specified, or null,
the platform default encoding is used.maxUploadSize
- maximum size of the upload in bytes. A value of -1 indicates no maximum.public Map<String,String> getMap(String hashName)
<input type="text" name="account[name]" /> <input type="text" name="account[number]" />will result in a map where keys are names of hash elements, and values are values of these elements from request. For the example above, the map will have these values:
{ "name":"John", "number": "123" }
hashName
- - name of a hash. In the example above, it will be "account".public Map<String,String> getMap(String hashName, List<FormItem> formItems)
multipart/form-data
request was used.
This method will skip files, and will only return form fields that are not files.
Returns a map parsed from a request if parameter names have a "hash" syntax:
<input type="text" name="account[name]" /> <input type="text" name="account[number]" />will result in a map where keys are names of hash elements, and values are values of these elements from request. For the example above, the map will have these values:
{ "name":"John", "number": "123" }
hashName
- - name of a hash. In the example above, it will be "account".formItems
- form items retrieved from multipart/form-data
request.protected void setRequestEncoding(String encoding)
encoding
- encoding to be set.protected void setResponseEncoding(String encoding)
encoding
- encoding to be set.protected void setEncoding(String encoding)
encoding
- character encoding for response.protected void encoding(String encoding)
setEncoding(String)
encoding
- encoding of response to clientprotected String getEncoding()
setEncoding(String)
trumps this setting.protected void setContentLength(int length)
length
- content length of response.protected void setLocale(Locale locale)
locale
- locale for response.protected void locale(Locale locale)
setLocale(java.util.Locale)
locale
- locale for responseprotected SessionFacade session()
protected void session(String name, Serializable value)
session().put(name, value)
name
- name of objectvalue
- object itself.protected Object sessionObject(String name)
session().get(name)
name
- name of object,protected String sessionString(String name)
String val = (String)session().get(name)
name
- name of objectprotected Integer sessionInteger(String name)
Integer val = (Integer)session().get(name)
name
- name of objectprotected Boolean sessionBoolean(String name)
Boolean val = (Boolean)session().get(name)
name
- name of objectprotected Double sessionDouble(String name)
Double val = (Double)session().get(name)
name
- name of objectprotected Float sessionFloat(String name)
Float val = (Float)session().get(name)
name
- name of objectprotected Long sessionLong(String name)
Long val = (Long)session().get(name)
name
- name of objectprotected boolean sessionHas(String name)
name
- name of object.public void sendCookie(Cookie cookie)
cookie
- cookie to send.public void sendCookie(String name, String value)
name
- name of cookievalue
- value of cookie.public void sendPermanentCookie(String name, String value)
name
- name of cookievalue
- value of cookie.protected void header(String name, String value)
name
- name of header.value
- value of header.protected void header(String name, Object value)
name
- name of header.value
- value of header.protected HttpSupport.HttpBuilder streamOut(InputStream in)
reader
to the HTTP client.in
- input stream to read bytes from.HttpSupport.HttpBuilder
, to accept additional information.protected String getRealPath(String path)
path
- a String specifying a virtual pathprotected OutputStream outputStream()
protected OutputStream outputStream(String contentType)
contentType
- content typeprotected OutputStream outputStream(String contentType, Map headers, int status)
contentType
- content typeheaders
- set of headers.status
- status.protected PrintWriter writer()
protected PrintWriter writer(String contentType, Map headers, int status)
contentType
- content type. If null - will not be set on the responseheaders
- headers. If null - will not be set on the responsestatus
- will be sent to browser.protected boolean blank(String... names)
names
- names of request parameters.protected String merge(String template, Map values)
template
- name of template - same as in regular templates. Example: "/email-templates/welcome"
.values
- values to be merged into template.public Map<String,String> getResponseHeaders()
protected String sanitize(String unsafeContent)
For example, input like this:
<html><script> alert('hello');</script><div>this is a clean part</div></html>Will produce output like this:
this is a clean part
unsafeContent
- unsafe content. Something that an end user typed into a text area, or input that may include
a script tag or other garbage.protected InputStream getRequestInputStream()
protected InputStream getRequestStream()
getRequestInputStream()
.protected String getRequestString()
getRequestInputStream()
.protected byte[] getRequestBytes()
protected List jsonList()
[1, 2, 3]
.protected Map jsonMap()
{"name":"John", "age":21}
.protected Map[] jsonMaps()
[{"name":"John", "age":21}, {"name":"Jane", "age":20}]
.protected Map values()
Map
with all values from/for current request context.Copyright © 2020 JavaLite. All rights reserved.