C
- the type of the parsed payloadpublic abstract class RestHandler<C> extends Object
This class provide utility methods to parse the received payload, handle its content, and define the content type that are accepted by the handler.
Constructor and Description |
---|
RestHandler() |
Modifier and Type | Method and Description |
---|---|
abstract boolean |
acceptContentType(String contentType)
Returns whether the
RestHandler accepts the provided contentType . |
Collection<String> |
getAccessControlAllowHeaders()
Returns the
Access-Control-Allow-Headers HTTP header values that must be set by the server when
calling this handler. |
protected String |
getHeaderValue(List<org.apache.http.Header> headers,
String headerKey)
Returns the
Header value associated to the provided headerKey . |
Object |
handleContent(List<org.apache.http.Header> headers,
List<org.apache.http.NameValuePair> params,
Object content)
Handles the raw HTTP request.
|
protected abstract Object |
handleParsedContent(List<org.apache.http.Header> headers,
List<org.apache.http.NameValuePair> params,
C content)
Handles the received
headers , params , and parsed payload. |
protected abstract C |
parseContent(Object content)
Parses the provided raw HTTP request content.
|
public Collection<String> getAccessControlAllowHeaders()
Access-Control-Allow-Headers
HTTP header values that must be set by the server when
calling this handler.
This method is used to ensure that cross-origin requests are accepted by client applications. Note that this headers can be empty if this provider is not intended to be accessed in the browser.
public abstract boolean acceptContentType(String contentType)
RestHandler
accepts the provided contentType
.contentType
- the content type to checktrue
if the RestHandler
accepts the provided contentType
, false
otherwise@Nullable protected abstract C parseContent(@Nullable Object content)
This method is internally used to fill the handleParsedContent(List, List, Object)
parameter with a
parsed representation of the raw request content.
content
- the raw HTTP request content to parsehandleParsedContent(List, List, Object)
@Nullable protected abstract Object handleParsedContent(@Nonnull List<org.apache.http.Header> headers, @Nonnull List<org.apache.http.NameValuePair> params, @Nullable C content) throws RestHandlerException
headers
, params
, and parsed payload.
The value returned by this method is embedded in the sever's response. If the return value is an intance of
HttpEntity
it is embedded as is, otherwise the server attempts to convert it into a
valid HttpEntity
using HttpEntityHelper.createHttpEntity(Object)
.
The exception thrown by this method is used to determine the HTTP response's status. By default the server
returns a status code 200
, but it will return a 404
if this method threw an exception.
headers
- the HTTP headers of the received requestparams
- the request parameterscontent
- the parsed request payload to handleObject
to embed in the server's responseRestHandlerException
- if an error occurred when handling the requestparseContent(Object)
,
HttpEntityHelper.createHttpEntity(Object)
@Nullable public final Object handleContent(@Nonnull List<org.apache.http.Header> headers, @Nonnull List<org.apache.http.NameValuePair> params, @Nullable Object content) throws RestHandlerException
This method parses the provided content
and internally calls
handleParsedContent(List, List, Object)
to handle the request.
This method is part of the core API and cannot be reimplemented by concrete subclasses. Use
handleParsedContent(List, List, Object)
to tune the request content processing.
headers
- the HTTP headers of the received requestparams
- the request parameterscontent
- the raw HTTP request content to handleObject
to embed in the server's responseRestHandlerException
- TODOparseContent(Object)
,
handleParsedContent(List, List, Object)
protected String getHeaderValue(List<org.apache.http.Header> headers, String headerKey)
Header
value associated to the provided headerKey
.
This method is an utility method that can be called by subclasses'
handleParsedContent(List, List, Object)
implementation to retrieve specific values from the request
Header
s.
headers
- the list of Header
to retrieve the value fromheaderKey
- the Header
key to retrieve the value ofHeader
value associated to the provided headerKey
if it exists, null
otherwiseCopyright © 2020 SOM Research Lab. All rights reserved.