public final class Service extends Object
ignite()
should be statically
imported and used to create instances. The instance should typically be named so when prefixing the 'routing' methods
the semantic makes sense. For example 'http' is a good variable name since when adding routes it would be:
Service http = ignite();
...
http.get("/hello", (q, a) -> "Hello World");Modifier and Type | Class and Description |
---|---|
class |
Service.StaticFiles
Provides static files utility methods.
|
Modifier and Type | Field and Description |
---|---|
protected static String |
DEFAULT_ACCEPT_TYPE |
protected boolean |
initialized |
protected String |
ipAddress |
protected int |
maxThreads |
protected int |
minThreads |
protected Deque<String> |
pathDeque |
protected int |
port |
Redirect |
redirect |
protected Routes |
routes |
protected EmbeddedServer |
server |
static int |
SPARK_DEFAULT_PORT |
protected SslStores |
sslStores |
Service.StaticFiles |
staticFiles |
protected int |
threadIdleTimeoutMillis |
protected Map<String,WebSocketHandlerWrapper> |
webSocketHandlers |
protected Optional<Integer> |
webSocketIdleTimeoutMillis |
Modifier and Type | Method and Description |
---|---|
int |
activeThreadCount() |
void |
addFilter(HttpMethod httpMethod,
FilterImpl filter)
Adds a filter
|
void |
addFilter(String httpMethod,
FilterImpl filter)
Deprecated.
|
void |
addRoute(HttpMethod httpMethod,
RouteImpl route)
Adds a route
|
void |
addRoute(String httpMethod,
RouteImpl route)
Deprecated.
|
void |
after(Filter filter)
Maps a filter to be executed after any matching routes
|
void |
after(String path,
Filter filter)
Maps a filter to be executed after any matching routes
|
void |
after(String path,
String acceptType,
Filter filter)
Maps a filter to be executed after any matching routes
|
void |
afterAfter(Filter filter)
Maps a filter to be executed after any matching routes even if the route throws any exception
|
void |
afterAfter(String path,
Filter filter)
Maps a filter to be executed after any matching routes even if the route throws any exception
|
void |
awaitInitialization()
Waits for the spark server to be initialized.
|
void |
before(Filter filter)
Maps a filter to be executed before any matching routes
|
void |
before(String path,
Filter filter)
Maps a filter to be executed before any matching routes
|
void |
before(String path,
String acceptType,
Filter filter)
Maps a filter to be executed before any matching routes
|
void |
connect(String path,
Route route)
Map the route for HTTP CONNECT requests
|
void |
connect(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP CONNECT requests
|
void |
connect(String path,
String acceptType,
Route route)
Map the route for HTTP CONNECT requests
|
void |
connect(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP CONNECT requests
|
void |
connect(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP CONNECT requests
|
void |
connect(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP CONNECT requests
|
void |
delete(String path,
Route route)
Map the route for HTTP DELETE requests
|
void |
delete(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP DELETE requests
|
void |
delete(String path,
String acceptType,
Route route)
Map the route for HTTP DELETE requests
|
void |
delete(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP DELETE requests
|
void |
delete(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP DELETE requests
|
void |
delete(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP DELETE requests
|
<T extends Exception> |
exception(Class<T> exceptionClass,
ExceptionHandler<? super T> handler)
Maps an exception handler to be executed when an exception occurs during routing
|
Service |
externalStaticFileLocation(String externalFolder)
Sets the external folder serving static files.
|
void |
get(String path,
Route route)
Map the route for HTTP GET requests
|
void |
get(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP GET requests
|
void |
get(String path,
String acceptType,
Route route)
Map the route for HTTP GET requests
|
void |
get(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP GET requests
|
void |
get(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP GET requests
|
void |
get(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP GET requests
|
String |
getPaths() |
HaltException |
halt()
Immediately stops a request within a filter or route
NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise
halt will not work
|
HaltException |
halt(int status)
Immediately stops a request within a filter or route with specified status code
NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise
halt will not work
|
HaltException |
halt(int status,
String body)
Immediately stops a request within a filter or route with specified status code and body content
NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise
halt will not work
|
HaltException |
halt(String body)
Immediately stops a request within a filter or route with specified body content
NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise
halt will not work
|
void |
head(String path,
Route route)
Map the route for HTTP HEAD requests
|
void |
head(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP HEAD requests
|
void |
head(String path,
String acceptType,
Route route)
Map the route for HTTP HEAD requests
|
void |
head(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP HEAD requests
|
void |
head(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP HEAD requests
|
void |
head(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP HEAD requests
|
static Service |
ignite()
Creates a new Service (a Spark instance).
|
void |
init() |
void |
initExceptionHandler(Consumer<Exception> initExceptionHandler)
Overrides default exception handler during initialization phase
|
void |
internalServerError(Route route)
Maps 500 internal server errors to the provided route.
|
void |
internalServerError(String page)
Maps 500 internal server errors to the provided custom page
|
Service |
ipAddress(String ipAddress)
Set the IP address that Spark should listen on.
|
void |
notFound(Route route)
Maps 404 errors to the provided route.
|
void |
notFound(String page)
Maps 404 errors to the provided custom page
|
void |
options(String path,
Route route)
Map the route for HTTP OPTIONS requests
|
void |
options(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP OPTIONS requests
|
void |
options(String path,
String acceptType,
Route route)
Map the route for HTTP OPTIONS requests
|
void |
options(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP OPTIONS requests
|
void |
options(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP OPTIONS requests
|
void |
options(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP OPTIONS requests
|
void |
patch(String path,
Route route)
Map the route for HTTP PATCH requests
|
void |
patch(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP PATCH requests
|
void |
patch(String path,
String acceptType,
Route route)
Map the route for HTTP PATCH requests
|
void |
patch(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP PATCH requests
|
void |
patch(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP PATCH requests
|
void |
patch(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP PATCH requests
|
void |
path(String path,
RouteGroup routeGroup)
Add a path-prefix to the routes declared in the routeGroup
The path() method adds a path-fragment to a path-stack, adds
routes from the routeGroup, then pops the path-fragment again.
|
int |
port()
Retrieves the port that Spark is listening on.
|
Service |
port(int port)
Set the port that Spark should listen on.
|
void |
post(String path,
Route route)
Map the route for HTTP POST requests
|
void |
post(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP POST requests
|
void |
post(String path,
String acceptType,
Route route)
Map the route for HTTP POST requests
|
void |
post(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP POST requests
|
void |
post(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP POST requests
|
void |
post(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP POST requests
|
void |
put(String path,
Route route)
Map the route for HTTP PUT requests
|
void |
put(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP PUT requests
|
void |
put(String path,
String acceptType,
Route route)
Map the route for HTTP PUT requests
|
void |
put(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP PUT requests
|
void |
put(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP PUT requests
|
void |
put(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP PUT requests
|
Service |
secure(String keystoreFile,
String keystorePassword,
String truststoreFile,
String truststorePassword)
Set the connection to be secure, using the specified keystore and
truststore.
|
Service |
secure(String keystoreFile,
String keystorePassword,
String truststoreFile,
String truststorePassword,
boolean needsClientCert)
Set the connection to be secure, using the specified keystore and
truststore.
|
Service |
staticFileLocation(String folder)
Sets the folder in classpath serving static files.
|
void |
stop()
Stops the Spark server and clears all routes
|
Service |
threadPool(int maxThreads)
Configures the embedded web server's thread pool.
|
Service |
threadPool(int maxThreads,
int minThreads,
int idleTimeoutMillis)
Configures the embedded web server's thread pool.
|
void |
trace(String path,
Route route)
Map the route for HTTP TRACE requests
|
void |
trace(String path,
Route route,
ResponseTransformer transformer)
Map the route for HTTP TRACE requests
|
void |
trace(String path,
String acceptType,
Route route)
Map the route for HTTP TRACE requests
|
void |
trace(String path,
String acceptType,
Route route,
ResponseTransformer transformer)
Map the route for HTTP TRACE requests
|
void |
trace(String path,
String acceptType,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP TRACE requests
|
void |
trace(String path,
TemplateViewRoute route,
TemplateEngine engine)
Map the route for HTTP TRACE requests
|
void |
webSocket(String path,
Class<?> handlerClass)
Maps the given path to the given WebSocket handler class.
|
void |
webSocket(String path,
Object handler)
Maps the given path to the given WebSocket handler instance.
|
Service |
webSocketIdleTimeoutMillis(int timeoutMillis)
Sets the max idle timeout in milliseconds for WebSocket connections.
|
public static final int SPARK_DEFAULT_PORT
protected static final String DEFAULT_ACCEPT_TYPE
protected boolean initialized
protected int port
protected String ipAddress
protected SslStores sslStores
protected Map<String,WebSocketHandlerWrapper> webSocketHandlers
protected int maxThreads
protected int minThreads
protected int threadIdleTimeoutMillis
protected EmbeddedServer server
protected Routes routes
public final Redirect redirect
public final Service.StaticFiles staticFiles
public static Service ignite()
public Service ipAddress(String ipAddress)
ipAddress
- The ipAddresspublic Service port(int port)
port
- The port numberpublic int port()
IllegalStateException
- when the server is not startedpublic Service secure(String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword)
keystoreFile
- The keystore file location as stringkeystorePassword
- the password for the keystoretruststoreFile
- the truststore file location as string, leave null to reuse
keystoretruststorePassword
- the trust store passwordpublic Service secure(String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword, boolean needsClientCert)
keystoreFile
- The keystore file location as stringkeystorePassword
- the password for the keystoretruststoreFile
- the truststore file location as string, leave null to reuse
keystoreneedsClientCert
- Whether to require client certificate to be supplied in
requesttruststorePassword
- the trust store passwordpublic Service threadPool(int maxThreads)
maxThreads
- max nbr of threads.public Service threadPool(int maxThreads, int minThreads, int idleTimeoutMillis)
maxThreads
- max nbr of threads.minThreads
- min nbr of threads.idleTimeoutMillis
- thread idle timeout (ms).public Service staticFileLocation(String folder)
folder
- the folder in classpath.public Service externalStaticFileLocation(String externalFolder)
externalFolder
- the external folder serving static files.public void webSocket(String path, Class<?> handlerClass)
This is currently only available in the embedded server mode.
path
- the WebSocket path.handlerClass
- the handler class that will manage the WebSocket connection to the given path.public void webSocket(String path, Object handler)
This is currently only available in the embedded server mode.
path
- the WebSocket path.handler
- the handler instance that will manage the WebSocket connection to the given path.public Service webSocketIdleTimeoutMillis(int timeoutMillis)
timeoutMillis
- The max idle timeout in milliseconds.public void notFound(String page)
page
- the custom 404 error page.public void internalServerError(String page)
page
- the custom 500 internal server error page.public void notFound(Route route)
public void internalServerError(Route route)
public void awaitInitialization()
public void stop()
public void path(String path, RouteGroup routeGroup)
path
- the path to prefix routes withrouteGroup
- group of routes (can also contain path() calls)public String getPaths()
public void addRoute(HttpMethod httpMethod, RouteImpl route)
httpMethod
- the HTTP methodroute
- the route implementationpublic void addFilter(HttpMethod httpMethod, FilterImpl filter)
httpMethod
- the HTTP methodfilter
- the route implementation@Deprecated public void addRoute(String httpMethod, RouteImpl route)
@Deprecated public void addFilter(String httpMethod, FilterImpl filter)
public void init()
public int activeThreadCount()
public <T extends Exception> void exception(Class<T> exceptionClass, ExceptionHandler<? super T> handler)
exceptionClass
- the exception classhandler
- The handlerpublic HaltException halt()
public HaltException halt(int status)
status
- the status codepublic HaltException halt(String body)
body
- The body contentpublic HaltException halt(int status, String body)
status
- The status codebody
- The body contentpublic void initExceptionHandler(Consumer<Exception> initExceptionHandler)
initExceptionHandler
- The custom init exception handlerpublic void get(String path, Route route)
path
- the pathroute
- The routepublic void post(String path, Route route)
path
- the pathroute
- The routepublic void put(String path, Route route)
path
- the pathroute
- The routepublic void patch(String path, Route route)
path
- the pathroute
- The routepublic void delete(String path, Route route)
path
- the pathroute
- The routepublic void head(String path, Route route)
path
- the pathroute
- The routepublic void trace(String path, Route route)
path
- the pathroute
- The routepublic void connect(String path, Route route)
path
- the pathroute
- The routepublic void options(String path, Route route)
path
- the pathroute
- The routepublic void before(String path, Filter filter)
path
- the pathfilter
- The filterpublic void after(String path, Filter filter)
path
- the pathfilter
- The filterpublic void get(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void post(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void put(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void patch(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void delete(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void head(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void trace(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void connect(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void options(String path, String acceptType, Route route)
path
- the pathacceptType
- the accept typeroute
- The routepublic void before(Filter filter)
filter
- The filterpublic void after(Filter filter)
filter
- The filterpublic void before(String path, String acceptType, Filter filter)
path
- the pathacceptType
- the accept typefilter
- The filterpublic void after(String path, String acceptType, Filter filter)
path
- the pathacceptType
- the accept typefilter
- The filterpublic void afterAfter(Filter filter)
filter
- The filterpublic void afterAfter(String path, Filter filter)
filter
- The filterpublic void get(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void get(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void post(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void post(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void put(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void put(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void delete(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void delete(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void patch(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void patch(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void head(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void head(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void trace(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void trace(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void connect(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void connect(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void options(String path, TemplateViewRoute route, TemplateEngine engine)
path
- the pathroute
- The routeengine
- the template enginepublic void options(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine)
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template enginepublic void get(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void get(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void post(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void post(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void put(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void put(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void delete(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void delete(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void head(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void head(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void connect(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void connect(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void trace(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void trace(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void options(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void options(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerpublic void patch(String path, Route route, ResponseTransformer transformer)
path
- the pathroute
- The routetransformer
- the response transformerpublic void patch(String path, String acceptType, Route route, ResponseTransformer transformer)
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformerCopyright © 2017. All rights reserved.