This class will accept JSON REST requests, find the named Spring Bean, find the method, and then invoke the method. Once complete, it will convert the return object to JSON and then send that back to the client. The requests are typically made from a Javascript client, although they could easily be made from Python, Java, or Objective C. The request comes in with http://yoursite.com/json/controllerName/methodName. The "json" part can be whatever name you map to this servlet in web.xml. The arguments are sent as the HTTP POST body, or they can be sent via query params like this: http://yoursite.com/json/Controller/methodName?json=[arg1, arg2,...] When calling the JsonServlet, it will always return an object in the form: {"data":v,"status":false|true|null} where the value 'v' is the return value of the Controller method called. The status is 'true' if the method call properly succeeded. Use the return value 'v' when status === true. If status === false then the communications reach the server, however, there was an exception. A Controller method threw an exception, invalid JSON was passed in, the name of the controller targeted was wrong, the controller targeted was not a BaseController, or the method on the controller was not found. The value 'v' when the the status === false will indicate the error. If the status === null then the call() method within the browser never reached the server. The value 'v' is a message indicating a network communications issue. The returned JSON is gzip compressed if the caller indicates that it accepts Content-Encoding of gzip AND the return message is greater than 512 bytes. The return stream from methods that return large arrays and/or object graphs compresses especially well.
Modifiers | Name | Description |
---|---|---|
static java.lang.String |
ATTRIBUTE_FAIL_MESSAGE |
|
static java.lang.String |
ATTRIBUTE_STATUS |
|
private static org.apache.logging.log4j.Logger |
LOG |
|
private NCubeConfigurationProvider |
nCubeCfgProvider |
|
static java.lang.ThreadLocal<javax.servlet.http.HttpServletRequest> |
servletRequest |
|
static java.lang.ThreadLocal<javax.servlet.http.HttpServletResponse> |
servletResponse |
|
private SpringConfigurationProvider |
springCfgProvider |
Constructor and description |
---|
JsonCommandServlet
() |
Type | Name and description |
---|---|
private static java.lang.String |
buildResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Envelope envelope) Build the response envelope as a String to be returned to the client. |
protected void |
doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handle JSON GET style request. |
protected void |
doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Process JSON POST style where the controller, method, and arguments are passed in as the POST data. |
static java.lang.Throwable |
getDeepestException(java.lang.Throwable e) Get the deepest (original cause) of the exception chain. |
private java.lang.Object |
getProvider(javax.servlet.http.HttpServletRequest request, java.lang.String json) @param request HttpServletRequest passed to the command servlet. |
private void |
handleRequestAndResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String json) This is the main driver of the command servlet. |
void |
init() |
private static void |
removeThreadLocals() Remove ThreadLocals containing HttpServletRequest and response so that when thread is returned to threadpool there are no lingering references to these values. |
void |
route(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) If using UrlRewrite from tuckey.org, then route HTTP Json commands via this 'route' method and list it in the urlrewrite.xml. |
private static void |
sendJsonResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Envelope envelope) Build and send the response Envelope to the client. |
private static void |
writeResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String json) Write the response Envelope to the client |
Methods inherited from class | Name |
---|---|
class javax.servlet.http.HttpServlet |
javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse), javax.servlet.http.HttpServlet#log(java.lang.String, java.lang.Throwable), javax.servlet.http.HttpServlet#log(java.lang.String), javax.servlet.http.HttpServlet#init(javax.servlet.ServletConfig), javax.servlet.http.HttpServlet#init(), javax.servlet.http.HttpServlet#destroy(), javax.servlet.http.HttpServlet#getServletConfig(), javax.servlet.http.HttpServlet#getServletInfo(), javax.servlet.http.HttpServlet#getServletName(), javax.servlet.http.HttpServlet#getServletContext(), javax.servlet.http.HttpServlet#getInitParameter(java.lang.String), javax.servlet.http.HttpServlet#getInitParameterNames(), javax.servlet.http.HttpServlet#wait(long, int), javax.servlet.http.HttpServlet#wait(long), javax.servlet.http.HttpServlet#wait(), javax.servlet.http.HttpServlet#equals(java.lang.Object), javax.servlet.http.HttpServlet#toString(), javax.servlet.http.HttpServlet#hashCode(), javax.servlet.http.HttpServlet#getClass(), javax.servlet.http.HttpServlet#notify(), javax.servlet.http.HttpServlet#notifyAll() |
class javax.servlet.GenericServlet |
javax.servlet.GenericServlet#log(java.lang.String, java.lang.Throwable), javax.servlet.GenericServlet#log(java.lang.String), javax.servlet.GenericServlet#init(javax.servlet.ServletConfig), javax.servlet.GenericServlet#init(), javax.servlet.GenericServlet#destroy(), javax.servlet.GenericServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse), javax.servlet.GenericServlet#getServletConfig(), javax.servlet.GenericServlet#getServletInfo(), javax.servlet.GenericServlet#getServletName(), javax.servlet.GenericServlet#getServletContext(), javax.servlet.GenericServlet#getInitParameter(java.lang.String), javax.servlet.GenericServlet#getInitParameterNames(), javax.servlet.GenericServlet#wait(long, int), javax.servlet.GenericServlet#wait(long), javax.servlet.GenericServlet#wait(), javax.servlet.GenericServlet#equals(java.lang.Object), javax.servlet.GenericServlet#toString(), javax.servlet.GenericServlet#hashCode(), javax.servlet.GenericServlet#getClass(), javax.servlet.GenericServlet#notify(), javax.servlet.GenericServlet#notifyAll() |
Build the response envelope as a String to be returned to the client.
request
- original servlet requestresponse
- original servlet responseenvelope
- data and status to be writtenHandle JSON GET style request. In this case, 'controller', 'method', and 'json' are specified as URL parameters. Example: http://cedarsoftware.com/coolApp/CoolController/coolMethod?json=[args...]
Process JSON POST style where the controller, method, and arguments are passed in as the POST data. Example: http://cedarsoftware.com/coolApp/CoolController/coolMethod Post body contains the arguments in JSON format.
Get the deepest (original cause) of the exception chain.
e
- Throwable exception that occurred.
request
- HttpServletRequest passed to the command servlet.This is the main driver of the command servlet. This code obtains the appropriate provider, calls the appropriate method on the provider, and then optionally writes the return response, if the called method did not write to the HTTP response.
request
- HttpServletRequest passed to the command servlet.response
- HttpServletResponse passed to the command servlet.json
- String arguments that were passed in JSON format.Remove ThreadLocals containing HttpServletRequest and response so that when thread is returned to threadpool there are no lingering references to these values.
If using UrlRewrite from tuckey.org, then route HTTP Json commands via this 'route' method and list it in the urlrewrite.xml.
Build and send the response Envelope to the client.
request
- original servlet requestresponse
- original servlet responseenvelope
- data and status to be writtenWrite the response Envelope to the client
request
- original servlet requestresponse
- original servlet responsejson
- String response to write