org.apache.oozie.servlet
Class JsonRestServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.apache.oozie.servlet.JsonRestServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
BaseAdminServlet, BaseJobServlet, BaseJobsServlet, CallbackServlet, SLAServlet, VersionServlet

public abstract class JsonRestServlet
extends javax.servlet.http.HttpServlet

Base class for Oozie web service API Servlets.

This class provides common instrumentation, error logging and other common functionality.

See Also:
Serialized Form

Nested Class Summary
static class JsonRestServlet.ParameterInfo
          This bean defines a query string parameter.
static class JsonRestServlet.ResourceInfo
          This bean defines a REST resource.
 
Field Summary
protected static String AUDIT_ERROR_CODE
           
protected static String AUDIT_ERROR_MESSAGE
           
protected static String AUDIT_HTTP_STATUS_CODE
           
protected static String AUDIT_OPERATION
           
protected static String AUDIT_PARAM
           
static String AUTH_TOKEN
          Request attribute constant for the authenticatio token.
protected static String INSTRUMENTATION_GROUP
          Name of the instrumentation group for the WS layer, value is 'webservices'.
protected static String TEXT_UTF8
           
protected static String UNDEF
           
static String USER_NAME
          Request attribute constant for the user name.
protected static String XML_UTF8
           
 
Constructor Summary
JsonRestServlet(String instrumentationName, JsonRestServlet.ResourceInfo... resourcesInfo)
          Creates a servlet with a specified instrumentation sampler name for its requests.
 
Method Summary
protected  String getAuthToken(javax.servlet.http.HttpServletRequest request)
          Return the authentication token of the request if any.
protected  String getContentType(javax.servlet.http.HttpServletRequest request)
          Return the request content type, lowercase and without attributes.
protected  String getResourceName(javax.servlet.http.HttpServletRequest request)
          Return the resource name of the request.
protected  String getUser(javax.servlet.http.HttpServletRequest request)
          Return the user name of the request if any.
 void init(javax.servlet.ServletConfig servletConfig)
          Initializes total request and servlet request samplers.
protected  void sendErrorResponse(javax.servlet.http.HttpServletResponse response, int statusCode, String error, String message)
          Sends a error response.
protected  void sendJsonResponse(javax.servlet.http.HttpServletResponse response, int statusCode, JsonBean bean, String timeZoneId)
          Sends a JSON response.
protected  void sendJsonResponse(javax.servlet.http.HttpServletResponse response, int statusCode, org.json.simple.JSONStreamAware json)
           
protected  void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Dispatches to super after loginfo and intrumentation handling.
protected  void setAllowSafeModeChanges(boolean allow)
          Enable HTTP POST/PUT/DELETE methods while in safe mode.
protected  void setLogInfo(String jobid, String actionid)
          Set the log info with the given information.
protected  void startCron()
          Start the request instrumentation cron.
protected  void stopCron()
          Stop the request instrumentation cron.
protected  String validateContentType(javax.servlet.http.HttpServletRequest request, String expected)
          Validate and return the content type of the request.
protected  void validateRestUrl(String method, String resourceName, Map<String,String[]> queryStringParams)
          Validates REST URL using the ResourceInfos of the servlet.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_UTF8

protected static final String XML_UTF8
See Also:
Constant Field Values

TEXT_UTF8

protected static final String TEXT_UTF8
See Also:
Constant Field Values

AUDIT_OPERATION

protected static final String AUDIT_OPERATION
See Also:
Constant Field Values

AUDIT_PARAM

protected static final String AUDIT_PARAM
See Also:
Constant Field Values

AUDIT_ERROR_CODE

protected static final String AUDIT_ERROR_CODE
See Also:
Constant Field Values

AUDIT_ERROR_MESSAGE

protected static final String AUDIT_ERROR_MESSAGE
See Also:
Constant Field Values

AUDIT_HTTP_STATUS_CODE

protected static final String AUDIT_HTTP_STATUS_CODE
See Also:
Constant Field Values

INSTRUMENTATION_GROUP

protected static final String INSTRUMENTATION_GROUP
Name of the instrumentation group for the WS layer, value is 'webservices'.

See Also:
Constant Field Values

AUTH_TOKEN

public static final String AUTH_TOKEN
Request attribute constant for the authenticatio token.

See Also:
Constant Field Values

USER_NAME

