| <!-- |
| /*************************************************************************************************************************** |
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations under the License. |
| ***************************************************************************************************************************/ |
| --> |
| |
| ConfigResource |
| |
| <p> |
| 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. |
| </p> |
| <p> |
| Pointing a browser to the resource shows the following: |
| </p> |
| <p class='bpcode w800'> |
| http://localhost:10000/config |
| </p> |
| <img class='bordered w800' src='doc-files/juneau-examples-rest.ConfigResource.1.png'> |
| <p> |
| An edit page is provided for altering the raw config file: |
| </p> |
| <p class='bpcode w800'> |
| http://localhost:10000/config/edit |
| </p> |
| <img class='bordered w800' src='doc-files/juneau-examples-rest.ConfigResource.2.png'> |
| <p> |
| The {@link oaj.config.Config} class is a serializable POJO, which makes the resource |
| relatively straightforward to implement. |
| </p> |
| |
| <h5 class='figure'>ConfigResource.java</h5> |
| <p class='bpcode w800'> |
| <jd>/** |
| * Shows contents of the microservice configuration file. |
| */</jd> |
| <ja>@Rest</ja>( |
| path=<js>"/config"</js>, |
| title=<js>"Configuration"</js>, |
| description=<js>"Contents of configuration file."</js>, |
| htmldoc=<ja>@HtmlDoc</ja>( |
| navlinks={ |
| <js>"up: request:/.."</js>, |
| <js>"options: servlet:/?method=OPTIONS"</js>, |
| <js>"edit: servlet:/edit"</js> |
| } |
| ) |
| ) |
| <jk>public class</jk> ConfigResource <jk>extends</jk> BasicRestServlet { |
| |
| <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/"</js>, description=<js>"Show contents of config file."</js>) |
| <jk>public</jk> ObjectMap getConfig() <jk>throws</jk> Exception { |
| <jk>return</jk> getServletConfig().getConfig().asMap(); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/edit"</js>, description=<js>"Edit config file."</js>) |
| <jk>public</jk> Form getConfigEditForm(RestRequest req) <jk>throws</jk> Exception { |
| <jk>return</jk> <jsm>form</jsm>().id(<js>"form"</js>).action(<js>"servlet:/"</js>).method(<js>"POST"</js>).enctype(<js>"application/x-www-form-urlencoded"</js>).children( |
| <jsm>div</jsm>()._class(<js>"data"</js>).children( |
| <jsm>table</jsm>( |
| <jsm>tr</jsm>(<jsm>td</jsm>().style(<js>"text-align:right"</js>).children(<jsm>button</jsm>(<js>"submit"</js>,<js>"Submit"</js>),<jsm>button</jsm>(<js>"reset"</js>,<js>"Reset"</js>))), |
| <jsm>tr</jsm>(<jsm>th</jsm>().child(<js>"Contents"</js>)), |
| <jsm>tr</jsm>(<jsm>th</jsm>().child( |
| <jsm>textarea</jsm>().name(<js>"contents"</js>).rows(40).cols(120).style(<js>"white-space:pre;word-wrap:normal;overflow-x:scroll;font-family:monospace;"</js>) |
| .text(getServletConfig().getConfig().toString())) |
| ) |
| ) |
| ) |
| ); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/{section}"</js>, |
| description=<js>"Show config file section."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{name:'section',in:'path',description:'Section name.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> ObjectMap getConfigSection(<ja>@Path</ja>(<js>"section"</js>) String section) <jk>throws</jk> Exception { |
| <jk>return</jk> getSection(section); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/{section}/{key}"</js>, |
| description=<js>"Show config file entry."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{name:'section',in:'path',description:'Section name.'},"</js>, |
| <js>"{name:'key',in:'path',description:'Entry name.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> String getConfigEntry(<ja>@Path</ja>(<js>"section"</js>) String section, <ja>@Path</ja>(<js>"key"</js>) String key) <jk>throws</jk> Exception { |
| <jk>return</jk> getSection(section).getString(key); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>POST</jsf>, path=<js>"/"</js>, |
| description=<js>"Sets contents of config file from a FORM post."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{name:'contents',in:'formData',description:'New contents in INI file format.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> Config setConfigContentsFormPost(<ja>@FormData</ja>(<js>"contents"</js>) String contents) <jk>throws</jk> Exception { |
| <jk>return</jk> setConfigContents(<jk>new</jk> StringReader(contents)); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>PUT</jsf>, path=<js>"/"</js>, |
| description=<js>"Sets contents of config file."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{in:'body',description:'New contents in INI file format.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> Config setConfigContents(<ja>@Body</ja> Reader contents) <jk>throws</jk> Exception { |
| <jk>return</jk> getServletConfig().getConfig().load(contents, <jk>true</jk>).asMap(); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>PUT</jsf>, path=<js>"/{section}"</js>, |
| description=<js>"Add or overwrite a config file section."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{name:'section',in:'path',description:'Section name.'}"</js>, |
| <js>"{in:'body',description:'New contents for section as a simple map with string keys and values.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> ObjectMap setConfigSection(<ja>@Path</ja>(<js>"section"</js>) String section, <ja>@Body</ja> Map<String,String> contents) <jk>throws</jk> Exception { |
| getServletConfig().getConfig().setSection(section, contents); |
| <jk>return</jk> getSection(section); |
| } |
| |
| <ja>@RestMethod</ja>(name=<jsf>PUT</jsf>, path=<js>"/{section}/{key}"</js>, |
| description=<js>"Add or overwrite a config file entry."</js>, |
| swagger={ |
| <js>"parameters:["</js>, |
| <js>"{name:'section',in:'path',description:'Section name.'}"</js>, |
| <js>"{name:'key',in:'path',description:'Entry name.'}"</js>, |
| <js>"{in:'body',description:'New value as a string.'}"</js>, |
| <js>"]"</js> |
| } |
| ) |
| <jk>public</jk> String setConfigSection(<ja>@Path</ja>(<js>"section"</js>) String section, <ja>@Path</ja>(<js>"key"</js>) String key, <ja>@Body</ja> String value) <jk>throws</jk> Exception { |
| getServletConfig().getConfig().put(section, key, value, <jk>false</jk>); |
| <jk>return</jk> getSection(section).getString(key); |
| } |
| |
| <jk>private</jk> ObjectMap getSection(String name) { |
| ObjectMap m = getServletConfig().getConfig().getSectionMap(name); |
| <jk>if</jk> (m == <jk>null</jk>) |
| <jk>throw new</jk> RestException(<jsf>SC_NOT_FOUND</jsf>, <js>"Section not found."</js>); |
| <jk>return</jk> m; |
| } |
| } |
| </p> |