DockerRegistryResource
The DockerRegistryResource class shows examples of the following:
-
Accessing a docker registry REST API as POJOs using {@link oajrc.RestClient}.
-
Using the {@link oajr.helper.ResourceDescription} class to implement a top-level
'router' page.
-
Using the {@link oajr.RestContext#getConfig()} method to access external
configuration file values.
Pointing a browser to the resource shows the following:
http://localhost:10000/docker
/**
* Sample resource that shows how to mirror query results from a Docker registry.
*/
@RestResource(
path="/docker",
title="Sample Docker resource",
description="Docker registry explorer",
htmldoc=@HtmlDoc(
navlinks={
"up: request:/..",
"options: servlet:/?method=OPTIONS",
"source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"
},
// Pull in aside contents from file.
aside="$F{resources/DockerRegistryResourceAside.html}"
)
)
public class DockerRegistryResource extends BasicRestServlet {
// Get registry URL from examples.cfg file.
private String registryUrl;
private RestClient rc;
/**
* Initializes the registry URL and rest client.
*
* @param builder The resource config.
* @throws Exception
*/
@RestHook(INIT)
public void initRegistry(RestContextBuilder builder) throws Exception {
Config cf = builder.getConfig();
registryUrl = cf.getString("DockerRegistry/url");
rc = RestClient.create().build();
}
@Override /* Servlet */
public synchronized void destroy() {
rc.closeQuietly();
super.destroy();
}
/** [GET /] - Show child resources. */
@RestMethod(name=GET, path="/")
public ResourceDescription[] getChildren(RestRequest req) {
return new ResourceDescription[] {
new ResourceDescription("search", "Search Registry")
};
}
/**
* PUT request handler.
* Replaces the feed with the specified content, and then mirrors it as the response.
*/
@RestMethod(name=GET, path="/search")
public QueryResults query(@Query("q") String q) throws Exception {
String url = registryUrl + "/search" + (q == null ? "" : "?q=" + q);
synchronized(rc) {
return rc.doGet(url).getResponse(QueryResults.class);
}
}
public static class QueryResults {
public int num_results;
public String query;
public List<DockerImage> results;
}
public static class DockerImage {
public String name, description;
}
}
In this example, we're pulling the aside message from an external file:
<div style='min-width:200px' class='text'>
<p>REST API for searching Docker registries.</p>
<p>To use, you must first specify the Docker registry URL in the <code>[Docker]</code> section of the config file.</p>
</div>
The Docker registry URL is specified in the examples.cfg file:
#================================================================================
# DockerRegistryResource properties
#================================================================================
[DockerRegistry]
url = http://clmdocker02.ratl.swg.usma.apache.org:5000/v1