public class BasicRestCallHandler extends Object implements RestCallHandler
RestCallHandler
.
Subclasses can override these methods to tailor how HTTP REST calls are handled.
Subclasses MUST implement a public constructor that takes in a RestContext
object.
RestCallHandler.Null
Constructor and Description |
---|
BasicRestCallHandler(RestContext context)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
RestRequest |
createRequest(javax.servlet.http.HttpServletRequest req)
Creates a
RestRequest object based on the specified incoming HttpServletRequest object. |
RestResponse |
createResponse(RestRequest req,
javax.servlet.http.HttpServletResponse res)
Creates a
RestResponse object based on the specified incoming HttpServletResponse object
and the request returned by createRequest(HttpServletRequest) . |
Map<String,Object> |
getSessionObjects(RestRequest req)
Returns the session objects for the specified request.
|
void |
handleError(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
RestException e)
Method for handling response errors.
|
void |
handleNotFound(int rc,
RestRequest req,
RestResponse res)
Handle the case where a matching method was not found.
|
void |
handleResponse(RestRequest req,
RestResponse res,
Object output)
The main method for serializing POJOs passed in through the
RestResponse.setOutput(Object) method or
returned by the Java method. |
void |
renderError(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
RestException e)
Method for rendering response errors.
|
void |
service(javax.servlet.http.HttpServletRequest r1,
javax.servlet.http.HttpServletResponse r2)
The main service method.
|
public BasicRestCallHandler(RestContext context)
context
- The resource context.public RestRequest createRequest(javax.servlet.http.HttpServletRequest req) throws javax.servlet.ServletException
RestRequest
object based on the specified incoming HttpServletRequest
object.
Subclasses may choose to override this method to provide a specialized request object.
createRequest
in interface RestCallHandler
req
- The request object from the service(HttpServletRequest, HttpServletResponse)
method.javax.servlet.ServletException
- If any errors occur trying to interpret the request.public RestResponse createResponse(RestRequest req, javax.servlet.http.HttpServletResponse res) throws javax.servlet.ServletException
RestResponse
object based on the specified incoming HttpServletResponse
object
and the request returned by createRequest(HttpServletRequest)
.
Subclasses may choose to override this method to provide a specialized response object.
createResponse
in interface RestCallHandler
req
- The request object returned by createRequest(HttpServletRequest)
.res
- The response object from the service(HttpServletRequest, HttpServletResponse)
method.javax.servlet.ServletException
- If any errors occur trying to interpret the request or response.public void service(javax.servlet.http.HttpServletRequest r1, javax.servlet.http.HttpServletResponse r2) throws javax.servlet.ServletException, IOException
Subclasses can optionally override this method if they want to tailor the behavior of requests.
service
in interface RestCallHandler
r1
- The incoming HTTP servlet request object.r2
- The incoming HTTP servlet response object.javax.servlet.ServletException
IOException
public void handleResponse(RestRequest req, RestResponse res, Object output) throws IOException, RestException
RestResponse.setOutput(Object)
method or
returned by the Java method.
Subclasses may override this method if they wish to modify the way the output is rendered or support other output formats.
The default implementation simply iterates through the response handlers on this resource
looking for the first one whose ResponseHandler.handle(RestRequest, RestResponse, Object)
method returns
handleResponse
in interface RestCallHandler
req
- The HTTP request.res
- The HTTP response.output
- The output to serialize in the response.IOException
RestException
public void handleNotFound(int rc, RestRequest req, RestResponse res) throws Exception
Subclasses can override this method to provide a 2nd-chance for specifying a response. The default implementation will simply throw an exception with an appropriate message.
handleNotFound
in interface RestCallHandler
rc
- The HTTP response code.req
- The HTTP request.res
- The HTTP response.Exception
public void handleError(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, RestException e) throws IOException
The default implementation logs the error and calls
renderError(HttpServletRequest,HttpServletResponse,RestException)
.
Subclasses can override this method to provide their own custom error response handling.
handleError
in interface RestCallHandler
req
- The servlet request.res
- The servlet response.e
- The exception that occurred.IOException
- Can be thrown if a problem occurred trying to write to the output stream.public void renderError(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, RestException e) throws IOException
The default implementation renders a plain text English message, optionally with a stack trace if
@RestResource.renderResponseStackTraces()
is enabled.
Subclasses can override this method to provide their own custom error response handling.
renderError
in interface RestCallHandler
req
- The servlet request.res
- The servlet response.e
- The exception that occurred.IOException
- Can be thrown if a problem occurred trying to write to the output stream.public Map<String,Object> getSessionObjects(RestRequest req)
The default implementation simply returns a single map containing {'req':req}
.
getSessionObjects
in interface RestCallHandler
req
- The REST request.Copyright © 2018 Apache. All rights reserved.