Package spark
Class Spark
java.lang.Object
spark.Spark
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 ClassesModifier and TypeClassDescriptionprivate static class
Initializes singleton. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Redirect
Statically import this for redirect utility functionality, seeRedirect
static final Service.StaticFiles
Statically import this for static files utility functionality, seeService.StaticFiles
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
static void
Maps one or many filters to be executed after any matching routesstatic void
Maps a filter to be executed after any matching routesstatic void
Maps an array of filters to be executed after any matching routesstatic void
Maps one or many filters to be executed after any matching routesstatic void
afterAfter
(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
Waits for the spark server to be initialized.static void
Waits for the Spark server to be stopped.static void
Maps one or many filters to be executed before any matching routesstatic void
Maps a filter to be executed before any matching routesstatic void
Maps an array of filters to be executed before any matching routesstatic void
Maps one or many filters to be executed before any matching routesstatic void
Map the route for HTTP CONNECT requestsstatic void
connect
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP CONNECT requestsstatic void
connect
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP CONNECT requestsstatic void
Map the route for HTTP CONNECT requestsstatic void
connect
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP CONNECT requestsstatic void
connect
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP CONNECT requestsstatic void
defaultResponseTransformer
(ResponseTransformer transformer) Set the default response transformer.static void
Map the route for HTTP DELETE requestsstatic void
delete
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP DELETE requestsstatic void
delete
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP DELETE requestsstatic void
Map the route for HTTP DELETE requestsstatic void
delete
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP DELETE requestsstatic void
delete
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP DELETE requestsstatic <T extends Exception>
voidexception
(Class<T> exceptionClass, ExceptionHandler<? super T> handler) Maps an exception handler to be executed when an exception occurs during routingstatic void
externalStaticFileLocation
(String externalFolder) Sets the external folder serving static files.static void
Map the route for HTTP GET requestsstatic void
get
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP GET requestsstatic void
get
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP GET requestsstatic void
Map the route for HTTP GET requestsstatic void
get
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP GET requestsstatic void
get
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP GET requestsprivate static Service
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
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
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
Map the route for HTTP HEAD requestsstatic void
head
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP HEAD requestsstatic void
head
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP HEAD requestsstatic void
Map the route for HTTP HEAD requestsstatic void
head
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP HEAD requestsstatic void
head
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP HEAD requestsstatic void
init()
Initializes the Spark server.static void
initExceptionHandler
(Consumer<Exception> initExceptionHandler) Overrides default exception handler during initialization phasestatic void
internalServerError
(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
Set the IP address that Spark should listen on.static ModelAndView
modelAndView
(Object model, String viewName) Constructs a ModelAndView with the provided model and view namestatic void
Maps 404 Not Found errors to the provided custom pagestatic void
Maps 404 Not Found errors to the provided route.static void
Map the route for HTTP OPTIONS requestsstatic void
options
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP OPTIONS requestsstatic void
options
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP OPTIONS requestsstatic void
Map the route for HTTP OPTIONS requestsstatic void
options
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP OPTIONS requestsstatic void
options
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP OPTIONS requestsstatic void
Map the route for HTTP PATCH requestsstatic void
patch
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP PATCH requestsstatic void
patch
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP PATCH requestsstatic void
Map the route for HTTP PATCH requestsstatic void
patch
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP PATCH requestsstatic void
patch
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP PATCH requestsstatic 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.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
Map the route for HTTP POST requestsstatic void
post
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP POST requestsstatic void
post
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP POST requestsstatic void
Map the route for HTTP POST requestsstatic void
post
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP POST requestsstatic void
post
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP POST requestsstatic void
Map the route for HTTP PUT requestsstatic void
put
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP PUT requestsstatic void
put
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP PUT requestsstatic void
Map the route for HTTP PUT requestsstatic void
put
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP PUT requestsstatic void
put
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP PUT requestsstatic List
<RouteMatch> routes()
static void
secure
(String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword) Set the connection to be secure, using the specified keystore and truststore.static void
secure
(String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword, boolean needsClientCert) Set the connection to be secure, using the specified keystore and truststore.static void
secure
(String keystoreFile, String keystorePassword, String certAlias, String truststoreFile, String truststorePassword) Set the connection to be secure, using the specified keystore and truststore.static void
secure
(String keystoreFile, String keystorePassword, String certAlias, String truststoreFile, String truststorePassword, boolean needsClientCert) Set the connection to be secure, using the specified keystore and truststore.static void
setIpAddress
(String ipAddress) Deprecated.static void
setPort
(int port) Deprecated.replaced byport(int)
static void
setSecure
(String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword) Deprecated.replaced bysecure(String, String, String, String)
static void
staticFileLocation
(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
Map the route for HTTP TRACE requestsstatic void
trace
(String path, String acceptType, Route route, ResponseTransformer transformer) Map the route for HTTP TRACE requestsstatic void
trace
(String path, String acceptType, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP TRACE requestsstatic void
Map the route for HTTP TRACE requestsstatic void
trace
(String path, Route route, ResponseTransformer transformer) Map the route for HTTP TRACE requestsstatic void
trace
(String path, TemplateViewRoute route, TemplateEngine engine) Map the route for HTTP TRACE requestsstatic boolean
Unmaps a particular route from the collection of those that have been previously routed.static boolean
Unmaps a particular route from the collection of those that have been previously routed.static void
Maps the given path to the given WebSocket handler.static void
static void
webSocketIdleTimeoutMillis
(int timeoutMillis) Sets the max idle timeout in milliseconds for WebSocket connections.
-
Field Details
-
redirect
Statically import this for redirect utility functionality, seeRedirect
-
staticFiles
Statically import this for static files utility functionality, seeService.StaticFiles
-
-
Constructor Details
-
Spark
protected Spark()
-
-
Method Details
-
getInstance
-
path
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
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The route
-
post
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The route
-
put
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The route
-
patch
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The route
-
delete
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The route
-
head
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The route
-
trace
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The route
-
connect
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The route
-
options
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The route
-
before
Maps a filter to be executed before any matching routes- Parameters:
path
- the pathfilter
- The filter
-
before
Maps an array of filters to be executed before any matching routes- Parameters:
path
- the pathfilters
- the filters
-
after
Maps a filter to be executed after any matching routes- Parameters:
path
- the pathfilter
- The filter
-
after
Maps an array of filters to be executed after any matching routes- Parameters:
path
- the pathfilters
- The filters
-
get
Map the route for HTTP GET requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
post
Map the route for HTTP POST requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
put
Map the route for HTTP PUT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
patch
Map the route for HTTP PATCH requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
delete
Map the route for HTTP DELETE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
head
Map the route for HTTP HEAD requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
trace
Map the route for HTTP TRACE requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
connect
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
options
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathacceptType
- the accept typeroute
- The route
-
before
Maps one or many filters to be executed before any matching routes- Parameters:
filters
- The filters
-
after
Maps one or many filters to be executed after any matching routes- Parameters:
filters
- The filters
-
before
Maps one or many filters to be executed before any matching routes- Parameters:
path
- the pathacceptType
- the accept typefilters
- The filters
-
after
Maps one or many filters to be executed after any matching routes- Parameters:
path
- the pathacceptType
- the accept typefilters
- The filters
-
afterAfter
Execute after route even if the route throws exception- Parameters:
path
- the pathfilter
- the filter
-
afterAfter
Execute after any matching route even if the route throws exception- Parameters:
filter
- the filter
-
get
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
get
public static void get(String path, 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
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
post
public static void post(String path, 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
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
put
public static void put(String path, 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
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
delete
public static void delete(String path, 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
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
patch
public static void patch(String path, 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
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
head
public static void head(String path, 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
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
trace
public static void trace(String path, 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
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
connect
public static void connect(String path, 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
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The routeengine
- the template engine
-
options
public static void options(String path, 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
Map the route for HTTP GET requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
get
public static void get(String path, 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
Map the route for HTTP POST requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
post
public static void post(String path, 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
Map the route for HTTP PUT requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
put
public static void put(String path, 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
Map the route for HTTP DELETE requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
delete
public static void delete(String path, 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
Map the route for HTTP HEAD requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
head
public static void head(String path, 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
Map the route for HTTP CONNECT requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
connect
public static void connect(String path, 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
Map the route for HTTP TRACE requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
trace
public static void trace(String path, 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
Map the route for HTTP OPTIONS requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
options
public static void options(String path, 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
Map the route for HTTP PATCH requests- Parameters:
path
- the pathroute
- The routetransformer
- the response transformer
-
patch
public static void patch(String path, 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
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:
IllegalArgumentException
- if path is null or blank
-
unmap
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:
IllegalArgumentException
- if path is null or blank or if httpMethod is null, blank, or an invalid HTTP method
-
exception
public static <T extends Exception> void exception(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
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
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
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
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
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
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
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:
IllegalStateException
- when the server is not started
-
setSecure
public static void setSecure(String keystoreFile, String keystorePassword, String truststoreFile, 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(String keystoreFile, String keystorePassword, String truststoreFile, 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(String keystoreFile, String keystorePassword, String certAlias, String truststoreFile, 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
Overrides default exception handler during initialization phase- Parameters:
initExceptionHandler
- The custom init exception handler
-
secure
public static void secure(String keystoreFile, String keystorePassword, String truststoreFile, 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 keystoretruststorePassword
- the trust store passwordneedsClientCert
- Whether to require client certificate to be supplied in request
-
secure
public static void secure(String keystoreFile, String keystorePassword, String certAlias, String truststoreFile, 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 keystoretruststorePassword
- the trust store passwordneedsClientCert
- Whether to require client certificate to be supplied in request
-
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
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
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
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
-
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
Maps 404 Not Found errors to the provided custom page -
internalServerError
Maps 500 internal server errors to the provided custom page -
notFound
Maps 404 Not Found errors to the provided route. -
internalServerError
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
Constructs a ModelAndView with the provided model and view name- Parameters:
model
- the modelviewName
- the view name- Returns:
- the model and view
-
routes
- Returns:
- All routes available
-
activeThreadCount
public static int activeThreadCount()- Returns:
- The approximate number of currently active threads in the embedded Jetty server
-
ipAddress(String)