Package ninja

Class Result

    • Constructor Detail

      • Result

        public Result​(int statusCode)
        A result. Sets utf-8 as charset and status code by default. Refer to SC_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 an IllegalArgumentException 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: an IllegalArgumentException is thrown. If the entry key already exists in the map of this.renderable an IllegalArgumentException 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 from
        close - 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 method charset(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 to SC_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 to TEXT_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 to APPLICATION_JSON.
        Returns:
        the same result where you executed this method on. But the content type is now APPLICATION_JSON.
      • text

        public Result text()
        Set the content type of this result to TEXT_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 to Result#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.