Package play.mvc

Class Result

java.lang.Object
play.mvc.Result
Direct Known Subclasses:
StatusHeader

public class Result extends Object
Any action result.
  • Constructor Details

    • Result

      public Result(ResponseHeader header, HttpEntity body, Http.Session session, Http.Flash flash, List<Http.Cookie> cookies, TypedMap attrs)
      Create a result from a Scala ResponseHeader and a body.
      Parameters:
      header - the response header
      body - the response body.
      session - the session set on the response.
      flash - the flash object on the response.
      cookies - the cookies set on the response.
      attrs - the typed attributes set on the response.
    • Result

      public Result(ResponseHeader header, HttpEntity body, Http.Session session, Http.Flash flash, List<Http.Cookie> cookies)
      Create a result from a Scala ResponseHeader and a body.
      Parameters:
      header - the response header
      body - the response body.
      session - the session set on the response.
      flash - the flash object on the response.
      cookies - the cookies set on the response.
    • Result

      public Result(ResponseHeader header, HttpEntity body)
      Create a result from a Scala ResponseHeader and a body.
      Parameters:
      header - the response header
      body - the response body.
    • Result

      public Result(int status, String reasonPhrase, Map<String,String> headers, HttpEntity body)
      Create a result.
      Parameters:
      status - The status.
      reasonPhrase - The reason phrase, if a non default reason phrase is required.
      headers - The headers.
      body - The body.
    • Result

      public Result(int status, Map<String,String> headers, HttpEntity body)
      Create a result.
      Parameters:
      status - The status.
      headers - The headers.
      body - The body.
    • Result

      public Result(int status, Map<String,String> headers)
      Create a result with no body.
      Parameters:
      status - The status.
      headers - The headers.
    • Result

      public Result(int status, HttpEntity body)
      Create a result.
      Parameters:
      status - The status.
      body - The entity.
    • Result

      public Result(int status)
      Create a result with no entity.
      Parameters:
      status - The status.
  • Method Details

    • status

      public int status()
      Get the status.
      Returns:
      the status
    • reasonPhrase

      public Optional<String> reasonPhrase()
      Get the reason phrase, if it was set.
      Returns:
      the reason phrase (e.g. "NOT FOUND")
    • getHeader

      protected ResponseHeader getHeader()
      Get the response header
      Returns:
      the header
    • body

      public HttpEntity body()
      Get the body of this result.
      Returns:
      the body
    • redirectLocation

      public Optional<String> redirectLocation()
      Extracts the Location header of this Result value if this Result is a Redirect.
      Returns:
      the location (if it was set)
    • header

      public Optional<String> header(String header)
      Extracts an Header value of this Result value.
      Parameters:
      header - the header name.
      Returns:
      the header (if it was set)
    • headers

      public Map<String,String> headers()
      Extracts all Headers of this Result value.

      The returned map is not modifiable.

      Returns:
      the immutable map of headers
    • contentType

      public Optional<String> contentType()
      Extracts the Content-Type of this Result value.
      Returns:
      the content type (if it was set)
    • charset

      public Optional<String> charset()
      Extracts the Charset of this Result value.
      Returns:
      the charset (if it was set)
    • flash

      public Http.Flash flash()
      Extracts the Flash values of this Result value.
      Returns:
      the flash (if it was set)
    • withFlash

      public Result withFlash(Http.Flash flash)
      Sets a new flash for this result, discarding the existing flash.
      Parameters:
      flash - the flash to set with this result
      Returns:
      the new result
    • withFlash

      public Result withFlash(Map<String,String> flash)
      Sets a new flash for this result, discarding the existing flash.
      Parameters:
      flash - the flash to set with this result
      Returns:
      the new result
    • withNewFlash

      public Result withNewFlash()
      Discards the existing flash for this result.
      Returns:
      the new result
    • flashing

      public Result flashing(Map<String,String> values)
      Adds values to the flash.
      Parameters:
      values - A map with values to add to this result's flash
      Returns:
      A copy of this result with values added to its flash scope.
    • flashing

      public Result flashing(String key, String value)
      Adds the given key and value to the flash.
      Parameters:
      key - The key to add to this result's flash
      value - The value to add to this result's flash
      Returns:
      A copy of this result with the key and value added to its flash scope.
    • removingFromFlash

      public Result removingFromFlash(String... keys)
      Removes values from the flash.
      Parameters:
      keys - Keys to remove from flash
      Returns:
      A copy of this result with keys removed from its flash scope.
    • session

      public Http.Session session()
      Extracts the Session of this Result value.
      Returns:
      the session (if it was set)
    • session

      public Http.Session session(Http.Request request)
      Parameters:
      request - Current request
      Returns:
      The session carried by this result. Reads the given request's session if this result does not has a session.
    • withSession

      public Result withSession(Http.Session session)
      Sets a new session for this result, discarding the existing session.
      Parameters:
      session - the session to set with this result
      Returns:
      the new result
    • withSession

      public Result withSession(Map<String,String> session)
      Sets a new session for this result, discarding the existing session.
      Parameters:
      session - the session to set with this result
      Returns:
      the new result
    • withNewSession

      public Result withNewSession()
      Discards the existing session for this result.
      Returns:
      the new result
    • addingToSession

      public Result addingToSession(Http.Request request, Map<String,String> values)
      Adds values to the session.
      Parameters:
      values - A map with values to add to this result's session
      Returns:
      A copy of this result with values added to its session scope.
    • addingToSession

      public Result addingToSession(Http.Request request, String key, String value)
      Adds the given key and value to the session.
      Parameters:
      key - The key to add to this result's session
      value - The value to add to this result's session
      Returns:
      A copy of this result with the key and value added to its session scope.
    • removingFromSession

      public Result removingFromSession(Http.Request request, String... keys)
      Removes values from the session.
      Parameters:
      keys - Keys to remove from session
      Returns:
      A copy of this result with keys removed from its session scope.
    • cookie

      public Optional<Http.Cookie> cookie(String name)
      Extracts a Cookie value from this Result value
      Parameters:
      name - the cookie's name.
      Returns:
      the optional cookie
    • getCookie

      @Deprecated public Optional<Http.Cookie> getCookie(String name)
      Deprecated.
      Deprecated as of 2.8.0. Renamed to cookie(String)
      Extracts a Cookie value from this Result value
      Parameters:
      name - the cookie's name.
      Returns:
      the optional cookie
    • cookies

      public Http.Cookies cookies()
      Extracts the Cookies (an iterator) from this result value.
      Returns:
      the cookies (if they were set)
    • withCookies

      public Result withCookies(Http.Cookie... newCookies)
      Returns a copy of this result with the given cookies.
      Parameters:
      newCookies - the cookies to add to the result.
      Returns:
      the transformed copy.
    • discardingCookie

      public Result discardingCookie(String name)
      Discard a cookie on the default path ("/") with no domain and that's not secure.
      Parameters:
      name - The name of the cookie to discard, must not be null
    • discardingCookie

      public Result discardingCookie(String name, String path)
      Discard a cookie on the given path with no domain and not that's secure.
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie to discard, may be null
    • discardingCookie

      public Result discardingCookie(String name, String path, String domain)
      Discard a cookie on the given path and domain that's not secure.
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie te discard, may be null
      domain - The domain of the cookie to discard, may be null
    • discardingCookie

      public Result discardingCookie(String name, String path, String domain, boolean secure)
      Discard a cookie in this result
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie te discard, may be null
      domain - The domain of the cookie to discard, may be null
      secure - Whether the cookie to discard is secure
    • discardingCookie

      public Result discardingCookie(String name, String path, String domain, boolean secure, boolean partitioned)
      Discard a cookie in this result
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie te discard, may be null
      domain - The domain of the cookie to discard, may be null
      secure - Whether the cookie to discard is secure
      partitioned - Whether the cookie to discard is partitioned
    • discardingCookie

      public Result discardingCookie(String name, String path, String domain, boolean secure, Http.Cookie.SameSite sameSite)
      Discard a cookie in this result
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie te discard, may be null
      domain - The domain of the cookie to discard, may be null
      secure - Whether the cookie to discard is secure
      sameSite - The SameSite attribute of the cookie to discard, may be null
    • discardingCookie

      public Result discardingCookie(String name, String path, String domain, boolean secure, Http.Cookie.SameSite sameSite, boolean partitioned)
      Discard a cookie in this result
      Parameters:
      name - The name of the cookie to discard, must not be null
      path - The path of the cookie te discard, may be null
      domain - The domain of the cookie to discard, may be null
      secure - Whether the cookie to discard is secure
      sameSite - The SameSite attribute of the cookie to discard, may be null
      partitioned - Whether the cookie to discard is partitioned
    • withHeader

      public Result withHeader(String name, String value)
      Return a copy of this result with the given header.
      Parameters:
      name - the header name
      value - the header value
      Returns:
      the transformed copy
    • withHeaders

      public Result withHeaders(String... nameValues)
      Return a copy of this result with the given headers.

      The headers are processed in pairs, so nameValues(0) is the first header's name, and nameValues(1) is the first header's value, nameValues(2) is second header's name, and so on.

      Parameters:
      nameValues - the array of names and values.
      Returns:
      the transformed copy
    • withoutHeader

      public Result withoutHeader(String name)
      Discard a HTTP header in this result.
      Parameters:
      name - the header name
      Returns:
      the transformed copy
    • as

      public Result as(String contentType)
      Return a copy of the result with a different Content-Type header.
      Parameters:
      contentType - the content type to set
      Returns:
      the transformed copy
    • withLang

      public Result withLang(Lang lang, MessagesApi messagesApi)
      Returns a new result with the given lang set in a cookie. For example:
      
       public Result action() {
           ok("Hello").withLang(Lang.forCode("es"), messagesApi);
       }
       
      Where messagesApi were injected.
      Parameters:
      lang - the new lang
      messagesApi - the messages api implementation
      Returns:
      a new result with the given lang.
      See Also:
    • withLang

      public Result withLang(Locale locale, MessagesApi messagesApi)
      Returns a new result with the given lang set in a cookie. For example:
      
       public Result action() {
           ok("Hello").withLang(new Locale("es"), messagesApi);
       }
       
      Where messagesApi were injected.
      Parameters:
      locale - the new lang
      messagesApi - the messages api implementation
      Returns:
      a new result with the given lang.
      See Also:
    • withoutLang

      public Result withoutLang(MessagesApi messagesApi)
      Clears the lang cookie from this result. For example:
      
       public Result action() {
           ok("Hello").withoutLang(messagesApi);
       }
       
      Where messagesApi were injected.
      Parameters:
      messagesApi - the messages api implementation
      Returns:
      a new result without the lang
      See Also:
    • attrs

      public TypedMap attrs()
      Returns:
      a map of typed attributes associated with the result.
    • withAttrs

      public Result withAttrs(TypedMap newAttrs)
      Create a new version of this object with the given attributes attached to it.
      Parameters:
      newAttrs - The new attributes to add.
      Returns:
      The new version of this object with the attributes attached.
    • addAttr

      public <A> Result addAttr(TypedKey<A> key, A value)
      Create a new versions of this object with the given attribute attached to it.
      Type Parameters:
      A - the attribute type
      Parameters:
      key - The new attribute key.
      value - The attribute value.
      Returns:
      The new version of this object with the new attribute.
    • addAttrs

      public Result addAttrs(TypedEntry<?> e1)
      Create a new versions of this object with the given attribute attached to it.
      Parameters:
      e1 - The new attribute.
      Returns:
      The new version of this object with the new attribute.
    • addAttrs

      public Result addAttrs(TypedEntry<?> e1, TypedEntry<?> e2)
      Create a new versions of this object with the given attributes attached to it.
      Parameters:
      e1 - The first new attribute.
      e2 - The second new attribute.
      Returns:
      The new version of this object with the new attributes.
    • addAttrs

      public Result addAttrs(TypedEntry<?> e1, TypedEntry<?> e2, TypedEntry<?> e3)
      Create a new versions of this object with the given attributes attached to it.
      Parameters:
      e1 - The first new attribute.
      e2 - The second new attribute.
      e3 - The third new attribute.
      Returns:
      The new version of this object with the new attributes.
    • addAttrs

      public Result addAttrs(TypedEntry<?>... entries)
      Create a new versions of this object with the given attributes attached to it.
      Parameters:
      entries - The new attributes.
      Returns:
      The new version of this object with the new attributes.
    • addAttrs

      public Result addAttrs(List<TypedEntry<?>> entries)
      Create a new versions of this object with the given attributes attached to it.
      Parameters:
      entries - The new attributes.
      Returns:
      The new version of this object with the new attributes.
    • removeAttr

      public Result removeAttr(TypedKey<?> key)
      Create a new versions of this object with the given attribute removed.
      Parameters:
      key - The key of the attribute to remove.
      Returns:
      The new version of this object with the attribute removed.
    • asScala

      public play.api.mvc.Result asScala()
      Convert this result to a Scala result.
      Returns:
      the Scala result.