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 String |
externalStaticFileFolder |
protected boolean |
initialized |
protected String |
ipAddress |
protected int |
maxThreads |
protected int |
minThreads |
protected int |
port |
Redirect |
redirect |
protected Routes |
routes |
protected EmbeddedServer |
server |
static int |
SPARK_DEFAULT_PORT |
protected SslStores |
sslStores |
protected String |
staticFileFolder |
Service.StaticFiles |
staticFiles |
protected int |
threadIdleTimeoutMillis |
protected Map<String,Class<?>> |
webSocketHandlers |
protected Optional<Integer> |
webSocketIdleTimeoutMillis |
Modifier and Type | Method and Description |
---|---|
void |
addFilter(String httpMethod,
FilterImpl filter)
Adds a filter
|
void |
addRoute(String httpMethod,
RouteImpl route)
Adds a route
|
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 |
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
|
void |
exception(Class<? extends Exception> exceptionClass,
ExceptionHandler 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
|
void |
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
|
void |
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
|
void |
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
|
void |
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() |
Service |
ipAddress(String ipAddress)
Set the IP address that Spark should listen on.
|
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
|
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 |
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<?> handler)
Maps the given path to the given WebSocket handler.
|
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 String staticFileFolder
protected String externalStaticFileFolder
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 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 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<?> handler)
This is currently only available in the embedded server mode.
path
- the WebSocket path.handler
- the handler class that will manage the WebSocket connection to the given path.public Service webSocketIdleTimeoutMillis(int timeoutMillis)
timeoutMillis
- The max idle timeout in milliseconds.public void awaitInitialization()
public void stop()
public void addRoute(String httpMethod, RouteImpl route)
httpMethod
- the HTTP methodroute
- the route implementationpublic void addFilter(String httpMethod, FilterImpl filter)
httpMethod
- the HTTP methodfilter
- the route implementationpublic void init()
public void exception(Class<? extends Exception> exceptionClass, ExceptionHandler handler)
exceptionClass
- the exception classhandler
- The handlerpublic void halt()
public void halt(int status)
status
- the status codepublic void halt(String body)
body
- The body contentpublic void halt(int status, String body)
status
- The status codebody
- The body contentpublic 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 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 © 2016. All rights reserved.