import java.util.*;
/** Base class for customizing REST API handlers.
Create a subclass of RestApiModel that defines Handler
classes (subclasses of RestApiHandler, which have custom
handler methods doGet(), doPost(), etc.) to
handle the four conventional REST requests:
POST for a Create serviceGET for a Read servicePATCH for an Update serviceDELETE for a Delete serviceDEBUG */
private static String DEBUG = "DEVEL ";
/** data structure of all Handler objects. See addHandler()
to add a handler to this data structure. */
public HashtableRestApiModel object
@return An initial value for handlers */
private Hashtablehandlers
data structure */
private String makeKey(String url, boolean id) {
return new String(url + (id ? " #" : ""));
}
/** helper method for generating Hashtable keys for handlers
data structure */
private String makeKey(String url) {
return url;
}
/** Add or replace a handler for a new API component.
@param url Base URL for the new API component
@param id True if API component expects a final integer path component
(typically representing a record ID number), false if no such final
integer should be used
@param handlr Handler object providing methods such as
doGet(), doPost(), etc., for implementing a
REST-style API. handlr is typically an object in a subclass
of RestApiHandler that implements some or all of those
four doX() methods. */
public void addHandler(String url, boolean id, RestApiHandler handlr) {
handlr.recordApi(url, id);
handlers.put(makeKey(url,id), handlr);
}
/** Add or replace a handler for a new API component without a final integer
in the URL.
@param url Base URL for the new API component
@param handlr Handler object providing methods such as
doGet(), doPost(), etc., for implementing a
REST-style API. handlr is typically an object in a subclass
of RestApiHandler that implements some or all of those
four doX() methods. */
public void addHandler(String url, RestApiHandler handlr) {
addHandler(url, false, handlr);
}
/** Retrieve a handler object for a particular API URL, with or without
a final integer ID component
@param url Base URL for an API request
@param id True if API component expects a final integer path component
(typically representing a record ID number), false if no such final
integer should be used
@return Handler object for that url/id
combination, or default handler (no services implemented) if no such
handler exists in handlers data structure */
public RestApiHandler getHandler(String url, boolean id) {
RestApiHandler h = handlers.get(makeKey(url,id));
if (h == null) {
System.out.println("Warning: REST API handler for URL \"" + url +
(id ? "/#" : "") + "\"\n" +
"not found, using default handler");
h = handlers.get("");
}
return h;
}
/** Retrieve a handler object for a particular API URL, with no final
integer ID component
@param url Base URL for an API request
@return Handler object for that url and no final integer ID
component, or default handler (no services implemented) if no such
handler exists in handlers data structure */
public RestApiHandler getHandler(String url) {
return getHandler(url, false);
}
/** retrieve all keys in handlers data structure.
@return A space-separated list of keys in the handlers
data structure. If an API url expects a final
integer component in its path, the base URL is followed by a space and
the character # in this return string. */
public String getHandlerKeys() {
String ret = new String();
for (Enumerationhandlers data structure.
@return A space-separated list of string representations of the handlers
in the handlers data structure. */
public String getHandlerValues() {
String ret = new String();
for (Enumeration