Package ninja
Class Result
- java.lang.Object
-
- ninja.Result
-
- Direct Known Subclasses:
AsyncResult
public class Result extends Object
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description Result(int statusCode)
A result.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Result
addCookie(Cookie cookie)
Result
addHeader(String headerName, String headerContent)
Result
charset(String charset)
Result
contentType(String contentType)
Sets the content type.Result
doNotCacheContent()
This function sets Cache-Control: no-cache, no-store Date: (current date) Expires: 1970 => it therefore effectively forces the browser and every proxy in between not to cache content.Optional<String>
fallbackContentType()
Result
fallbackContentType(String fallbackContentType)
String
getCharset()
String
getContentType()
Cookie
getCookie(String cookieName)
Returns cookie with that name or null.List<Cookie>
getCookies()
Map<String,String>
getHeaders()
Class<?>
getJsonView()
Object
getRenderable()
int
getStatusCode()
String
getTemplate()
Result
html()
Set the content type of this result toTEXT_HTML
.Result
json()
Set the content type of this result toAPPLICATION_JSON
.Result
jsonp()
Set the content type of this result toAPPLICATION_JSONP
.Result
jsonView(Class<?> jsonView)
Set the Jackson JSON View.Result
redirect(String url)
A redirect that uses 303 see other.Result
redirectTemporary(String url)
A redirect that uses 307 see other.Result
render(InputStream input, boolean close)
Renders all bytes from the input stream to the output stream.Result
render(Object object)
This method handles two principal cases: 1) If the this.renderable of this result is null, the object passed is simply set as renderable for this Result 2) If the this.renderable of this result is not null an new map is generated as object to render and both the former renderable and the new object added to the map.Result
render(String key, Object value)
Implicitly generates a hashmap as object being rendered and adds this key, value pair.Result
render(Map.Entry<String,Object> entry)
Handles following cases: 1) If this.renderable is null: a new HashMap is generated and this entry being added to the map.Result
render(Map<String,Object> mapToRender)
Replaces the object being passed by this result to the rendering engine with this map.Result
render(Renderable renderable)
Sets this renderable as object to render.Result
renderRaw(byte[] bytes)
This method directly renders the byte array to the output.Result
renderRaw(String string)
Deprecated.=> use text().render(string), html().render(string), json().render(string), xml().render(string), or contentType(type).render(string).Result
setContentType(String contentType)
Deprecated.Result
status(int statusCode)
Set the status of this result.Result
supportedContentType(String contentTypeSupportedByThisResult)
Will add a content type to the list of supported content types.List<String>
supportedContentTypes()
Returns immutable list of supported content types by this request.Result
supportedContentTypes(String... contentTypesSupportedByThisResult)
Will add the content types to the list of supported content types.Result
template(String template)
Set the template to render.Result
text()
Set the content type of this result toTEXT_PLAIN
.Result
unsetCookie(String name)
Result
xml()
Set the content type of this result toResult#APPLICATON_XML
.
-
-
-
Field Detail
-
SC_101_SWITCHING_PROTOCOLS
public static final int SC_101_SWITCHING_PROTOCOLS
- See Also:
- Constant Field Values
-
SC_200_OK
public static final int SC_200_OK
- See Also:
- Constant Field Values
-
SC_201_CREATED
public static final int SC_201_CREATED
- See Also:
- Constant Field Values
-
SC_204_NO_CONTENT
public static final int SC_204_NO_CONTENT
- See Also:
- Constant Field Values
-
SC_300_MULTIPLE_CHOICES
public static final int SC_300_MULTIPLE_CHOICES
- See Also:
- Constant Field Values
-
SC_301_MOVED_PERMANENTLY
public static final int SC_301_MOVED_PERMANENTLY
- See Also:
- Constant Field Values
-
SC_302_FOUND
public static final int SC_302_FOUND
- See Also:
- Constant Field Values
-
SC_303_SEE_OTHER
public static final int SC_303_SEE_OTHER
- See Also:
- Constant Field Values
-
SC_304_NOT_MODIFIED
public static final int SC_304_NOT_MODIFIED
- See Also:
- Constant Field Values
-
SC_307_TEMPORARY_REDIRECT
public static final int SC_307_TEMPORARY_REDIRECT
- See Also:
- Constant Field Values
-
SC_400_BAD_REQUEST
public static final int SC_400_BAD_REQUEST
- See Also:
- Constant Field Values
-
SC_401_UNAUTHORIZED
public static final int SC_401_UNAUTHORIZED
- See Also:
- Constant Field Values
-
SC_403_FORBIDDEN
public static final int SC_403_FORBIDDEN
- See Also:
- Constant Field Values
-
SC_404_NOT_FOUND
public static final int SC_404_NOT_FOUND
- See Also:
- Constant Field Values
-
SC_500_INTERNAL_SERVER_ERROR
public static final int SC_500_INTERNAL_SERVER_ERROR
- See Also:
- Constant Field Values
-
SC_501_NOT_IMPLEMENTED
public static final int SC_501_NOT_IMPLEMENTED
- See Also:
- Constant Field Values
-
TEXT_HTML
public static final String TEXT_HTML
- See Also:
- Constant Field Values
-
TEXT_PLAIN
public static final String TEXT_PLAIN
- See Also:
- Constant Field Values
-
APPLICATION_JSON
public static final String APPLICATION_JSON
- See Also:
- Constant Field Values
-
APPLICATON_JSON
@Deprecated public static final String APPLICATON_JSON
Deprecated.- See Also:
- Constant Field Values
-
APPLICATION_JSONP
public static final String APPLICATION_JSONP
- See Also:
- Constant Field Values
-
APPLICATON_JSONP
@Deprecated public static final String APPLICATON_JSONP
Deprecated.- See Also:
- Constant Field Values
-
APPLICATION_XML
public static final String APPLICATION_XML
- See Also:
- Constant Field Values
-
APPLICATION_OCTET_STREAM
public static final String APPLICATION_OCTET_STREAM
- See Also:
- Constant Field Values
-
NO_HTTP_BODY
public static final NoHttpBody NO_HTTP_BODY
-
LOCATION
public static final String LOCATION
- See Also:
- Constant Field Values
-
CACHE_CONTROL
public static final String CACHE_CONTROL
- See Also:
- Constant Field Values
-
CACHE_CONTROL_DEFAULT_NOCACHE_VALUE
public static final String CACHE_CONTROL_DEFAULT_NOCACHE_VALUE
- See Also:
- Constant Field Values
-
DATE
public static final String DATE
- See Also:
- Constant Field Values
-
EXPIRES
public static final String EXPIRES
- See Also:
- Constant Field Values
-
WWW_AUTHENTICATE
public static final String WWW_AUTHENTICATE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Result
public Result(int statusCode)
A result. Sets utf-8 as charset and status code by default. Refer toSC_200_OK
,SC_204_NO_CONTENT
and so on for some short cuts to predefined results.- Parameters:
statusCode
- The status code to set for the result. Shortcuts to the code at:SC_200_OK
-
-
Method Detail
-
getRenderable
public Object getRenderable()
-
render
public Result render(Object object)
This method handles two principal cases: 1) If the this.renderable of this result is null, the object passed is simply set as renderable for this Result 2) If the this.renderable of this result is not null an new map is generated as object to render and both the former renderable and the new object added to the map. The former object is gets the class name in camelCase as key. If the converted camelCase key of this object already exists anIllegalArgumentException
is being thrown.- Parameters:
object
- The object to add (either an arbitrary class or Renderable).- Returns:
- Result this result for chaining.
-
render
public Result render(Map<String,Object> mapToRender)
Replaces the object being passed by this result to the rendering engine with this map. It will overwrite any previously set render(...) calls.- Parameters:
mapToRender
- The map being passed to the templating engine.- Returns:
- This Result for chaining.
-
render
public Result render(Map.Entry<String,Object> entry)
Handles following cases: 1) If this.renderable is null: a new HashMap is generated and this entry being added to the map. 2) If this.renderable is a Map: the entry is added 3) If this.renderable is an object (not a renderable): a Map is generated and both the former object and the new entry are being added. 4) If this.renderable is a Renderable: anIllegalArgumentException
is thrown. If the entry key already exists in the map of this.renderable anIllegalArgumentException
is thrown.- Parameters:
entry
- The entry to add.- Returns:
- The result for further chaining.
-
render
public Result render(Renderable renderable)
Sets this renderable as object to render. Usually this renderable does rendering itself and will not call any templating engine.- Parameters:
renderable
- The renderable that will handle everything after returing the result.- Returns:
- This result for chaining.
-
render
public Result render(String key, Object value)
Implicitly generates a hashmap as object being rendered and adds this key, value pair. If the object being rendered is already a hashmap it simply adds this key value pair to it.- Parameters:
key
- The key to use.value
- The value to use.- Returns:
- The Result for chaining.
-
renderRaw
@Deprecated public Result renderRaw(String string)
Deprecated.=> use text().render(string), html().render(string), json().render(string), xml().render(string), or contentType(type).render(string).This method directly renders the String to the output. It completely bypasses any rendering engine. Thus you can render anything you want. Chaining of resultRaw().resultRaw()... is NOT supported. Mixing with render() is NOT supported. It is always recommended to implement your own RenderingEngine OR use existing rendering engines. Example:public Result controllerMethod() { String customJson = "{\"user\" : \"[email protected]\"}"; return Results.json().renderRaw(customJson); }
- Parameters:
string
- The string to render.- Returns:
- A result that will render the string directly to the output stream.
-
renderRaw
public Result renderRaw(byte[] bytes)
This method directly renders the byte array to the output. It completely bypasses any rendering engine. Thus you can render anything you want. Chaining of resultRaw().resultRaw()... is NOT supported. Mixing with render() is NOT supported. It is always recommended to implement your own RenderingEngine OR use existing rendering engines.- Parameters:
bytes
- The bytes to render.- Returns:
- A result that will render the string directly to the output stream.
-
render
public Result render(InputStream input, boolean close)
Renders all bytes from the input stream to the output stream. Does not close or flush either stream unless you explicitly tell this method to. Will set the content type to application/octet-stream, unless the content-type was already set. If any exception is thrown during the render, and internal server error exception will be thrown.- Parameters:
input
- The input stream to read fromclose
- If true this method will close the input stream after copying all the bytes, otherwise that is up to the caller.- Returns:
- This result
-
getContentType
public String getContentType()
-
getCharset
public String getCharset()
- Returns:
- Charset of the current result that will be used. Will be "utf-8" by default.
-
charset
public Result charset(String charset)
- Parameters:
charset
- Set the charset of the result. Is "utf-8" by default.- Returns:
- The result for chaining.
-
setContentType
@Deprecated public Result setContentType(String contentType)
Deprecated.Sets the content type- Parameters:
contentType
-
-
contentType
public Result contentType(String contentType)
Sets the content type. Must not contain any charset WRONG: "text/html; charset=utf8". If you want to set the charset use methodcharset(String)
;- Parameters:
contentType
- (without encoding) something like "text/html" or "application/json"- Returns:
- The result for chaining.
-
supportedContentType
public Result supportedContentType(String contentTypeSupportedByThisResult)
Will add a content type to the list of supported content types. Calling that method two times with different content types will add both content types.- Parameters:
contentTypeSupportedByThisResult
- The content type to add. Eg. "application/xml"- Returns:
- The result for chaining.
-
supportedContentTypes
public Result supportedContentTypes(String... contentTypesSupportedByThisResult)
Will add the content types to the list of supported content types.- Parameters:
contentTypesSupportedByThisResult
- The content type to add. Eg. "application/xml", "applcation/json"- Returns:
- The result for chaining.
-
supportedContentTypes
public List<String> supportedContentTypes()
Returns immutable list of supported content types by this request.- Returns:
- immutable list of supported content types. Either the default content types if no one has been set (html, json, xml) or the content types set by the user and supportedContentType(...).
-
fallbackContentType
public Optional<String> fallbackContentType()
- Returns:
- The fallback content type. This will be the content type used when none of the supported content types matches the accept content type of the request.
-
fallbackContentType
public Result fallbackContentType(String fallbackContentType)
- Parameters:
fallbackContentType
- The content type to use as fallback when neither contentType set and supportedContentTypes do not match request.- Returns:
- This result for chaining.
-
getCookie
public Cookie getCookie(String cookieName)
Returns cookie with that name or null.- Parameters:
cookieName
- Name of the cookie- Returns:
- The cookie or null if not found.
-
getStatusCode
public int getStatusCode()
-
status
public Result status(int statusCode)
Set the status of this result. Refer toSC_200_OK
,SC_204_NO_CONTENT
and so on for some short cuts to predefined results.- Parameters:
statusCode
- The status code. Result (SC_200_OK
) provides some helpers.- Returns:
- The result you executed the method on for method chaining.
-
getTemplate
public String getTemplate()
-
template
public Result template(String template)
Set the template to render. For instance template("views/AnotherController/anotherview.ftl.html");- Parameters:
template
- The view to render. Eg. views/AnotherController/anotherview.ftl.html- Returns:
- The result that you executed the method on for chaining.
-
getJsonView
public Class<?> getJsonView()
-
jsonView
public Result jsonView(Class<?> jsonView)
Set the Jackson JSON View. See http://wiki.fasterxml.com/JacksonJsonViews- Parameters:
jsonView
- JSON serialization view class to use when rendering- Returns:
- The result that you executed the method on for chaining, with JSON view set
-
redirect
public Result redirect(String url)
A redirect that uses 303 see other.- Parameters:
url
- The url used as redirect target.- Returns:
- A nicely configured result with status code 303 and the url set as Location header.
-
redirectTemporary
public Result redirectTemporary(String url)
A redirect that uses 307 see other.- Parameters:
url
- The url used as redirect target.- Returns:
- A nicely configured result with status code 307 and the url set as Location header.
-
html
public Result html()
Set the content type of this result toTEXT_HTML
.- Returns:
- the same result where you executed this method on. But the content type is now
TEXT_HTML
.
-
json
public Result json()
Set the content type of this result toAPPLICATION_JSON
.- Returns:
- the same result where you executed this method on. But the content type is now
APPLICATION_JSON
.
-
jsonp
public Result jsonp()
Set the content type of this result toAPPLICATION_JSONP
.- Returns:
- the same result where you executed this method on. But the content type is now
APPLICATION_JSONP
.
-
text
public Result text()
Set the content type of this result toTEXT_PLAIN
.- Returns:
- the same result where you executed this method on. But the content type is now
TEXT_PLAIN
.
-
xml
public Result xml()
Set the content type of this result toResult#APPLICATON_XML
.- Returns:
- the same result where you executed this method on. But the content type is now
Result#APPLICATON_XML
.
-
doNotCacheContent
public Result doNotCacheContent()
This function sets Cache-Control: no-cache, no-store Date: (current date) Expires: 1970 => it therefore effectively forces the browser and every proxy in between not to cache content. See also https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers- Returns:
- this result for chaining.
-
-