All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.plugins.spincast-response.spincast-response.html Maven / Gradle / Ivy

{#==========================================
Spincast Response plugin
==========================================#}
{% extends "../../layout.html" %}

{% block sectionClasses %}plugins plugins-spincast-response{% endblock %}
{% block meta_title %}Plugins - Spincast Response{% endblock %}
{% block meta_description %}Spincast Response plugin to manipulate the headers and content sent.{% endblock %}

{% block scripts %}

{% endblock %}

{% block body %}

Overview

The Spincast Response plugin provides a request context add-on : "IResponseRequestContextAddon". This add-on allows your route handlers to manipulate the headers and content sent as the response for the current request. It is mounted as .response() on the default Spincast request context.

Installation

If you use the spincast-default artifact, this plugin is already installed so you have nothing more to do!

If you start from scratch using the spincast-core artifact, you can use the plugin by adding this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-response</artifactId>
    <version>{{spincastCurrrentVersion}}</version>
</dependency>

You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new SpincastCoreGuiceModule(args),
        new SpincastResponsePluginGuiceModule(IAppRequestContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastCoreGuiceModule {

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastResponsePluginGuiceModule(getRequestContextType()));
        // other modules...
    }
    
    // ...
}

The request context add-on

Quick example :

public void myHandler(IAppRequestContext context) {
    context.response().setStatusCode(418);
    context.response().sendPlainText("Drink tea!");
}