public static final String USER_NAME
Request attribute constant for the user name.

See Also:
Constant Field Values

UNDEF

protected static final String UNDEF
See Also:
Constant Field Values
Constructor Detail

JsonRestServlet

public JsonRestServlet(String instrumentationName,
                       JsonRestServlet.ResourceInfo... resourcesInfo)
Creates a servlet with a specified instrumentation sampler name for its requests.

Parameters:
instrumentationName - instrumentation name for timer and samplers for the servlet.
resourcesInfo - list of resource definitions supported by the servlet, empty and wildcard resources must be the last ones, in that order, first empty and the wildcard.
Method Detail

setAllowSafeModeChanges

protected void setAllowSafeModeChanges(boolean allow)
Enable HTTP POST/PUT/DELETE methods while in safe mode.

Parameters:
allow - true enabled safe mode changes, false disable safe mode changes (default).

startCron

protected void startCron()
Start the request instrumentation cron.


stopCron

protected void stopCron()
Stop the request instrumentation cron.


init

public void init(javax.servlet.ServletConfig servletConfig)
          throws javax.servlet.ServletException
Initializes total request and servlet request samplers.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

service

protected final void service(javax.servlet.http.HttpServletRequest request,
                             javax.servlet.http.HttpServletResponse response)
                      throws javax.servlet.ServletException,
                             IOException
Dispatches to super after loginfo and intrumentation handling. In case of errors dispatches error response codes and does error logging.

Overrides:
service in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

sendJsonResponse

protected void sendJsonResponse(javax.servlet.http.HttpServletResponse response,
                                int statusCode,
                                JsonBean bean,
                                String timeZoneId)
                         throws IOException
Sends a JSON response.

Parameters:
response - servlet response.
statusCode - HTTP status code.
bean - bean to send as JSON response.
timeZoneId - time zone to use for dates in the JSON response.
Throws:
IOException - thrown if the bean could not be serialized to the response output stream.

sendErrorResponse

protected void sendErrorResponse(javax.servlet.http.HttpServletResponse response,
                                 int statusCode,
                                 String error,
                                 String message)
                          throws IOException
Sends a error response.

Parameters:
response - servlet response.
statusCode - HTTP status code.
error - error code.
message - error message.
Throws:
IOException - thrown if the error response could not be set.

sendJsonResponse

protected void sendJsonResponse(javax.servlet.http.HttpServletResponse response,
                                int statusCode,
                                org.json.simple.JSONStreamAware json)
                         throws IOException
Throws:
IOException

validateRestUrl

protected void validateRestUrl(String method,
                               String resourceName,
                               Map<String,String[]> queryStringParams)
                        throws javax.servlet.ServletException
Validates REST URL using the ResourceInfos of the servlet.

Parameters:
method - HTTP method.
resourceName - resource name.
queryStringParams - query string parameters.
Throws:
javax.servlet.ServletException - thrown if the resource name or parameters are incorrect.

getResourceName

protected String getResourceName(javax.servlet.http.HttpServletRequest request)
Return the resource name of the request.

The resource name is the whole extra path. If the extra path starts with '/', the first '/' is trimmed.

Parameters:
request - request instance
Returns:
the resource name, null if none.

getContentType

protected String getContentType(javax.servlet.http.HttpServletRequest request)
Return the request content type, lowercase and without attributes.

Parameters:
request - servlet request.
Returns:
the request content type, null if none.

validateContentType

protected String validateContentType(javax.servlet.http.HttpServletRequest request,
                                     String expected)
                              throws XServletException
Validate and return the content type of the request.

Parameters:
request - servlet request.
expected - expected contentType.
Returns:
the normalized content type (lowercase and without modifiers).
Throws:
XServletException - thrown if the content type is invalid.

getAuthToken

protected String getAuthToken(javax.servlet.http.HttpServletRequest request)
Return the authentication token of the request if any.

Parameters:
request - request.
Returns:
the authentication token, null if there is none.

getUser

protected String getUser(javax.servlet.http.HttpServletRequest request)
Return the user name of the request if any.

Parameters:
request - request.
Returns:
the user name, null if there is none.

setLogInfo

protected void setLogInfo(String jobid,
                          String actionid)
Set the log info with the given information.

Parameters:
jobid - job ID.
actionid - action ID.


Copyright © 2013 Apache Software Foundation. All Rights Reserved.