public abstract class ServerResource extends Resource
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 Resource.init(Context, Request, Response)
method is invoked,
setting the
context, request and response. You can intercept this by overriding the Resource.doInit()
method. Then, if the
response status is still a success,
the handle()
method is invoked to actually handle the call. Finally,
the final Resource.release()
method is invoked to do the necessary clean-up,
which you can intercept by overriding the Resource.doRelease()
method. During
this life cycle, if any exception is caught, then the 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 void |
doCatch(java.lang.Throwable throwable)
Invoked when an error or an exception is caught during initialization,
handling or releasing.
|
protected Representation |
doConditionalHandle()
Handles a call by first verifying the optional request conditions and
continue the processing if possible.
|
protected void |
doError(Status errorStatus)
By default, it sets the status on the response.
|
protected Representation |
doHandle()
Effectively handles a call without content negotiation of the response
entity.
|
protected Representation |
doHandle(org.restlet.engine.resource.MethodAnnotationInfo annotationInfo,
Variant variant)
Effectively handles a call with content negotiation of the response
entity using an annotated method.
|
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 org.restlet.engine.resource.MethodAnnotationInfo |
getAnnotation(Method method)
Returns the first annotation descriptor matching the given method.
|
protected org.restlet.engine.resource.MethodAnnotationInfo |
getAnnotation(Method method,
Form query,
Representation entity)
Returns the first annotation descriptor matching the given method.
|
protected java.util.List<org.restlet.engine.resource.AnnotationInfo> |
getAnnotations()
Returns the annotation descriptors.
|
java.lang.String |
getAttribute(java.lang.String name)
Returns the attribute value by looking up the given name in the request
attributes maps.
|
java.lang.String |
getDescription()
Returns the description.
|
protected RepresentationInfo |
getInfo()
Returns information about the resource's representation.
|
protected RepresentationInfo |
getInfo(Variant variant)
Returns information about the resource's representation.
|
java.lang.String |
getName()
Returns the display name.
|
Uniform |
getOnSent()
Returns the callback invoked after sending the response.
|
protected Variant |
getPreferredVariant(java.util.List<Variant> variants)
Returns the preferred variant among a list of available variants.
|
Role |
getRole(java.lang.String name)
Retrieves an existing role or creates a new one if needed based on its
name.
|
java.util.List<Variant> |
getVariants()
Returns a modifiable list of exposed variants for the current request
method.
|
protected java.util.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 boolean |
hasAnnotations()
Indicates if annotations were defined on 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(java.lang.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 |
patch(Representation entity)
Apply a patch entity to the current representation of the resource
retrieved by calling
get() . |
protected Representation |
patch(Representation entity,
Variant variant)
Apply a patch entity to the current representation of the resource
retrieved by calling
get() . |
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(java.lang.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(java.lang.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(java.lang.String targetUri)
Temporarily redirects the client to a target URI.
|
void |
setAllowedMethods(java.util.Set<Method> allowedMethods)
Sets the set of methods allowed on the requested resource.
|
void |
setAnnotated(boolean annotated)
Indicates if annotations are supported.
|
void |
setAttribute(java.lang.String name,
java.lang.Object value)
Sets the response attribute value.
|
void |
setAutoCommitting(boolean autoCommitting)
Indicates if the response should be automatically committed.
|
void |
setChallengeRequests(java.util.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 |
setDescription(java.lang.String description)
Sets the description.
|
void |
setDimensions(java.util.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(java.lang.String locationUri)
Sets the reference that the client should follow for redirections or
resource creations.
|
void |
setName(java.lang.String name)
Sets the display name.
|
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 |
setProxyChallengeRequests(java.util.List<ChallengeRequest> requests)
Sets the list of proxy authentication requests sent by an origin server
to a client.
|
void |
setServerInfo(ServerInfo serverInfo)
Sets the server-specific information.
|
void |
setStatus(Status status)
Sets the status.
|
void |
setStatus(Status status,
java.lang.String message)
Sets the status.
|
void |
setStatus(Status status,
java.lang.Throwable throwable)
Sets the status.
|
void |
setStatus(Status status,
java.lang.Throwable throwable,
java.lang.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.
|
doError, doInit, doRelease, getAllowedMethods, getApplication, getChallengeRequests, getChallengeResponse, getClientInfo, getConditions, getConnegService, getContext, getConverterService, getCookies, getCookieSettings, getDimensions, getHostRef, getLocationRef, getLogger, getMatrix, getMatrixValue, getMaxForwards, getMetadataService, getMethod, getOriginalRef, getProtocol, getProxyChallengeRequests, getProxyChallengeResponse, getQuery, getQueryValue, getRanges, getReference, getReferrerRef, getRequest, getRequestAttributes, getRequestCacheDirectives, getRequestEntity, getResponse, getResponseAttributes, getResponseCacheDirectives, getResponseEntity, getRootRef, getServerInfo, getStatus, getStatusService, init, isConfidential, isLoggable, release, setApplication, setQueryValue, setRequest, setResponse, toBoolean, toByte, toDouble, toFloat, toInteger, toLong, toObject, toRepresentation, toRepresentation, toRepresentation, toShort, toString
public ServerResource()
Resource.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 void doCatch(java.lang.Throwable throwable)
StatusService.toStatus(Throwable, Resource)
.protected Representation doConditionalHandle() throws ResourceException
getInfo()
or getInfo(Variant)
methods might
be invoked.ResourceException
protected void doError(Status errorStatus)
protected Representation doHandle() throws ResourceException
get()
, post(Representation)
,
put(Representation)
, delete()
, head()
or options()
methods.ResourceException
protected Representation doHandle(org.restlet.engine.resource.MethodAnnotationInfo annotationInfo, Variant variant) throws ResourceException
annotationInfo
- The annotation descriptor.variant
- The response variant expected (can be null).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 org.restlet.engine.resource.MethodAnnotationInfo getAnnotation(Method method) throws java.io.IOException
method
- The method to match.java.io.IOException
protected org.restlet.engine.resource.MethodAnnotationInfo getAnnotation(Method method, Form query, Representation entity) throws java.io.IOException
method
- The method to match.query
- The query parameters.entity
- The request entity or null.java.io.IOException
protected java.util.List<org.restlet.engine.resource.AnnotationInfo> getAnnotations()
public java.lang.String getAttribute(java.lang.String name)
getAttribute
in class Resource
name
- The attribute name.public java.lang.String getDescription()
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 java.lang.String getName()
public Uniform getOnSent()
protected Variant getPreferredVariant(java.util.List<Variant> variants)
ConnegService.getPreferredVariant(List, Request, org.restlet.service.MetadataService)
method.variants
- The available variants.public Role getRole(java.lang.String name)
name
- The role name to find or create.public java.util.List<Variant> getVariants()
java.io.IOException
protected java.util.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.protected boolean hasAnnotations()
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(java.lang.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 patch(Representation entity) throws ResourceException
get()
. By default, the
ConverterService.applyPatch(Representation, Representation)
method is used and then the
put(Representation)
method called.entity
- The patch entity to apply.ResourceException
protected Representation patch(Representation entity, Variant variant) throws ResourceException
get()
. By default, the
ConverterService.applyPatch(Representation, Representation)
method is used and then the
put(Representation, Variant)
method
called.entity
- The patch entity to apply.variant
- The variant of the response entity.ResourceException
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(java.lang.String targetUri)
Request.getResourceRef()
and
Reference.getBaseRef()
.targetUri
- The target URI.public void redirectSeeOther(Reference targetRef)
targetRef
- The target reference.public void redirectSeeOther(java.lang.String targetUri)
Request.getResourceRef()
and
Reference.getBaseRef()
.targetUri
- The target URI.public void redirectTemporary(Reference targetRef)
targetRef
- The target reference.public void redirectTemporary(java.lang.String targetUri)
Request.getResourceRef()
and
Reference.getBaseRef()
.targetUri
- The target URI.public void setAllowedMethods(java.util.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 setAttribute(java.lang.String name, java.lang.Object value)
setAttribute
in class Resource
name
- The attribute name.value
- The attribute to set.public void setAutoCommitting(boolean autoCommitting)
autoCommitting
- True if the response should be automatically committedpublic void setChallengeRequests(java.util.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 setDescription(java.lang.String description)
description
- The description.public void setDimensions(java.util.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(java.lang.String locationUri)
Request.getResourceRef()
and Reference.getBaseRef()
.locationUri
- The URI to set.Response.setLocationRef(String)
public void setName(java.lang.String name)
name
- The display name.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 setProxyChallengeRequests(java.util.List<ChallengeRequest> requests)
CopyOnWriteArrayList
for example.requests
- The list of proxy authentication requests sent by an origin
server to a client.Response.setProxyChallengeRequests(List)
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, java.lang.String message)
status
- The status to set.message
- The status message.Response.setStatus(Status, String)
public void setStatus(Status status, java.lang.Throwable throwable)
status
- The status to set.throwable
- The related error or exception.Response.setStatus(Status, Throwable)
public void setStatus(Status status, java.lang.Throwable throwable, java.lang.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()
Resource.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-2024 Restlet.