public abstract class JsonRestServlet extends javax.servlet.http.HttpServlet
This class provides common instrumentation, error logging and other common functionality.
Modifier and Type | Class and Description |
---|---|
static class |
JsonRestServlet.ParameterInfo
This bean defines a query string parameter.
|
static class |
JsonRestServlet.ResourceInfo
This bean defines a REST resource.
|
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
JsonRestServlet(String instrumentationName,
JsonRestServlet.ResourceInfo... resourcesInfo)
Creates a servlet with a specified instrumentation sampler name for its requests.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addCron(String name,
Instrumentation.Cron cron)
Add an instrumentation cron.
|
protected String |
getContentType(javax.servlet.http.HttpServletRequest request)
Return the request content type, lowercase and without attributes.
|
protected String |
getProxyUser(javax.servlet.http.HttpServletRequest request)
Gets proxy user.
|
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.
|
protected void |
incrCounter(String name,
int count)
Convenience method for instrumentation counters.
|
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 actionid)
Set the thread local 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.
|
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
protected static final String XML_UTF8
protected static final String TEXT_UTF8
protected static final String AUDIT_OPERATION
protected static final String AUDIT_PARAM
protected static final String AUDIT_ERROR_CODE
protected static final String AUDIT_ERROR_MESSAGE
protected static final String AUDIT_HTTP_STATUS_CODE
protected static final String INSTRUMENTATION_GROUP
public static final String AUTH_TOKEN
public static final String USER_NAME
protected static final String UNDEF
public JsonRestServlet(String instrumentationName, JsonRestServlet.ResourceInfo... resourcesInfo)
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.protected void setAllowSafeModeChanges(boolean allow)
allow
- true
enabled safe mode changes, false
disable safe mode changes
(default).protected void addCron(String name, Instrumentation.Cron cron)
name
- name of the timer for the cron.cron
- cron to add to a instrumentation timer.protected void startCron()
protected void stopCron()
public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
javax.servlet.ServletException
protected void incrCounter(String name, int count)
name
- counter name.count
- count to increment the counter.protected final void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
service
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
protected void sendJsonResponse(javax.servlet.http.HttpServletResponse response, int statusCode, JsonBean bean, String timeZoneId) throws IOException
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.IOException
- thrown if the bean could not be serialized to the response output stream.protected void sendErrorResponse(javax.servlet.http.HttpServletResponse response, int statusCode, String error, String message) throws IOException
response
- servlet response.statusCode
- HTTP status code.error
- error code.message
- error message.IOException
- thrown if the error response could not be set.protected void sendJsonResponse(javax.servlet.http.HttpServletResponse response, int statusCode, org.json.simple.JSONStreamAware json) throws IOException
IOException
protected void validateRestUrl(String method, String resourceName, Map<String,String[]> queryStringParams) throws javax.servlet.ServletException
method
- HTTP method.resourceName
- resource name.queryStringParams
- query string parameters.javax.servlet.ServletException
- thrown if the resource name or parameters are incorrect.protected String getResourceName(javax.servlet.http.HttpServletRequest request)
The resource name is the whole extra path. If the extra path starts with '/', the first '/' is trimmed.
request
- request instancenull
if none.protected String getContentType(javax.servlet.http.HttpServletRequest request)
request
- servlet request.null
if none.protected String validateContentType(javax.servlet.http.HttpServletRequest request, String expected) throws XServletException
request
- servlet request.expected
- expected contentType.XServletException
- thrown if the content type is invalid.protected String getUser(javax.servlet.http.HttpServletRequest request)
request
- request.null
if there is none.protected String getProxyUser(javax.servlet.http.HttpServletRequest request)
HttpServletRequest
contains proxy user.
Otherwise it is the normal user.request
- the requestprotected void setLogInfo(String actionid)
actionid
- action ID.Copyright © 2018 Apache Software Foundation. All rights reserved.