-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
Using Arcilla is quite simple, and works out of the box with just a few calls.
How to Create the Http or Https Server
To create the server you just need to create a new instance of either Arcilla or SecureArcilla specifying, if required, the port or ip to bind to. If not specified it will use the default values (localhost, and port 80 or 443 depending on the protocol).
Example of Arcilla server with specific port and IP
Arcilla arcillaServer = new Arcilla(InetAddress.getLoopbackAddress(), 80));
After having created the server, and before you actually start it, you should bind all the REST endpoints you want to serve.
How to bind a REST endpoint
After you have created the Arcilla instance, you can easily set up rest end point by calling the addRestEndpointRequestHandler method, which will receive the REST path (without the domain) and a request handler which will receive the petitions for that endpoint with all the needed data to generate a response.
Example of rest binding
arcillaServer.addRestEndpointRequestHandler("/server/list", new ServerListHandler());
How to correctly process requests
The RequestHandler interface received by the addRestEndpointRequestHandler method only enforces implementing one method, which receives a RequestInformation object as parameter, from where you can fetch all the information related to the request, including the URI, request body, attributes, etc. and returning an HttpResponse which is a plain object only consisting of a response code (Standard http codes apply here, meaning 200 for ok, 50X for server errors, etc.) and a response body, which is the string to be printed as output to the caller, and can be empty.
By default, if you build an HttpResponse object with no parameters, it will be generated with an empty body and a return code vale of 200, which is the minimum viable HttpResponse you can generate for success.
Example of simple RequestHandler for the /server/list REST endpoint used in the previous example.
public class ServerListHandler implements RequestHandler {
public HttpResponse processRequest(RequestInformation requestInformation) {
// Populate the list
List<String> serverListUrls = <...>;
// For example, encode the list as Json to generate the response body string
String responseBody = new Gson().toJson(serverListUrls);
return new HttpResponse(200, responseBody);
}
}