{#========================================== 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 %}
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.
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, IAppWebsocketContext.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(),
getWebsocketContextType()));
// other modules...
}
// ...
}
public void myHandler(IAppRequestContext context) {
context.response().setStatusCode(418);
context.response().sendPlainText("Drink tea!");
}
boolean isClosed()
void end()
route handlers will still
be called).
boolean isHeadersSent()
void sendBytes(byte[] bytes)
bytes, without flushing.
void sendBytes(byte[] bytes, String contentType)
bytes using the specified Content-Type,
without flushing.
void sendBytes(byte[] bytes, String contentType, boolean flush)
bytes using the specified Content-Type and flushes,
if specified.
String getCharactersCharsetName()
"UTF-8".
void setCharactersCharsetName(String name)
"UTF-8".
"Content-Type" header (which
is also "UTF-8" by default, if you send data using
sendPlainText(), sendHtml() or sendJson()).
void sendCharacters(String content)
getCharactersCharsetName,
without flushing.
void sendCharacters(String content, String contentType)
getCharactersCharsetName and
using the specified Content-Type, without flushing.
void sendCharacters(String content, String contentType, boolean flush)
getCharactersCharsetName
and using the specified Content-Type. Flushes the response, if specified.
void sendPlainText(String string)
text/plain, UTF-8 encoded, without flushing.
void sendPlainText(String string, boolean flush)
text/plain, UTF-8 encoded,
and flushes, if specified.
void sendJson(String jsonString)
Json String using the application/json Content-Type,
without flushing.
Synonym of : sendCharacters(jsonString, "application/json")
void sendJson(String jsonString, boolean flush)
Json String using the application/json Content-Type,
and flushes, if specified.
Note that once the response is flushed, no header can be added or changed anymore.
Synonym of : sendCharacters(jsonString, "application/json", flush)
void sendJsonObj(Object obj)
Json and sends as application/json, without flushing.
void sendJsonObj(Object obj, boolean flush)
Json, sends as application/json,
and flushes, if specified.
void sendXml(String xml)
XML using the application/xml Content-Type,
without flushing.
Synonym of : sendCharacters(xml, "application/xml")
void sendXml(String xml, boolean flush)
XML using the application/xml Content-Type,
and flushes, if specified.
Note that once the response is flushed, no header can be added or changed anymore.
Synonym of : sendCharacters(xml, "application/xml", flush)
void sendXmlObj(Object obj)
XML and sends as application/xml, without flushing.
void sendXmlObj(Object obj, boolean flush)
XML, sends as application/xml, and flushes, if specified.
void sendHtml(String html)
text/html, UTF-8 encoded, without flushing.
void sendHtml(String html, boolean flush)
text/html, UTF-8 encoded,
and flushes, if specified.
void sendHtmlParse(String html, Map<String, Object> params)
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)
ITemplatingEngine and
the given parameters, then sends the result as text/html,
UTF-8 encoded, and flushes, if specified.
void sendParse(String content, String contentType, Map<String, Object> params)
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)
ITemplatingEngine and
the given parameters, then sends the result using the specified
Content-Type, and flushes, if specified.
void sendHtmlTemplate(String templatePath, Map<String, Object> params)
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)
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.
void sendTemplate(String templatePath, String contentType, Map<String, Object> params)
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)
ITemplatingEngine, evaluates it using
the given parameters, then sends the
result using the given contentType, and flushes, if specified.
void sendHtmlTemplate(String templatePath, boolean isClasspathPath, Map<String, Object> params)
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, boolean isClasspathPath, Map<String, Object> params, boolean flush)
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.
void sendTemplate(String templatePath, boolean isClasspathPath, String contentType, Map<String, Object> params)
ITemplatingEngine, evaluates it using
the given parameters, then sends the
result using the given contentType, without flushing.
void sendTemplate(String templatePath, boolean isClasspathPath, String contentType, Map<String, Object> params, boolean flush)
ITemplatingEngine, evaluates it using
the given parameters, then sends the
result using the given contentType, and flushes, if specified.
void flush()
status code and Content-Type will be added.
void flush(boolean end)
status code and content-type will be added.
void setStatusCode(int statusCode)
status code to use. Uses 200
by default.
status code can be changed automatically
if an exception is thrown.
int getStatusCode()
status code sent or to be send.
void setContentType(String responseContentType)
Content-Type header to use for the response. Most
sendXXX() methods will set this automatically.
String getContentType()
Content-Type sent or to be send.
void redirect(String newUrl, boolean permanently)
RedirectException instead if you want to redirect the user
immediately.
void redirect(String newUrl, int specific3xxCode)
3xx status code.
RedirectException instead if you want to redirect the user
immediately.
void addHeaderValue(String name, String value)
null, it won't be added.
void addHeaderValues(String name, List<String> values)
null, nothing will be added.
void setHeader(String name, String value)
null, the header will be
removed (same behavior as removeHeader(String name))
void setHeader(String name, List<String> values)
null or empty, the header will be
removed (same behavior as removeHeader(String name))
void removeHeader(String name)
Map<String, List<String>> getHeaders()
TreeMap with case insensitive keys.
List<String> getHeader(String name)
String getHeaderFirst(String name)
null if the header is not found.
void resetBuffer()
void resetEverything()
Content-Type and
sets the status code back to 200.
byte[] getUnsentBytes()
String getUnsentCharacters()
UTF-8 encoding.
String getUnsentCharacters(String encoding)
void setGzipOption(GzipOption gzipOption)
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()