{#========================================== 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, 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 SpincastRoutingPluginGuiceModule(getRequestContextType(),
getWebsocketContextType()));
// 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
void before(String path, IHandler<R> handler)
ALL(path).pos(-1).save(handler)
and with the Routing types as returned by
void after(IHandler<R> handler)
ALL("/*{path}").pos(1).save(handler)
and with the Routing types as returned by
void after(String path, IHandler<R> handler)
ALL(path).pos(1).save(handler)
and with the Routing types as returned by
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
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
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()
void httpAuth(String pathPrefix, String realmName)
IWebsocketRouteBuilder<R, W> websocket(String path)
Websocket route.
void addWebsocketRoute(IWebsocketRoute<R, W> websocketRoute)
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.
This route will only be considered for requests specifying those
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> acceptAsString(Set<String> acceptedContentTypes)
Content-Types.
This route will only be considered for requests specifying those
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> accept(ContentTypeDefaults... acceptedContentTypes)
Content-Types.
This route will only be considered for requests specifying those
Content-Types as being accepted
(using the Accept header).
IRouteBuilder<R> accept(Set<ContentTypeDefaults> acceptedContentTypes)
Content-Types.
This route will only be considered for requests specifying those
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> GET()
GET as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> POST()
POST as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> PUT()
PUT as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> DELETE()
DELETE as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> OPTIONS()
OPTIONS as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> TRACE()
TRACE as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> HEAD()
HEAD as a supported HTTP method.
If you started the creation of the route from an
IRouter object, you already specified some
supported HTTP methods. This one will simply be added.
IRouteBuilder<R> PATCH()
PATCH as a supported HTTP method.
If you started the creation of the route from an
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!
IRouteBuilder<R> noCache()
IRouteBuilder<R> cache()
Uses the default cache configurations, provided
by org.spincast.core.config.ISpincastConfig ISpincastConfig
IRouteBuilder<R> cache(int seconds)
IRouteBuilder<R> cache(int seconds, boolean isPrivate)
IRouteBuilder<R> cache(int seconds, boolean isPrivate, Integer secondsCdn)
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> pathAbsolute(String absolutePath)
IStaticResourceBuilder<R> pathRelative(String relativePath)
ISpincastConfig::getSpincastTempDir()
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, Set<String> extraHeadersAllowedToBeSent)
IStaticResourceBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies)
IStaticResourceBuilder<R> cors(Set<String> allowedOrigins, Set<String> extraHeadersAllowedToBeRead, Set<String> extraHeadersAllowedToBeSent, boolean allowCookies, int maxAgeInSeconds)
IStaticResourceBuilder<R> cache(int seconds)
IStaticResourceBuilder<R> cache(int seconds, boolean isPrivate)
IStaticResourceBuilder<R> cache(int seconds, boolean isPrivate, Integer secondsCdn)
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!
IWebsocketRouteBuilder interface
When you start the creation of a Websocket route, using
router.websocket("/somePath"), a IWebsocketRouteBuilder
instance is returned. You use this builder object to complete
the creation of your Websocket route.
IWebsocketRouteBuilder<R, W> path(String path)
IWebsocketRouteBuilder<R, W> id(String id)
IWebsocketRouteBuilder<R, W> before(IHandler<R> beforeFilter)
Note that there are no "after" filters because once a
Websocket connection is established, the HTTP one
is no more.
void save(IWebsocketController<R, W> websocketController)
IWebsocketRoute<R, W> create(IWebsocketController<R, W> websocketController)
save(...) instead to save the route
to the router at the end of the build process!
You can bind a ISpincastRouterConfig implementation to tweak
the default configurations
used by the components this plugin provides. By default, the
SpincastRouterConfigDefault class is used as the implementation.
Set<RoutingType> getFilterDefaultRoutingTypes()
routing types to apply a filter to when none is
explicitly specified.
int getCorsFilterPosition()
cors "before" filter.
Must be < 0 !
-100.
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()