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 |
---|---|
Throwable |
convertThrowable(Throwable t)
Method that can be subclassed to allow uncaught throwables to be treated as other types of throwables.
|
RestCall |
createCall(HttpServletRequest req,
HttpServletResponse res)
Wraps an incoming servlet request/response pair into a single
RestCall object. |
RestRequest |
createRequest(RestCall call)
Creates a
RestRequest object based on the specified incoming HttpServletRequest object. |
RestResponse |
createResponse(RestCall call)
Creates a
RestResponse object based on the specified incoming HttpServletResponse object
and the request returned by RestCallHandler.createRequest(RestCall) . |
Map<String,Object> |
getSessionObjects(RestRequest req,
RestResponse res)
Returns the session objects for the specified request.
|
void |
handleError(RestCall call,
Throwable e)
Method for handling response errors.
|
void |
handleNotFound(RestCall call)
Handle the case where a matching method was not found.
|
void |
handleResponse(RestCall call)
The main method for serializing POJOs passed in through the
RestResponse.setOutput(Object) method or
returned by the Java method. |
void |
service(HttpServletRequest r1,
HttpServletResponse r2)
The main service method.
|
public BasicRestCallHandler(RestContext context)
context
- The resource context.public RestCall createCall(HttpServletRequest req, HttpServletResponse res)
RestCallHandler
RestCall
object.createCall
in interface RestCallHandler
req
- The rest request.res
- The rest response.public RestRequest createRequest(RestCall call) throws ServletException
RestCallHandler
RestRequest
object based on the specified incoming HttpServletRequest
object.createRequest
in interface RestCallHandler
call
- The current REST call.ServletException
- If any errors occur trying to interpret the request.public RestResponse createResponse(RestCall call) throws ServletException
RestCallHandler
RestResponse
object based on the specified incoming HttpServletResponse
object
and the request returned by RestCallHandler.createRequest(RestCall)
.createResponse
in interface RestCallHandler
call
- The current REST call.ServletException
- If any errors occur trying to interpret the request or response.public void service(HttpServletRequest r1, HttpServletResponse r2) throws 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.ServletException
- General servlet exception.IOException
- Thrown by underlying stream.public void handleResponse(RestCall call) throws IOException, HttpException, NotImplemented
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)
method returns
handleResponse
in interface RestCallHandler
call
- The HTTP call.IOException
- Thrown by underlying stream.HttpException
- Non-200 response.NotImplemented
public Throwable convertThrowable(Throwable t)
The default implementation looks at the throwable class name to determine whether it can be converted to another type:
Unauthorized
.
NotFound
.
convertThrowable
in interface RestCallHandler
t
- The thrown object.public void handleNotFound(RestCall call) 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
call
- The HTTP call.Exception
- Can be thrown if error occurred while handling response.public void handleError(RestCall call, Throwable e) throws IOException
Subclasses can override this method to provide their own custom error response handling.
handleError
in interface RestCallHandler
call
- The rest call.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, RestResponse res)
The default implementation simply returns a single map containing
getSessionObjects
in interface RestCallHandler
req
- The REST request.res
- The REST response.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.