{#========================================== Spincast Routing plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins plugins-spincast-routing{% endblock %} {% block meta_title %}Plugins - Spincast Routing{% endblock %} {% block meta_description %}Routing related components.{% endblock %} {% block scripts %} {% endblock %} {% block body %}
The Spincast Routing plugin contains the Spincast router, which is an implementation
of the IRouter interface and one
of the most important component of a Spincast application.
It also provides a set of classes to help creating routes and manipulating them.
Finally, it provides an IRoutingRequestContextAddon
implementation, which is
a request context add-on giving your route handlers access to information about the current
routing process.
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 Maven artifact to your project:
<dependency>
<groupId>org.spincast</groupId>
<artifactId>spincast-plugins-routing</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 SpincastRoutingPluginGuiceModule(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 SpincastRoutingPluginGuiceModule(getRequestContextType()));
// other modules...
}
// ...
}
IRouter interface
To learn in depth about the router object, the routing process and how to
create routes, please read the
Routing section of the documentation! Here we are only going to list the
available methods.
IRouteBuilder<R> GET(String path)
GET route.
IRouteBuilder<R> POST(String path)
POST route.
IRouteBuilder<R> PUT(String path)
PUT route.
IRouteBuilder<R> DELETE(String path)
DELETE route.
IRouteBuilder<R> OPTIONS(String path)
OPTIONS route.
IRouteBuilder<R> TRACE(String path)
TRACE route.
IRouteBuilder<R> HEAD(String path)
HEAD route.
IRouteBuilder<R> PATCH(String path)
PATCH route.
IRouteBuilder<R> ALL(String path)
IRouteBuilder<R> SOME(String path, Set<HttpMethod> httpMethods)
IRouteBuilder<R> SOME(String path, HttpMethod... httpMethods)
void before(IHandler<R> handler)
ALL("/*{path}").pos(-1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void before(String path, IHandler<R> handler)
ALL(path).pos(-1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void after(IHandler<R> handler)
ALL("/*{path}").pos(1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void after(String path, IHandler<R> handler)
ALL(path).pos(1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void beforeAndAfter(IHandler<R> handler)
ALL("/*{path}").pos(-1).save(handler)
and
ALL("/*{path}").pos(1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void beforeAndAfter(String path, IHandler<R> handler)
ALL(path).pos(-1).save(handler)
and
ALL(path).pos(1).save(handler)
and with the Routing types as returned by
ISpincastRouterConfig#getFilterDefaultRoutingTypes()
void exception(IHandler<R> handler)
ALL("/*{path}").exception().save(handler)
void exception(String path, IHandler<R> handler)
ALL(path).exception().save(handler)
void notFound(IHandler<R> handler)
ALL("/*{path}").notFound().save(handler)
void notFound(String path, IHandler<R> handler)
ALL(path).notFound().save(handler)
IStaticResourceBuilder<R> file(String url)
static resource file.
IStaticResourceBuilder<R> dir(String url)
static resource directory.
void cors()
void cors(Set<String> allowedOrigins)
void cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead)
void cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent)
void cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies)
void cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods)
void cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods, int maxAgeInSeconds)
void cors(String path)
void cors(String path, Set<String> allowedOrigins)
void cors(String path, Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead)
void cors(String path, Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent)
void cors(String path, Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies)
void cors(String path, Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods)
void cors(String path, Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods, int maxAgeInSeconds)
void addStaticResource(IStaticResource<R> staticResource)
static resource route, directly.
IRoutingResult<R> route(R requestContext)
IRoutingResult<R> route(R requestContext, RoutingType routingType)
void addRoute(IRoute<R> route)
void removeAllRoutes()
void removeRoute(String routeId)
routeId.
IRoute<R> getRoute(String routeId)
routeId.
List<IRoute<R>> getGlobalBeforeFiltersRoutes()
List<IRoute<R>> getMainRoutes()
List<IRoute<R>> getGlobalAfterFiltersRoutes()
void addRouteParamPatternAlias(String alias, String pattern)
"/${param1:<XXX>}" : here "XXX" is the alias for the
regular expression pattern to use.
Map<String, String> getRouteParamPatternAliases()
IRouteBuilder interface
When you start the creation of a route, for example using
router.GET("/"), a IRouteBuilder
instance is returned. You use this builder object to complete
the creation of your route.
IRouteBuilder<R> id(String id)
IRouteBuilder<R> path(String path)
IRouteBuilder<R> pos(int position)
IRouteBuilder<R> found()
Found routing process.
IRouteBuilder<R> notFound()
Not Found routing process.
IRouteBuilder<R> exception()
Exception routing process.
IRouteBuilder<R> allRoutingTypes()
IRouteBuilder<R> before(IHandler<R> beforeFilter)
IRouteBuilder<R> after(IHandler<R> afterFilter)
IRouteBuilder<R> acceptAsString(String... acceptedContentTypes)
Content-Types.
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> acceptAsString(Set<String> acceptedContentTypes)
Content-Types.
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> accept(ContentTypeDefaults... acceptedContentTypes)
Content-Types.
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> accept(Set<ContentTypeDefaults> acceptedContentTypes)
Content-Types.
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> html()
application/html as an accepted Content-Type.
IRouteBuilder<R> json()
application/json as an accepted Content-Type.
IRouteBuilder<R> xml()
application/xml as an accepted Content-Type.
IRouteBuilder<R> cors()
IRouteBuilder<R> cors(Set<String> allowedOrigins)
IRouteBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead)
IRouteBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent)
IRouteBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies)
IRouteBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods)
IRouteBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, Set<HttpMethod> allowedMethods, int maxAgeInSeconds)
IRouteBuilder<R> GET()
GET as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> POST()
POST as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> PUT()
PUT as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> DELETE()
DELETE as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> OPTIONS()
OPTIONS as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> TRACE()
TRACE as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> HEAD()
HEAD as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> PATCH()
PATCH as a supported HTTP method.
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> ALL()
IRouter object, you already specified some
supported HTTP methods. By calling this method, all
methods will now be suported.
IRouteBuilder<R> SOME(Set<HttpMethod> httpMethods)
IRouter object, you already specified some
supported HTTP methods. Those new ones will simply be added.
IRouteBuilder<R> SOME(HttpMethod... httpMethods)
IRouter object, you already specified some
supported HTTP methods. Those new ones will simply be added.
void save(IHandler<R> mainHandler)
IRouter object, an exception will be
thrown.
IRoute<R> create(IHandler<R> mainHandler)
save(...) instead to save the route
to the router at the end of the build process!
IStaticResourceBuilder interface
When you start the creation of a static resource, for example using
router.dir("/public"), a IStaticResourceBuilder
instance is returned. You use this builder object to complete
the creation of the resource route.
IStaticResourceBuilder<R> url(String url)
IStaticResourceBuilder<R> classpath(String path)
IStaticResourceBuilder<R> fileSystem(String path)
IStaticResourceBuilder<R> cors()
IStaticResourceBuilder<R> cors(Set<String> allowedOrigins)
IStaticResourceBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead)
IStaticResourceBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, boolean allowCookies)
void save()
IRouter object, an exception will be
thrown.
void save(IHandler<R> generator)
IRouter object, an exception will be
thrown.
IStaticResource<R> create()
save(...) instead to save the static resource
to the router at the end of the build process!
The plugin provides a IRoutingRequestContextAddon request context add-on. This add-on is
mounted as .routing() on the default Spincast request context.
public void myHandler(IAppRequestContext context) {
// Get information about the matches returned by
// the router.
IRoutingResult<IAppRequestContext> routingResult = context.routing().getRoutingResult();
}
boolean isNotFoundRoute()
boolean isExceptionRoute()
boolean isForwarded()
IRouteHandlerMatch<R> getCurrentRouteHandlerMatch()
IRoutingResult<R> getRoutingResult()
int getPosition()