public class XatkitServer extends Object
The XatkitServer provides a simple REST API that accepts POST methods on port 5000. Incoming
requests are parsed and sent to the registered WebhookEventProviders, that transform the
original request into EventInstances that can be used to trigger actions.
| Constructor and Description |
|---|
XatkitServer(Configuration configuration)
Constructs a new
XatkitServer with the given Configuration. |
| Modifier and Type | Method and Description |
|---|---|
void |
clearRegisteredRestHandlers()
Clears the registered
RestHandlers. |
File |
createOrReplacePublicFile(XatkitSession session,
String path,
byte[] content) |
File |
createOrReplacePublicFile(XatkitSession session,
String path,
File origin)
Creates a publicly accessible file from the provided
session, path, and origin. |
File |
createOrReplacePublicFile(XatkitSession session,
String path,
String content)
Creates a publicly accessible file from the provided
session, path, and content. |
String |
getBaseURL()
Returns the URL of the
XatkitServer. |
protected org.apache.http.impl.bootstrap.HttpServer |
getHttpServer()
Returns the underlying
HttpServer used to receive requests. |
int |
getPort()
Returns the port the server is listening to.
|
File |
getPublicFile(String filePath)
Retrieves the public
File associated to the provided filePath. |
File |
getPublicFile(XatkitSession session,
String filePath)
|
String |
getPublicURL(File file)
Retrieves the public URL associated to the provided
file if it exists. |
RestHandler |
getRegisteredRestHandler(HttpMethod httpMethod,
String uri)
|
Collection<RestHandler> |
getRegisteredRestHandlers()
Returns a
Collection containing the registered RestHandlers. |
boolean |
isRestEndpoint(HttpMethod httpMethod,
String uri)
Returns whether the provided
httpMethod on the given uri is associated to a REST endpoint. |
boolean |
isStarted()
|
Object |
notifyRestHandler(HttpMethod httpMethod,
String uri,
List<org.apache.http.Header> headers,
List<org.apache.http.NameValuePair> params,
Object content,
String contentType)
Notifies the REST endpoint associated with the provided
uri. |
void |
registerRestEndpoint(HttpMethod httpMethod,
String uri,
RestHandler handler)
Registers the provided
handler as a REST endpoint for the provided uri. |
void |
registerWebhookEventProvider(WebhookEventProvider webhookEventProvider)
Register a
WebhookEventProvider. |
void |
start()
Starts the underlying
HttpServer. |
void |
stop()
Stops the underlying
HttpServer. |
void |
unregisterWebhookEventProvider(WebhookEventProvider webhookEventProvider)
Unregistered a
WebhookEventProvider. |
public XatkitServer(Configuration configuration)
XatkitServer with the given Configuration.
The provided Configuration is used to specify the port the server should listen to (see
XatkitServerUtils.SERVER_PORT_KEY). If the Configuration does not specify a port the default
value (5000) is used.
Note: this method does not start the underlying HttpServer. Use start() to start the
HttpServer in a dedicated thread.
configuration - the Configuration used to initialize the XatkitServerNullPointerException - if the provided configuration is nullstart(),
stop()public int getPort()
public String getBaseURL()
XatkitServer.
This URL contains the base address and port (<address:port>), and can be used to access the
XatkitServer handlers.
XatkitServerprotected org.apache.http.impl.bootstrap.HttpServer getHttpServer()
HttpServer used to receive requests.
Note: this method is protected for testing purposes, and should not be called by client code.
HttpServer used to receive requestspublic boolean isStarted()
true if the XatkitServer is started, false otherwisepublic void start()
HttpServer.
This method registered a shutdown hook that is used to close the HttpServer when the application
terminates. To manually close the underlying HttpServer see stop().
public void stop()
HttpServer.public void registerRestEndpoint(HttpMethod httpMethod, String uri, RestHandler handler)
handler as a REST endpoint for the provided uri.
The provided handler will receive the HTTP requests that are sent to the provided uri through
the notifyRestHandler(HttpMethod, String, List, List, Object, String) method.
Note: the provided uri must start with a leading /.
httpMethod - the Http method of the REST endpointuri - the URI of the REST endpointhandler - the JsonRestHandler to associate to the REST endpointNullPointerException - if the provided uri or handler is nullIllegalArgumentException - if the provided uri does not start with a leading /public boolean isRestEndpoint(HttpMethod httpMethod, String uri)
httpMethod on the given uri is associated to a REST endpoint.httpMethod - the Http method of the REST endpoint to checkuri - the URI of the REST endpoint to checktrue if there is a REST endpoint associated to the provided uri, false otherwiseNullPointerException - if the provided uri is null@Nullable public RestHandler getRegisteredRestHandler(HttpMethod httpMethod, String uri)
httpMethod - the Http method to retrieve the handler foruri - the URI to retrieve the handler forRestHandler if it exists, null otherwisepublic Collection<RestHandler> getRegisteredRestHandlers()
Collection containing the registered RestHandlers.Collection containing the registered RestHandlerspublic void clearRegisteredRestHandlers()
RestHandlers.
This method only removes the uri -> handler bindings, and does not ensure the the RestHandlers
have been properly stopped.
public Object notifyRestHandler(HttpMethod httpMethod, String uri, List<org.apache.http.Header> headers, List<org.apache.http.NameValuePair> params, @Nullable Object content, String contentType) throws RestHandlerException
uri.httpMethod - the Http method of the REST endpoint to notifyuri - the URI of the REST endpoint to notifyheaders - the HTTP Headers of the request sent to the endpointparams - the HTTP parameters of the request sent to the endpointcontent - the JsonElement representing the content of the request sent to the endpointJsonElement returned by the endpoint, or null if the endpoint does not return
anythingNullPointerException - if the provided uri, header, or params is nullXatkitException - if there is no REST endpoint registered for the provided uriRestHandlerException - if an error occurred when processing the RestHandler's logicregisterRestEndpoint(HttpMethod, String, RestHandler)public void registerWebhookEventProvider(WebhookEventProvider webhookEventProvider)
WebhookEventProvider.
The registered webhookEventProvider's handler will be notified when a new request is received. If the
provider's handler supports the request content type (see RestHandler.acceptContentType(String), it will
receive the request content that will be used to create the associated EventInstance.
webhookEventProvider - the WebhookEventProvider to registerNullPointerException - if the provided webhookEventProvider is null(HttpMethod, String, List, List, Object, String),
RestHandler.acceptContentType(String),
RestHandler.handleContent(List, List, Object)public void unregisterWebhookEventProvider(WebhookEventProvider webhookEventProvider)
WebhookEventProvider.
The provided webhookEventProvider will not be notified when new request are received, and cannot be
used to create EventInstances.
webhookEventProvider - the WebhookEventProvider to unregisterNullPointerException - if the provided webhookEventProvider is nullpublic File createOrReplacePublicFile(XatkitSession session, String path, File origin)
session, path, and origin.
The created file can be accessed through <baseURL/content/sessionId/path>, and contains the content of
the provided origin File.
session - the XatkitSession used to create the filepath - the path of the file to createorigin - the origin file to copyFileNullPointerException - if the provided session, path, or origin is nullXatkitException - if an error occurred when reading the provided origin File, or if
the existing file at the given path cannot be deleted or if the provided
path refers to an illegal locationcreateOrReplacePublicFile(XatkitSession, String, String)public File createOrReplacePublicFile(XatkitSession session, String path, String content)
session, path, and content.
The created file can be accessed through <baseURL/content/sessionId/path, and contains the provided
content.
session - the XatkitSession used to create the filepath - the the path of the file to createcontent - the content of the file to createFileNullPointerException - if the provided session or path is nullXatkitException - if an error occurred when computing the provided path, or if the existing
file at the given path cannot be deleted, or if the provided path
refers to an illegal locationpublic File createOrReplacePublicFile(XatkitSession session, String path, byte[] content)
@Nullable public String getPublicURL(File file)
file if it exists.
This method is typically called to retrieve the public URL of a file created with
createOrReplacePublicFile(XatkitSession, String, String).
file - the File to retrieve the public URL fornull otherwiseNullPointerException - if the provided file is nullXatkitException - if the provided file corresponds to an illegal location@Nullable public File getPublicFile(XatkitSession session, String filePath)
session - the XatkitSession to retrieve the File fromfilePath - the path of the File to retrieveFile if it exists, null otherwiseNullPointerException - if the provided session if filePath is nullXatkitException - if the provided filePath refers to an illegal locationgetPublicFile(String)@Nullable public File getPublicFile(String filePath)
File associated to the provided filePath.
This method expects a full relative path under the /content/ location. This means that File
associated to XatkitSessions must be retrieved with the following path: sessionId/path. Check
getPublicFile(XatkitSession, String) to easily retrieve a File associated to a
XatkitSession.
filePath - the path of the File to retrieveFile if it exists, null otherwiseNullPointerException - if the provided filePath is nullXatkitException - if the provided filePath refers to an illegal locationgetPublicFile(XatkitSession, String)Copyright © 2020 SOM Research Lab. All rights reserved.