{#========================================== Spincast Request plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins plugins-spincast-request{% endblock %} {% block meta_title %}Plugins - Spincast Request{% endblock %} {% block meta_description %}Spincast Request plugin to access information about the current HTTP request.{% endblock %} {% block scripts %} {% endblock %} {% block body %}
The Spincast Request plugin provides a request context add-on:
"IRequestRequestContextAddon". This add-on allows your route handlers to get information
about the current request. It is mounted as .request()
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-request</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 SpincastRequestPluginGuiceModule(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 SpincastRequestPluginGuiceModule(getRequestContextType(),
getWebsocketContextType()));
// other modules...
}
// ...
}
public void myHandler(IAppRequestContext context) {
String userId = context.request().getPathParam("userId");
//...
}
HttpMethod getHttpMethod()
HTTP method.
ContentTypeDefaults getContentTypeBestMatch()
Content-Type to use for a response
using the "Accept" header of the request. It will
fallback to ContentTypeDefaults.TEXT if nothing more specific
is found.
boolean isJsonShouldBeReturn()
true if the request specifies
that Json is the most appropriate format
to return.
boolean isHTMLShouldBeReturn()
true if the request specifies
that HTML is the most appropriate format
to return.
boolean isXMLShouldBeReturn()
true if the request specifies
that XML is the most appropriate format
to return.
boolean isPlainTextShouldBeReturn()
true if the request specifies
that plain-text is the most appropriate format
to return.
Locale getLocaleBestMatch()
Locale to use for a response
using the "Accept-Language" header of
the request.
Returns the default Locale (taken from the configurations)
if nothing more specific is found.
Map<String, List<String>> getHeaders()
TreeMap which iscase insensitive for the keys.
The map is immutable.
List<String> getHeader(String name)
name is case insensitive.
The list is immutable.
String getHeaderFirst(String name)
name is case insensitive.
Returns null if the header is not found.
String getContentType()
String getFullUrl()
In case the request has been forwarded, this will be the *new*,
the current URL. Use getFullUrlOriginal()
to get the original URL, before the request was forwarded.
If a reverse proxy has been used, this URL will contain the
scheme, host and port from the
original URL, as seen by the user.
In general, this is what you want to use in your application.
String getFullUrl(boolean keepCacheBusters)
In case the request has been forwarded, this will be the *new*,
the current URL. Use getFullUrlOriginal()
to get the original URL, before the request was forwarded.
If a reverse proxy has been used, this URL will contain the
scheme, host and port from the
original URL, as seen by the user.
In general, this is what you want to use in your application.
String getFullUrlOriginal()
Use getFullUrl() to get the current, potentially
forwarded URL.
If a reverse proxy has been used, this URL will contain the
scheme, host and port from the
original URL, as seen by the user.
In general, this is what you want to use in your application.
String getFullUrlOriginal(boolean keepCacheBusters)
Use getFullUrl() to get the current, potentially
forwarded URL.
If a reverse proxy has been used, this URL will contain the
scheme, host and port from the
original URL, as seen by the user.
In general, this is what you want to use in your application.
String getFullUrlProxied()
scheme, host and port
as forwarded by the reserve proxy, not as seen by the user.
Cache buster codes are removed, if there were any.
If the request has been forwarded, this is going to return the original URL, not the current, forwarded, one.
In general, you should probably use getFullUrl()
or getFullUrlOriginal() instead of this
one.
String getFullUrlProxied(boolean keepCacheBusters)
scheme, host and port
as forwarded by the reserve proxy, not as seen by the user.
Cache buster codes are removed, if there were any.
If the request has been forwarded, this is going to return the original URL, not the current, forwarded, one.
In general, you should probably use getFullUrl()
or getFullUrlOriginal() instead of this
one.
String getRequestPath()
String getRequestPath(boolean keepCacheBusters)
Map<String, String> getPathParams()
String getPathParam(String name)
name is case sensitive, since you have easy control over it.
String getQueryString(boolean withQuestionMark)
Map<String, List<String>> getQueryStringParams()
List<String> getQueryStringParam(String name)
String getQueryStringParamFirst(String name)
null if the parameter doesn't exist.
InputStream getBodyAsInputStream()
byte[] getBodyAsByteArray()
String getBodyAsString()
UTF-8 encoding.
Note that the characters read cannot be read again!
String getBodyAsString(String encoding)
IJsonObject getJsonBodyAsJsonObject()
IJsonObject. A valid Json body
is expected.
Note that this can only be called once.
Map<String, Object> getJsonBodyAsMap()
Map<String, Object>. A valid Json body
is expected.
Note that this can only be called once.
<T> T getJsonBody(Class<T> clazz)
class. A valid Json body
is expected.
Note that this can only be called once.
IJsonObject getXmlBodyAsJsonObject()
IJsonObject. A valid XML body
is expected.
Note that this can only be called once.
Map<String, Object> getXmlBodyAsMap()
Map<String, Object>. A valid XML body
is expected.
Note that this can only be called once.
<T> T getXmlBody(Class<T> clazz)
class. A valid XML body
is expected.
Note that this can only be called once.
Map<String, List<String>> getFormDatas()
FORM via a POST method.
More than one value with the same key is possible.
The names are case sensitive.
The map is immutable.
List<String> getFormData(String name)
FORM via a POST method.
More than one value with the same name is possible.
The name is case sensitive.
The list is immutable.
String getFormDataFirst(String name)
FORM via a POST method.
The name is case sensitive.
Returns null if the parameter doesn't exist.
Map<String, List<File>> getUploadedFiles()
List<File> getUploadedFiles(String name)
name is possible.
The list is immutable.
Returns an empty list if no uploaded files of this name exists.
File getUploadedFileFirst(String name)
null if no uploaded file of this name exists.
boolean isHttps()
List<IETag> getEtagsFromIfNoneMatchHeader()
ETags from
the If-None-Match header, if any.
List<IETag> getEtagsFromIfMatchHeader()
ETags from
the If-Match header, if any.
Date getDateFromIfModifiedSinceHeader()
If-Modified-Since
header as a Date or null if it doesn't
exist.
Date getDateFromIfUnmodifiedSinceHeader()
If-Unmodified-Since
header as a Date or null if it doesn't
exist.