Add-on methods :

  • boolean isClosed()
    Is the response closed? If so, nothing more can be sent...
  • void end()
    Flushes everything and closes the response.
    Nothing more can be sent after this (but the remaining route handlers will still be called).
  • boolean isHeadersSent()
    Are the response headers sent? If this is the case, then the headers can't be changed anymore.
  • void sendBytes(byte[] bytes)
    Sends some bytes, without flushing.
  • void sendBytes(byte[] bytes, String contentType)
    Sends some bytes using the specified Content-Type, without flushing.
  • void sendBytes(byte[] bytes, String contentType, boolean flush)
    Sends some bytes using the specified Content-Type and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • String getCharactersCharsetName()
    The charset to use to convert characters to bytes.
    Defaults to "UTF-8".
  • void setCharactersCharsetName(String name)
    Sets the charset to use to convert characters to bytes.
    Defaults to "UTF-8".
    Make sure you use the same charset here than the one that is sent as the "Content-Type" header (which is also "UTF-8" by default, if you send data using sendPlainText(), sendHtml() or sendJson()).
  • void sendCharacters(String content)
    Sends a String, using the encoding returned by getCharactersCharsetName, without flushing.
  • void sendCharacters(String content, String contentType)
    Sends a String, using the encoding returned by getCharactersCharsetName and using the specified Content-Type, without flushing.
  • void sendCharacters(String content, String contentType, boolean flush)
    Sends a String, using the encoding returned by getCharactersCharsetName and using the specified Content-Type. Flushes the response, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendPlainText(String string)
    Sends a String as text/plain, UTF-8 encoded, without flushing.
  • void sendPlainText(String string, boolean flush)
    Sends a String as text/plain, UTF-8 encoded, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendJson(Object obj)
    Serializes to Json and sends as application/json, without flushing.
  • void sendJson(Object obj, boolean flush)
    Serializes to Json, sends as application/json , and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendXml(Object obj)
    Serializes to XML and sends as application/xml, without flushing.
  • void sendXml(Object obj, boolean flush)
    Serializes to XML, sends as application/xml, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendHtml(String html)
    Sends a String as text/html, UTF-8 encoded, without flushing.
  • void sendHtml(String html, boolean flush)
    Sends a String as text/html, UTF-8 encoded, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendHtmlParse(String html, Map<String, Object> params)
    Parses the given String using the ITemplatingEngine and the given parameters, then sends the result as text/html, UTF-8 encoded, without flushing.
  • void sendHtmlParse(String html, Map<String, Object> params, boolean flush)
    Parses the given String using the ITemplatingEngine and the given parameters, then sends the result as text/html, UTF-8 encoded, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendParse(String content, String contentType, Map<String, Object> params)
    Parses the given String using the ITemplatingEngine and the given parameters, then sends the result using the specified Content-Type, without flushing.
  • void sendParse(String content, String contentType, Map<String, Object> params, boolean flush)
    Parses the given String using the ITemplatingEngine and the given parameters, then sends the result using the specified Content-Type, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendHtmlTemplate(String templatePath, Map<String, Object> params)
    Finds the HTML template using the ITemplatingEngine, evaluates it using the given parameters, then sends the result as text/html, UTF-8 encoded, without flushing.
  • void sendHtmlTemplate(String templatePath, Map<String, Object> params, boolean flush)
    Finds the HTML template using the ITemplatingEngine, evaluates it using the given parameters, then sends the result as text/html, UTF-8 encoded, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void sendTemplate(String templatePath, String contentType, Map<String, Object> params)
    Finds the specified template using the ITemplatingEngine, evaluates it using the given parameters, then sends the result using the given contentType, without flushing.
  • void sendTemplate(String templatePath, String contentType, Map<String, Object> params, boolean flush)
    Finds the specified template using the ITemplatingEngine, evaluates it using the given parameters, then sends the result using the given contentType, and flushes, if specified.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void flush()
    Flushes the current response. If not already specified, a default status code and Content-Type will be added.
    Note that once the response is flushed, no header can be added or changed anymore.
  • void flush(boolean end)
    Flushes the current response. If not already specified on the response, a default status code and content-type will be added.
    Note that once the response is flushed, no header can be added or changed anymore.
    @param end If true, the exchange will be closed and no more data can be sent.
  • void setStatusCode(int statusCode)
    Sets the response's status code to use. Uses 200 by default.
    Note that this status code can be changed automatically if an exception is thrown.
  • int getStatusCode()
    The current status code sent or to be send.
  • void setContentType(String responseContentType)
    The Content-Type header to use for the response. Most sendXXX() methods will set this automatically.
  • String getContentType()
    The current Content-Type sent or to be send.
  • void redirect(String newUrl, boolean permanently)
    Sets a redirection header.
    This will NOT close the response and will NOT skip the remaining handlers!
    Throw a RedirectException instead if you want to redirect the user immediately.
    @param permanently If true, a 301 header will be sent. If false, 302 will be sent.
  • void redirect(String newUrl, int specific3xxCode)
    Sets the redirection headers with a specified 3xx status code.
    This will NOT close the response and will NOT skip the remaining handlers!
    Throw a RedirectException instead if you want to redirect the user immediately.
  • void addHeaderValue(String name, String value)
    Adds a value to a response header. If the header already has values, the new one is added.
    If the value is null, it won't be added.
  • void addHeaderValues(String name, List<String> values)
    Adds a list of values to a response header. If the header already has values, the new ones are added.
    If the values are null, nothing will be added.
  • void setHeader(String name, String value)
    Set the value to a response header. If the header already exists, its current values are overwritten.
    If the value is null, the header will be removed (same behavior as removeHeader(String name))
  • void setHeader(String name, List<String> values)
    Set multiple values to a response header. If the header already exists, its current values are overwritten.
    If the lists is null or empty, the header will be removed (same behavior as removeHeader(String name))
  • void removeHeader(String name)
    Removes an header by its name.
  • Map<String, List<String>> getHeaders()
    The currently set response headers.
    The map is mutable! Also the returned implementation is a TreeMap with case insensitive keys.
  • List<String> getHeader(String name)
    The values of a specific response header.
    The list is mutable!
    The name is case insensitive.
    Returns an empty list if the header was not found.
  • String getHeaderFirst(String name)
    The first value of a specific header.
    The name is case insensitive.
    Returns null if the header is not found.
  • void resetBuffer()
    Clears the buffer (the unsent buffer, of course).
  • void resetEverything()
    Clears the buffer (the unsent buffer, of course), resets the cookies, the headers, the Content-Type and sets the status code back to 200.
  • byte[] getUnsentBytes()
    Gets the current unsent bytes (AKA the buffer).
  • String getUnsentCharacters()
    Gets the current unsent characters (AKA the buffer), using the UTF-8 encoding.
  • String getUnsentCharacters(String encoding)
    Gets the current unsent characters (AKA the buffer), using the specified encoding.
  • void setGzipOption(GzipOption gzipOption)
    Enable or disable gzipping of the response.
    The default is GzipOption.DEFAULT which will gzip the response only for some Content-Types and only if a gzip 'Accept-Encoding' header has been received from the client.
  • GzipOption getGzipOption()
    The currently set gzip options.

{% endblock %}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy