public abstract class ServerResource extends UniformResource
Resource
class. It acts as a wrapper to a given call,
including the incoming Request
and the outgoing Response
. Finder
created either explicitly or
more likely implicitly when your ServerResource
subclass is attached
to a Filter
or a Router
via the Filter.setNext(Class)
or Router.attach(String, Class)
methods for example. After
instantiation using the default constructor, the final
UniformResource.init(Context, Request, Response)
method is invoked, setting the
context, request and response. You can intercept this by overriding the
UniformResource.doInit()
method. Then, if the response status is still a success,
the handle()
method is invoked to actually handle the call. Finally,
the final UniformResource.release()
method is invoked to do the necessary clean-up,
which you can intercept by overriding the UniformResource.doRelease()
method. During
this life cycle, if any exception is caught, then the
UniformResource.doCatch(Throwable)
method is invoked.getVariants()
method and another one on the annotated
methods. Both approaches can't however be used at the same time for now.Uniform
class and its
main Restlet
subclass where a single instance can handle several
calls concurrently, one instance of ServerResource
is created for
each call handled and accessed by only one thread at a time.Constructor and Description |
---|
ServerResource()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Ask the connector to abort the related network connection, for example
immediately closing the socket.
|
void |
commit()
Asks the response to immediately commit making it ready to be sent back
to the client.
|
protected Representation |
delete()
Deletes the resource and all its representations.
|
protected Representation |
delete(Variant variant)
Deletes the resource and all its representations.
|
protected Representation |
describeVariants()
Describes the available variants to help client-side content negotiation.
|
protected Representation |
doConditionalHandle()
Handles a call by first verifying the optional request conditions and
continue the processing if possible.
|
protected Representation |
doHandle()
Effectively handles a call without content negotiation of the response
entity.
|
protected Representation |
doHandle(Variant variant)
Effectively handles a call with content negotiation of the response
entity.
|
protected Representation |
doNegotiatedHandle()
Effectively handles a call with content negotiation of the response
entity.
|
protected Representation |
get()
Returns a full representation.
|
protected Representation |
get(Variant variant)
Returns a full representation for a given variant.
|
protected RepresentationInfo |
getInfo()
Returns information about the resource's representation.
|
protected RepresentationInfo |
getInfo(Variant variant)
Returns information about the resource's representation.
|
Uniform |
getOnSent()
Returns the callback invoked after sending the response.
|
protected Variant |
getPreferredVariant(List<Variant> variants)
Returns the preferred variant among a list of available variants.
|
List<Variant> |
getVariants()
Returns a modifiable list of exposed variants for the current request
method.
|
protected List<Variant> |
getVariants(Method method)
Returns a modifiable list of exposed variants for the given method.
|
Representation |
handle()
Handles any call to this resource.
|
protected Representation |
head()
Returns a representation whose metadata will be returned to the client.
|
protected Representation |
head(Variant variant)
Returns a representation whose metadata will be returned to the client.
|
boolean |
isAnnotated()
Indicates if annotations are supported.
|
boolean |
isAutoCommitting()
Indicates if the response should be automatically committed.
|
boolean |
isCommitted()
Indicates if the response has already been committed.
|
boolean |
isConditional()
Indicates if conditional handling is enabled.
|
boolean |
isExisting()
Indicates if the identified resource exists.
|
boolean |
isInRole(String roleName)
Indicates if the authenticated client user associated to the current
request is in the given role name.
|
boolean |
isNegotiated()
Indicates if content negotiation of response entities is enabled.
|
protected Representation |
options()
Indicates the communication options available for this resource.
|
protected Representation |
options(Variant variant)
Indicates the communication options available for this resource.
|
protected Representation |
post(Representation entity)
Posts a representation to the resource at the target URI reference.
|
protected Representation |
post(Representation entity,
Variant variant)
Posts a representation to the resource at the target URI reference.
|
protected Representation |
put(Representation entity)
Creates or updates a resource with the given representation as new state
to be stored.
|
protected Representation |
put(Representation representation,
Variant variant)
Creates or updates a resource with the given representation as new state
to be stored.
|
void |
redirectPermanent(Reference targetRef)
Permanently redirects the client to a target URI.
|
void |
redirectPermanent(String targetUri)
Permanently redirects the client to a target URI.
|
void |
redirectSeeOther(Reference targetRef)
Redirects the client to a different URI that SHOULD be retrieved using a
GET method on that resource.
|
void |
redirectSeeOther(String targetUri)
Redirects the client to a different URI that SHOULD be retrieved using a
GET method on that resource.
|
void |
redirectTemporary(Reference targetRef)
Temporarily redirects the client to a target URI.
|
void |
redirectTemporary(String targetUri)
Temporarily redirects the client to a target URI.
|
void |
setAllowedMethods(Set<Method> allowedMethods)
Sets the set of methods allowed on the requested resource.
|
void |
setAnnotated(boolean annotated)
Indicates if annotations are supported.
|
void |
setAutoCommitting(boolean autoCommitting)
Indicates if the response should be automatically committed.
|
void |
setChallengeRequests(List<ChallengeRequest> requests)
Sets the list of authentication requests sent by an origin server to a
client.
|
void |
setCommitted(boolean committed)
Indicates if the response has already been committed.
|
void |
setConditional(boolean conditional)
Indicates if conditional handling is enabled.
|
void |
setCookieSettings(Series<CookieSetting> cookieSettings)
Sets the cookie settings provided by the server.
|
void |
setDimensions(Set<Dimension> dimensions)
Sets the set of dimensions on which the response entity may vary.
|
void |
setExisting(boolean exists)
Indicates if the identified resource exists.
|
void |
setLocationRef(Reference locationRef)
Sets the reference that the client should follow for redirections or
resource creations.
|
void |
setLocationRef(String locationUri)
Sets the reference that the client should follow for redirections or
resource creations.
|
void |
setNegotiated(boolean negotiateContent)
Indicates if content negotiation of response entities is enabled.
|
void |
setOnSent(Uniform onSentCallback)
Sets the callback invoked after sending the response.
|
void |
setServerInfo(ServerInfo serverInfo)
Sets the server-specific information.
|
void |
setStatus(Status status)
Sets the status.
|
void |
setStatus(Status status,
String message)
Sets the status.
|
void |
setStatus(Status status,
Throwable throwable)
Sets the status.
|
void |
setStatus(Status status,
Throwable throwable,
String message)
Sets the status.
|
void |
updateAllowedMethods()
Invoked when the list of allowed methods needs to be updated.
|
protected void |
updateDimensions()
Update the dimensions that were used for content negotiation.
|
doCatch, doInit, doRelease, getAllowedMethods, getApplication, getChallengeRequests, getChallengeResponse, getClientInfo, getConditions, getContext, getConverterService, getCookies, getCookieSettings, getDimensions, getHostRef, getLocationRef, getLogger, getMatrix, getMaxForwards, getMetadataService, getMethod, getOriginalRef, getProtocol, getQuery, getRanges, getReference, getReferrerRef, getRequest, getRequestAttributes, getRequestEntity, getResponse, getResponseAttributes, getResponseEntity, getRootRef, getServerInfo, getStatus, getStatusService, init, isConfidential, release, setRequest, setResponse, toObject, toRepresentation
public ServerResource()
UniformResource.init(Context, Request, Response)
() method will be invoked right
after the creation of the resource.public void abort()
public void commit()
protected Representation delete() throws ResourceException
isNegotiated()
, otherwise the delete(Variant)
method is
invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.ResourceException
protected Representation delete(Variant variant) throws ResourceException
isNegotiated()
, otherwise the delete()
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.variant
- The variant of the response entity.ResourceException
get(Variant)
,
HTTP DELETE methodprotected Representation describeVariants()
protected Representation doConditionalHandle() throws ResourceException
getInfo()
or getInfo(Variant)
methods might
be invoked.ResourceException
protected Representation doHandle() throws ResourceException
get()
, post(Representation)
,
put(Representation)
, delete()
, head()
or
options()
methods.ResourceException
protected Representation doHandle(Variant variant) throws ResourceException
get(Variant)
, post(Representation,Variant)
,
put(Representation,Variant)
, delete(Variant)
,
head(Variant)
or options(Variant)
methods.variant
- The response variant expected.ResourceException
protected Representation doNegotiatedHandle() throws ResourceException
get(Variant)
,
post(Representation,Variant)
,
put(Representation,Variant)
, delete(Variant)
,
head(Variant)
or options(Variant)
methods.Status.CLIENT_ERROR_NOT_ACCEPTABLE
status is set.ResourceException
protected Representation get() throws ResourceException
isNegotiated()
, otherwise the get(Variant)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.ResourceException
protected Representation get(Variant variant) throws ResourceException
isNegotiated()
, otherwise the get()
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.variant
- The variant whose full representation must be returned.ResourceException
get(Variant)
protected RepresentationInfo getInfo() throws ResourceException
Representation
class is that it is much lighter to
create. This method is only invoked if content negotiation has been
disabled as indicated by the isNegotiated()
, otherwise the
getInfo(Variant)
method is invoked.get()
method.ResourceException
protected RepresentationInfo getInfo(Variant variant) throws ResourceException
Representation
class is that it is much lighter to
create. A variant parameter is passed to indicate which representation
should be returned if any.isNegotiated()
, otherwise the
getInfo(Variant)
method is invoked.get(Variant)
method.variant
- The variant whose representation information must be returned.ResourceException
public Uniform getOnSent()
protected Variant getPreferredVariant(List<Variant> variants)
ClientInfo.getPreferredVariant(List, MetadataService)
method.variants
- The available variants.public List<Variant> getVariants()
protected List<Variant> getVariants(Method method)
method
- The method.public Representation handle()
isConditional()
and isNegotiated()
method to determine
which one of the doConditionalHandle()
,
doNegotiatedHandle()
and doHandle()
methods should be
invoked. It also catches any ResourceException
thrown and updates
the response status using the
setStatus(Status, Throwable, String)
method.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
, then
updateAllowedMethods()
is invoked to give the resource a chance
to inform the client about the allowed methods.handle
in class UniformResource
protected Representation head() throws ResourceException
isNegotiated()
, otherwise the
head(Variant)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.ResourceException
protected Representation head(Variant variant) throws ResourceException
isNegotiated()
, otherwise the head()
method is invoked.Representation
. In other cases, you need to
override this method in order to provide your own implementation. *variant
- The variant whose full representation must be returned.ResourceException
get(Variant)
public boolean isAnnotated()
public boolean isAutoCommitting()
commit()
later on, using another thread.public boolean isCommitted()
public boolean isConditional()
public boolean isExisting()
public boolean isInRole(String roleName)
roleName
- The role name to test.public boolean isNegotiated()
protected Representation options() throws ResourceException
isNegotiated()
, otherwise the
options(Variant)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.ResourceException
protected Representation options(Variant variant) throws ResourceException
isNegotiated()
, otherwise the
options()
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.variant
- The variant of the response entity.ResourceException
get(Variant)
protected Representation post(Representation entity) throws ResourceException
isNegotiated()
, otherwise the
post(Representation, Variant)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.entity
- The posted entity.ResourceException
get(Variant)
,
HTTP
POST methodprotected Representation post(Representation entity, Variant variant) throws ResourceException
isNegotiated()
, otherwise the
post(Representation)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.entity
- The posted entity.variant
- The variant of the response entity.ResourceException
protected Representation put(Representation entity) throws ResourceException
isNegotiated()
, otherwise the
put(Representation, Variant)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.entity
- The representation to store.ResourceException
protected Representation put(Representation representation, Variant variant) throws ResourceException
isNegotiated()
, otherwise the
put(Representation)
method is invoked.Status.CLIENT_ERROR_METHOD_NOT_ALLOWED
.representation
- The representation to store.variant
- The variant of the response entity.ResourceException
get(Variant)
,
HTTP PUT methodpublic void redirectPermanent(Reference targetRef)
targetRef
- The target URI reference.public void redirectPermanent(String targetUri)
Request.getResourceRef()
and Reference.getBaseRef()
.targetUri
- The target URI.public void redirectSeeOther(Reference targetRef)
targetRef
- The target reference.public void redirectSeeOther(String targetUri)
Request.getResourceRef()
and Reference.getBaseRef()
.targetUri
- The target URI.public void redirectTemporary(Reference targetRef)
targetRef
- The target reference.public void redirectTemporary(String targetUri)
Request.getResourceRef()
and Reference.getBaseRef()
.targetUri
- The target URI.public void setAllowedMethods(Set<Method> allowedMethods)
CopyOnWriteArraySet
for
example.allowedMethods
- The set of methods allowed on the requested resource.Response.setAllowedMethods(Set)
public void setAnnotated(boolean annotated)
annotated
- Indicates if annotations are supported.public void setAutoCommitting(boolean autoCommitting)
autoCommitting
- True if the response should be automatically committedpublic void setChallengeRequests(List<ChallengeRequest> requests)
CopyOnWriteArrayList
for example.requests
- The list of authentication requests sent by an origin server
to a client.Response.setChallengeRequests(List)
public void setCommitted(boolean committed)
committed
- True if the response has already been committed.public void setConditional(boolean conditional)
conditional
- True if conditional handling is enabled.public void setCookieSettings(Series<CookieSetting> cookieSettings)
cookieSettings
- The cookie settings provided by the server.Response.setCookieSettings(Series)
public void setDimensions(Set<Dimension> dimensions)
CopyOnWriteArraySet
for
example.dimensions
- The set of dimensions on which the response entity may vary.Response.setDimensions(Set)
public void setExisting(boolean exists)
exists
- Indicates if the identified resource exists.public void setLocationRef(Reference locationRef)
locationRef
- The reference to set.Response.setLocationRef(Reference)
public void setLocationRef(String locationUri)
Request.getResourceRef()
and
Reference.getBaseRef()
.locationUri
- The URI to set.Response.setLocationRef(String)
public void setNegotiated(boolean negotiateContent)
negotiateContent
- True if content negotiation of response entities is enabled.public void setOnSent(Uniform onSentCallback)
onSentCallback
- The callback invoked after sending the response.public void setServerInfo(ServerInfo serverInfo)
serverInfo
- The server-specific information.Response.setServerInfo(ServerInfo)
public void setStatus(Status status)
status
- The status to set.Response.setStatus(Status)
public void setStatus(Status status, String message)
status
- The status to set.message
- The status message.Response.setStatus(Status, String)
public void setStatus(Status status, Throwable throwable)
status
- The status to set.throwable
- The related error or exception.Response.setStatus(Status, Throwable)
public void setStatus(Status status, Throwable throwable, String message)
status
- The status to set.throwable
- The related error or exception.message
- The status message.Response.setStatus(Status, Throwable, String)
public void updateAllowedMethods()
UniformResource.getAllowedMethods()
or the setAllowedMethods(Set)
methods should be used. The default implementation lists the annotated
methods.protected void updateDimensions()
Dimension.CHARACTER_SET
, Dimension.ENCODING
,
Dimension.LANGUAGE
and Dimension.MEDIA_TYPE
constants.Copyright © 2005–2016. All rights reserved.