Contexts

The controller

essential is designed to work in every Java environment. Therefor, it provides a simple and straightforward facade which a context implementation can program against. This facade is called controller. All that is needed to initialize a controller, is the package path where the resource implementations can be found.

Controller controller = Controller.getInstance("my.resources");

A controller can perform requests providing inputs in a natural way

controller.perform(httpMethod, urlInfoPart, requestHeaders, requestParameters, requestBody, responseBody);

or by providing an implementation of the Request and Response interface

controller.perform(request, response);

A context implementation provides exactly that: An implementation of the Request and Response interface. It can be seen as a translator for the context specific representation of an HTTP request to a harmonized essential-specific representation.

Premade contexts

Obviously, it is not practical to let each developer implementing a context for his/her environment. That is why one goal of the project is to provide premade contexts for at least the majority of used environments.

Currently essential provides the following context implementations:

Creating contexts

Creating own contexts starts with implementing the following two interfaces:

  • net.craftforge.essential.controller.Request
  • net.craftforge.essential.controller.Response

After that, you can assembly your request and response from the data provided by your context and call

  • controller.perform(request, response);

essential can throw a ControllerException which must be handled by the context implementer. A ControllerException contains the HTTP status code best fitting the exception. E.g. in case of a path that does not match a resource implementation, the status code 404 is assigned to the ControllerException.

Creating new context implementations, I really want to encourage you to provide these to the community, so they can be used by other developers as well.