Analysis of resource and its subclass

Source: Internet
Author: User
Resource is an abstract class.

Is a basic resource class that exposes a unified rest interface. Resource encapsulates context, request, and response to convert the concept from rest to hypertext.

From the compoent, application, services, filter, and router mentioned earlier to the finder mentioned later, they are all concepts at the rest layer. resource is the conversion interface from rest to HTML, it is a rest terminal (endpoint ).

From component to Resource, after application, services, filter, route, and finder, the handle () method of uniform is called. From the perspective of design pattern, it looks like the modifier pattern.

Services provides a variety of configurable services that can be added as needed. filter, router, and finder can also modify system behavior as needed. This design, similar to the decorator model, provides flexible reuse and expansion space. The core of the restlet framework is the decorator mode, and the object of the decoration is resource.


1. Resource Lifecycle

Resource has a clear life cycle:

First, the instance of the resource subclass is created.
Then, call the init (context, request, response) method.
Then, doinit () provides some customization behavior for the subclass.
After the preceding method defined by resource is executed, the unified handle () method is executed. The handle () method is implemented differently in clientresource and serverresource.
Finally, the dorelease () method is called before the release () method is called. This method is also an abstract method defined by resource to provide some operation extension for the subclass.

When errors and exceptions occur, You can overwrite docatch (throwable) to capture and handle errors and exceptions.


Ii. serverresource

Serverresource is a sub-type of resource. It is the basic resource class on the server side and encapsulates request and response.

In two cases, the lifecycle of serverresource is managed by the finder:
1. When a subclass is created, the CREATE () method provided by the finder is directly created;
2. A subclass is created. When associating components, filter. setnext () or router. Attach () is used to set them to filter or router.

The handle () method of serverresource is the place where rest begins to convert to HTML.
1. Determine isconditional () and isnegotiated () to determine which method doconditionalhandle (), donegotiatedhandle (), and dohandle () are executed.
2. During execution, any resourceexception will be recorded in the current thread environment and set the status through setstatus (status, throwable, string) to provide the status for statusservice processing.

3. Judgment process of doconditionalhandle (), donegotiatedhandle () and dohandle ()


1. isconditional () is used to determine whether the request contains the following content:


If-match
If-modified-since
If-None-match
If-Range
If-unmodified-since
If no, execute the next judgment isnegotiated ().

2. isnegotiated () is used to determine whether a parameter is required for the called method. If not, skip this step and execute dohandle ().

3. dohandle ()

From dohanlde (), call the methods corresponding to the HTTP method, such as put (), get (), post (), delete (), head (), and options () and other custom methods.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.