Package org.restlet.resource
Class Resource
java.lang.Object
org.restlet.resource.Resource
- Direct Known Subclasses:
ClientResource,ServerResource
Base resource class exposing the uniform REST interface. Intended conceptual
target of a hypertext reference. An uniform resource encapsulates a
It also defines a precise life cycle. First, the instance is created and the
Then, the abstract
Finally, the final
Note also that throwable raised such as
"The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface between components. By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. Implementations are decoupled from the services they provide, which encourages independent evolvability." Roy T. Fielding
Concurrency note: contrary to the
Context, a Request and a Response, corresponding to a
specific target resource.It also defines a precise life cycle. First, the instance is created and the
init(Context, Request, Response) method is invoked. If you need to
do some additional initialization, you should just override the
doInit() method.Then, the abstract
handle() method can be invoked. For concrete
behavior, see the ClientResource and ServerResource
subclasses. Note that the state of the resource can be changed several times
and the handle() method called more than once, but always by the
same thread.Finally, the final
release() method can be called to clean-up the
resource, with a chance for the developer to do some additional clean-up by
overriding the doRelease() method.Note also that throwable raised such as
Error and Exception
can be caught in a single point by overriding the doCatch(Throwable)
method."The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface between components. By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. Implementations are decoupled from the services they provide, which encourages independent evolvability." Roy T. Fielding
Concurrency note: contrary to the
Uniform class and its
main Restlet subclass where a single instance can handle several
calls concurrently, one instance of Resource is created for each call
handled and accessed by only one thread at a time.- Author:
- Jerome Louvel
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidInvoked when aThrowableis caught during initialization, handling or releasing.protected voidInvoked when an error response status is received.protected final voidInvoked when an error response status is received.protected voiddoInit()Set-up method that can be overridden in order to initialize the state of the resource.protected voidClean-up method that can be overridden in order to release the state of the resource.Returns the set of methods allowed for the current client by the resource.Returns the parent application.abstract StringgetAttribute(String name) Returns the attribute value by looking up the given name in the request or response attributes maps.Returns the list of authentication requests sent by an origin server to a client.Returns the authentication response sent by a client to an origin server.Returns the client-specific information.Returns the modifiable conditions applying to this request.Returns the application's content negotiation service or create a new one.Returns the current context.Returns the application's converter service or create a new one.Returns the modifiable series of cookies provided by the client.Returns the modifiable series of cookie settings provided by the server.Returns the modifiable set of selecting dimensions on which the response entity may vary.Returns the host reference.Returns the reference that the client should follow for redirections or resource creations.Returns the logger.Returns the resource reference's optional matrix.getMatrixValue(String name) Returns the first value of the matrix parameter given its name if existing, or null.intReturns the maximum number of intermediaries.Returns the application's metadata service or create a new one.Returns the method.Returns the original reference as requested by the client.Returns the protocol by first returning the resourceRef.schemeProtocol property if it is set, or the baseRef.schemeProtocol property otherwise.Returns the list of proxy authentication requests sent by an origin server to a client.Returns the proxy authentication response sent by a client to an origin server.getQuery()Returns the resource reference's optional query.getQueryValue(String name) Returns the first value of the query parameter given its name if existing, or null.Returns the ranges to return from the target resource's representation.Returns the URI reference.Returns the referrer reference if available.Returns the handled request.Returns the request attributes.Returns the request cache directives.Returns the request entity representation.Returns the handled response.Returns the response attributes.Returns the response cache directives.Returns the response entity representation.Returns the application root reference.Returns the server-specific information.Returns the status.Returns the application's status service or create a new one.abstract Representationhandle()Handles the call composed of the current context, request and response.voidInitialization method setting the environment of the current resource instance.booleanIndicates if the message was or will be exchanged confidentially, for example via a SSL-secured connection.booleanIndicates if the call is loggablefinal voidrelease()Releases the resource by callingdoRelease().voidsetApplication(Application application) Sets the parent application.abstract voidsetAttribute(String name, Object value) Sets the request or response attribute value.voidsetQueryValue(String name, String value) Sets the query value for the named parameter.voidsetRequest(Request request) Sets the handled request.voidsetResponse(Response response) Sets the handled response.static Booleanstatic Bytestatic Doublestatic Floatstatic Integerstatic Long<T> TtoObject(Representation source, Class<T> target) Converts a representation into a Java object.toRepresentation(Object source) Converts an object into a representation based on the default converter service variant.toRepresentation(Object source, MediaType target) Converts an object into a representation based on a given media type.toRepresentation(Object source, Variant target) Converts an object into a representation based on client preferences.static ShorttoString()
-
Constructor Details
-
Resource
public Resource()
-
-
Method Details
-
toBoolean
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Booleanvalue or null.
-
toByte
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Bytevalue or null.
-
toDouble
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Doublevalue or null.
-
toFloat
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Floatvalue or null.
-
toInteger
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Integervalue or null.
-
toLong
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Longvalue or null.
-
toShort
- Parameters:
value- The value to convert or null.- Returns:
- The converted
Shortvalue or null.
-
doCatch
Invoked when aThrowableis caught during initialization, handling or releasing.- Parameters:
throwable- The caught error or exception.
-
doError
Invoked when an error response status is received.- Parameters:
errorStatus- The error status received.
-
doError
Invoked when an error response status is received.- Parameters:
errorStatus- The error status received.errorMessage- The custom error message.
-
doInit
Set-up method that can be overridden in order to initialize the state of the resource. By default it does nothing.- Throws:
ResourceException- See Also:
-
doRelease
Clean-up method that can be overridden in order to release the state of the resource. By default it does nothing.- Throws:
ResourceException- See Also:
-
getAllowedMethods
Returns the set of methods allowed for the current client by the resource. The result can vary based on the client's user agent, authentication and authorization data provided by the client.- Returns:
- The set of allowed methods.
-
getApplication
Returns the parent application. If it wasn't set, it attempts to retrieve the current one viaApplication.getCurrent()if it exists, or instantiates a new one as a last resort.- Returns:
- The parent application if it exists, or a new one.
-
getAttribute
Returns the attribute value by looking up the given name in the request or response attributes maps. This is typically used for variables that are declared in the URI template used to route the call to this resource.- Parameters:
name- The attribute name.- Returns:
- The matching request or response attribute value.
-
getChallengeRequests
Returns the list of authentication requests sent by an origin server to a client. If none is available, an empty list is returned.- Returns:
- The list of authentication requests.
- See Also:
-
getChallengeResponse
Returns the authentication response sent by a client to an origin server.- Returns:
- The authentication response sent by a client to an origin server.
- See Also:
-
getClientInfo
Returns the client-specific information. Creates a new instance if no one has been set.- Returns:
- The client-specific information.
- See Also:
-
getConditions
Returns the modifiable conditions applying to this request. Creates a new instance if no one has been set.- Returns:
- The conditions applying to this call.
- See Also:
-
getConnegService
Returns the application's content negotiation service or create a new one.- Returns:
- The content negotiation service.
-
getContext
Returns the current context.- Returns:
- The current context.
-
getConverterService
Returns the application's converter service or create a new one.- Returns:
- The converter service.
-
getCookies
Returns the modifiable series of cookies provided by the client. Creates a new instance if no one has been set.- Returns:
- The cookies provided by the client.
- See Also:
-
getCookieSettings
Returns the modifiable series of cookie settings provided by the server. Creates a new instance if no one has been set.- Returns:
- The cookie settings provided by the server.
- See Also:
-
getDimensions
Returns the modifiable set of selecting dimensions on which the response entity may vary. If some server-side content negotiation is done, this set should be properly updated, other it can be left empty. Creates a new instance if no one has been set.- Returns:
- The set of dimensions on which the response entity may vary.
- See Also:
-
getHostRef
Returns the host reference. This may be different from the resourceRef's host, for example for URNs and other URIs that don't contain host information.- Returns:
- The host reference.
- See Also:
-
getLocationRef
Returns the reference that the client should follow for redirections or resource creations.- Returns:
- The redirection reference.
- See Also:
-
getLogger
Returns the logger.- Returns:
- The logger.
-
getMatrix
Returns the resource reference's optional matrix.- Returns:
- The resource reference's optional matrix.
- See Also:
-
getMatrixValue
Returns the first value of the matrix parameter given its name if existing, or null.- Parameters:
name- The matrix parameter name.- Returns:
- The first value of the matrix parameter.
-
getMaxForwards
public int getMaxForwards()Returns the maximum number of intermediaries.- Returns:
- The maximum number of intermediaries.
-
getMetadataService
Returns the application's metadata service or create a new one.- Returns:
- The metadata service.
-
getMethod
Returns the method.- Returns:
- The method.
- See Also:
-
getOriginalRef
Returns the original reference as requested by the client. Note that this property is not used during request routing.- Returns:
- The original reference.
- See Also:
-
getProtocol
Returns the protocol by first returning the resourceRef.schemeProtocol property if it is set, or the baseRef.schemeProtocol property otherwise.- Returns:
- The protocol or null if not available.
- See Also:
-
getProxyChallengeRequests
Returns the list of proxy authentication requests sent by an origin server to a client. If none is available, an empty list is returned.- Returns:
- The list of proxy authentication requests.
- See Also:
-
getProxyChallengeResponse
Returns the proxy authentication response sent by a client to an origin server.- Returns:
- The proxy authentication response sent by a client to an origin server.
- See Also:
-
getQuery
Returns the resource reference's optional query. Note that modifications to the returnedFormobject aren't reported to the underlying reference.- Returns:
- The resource reference's optional query.
- See Also:
-
getQueryValue
Returns the first value of the query parameter given its name if existing, or null.- Parameters:
name- The query parameter name.- Returns:
- The first value of the query parameter.
-
getRanges
Returns the ranges to return from the target resource's representation.- Returns:
- The ranges to return.
- See Also:
-
getReference
Returns the URI reference.- Returns:
- The URI reference.
-
getReferrerRef
Returns the referrer reference if available.- Returns:
- The referrer reference.
-
getRequest
Returns the handled request.- Returns:
- The handled request.
-
getRequestAttributes
Returns the request attributes.- Returns:
- The request attributes.
- See Also:
-
getRequestCacheDirectives
Returns the request cache directives. Note that when used with HTTP connectors, this property maps to the "Cache-Control" header.- Returns:
- The cache directives.
-
getRequestEntity
Returns the request entity representation.- Returns:
- The request entity representation.
-
getResponse
Returns the handled response.- Returns:
- The handled response.
-
getResponseAttributes
Returns the response attributes.- Returns:
- The response attributes.
- See Also:
-
getResponseCacheDirectives
Returns the response cache directives. Note that when used with HTTP connectors, this property maps to the "Cache-Control" header.- Returns:
- The cache directives.
-
getResponseEntity
Returns the response entity representation.- Returns:
- The response entity representation.
-
getRootRef
Returns the application root reference.- Returns:
- The application root reference.
- See Also:
-
getServerInfo
Returns the server-specific information. Creates a new instance if no one has been set.- Returns:
- The server-specific information.
- See Also:
-
getStatus
Returns the status.- Returns:
- The status.
- See Also:
-
getStatusService
Returns the application's status service or create a new one.- Returns:
- The status service.
-
handle
Handles the call composed of the current context, request and response.- Returns:
- The optional response entity.
-
init
Initialization method setting the environment of the current resource instance. It the calls thedoInit()method that can be overridden.- Parameters:
context- The current context.request- The handled request.response- The handled response.
-
isConfidential
public boolean isConfidential()Indicates if the message was or will be exchanged confidentially, for example via a SSL-secured connection.- Returns:
- True if the message is confidential.
- See Also:
-
isLoggable
public boolean isLoggable()Indicates if the call is loggable- Returns:
- True if the call is loggable
-
release
public final void release()Releases the resource by callingdoRelease(). -
setApplication
Sets the parent application.- Parameters:
application- The parent application.
-
setAttribute
Sets the request or response attribute value.- Parameters:
name- The attribute name.value- The attribute to set.
-
setQueryValue
Sets the query value for the named parameter. If no query is defined, it creates one. If the same parameter exists, it replaces it altogether.- Parameters:
name- The query parameter name.value- The query parameter value.
-
setRequest
Sets the handled request.- Parameters:
request- The handled request.
-
setResponse
Sets the handled response.- Parameters:
response- The handled response.
-
toObject
Converts a representation into a Java object. Leverages theConverterService.- Type Parameters:
T- The expected class of the Java object.- Parameters:
source- The source representation to convert.target- The target class of the Java object.- Returns:
- The converted Java object.
- Throws:
ResourceException
-
toRepresentation
Converts an object into a representation based on the default converter service variant.- Parameters:
source- The object to convert.- Returns:
- The wrapper representation.
- Throws:
IOException
-
toRepresentation
Converts an object into a representation based on a given media type.- Parameters:
source- The object to convert.target- The target representation media type.- Returns:
- The wrapper representation.
- Throws:
IOException
-
toRepresentation
Converts an object into a representation based on client preferences.- Parameters:
source- The object to convert.target- The target representation variant.- Returns:
- The wrapper representation.
- Throws:
IOException
-
toString
-