Config File API

There are 3 primary ways of getting access to the config file.

- {@link oaj.microservice.Microservice#getConfig()}
  Any {@doc DefaultRestSvlVariables initialization-time variables} can be used.
- {@link oajr.RestContext#getConfig()}
  Any {@doc DefaultRestSvlVariables initialization-time variables} can be used.

  ##### Example usage:

  #------------------------------- # Properties for MyHelloResource #------------------------------- [MyHelloResource] greeting = Hello world!

  @RestResource(...) public class MyHelloResource extends BasicRestServlet { // Access config file when initializing fields. private String greeting = getConfig().getString("MyHelloResource/greeting"); // Or access config file in servlet init method. @Override /* Servlet */ public void init() { String greeting = getConfig().getString("MyHelloResource/greeting"); } }

  Additional user-defined variables can be defined at this level by adding a {@link oajr.annotation.HookEvent#INIT} hook method and using the {@link oajr.RestContextBuilder#vars(Class...)} method.

- {@link oajr.RestRequest#getConfig()} - An instance method to access it from inside a REST method.
  Any {@doc DefaultRestSvlVariables initialization-time or request-time variables} can be used.

  ##### Example usage:

  #----------------------------- # Contents of microservice.cfg #----------------------------- [MyHelloResource] greeting = Hello $RP{person}! localizedGreeting = $L{HelloMessage,$RP{person}}

  #--------------------------------- # Contents of MyHelloResource.java #--------------------------------- @RestResource( path="/hello", messages="nls/Messages", ... ) public class MyHelloResource extends BasicRestServlet { /** Standard hello message. */ @RestMethod(name=GET, path="/{person}") public String sayHello(RestRequest req) { return req.getConfig().getString("MyHelloResource/greeting"); } /** Hello message in users language. */ @RestMethod(name=GET, path="/localized/{person}") public String sayLocalizedHello(RestRequest req) { return req.getConfig().getString("MyHelloResource/localizedGreeting"); } }

  #--------------------------------------- # Contents of nls/Messages_en.properties #--------------------------------------- MyHelloResource.HelloMessage = Hello {0}!

  Additional user-defined variables can be defined at this level by overriding the {@link oajr.RestContextBuilder#vars(Class...)} method.

That sayLocalizedHello() example might need some explanation since there's a lot going on there.
Here's what happens when an HTTP call is made to GET /hello/localized/Bob:

1. The HTTP call matches the /hello path on the MyHelloResource class.
2. The HTTP call matches the /localized/{person} path on the sayLocalizedHello() method.
3. The request attribute person gets assigned the value "Bob".
4. The call to req.getConfig().getString("MyHelloResource/localizedGreeting") finds the value "$L{HelloMessage,$RP{person}}".
5. The arguments in the $L{} variable get resolved, resulting in "$L{HelloMessage,Bob}".
6. The $L{} variable gets resolved to the message "Hello {0}!" in the localized properties file of the servlet based on the Accept-Language header on the request.
7. The arguments get replaced in the message resulting in "Hello Bob!".
8. The resulting message "Hello Bob!" is returned as a POJO to be serialized to whatever content type was specified on the Accept header on the request.

This particular example is needlessly complex, but it gives an idea of how variables can be used recursively to produce sophisticated results