SystemPropertiesResource
The SystemProperties class is a resource that shows off a typical REST design pattern
of GET/PUT/POST/DELETE commands for modifying the JVM system properties.
It demonstrates several capabilities including:
-
Using the {@link oajr.annotation.HtmlDoc @HtmlDoc} annotation to customize the HTML view.
-
Defining Swagger documentation through annotations.
-
Using Guards to limit access to certain methods.
-
Creating form entry pages using HTML5 beans.
@RestResource(
path="/systemProperties",
// Title and description that show up on HTML rendition page.
// Also used in Swagger doc.
title="System properties resource",
description="REST interface for performing CRUD operations on system properties.",
htmldoc=@HtmlDoc(
// Widget used for content-type and styles pull-down menus.
widgets={
ContentTypeMenuItem.class,
StyleMenuItem.class
},
// Links on the HTML rendition page.
// "request:/..." URIs are relative to the request URI.
// "servlet:/..." URIs are relative to the servlet URI.
// "$C{...}" variables are pulled from the config file.
navlinks={
"up: request:/..",
"options: servlet:/?method=OPTIONS",
"form: servlet:/formPage",
"$W{ContentTypeMenuItem}",
"$W{StyleMenuItem}",
"source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"
},
// Custom page text in aside section.
aside={
"<div style='max-width:800px' class='text'>",
" <p>Shows standard GET/PUT/POST/DELETE operations and use of Swagger annotations.</p>",
"</div>"
},
// Custom CSS styles applied to HTML view.
style={
"aside {display:table-caption} ",
"aside p {margin: 0px 20px;}"
}
),
// Properties that get applied to all serializers and parsers.
properties={
// Use single quotes.
@Property(name=SERIALIZER_quoteChar, value="'")
},
// Support GZIP encoding on Accept-Encoding header.
encoders=GzipEncoder.class,
swagger={
"contact:{name:'John Smith',email:'john@smith.com'},",
"license:{name:'Apache 2.0',url:'http://www.apache.org/licenses/LICENSE-2.0.html'},",
"version:'2.0',",
"termsOfService:'You are on your own.',",
"tags:[{name:'Java',description:'Java utility'}],",
"externalDocs:{description:'Home page',url:'http://juneau.apache.org'}"
}
)
public class SystemPropertiesResource extends BasicRestServlet {
@RestMethod(
name=GET, path="/",
summary="Show all system properties",
description="Returns all system properties defined in the JVM.",
swagger={
"parameters:[",
"{name:'sort',in:'query',description:'Sort results alphabetically',default:'false'}",
"],",
"responses:{",
"200: {description:'Returns a map of key/value pairs.'}",
"}"
}
)
public Map getSystemProperties(@Query("sort") boolean sort) throws Throwable {
if (sort)
return new TreeMap(System.getProperties());
return System.getProperties();
}
@RestMethod(
name=GET, path="/{propertyName}",
summary="Get system property",
description="Returns the value of the specified system property.",
swagger={
"parameters:[",
"{name:'propertyName',in:'path',description:'The system property name.'}",
"],",
"responses:{",
"200: {description:'The system property value, or null if not found.'}",
"}"
}
)
public String getSystemProperty(@Path("propertyName") String propertyName) throws Throwable {
return System.getProperty(propertyName);
}
@RestMethod(
name=PUT, path="/{propertyName}",
summary="Replace system property",
description="Sets a new value for the specified system property.",
guards=AdminGuard.class,
swagger={
"parameters:[",
"{name:'propertyName',in:'path',description:'The system property name.'},",
"{in:'body',description:'The new system property value.'}",
"],",
"responses:{",
"302: {headers:{Location:{description:'The root URL of this resource.'}}},",
"403: {description:'User is not an admin.'}",
"}"
}
)
public Redirect setSystemProperty(@Path("propertyName") String propertyName, @Body String value) {
System.setProperty(propertyName, value);
return new Redirect("servlet:/");
}
@RestMethod(
name=POST, path="/",
summary="Add an entire set of system properties",
description="Takes in a map of key/value pairs and creates a set of new system properties.",
guards=AdminGuard.class,
swagger={
"parameters:[",
"{name:'propertyName',in:'path',description:'The system property name.'},",
"{in:'body',description:'The new system property values.',schema:{example:{key1:'val1',key2:123}}}",
"],",
"responses:{",
"302: {headers:{Location:{description:'The root URL of this resource.'}}},",
"403: {description:'User is not an admin.'}",
"}"
}
)
public Redirect setSystemProperties(@Body java.util.Properties newProperties) {
System.setProperties(newProperties);
return new Redirect("servlet:/");
}
@RestMethod(
name=DELETE, path="/{propertyName}",
summary="Delete system property",
description="Deletes the specified system property.",
guards=AdminGuard.class,
swagger={
"parameters:[",
"{name:'propertyName',in:'path',description:'The system property name.'}",
"],",
"responses:{",
"302: {headers:{Location:{description:'The root URL of this resource.'}}},",
"403: {description:'User is not an admin.'}",
"}"
}
)
public Redirect deleteSystemProperty(@Path("propertyName") String propertyName) {
System.clearProperty(propertyName);
return new Redirect("servlet:/");
}
@RestMethod(
name=GET, path="/formPage",
summary="Form entry page",
description="A form post page for setting a single system property value",
guards=AdminGuard.class,
htmldoc=@HtmlDoc(
aside={
"<div class='text'>",
" <p>Shows how HTML5 beans can be used to quickly create arbitrary HTML.</p>",
"</div>"
}
)
)
public Form getFormPage() {
return form().method(POST).action("servlet:/formPagePost").children(
table(
tr(
th("Set system property").colspan(2)
),
tr(
td("Name: "), td(input("text").name("name"))
),
tr(
td("Value: "), td(input("text").name("value"))
)
),
button("submit","Click me!").style("float:right")
);
}
@RestMethod(
name=POST, path="/formPagePost",
description="Accepts a simple form post of a system property name/value pair.",
guards=AdminGuard.class
)
public Redirect formPagePost(@FormData("name") String name, @FormData("value") String value) {
System.setProperty(name, value);
return new Redirect("servlet:/");
}
}
Pointing a browser to the resource shows the following:
http://localhost:10000/systemProperties
Clicking the OPTIONS link shows you the generated Swagger:
http://localhost:10000/systemProperties?method=OPTIONS
Clicking the FORM link shows you the generated form entry page:
http://localhost:10000/systemProperties/formPage