ConfigResource

The {@link oaj.microservice.resources.ConfigResource} class is a predefined reusable resource. It provides a REST interface for reading and altering the microservice config file.

Pointing a browser to the resource shows the following:

http://localhost:10000/config

An edit page is provided for altering the raw config file:

http://localhost:10000/config/edit

The {@link oaj.config.Config} class is a serializable POJO, which makes the resource relatively straightforward to implement.

ConfigResource.java

/** * Shows contents of the microservice configuration file. */ @Rest( path="/config", title="Configuration", description="Contents of configuration file.", htmldoc=@HtmlDoc( navlinks={ "up: request:/..", "options: servlet:/?method=OPTIONS", "edit: servlet:/edit" } ) ) public class ConfigResource extends BasicRestServlet { @RestMethod(name=GET, path="/", description="Show contents of config file.") public OMap getConfig() throws Exception { return getServletConfig().getConfig().asMap(); } @RestMethod(name=GET, path="/edit", description="Edit config file.") public Form getConfigEditForm(RestRequest req) throws Exception { return form().id("form").action("servlet:/").method("POST").enctype("application/x-www-form-urlencoded").children( div()._class("data").children( table( tr(td().style("text-align:right").children(button("submit","Submit"),button("reset","Reset"))), tr(th().child("Contents")), tr(th().child( textarea().name("contents").rows(40).cols(120).style("white-space:pre;word-wrap:normal;overflow-x:scroll;font-family:monospace;") .text(getServletConfig().getConfig().toString())) ) ) ) ); } @RestMethod(name=GET, path="/{section}", description="Show config file section.", swagger={ "parameters:[", "{name:'section',in:'path',description:'Section name.'}", "]" } ) public OMap getConfigSection(@Path("section") String section) throws Exception { return getSection(section); } @RestMethod(name=GET, path="/{section}/{key}", description="Show config file entry.", swagger={ "parameters:[", "{name:'section',in:'path',description:'Section name.'},", "{name:'key',in:'path',description:'Entry name.'}", "]" } ) public String getConfigEntry(@Path("section") String section, @Path("key") String key) throws Exception { return getSection(section).getString(key); } @RestMethod(name=POST, path="/", description="Sets contents of config file from a FORM post.", swagger={ "parameters:[", "{name:'contents',in:'formData',description:'New contents in INI file format.'}", "]" } ) public Config setConfigContentsFormPost(@FormData("contents") String contents) throws Exception { return setConfigContents(new StringReader(contents)); } @RestMethod(name=PUT, path="/", description="Sets contents of config file.", swagger={ "parameters:[", "{in:'body',description:'New contents in INI file format.'}", "]" } ) public Config setConfigContents(@Body Reader contents) throws Exception { return getServletConfig().getConfig().load(contents, true).asMap(); } @RestMethod(name=PUT, path="/{section}", description="Add or overwrite a config file section.", swagger={ "parameters:[", "{name:'section',in:'path',description:'Section name.'}", "{in:'body',description:'New contents for section as a simple map with string keys and values.'}", "]" } ) public OMap setConfigSection(@Path("section") String section, @Body Map<String,String> contents) throws Exception { getServletConfig().getConfig().setSection(section, contents); return getSection(section); } @RestMethod(name=PUT, path="/{section}/{key}", description="Add or overwrite a config file entry.", swagger={ "parameters:[", "{name:'section',in:'path',description:'Section name.'}", "{name:'key',in:'path',description:'Entry name.'}", "{in:'body',description:'New value as a string.'}", "]" } ) public String setConfigSection(@Path("section") String section, @Path("key") String key, @Body String value) throws Exception { getServletConfig().getConfig().put(section, key, value, false); return getSection(section).getString(key); } private OMap getSection(String name) { OMap m = getServletConfig().getConfig().getSectionMap(name); if (m == null) throw new RestException(SC_NOT_FOUND, "Section not found."); return m; } }