Package spark
Class Spark
- java.lang.Object
-
- spark.Spark
-
public class Spark extends java.lang.Object
The main building block of a Spark application is a set of routes. A route is made up of three simple pieces:- A verb (get, post, put, delete, head, trace, connect, options)
- A path (/hello, /users/:name)
- A callback (request, response)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Spark.SingletonHolder
Initializes singleton.
-
Field Summary
Fields Modifier and Type Field Description static Redirect
redirect
Statically import this for redirect utility functionality, seeRedirect
static Service.StaticFiles
staticFiles
Statically import this for static files utility functionality, seeService.StaticFiles
-
Constructor Summary
Constructors Modifier Constructor Description protected
Spark()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static int
activeThreadCount()
static void
after(java.lang.String path, java.lang.String acceptType, Filter... filters)
Maps one or many filters to be executed after any matching routesstatic void
after(java.lang.String path, Filter filter)
Maps a filter to be executed after any matching routesstatic void
after(java.lang.String path, Filter... filters)
Maps an array of filters to be executed after any matching routesstatic void
after(Filter... filters)
Maps one or many filters to be executed after any matching routesstatic void
afterAfter(java.lang.String path, Filter filter)
Execute after route even if the route throws exceptionstatic void
afterAfter(Filter filter)
Execute after any matching route even if the route throws exceptionstatic void
awaitInitialization()
Waits for the spark server to be initialized.static void
awaitStop()
Waits for the Spark server to be stopped.static void
before(java.lang.String path, java.lang.String acceptType, Filter... filters)
Maps one or many filters to be executed before any matching routesstatic void
before(java.lang.String path, Filter filter)
Maps a filter to be executed before any matching routesstatic void
before(java.lang.String path, Filter... filters)
Maps an array of filters to be executed before any matching routesstatic void
before(Filter... filters)
Maps one or many filters to be executed before any matching routesstatic void
connect(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP CONNECT requestsstatic void
connect(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP CONNECT requestsstatic void
connect(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP CONNECT requestsstatic void
connect(java.lang.String path, Route route)
Map the route for HTTP CONNECT requestsstatic void
connect(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP CONNECT requestsstatic void
connect(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP CONNECT requestsstatic void
defaultResponseTransformer(ResponseTransformer transformer)
Set the default response transformer.static void
delete(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP DELETE requestsstatic void
delete(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP DELETE requestsstatic void
delete(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP DELETE requestsstatic void
delete(java.lang.String path, Route route)
Map the route for HTTP DELETE requestsstatic void
delete(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP DELETE requestsstatic void
delete(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP DELETE requestsstatic <T extends java.lang.Exception>
voidexception(java.lang.Class<T> exceptionClass, ExceptionHandler<? super T> handler)
Maps an exception handler to be executed when an exception occurs during routingstatic void
externalStaticFileLocation(java.lang.String externalFolder)
Sets the external folder serving static files.static void
get(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP GET requestsstatic void
get(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP GET requestsstatic void
get(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP GET requestsstatic void
get(java.lang.String path, Route route)
Map the route for HTTP GET requestsstatic void
get(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP GET requestsstatic void
get(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP GET requestsprivate static Service
getInstance()
static 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 workstatic 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 workstatic HaltException
halt(int status, java.lang.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 workstatic HaltException
halt(java.lang.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 workstatic void
head(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP HEAD requestsstatic void
head(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP HEAD requestsstatic void
head(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP HEAD requestsstatic void
head(java.lang.String path, Route route)
Map the route for HTTP HEAD requestsstatic void
head(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP HEAD requestsstatic void
head(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP HEAD requestsstatic void
init()
Initializes the Spark server.static void
initExceptionHandler(java.util.function.Consumer<java.lang.Exception> initExceptionHandler)
Overrides default exception handler during initialization phasestatic void
internalServerError(java.lang.String page)
Maps 500 internal server errors to the provided custom pagestatic void
internalServerError(Route route)
Maps 500 internal server errors to the provided route.static void
ipAddress(java.lang.String ipAddress)
Set the IP address that Spark should listen on.static ModelAndView
modelAndView(java.lang.Object model, java.lang.String viewName)
Constructs a ModelAndView with the provided model and view namestatic void
notFound(java.lang.String page)
Maps 404 Not Found errors to the provided custom pagestatic void
notFound(Route route)
Maps 404 Not Found errors to the provided route.static void
options(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP OPTIONS requestsstatic void
options(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP OPTIONS requestsstatic void
options(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP OPTIONS requestsstatic void
options(java.lang.String path, Route route)
Map the route for HTTP OPTIONS requestsstatic void
options(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP OPTIONS requestsstatic void
options(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP OPTIONS requestsstatic void
patch(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP PATCH requestsstatic void
patch(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP PATCH requestsstatic void
patch(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PATCH requestsstatic void
patch(java.lang.String path, Route route)
Map the route for HTTP PATCH requestsstatic void
patch(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP PATCH requestsstatic void
patch(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PATCH requestsstatic void
path(java.lang.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.static int
port()
Retrieves the port that Spark is listening on.static void
port(int port)
Set the port that Spark should listen on.static void
post(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP POST requestsstatic void
post(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP POST requestsstatic void
post(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP POST requestsstatic void
post(java.lang.String path, Route route)
Map the route for HTTP POST requestsstatic void
post(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP POST requestsstatic void
post(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP POST requestsstatic void
put(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP PUT requestsstatic void
put(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP PUT requestsstatic void
put(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PUT requestsstatic void
put(java.lang.String path, Route route)
Map the route for HTTP PUT requestsstatic void
put(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP PUT requestsstatic void
put(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PUT requestsstatic java.util.List<RouteMatch>
routes()
static void
secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
Set the connection to be secure, using the specified keystore and truststore.static void
secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
Set the connection to be secure, using the specified keystore and truststore.static void
secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword)
Set the connection to be secure, using the specified keystore and truststore.static void
secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
Set the connection to be secure, using the specified keystore and truststore.static void
setIpAddress(java.lang.String ipAddress)
Deprecated.replaced byipAddress(String)
static void
setPort(int port)
Deprecated.replaced byport(int)
static void
setSecure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
Deprecated.replaced bysecure(String, String, String, String)
static void
staticFileLocation(java.lang.String folder)
Sets the folder in classpath serving static files.static void
stop()
Stops the Spark server and clears all routesstatic void
threadPool(int maxThreads)
Configures the embedded web server's thread pool.static void
threadPool(int maxThreads, int minThreads, int idleTimeoutMillis)
Configures the embedded web server's thread pool.static void
trace(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP TRACE requestsstatic void
trace(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP TRACE requestsstatic void
trace(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP TRACE requestsstatic void
trace(java.lang.String path, Route route)
Map the route for HTTP TRACE requestsstatic void
trace(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP TRACE requestsstatic void
trace(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP TRACE requestsstatic boolean
unmap(java.lang.String path)
Unmaps a particular route from the collection of those that have been previously routed.static boolean
unmap(java.lang.String path, java.lang.String httpMethod)
Unmaps a particular route from the collection of those that have been previously routed.static void
webSocket(java.lang.String path, java.lang.Class<?> handler)
Maps the given path to the given WebSocket handler.static void
webSocket(java.lang.String path, java.lang.Object handler)
static void
webSocketIdleTimeoutMillis(int timeoutMillis)
Sets the max idle timeout in milliseconds for WebSocket connections.
-
-
-
Field Detail
-
redirect
public static final Redirect redirect
Statically import this for redirect utility functionality, seeRedirect
-
staticFiles
public static final Service.StaticFiles staticFiles
Statically import this for static files utility functionality, seeService.StaticFiles
-
-
Method Detail
-
getInstance
private static Service getInstance()
-
path
public static void path(java.lang.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. It's used for separating routes into groups, for example: path("/api/email", () -> { ....post("/add", EmailApi::addEmail); ....put("/change", EmailApi::changeEmail); ....etc }); Multiple path() calls can be nested.- Parameters:
path
- the path to prefix routes withrouteGroup
- group of routes (can also contain path() calls)
-
get
public static void get(java.lang.String path, Route route)
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The route
-
post
public static void post(java.lang.String path, Route route)
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The route
-
put
public static void put(java.lang.String path, Route route)
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The route
-
patch
public static void patch(java.lang.String path, Route route)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The route
-
delete
public static void delete(java.lang.String path, Route route)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The route
-
head
public static void head(java.lang.String path, Route route)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The route
-
trace
public static void trace(java.lang.String path, Route route)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The route
-
connect
public static void connect(java.lang.String path, Route route)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The route
-
options
public static void options(java.lang.String path, Route route)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The route
-
before
public static void before(java.lang.String path, Filter filter)
Maps a filter to be executed before any matching routes- Parameters:
path
- the pathfilter
- The filter
-
before
public static void before(java.lang.String path, Filter... filters)
Maps an array of filters to be executed before any matching routes- Parameters:
path
- the pathfilters
- the filters
-
after
public static void after(java.lang.String path, Filter filter)
Maps a filter to be executed after any matching routes- Parameters:
path
- the pathfilter
- The filter
-
after
public static void after(java.lang.String path, Filter... filters)
Maps an array of filters to be executed after any matching routes- Parameters:
path
- the pathfilters
- The filters
-
get
public static void get(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP GET requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
post
public static void post(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP POST requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
put
public static void put(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP PUT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
patch
public static void patch(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
delete
public static void delete(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
head
public static void head(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
trace
public static void trace(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
connect
public static void connect(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
options
public static void options(java.lang.String path, java.lang.String acceptType, Route route)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
before
public static void before(Filter... filters)
Maps one or many filters to be executed before any matching routes- Parameters:
filters
- The filters
-
after
public static void after(Filter... filters)
Maps one or many filters to be executed after any matching routes- Parameters:
filters
- The filters
-
before
public static void before(java.lang.String path, java.lang.String acceptType, Filter... filters)
Maps one or many filters to be executed before any matching routes- Parameters:
path
- the pathacceptType
- the accept typefilters
- The filters
-
after
public static void after(java.lang.String path, java.lang.String acceptType, Filter... filters)
Maps one or many filters to be executed after any matching routes- Parameters:
path
- the pathacceptType
- the accept typefilters
- The filters
-
afterAfter
public static void afterAfter(java.lang.String path, Filter filter)
Execute after route even if the route throws exception- Parameters:
path
- the pathfilter
- the filter
-
afterAfter
public static void afterAfter(Filter filter)
Execute after any matching route even if the route throws exception- Parameters:
filter
- the filter
-
get
public static void get(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
get
public static void get(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP GET requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
post
public static void post(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
post
public static void post(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP POST requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
put
public static void put(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
put
public static void put(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PUT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
delete
public static void delete(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
delete
public static void delete(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
patch
public static void patch(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
patch
public static void patch(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
head
public static void head(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
head
public static void head(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
trace
public static void trace(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
trace
public static void trace(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
connect
public static void connect(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
connect
public static void connect(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
options
public static void options(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
options
public static void options(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routeengine
- the template engine
-
get
public static void get(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
get
public static void get(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP GET requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
post
public static void post(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
post
public static void post(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP POST requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
put
public static void put(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
put
public static void put(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP PUT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
delete
public static void delete(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
delete
public static void delete(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP DELETE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
head
public static void head(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
head
public static void head(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP HEAD requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
connect
public static void connect(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
connect
public static void connect(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
trace
public static void trace(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
trace
public static void trace(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP TRACE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
options
public static void options(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
options
public static void options(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
patch
public static void patch(java.lang.String path, Route route, ResponseTransformer transformer)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
patch
public static void patch(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
Map the route for HTTP PATCH requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The routetransformer
- the response transformer
-
unmap
public static boolean unmap(java.lang.String path)
Unmaps a particular route from the collection of those that have been previously routed. Search for previously established routes using the given path and unmaps any matches that are found.- Parameters:
path
- the route path- Returns:
- true if this is a matching route which has been previously routed
- Throws:
java.lang.IllegalArgumentException
- if path is null or blank
-
unmap
public static boolean unmap(java.lang.String path, java.lang.String httpMethod)
Unmaps a particular route from the collection of those that have been previously routed. Search for previously established routes using the given path and HTTP method, unmaps any matches that are found.- Parameters:
path
- the route pathhttpMethod
- the http method- Returns:
- true if this is a matching route that has been previously routed
- Throws:
java.lang.IllegalArgumentException
- if path is null or blank or if httpMethod is null, blank, or an invalid HTTP method
-
exception
public static <T extends java.lang.Exception> void exception(java.lang.Class<T> exceptionClass, ExceptionHandler<? super T> handler)
Maps an exception handler to be executed when an exception occurs during routing- Parameters:
exceptionClass
- the exception classhandler
- The handler
-
halt
public static 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
-
halt
public static 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- Parameters:
status
- the status code
-
halt
public static HaltException halt(java.lang.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- Parameters:
body
- The body content
-
halt
public static HaltException halt(int status, java.lang.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- Parameters:
status
- The status codebody
- The body content
-
setIpAddress
public static void setIpAddress(java.lang.String ipAddress)
Deprecated.replaced byipAddress(String)
Set the IP address that Spark should listen on. If not called the default address is '0.0.0.0'. This has to be called before any route mapping is done.- Parameters:
ipAddress
- The ipAddress
-
ipAddress
public static void ipAddress(java.lang.String ipAddress)
Set the IP address that Spark should listen on. If not called the default address is '0.0.0.0'. This has to be called before any route mapping is done.- Parameters:
ipAddress
- The ipAddress
-
defaultResponseTransformer
public static void defaultResponseTransformer(ResponseTransformer transformer)
Set the default response transformer. All requests not using a custom transformer will use this one- Parameters:
transformer
-
-
setPort
public static void setPort(int port)
Deprecated.replaced byport(int)
Set the port that Spark should listen on. If not called the default port is 4567. This has to be called before any route mapping is done. If provided port = 0 then the an arbitrary available port will be used.- Parameters:
port
- The port number
-
port
public static void port(int port)
Set the port that Spark should listen on. If not called the default port is 4567. This has to be called before any route mapping is done. If provided port = 0 then the an arbitrary available port will be used.- Parameters:
port
- The port number
-
port
public static int port()
Retrieves the port that Spark is listening on.- Returns:
- The port Spark server is listening on.
- Throws:
java.lang.IllegalStateException
- when the server is not started
-
setSecure
public static void setSecure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
Deprecated.replaced bysecure(String, String, String, String)
Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container- Parameters:
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 password
-
secure
public static void secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container- Parameters:
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 password
-
secure
public static void secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword)
Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container- Parameters:
keystoreFile
- The keystore file location as stringkeystorePassword
- the password for the keystorecertAlias
- the default certificate AliastruststoreFile
- the truststore file location as string, leave null to reuse keystoretruststorePassword
- the trust store password
-
initExceptionHandler
public static void initExceptionHandler(java.util.function.Consumer<java.lang.Exception> initExceptionHandler)
Overrides default exception handler during initialization phase- Parameters:
initExceptionHandler
- The custom init exception handler
-
secure
public static void secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container- Parameters:
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 password
-
secure
public static void secure(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container- Parameters:
keystoreFile
- The keystore file location as stringkeystorePassword
- the password for the keystorecertAlias
- the default certificate AliastruststoreFile
- the truststore file location as string, leave null to reuse keystoreneedsClientCert
- Whether to require client certificate to be supplied in requesttruststorePassword
- the trust store password
-
threadPool
public static void threadPool(int maxThreads)
Configures the embedded web server's thread pool.- Parameters:
maxThreads
- max nbr of threads.
-
threadPool
public static void threadPool(int maxThreads, int minThreads, int idleTimeoutMillis)
Configures the embedded web server's thread pool.- Parameters:
maxThreads
- max nbr of threads.minThreads
- min nbr of threads.idleTimeoutMillis
- thread idle timeout (ms).
-
staticFileLocation
public static void staticFileLocation(java.lang.String folder)
Sets the folder in classpath serving static files. Observe: this method must be called before all other methods. - Note: contemplate changing tonew static files paradigmService.StaticFiles
- Parameters:
folder
- the folder in classpath.
-
externalStaticFileLocation
public static void externalStaticFileLocation(java.lang.String externalFolder)
Sets the external folder serving static files. Observe: this method must be called before all other methods. - Note: contemplate use of new static files paradigmService.StaticFiles
- Parameters:
externalFolder
- the external folder serving static files.
-
awaitInitialization
public static void awaitInitialization()
Waits for the spark server to be initialized. If it's already initialized will return immediately
-
stop
public static void stop()
Stops the Spark server and clears all routes
-
awaitStop
public static void awaitStop()
Waits for the Spark server to be stopped. If it's already stopped, will return immediately.
-
webSocket
public static void webSocket(java.lang.String path, java.lang.Class<?> handler)
Maps the given path to the given WebSocket handler.This is currently only available in the embedded server mode.
- Parameters:
path
- the WebSocket path.handler
- the handler class that will manage the WebSocket connection to the given path.
-
webSocket
public static void webSocket(java.lang.String path, java.lang.Object handler)
-
webSocketIdleTimeoutMillis
public static void webSocketIdleTimeoutMillis(int timeoutMillis)
Sets the max idle timeout in milliseconds for WebSocket connections.- Parameters:
timeoutMillis
- The max idle timeout in milliseconds.
-
notFound
public static void notFound(java.lang.String page)
Maps 404 Not Found errors to the provided custom page
-
internalServerError
public static void internalServerError(java.lang.String page)
Maps 500 internal server errors to the provided custom page
-
notFound
public static void notFound(Route route)
Maps 404 Not Found errors to the provided route.
-
internalServerError
public static void internalServerError(Route route)
Maps 500 internal server errors to the provided route.
-
init
public static void init()
Initializes the Spark server. SHOULD just be used when using the Websockets functionality.
-
modelAndView
public static ModelAndView modelAndView(java.lang.Object model, java.lang.String viewName)
Constructs a ModelAndView with the provided model and view name- Parameters:
model
- the modelviewName
- the view name- Returns:
- the model and view
-
routes
public static java.util.List<RouteMatch> routes()
- Returns:
- All routes available
-
activeThreadCount
public static int activeThreadCount()
- Returns:
- The approximate number of currently active threads in the embedded Jetty server
-
-