restcgi::resource Class Reference

List of all members.

Detailed Description

Base class for REST resource.

The application uses this as the base class for its resources or as the base class for proxies for its resources (non-intrusive).

The basic operation of resources is as follows:

  1. locate() is called recursively on this class but on different resource objects to narrow to the resource identified in the method. The application is creating the resource objects as this proceeds.
  2. On the located resource object, this class's method is called which corresponds with the type in the request: get(), put(), etc.
  3. This class implements the method, e.g. get(), as series of calls to the derived class, e.g. read(). The method itself can be overridden, but it is usually not necessary. The submethods must be overridden by the derived class to do the actual work.
  4. Exceptions can be thrown and will be automatically processed. The explicit status exceptions, e.g. bad_request, will result in the given status returned in the response.

Note that the web server handles the TRACE method without calling the application.

See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

rest


Public Types

typedef boost::shared_ptr<
resource
pointer
 shared ptr
typedef boost::shared_ptr<
const resource
const_pointer
 const shared ptr
typedef restcgi::method method_type
 method type
typedef boost::shared_ptr<
method_type
method_pointer
 method shared ptr
typedef uripp::path uri_path_type
 URI path type.
typedef restcgi::uri_info uri_info_type
 uri_info type
typedef restcgi::version version_type
 version type
typedef restcgi::version_tag version_tag_type
 version tag type
typedef restcgi::version_constraint version_constraint_type
 version_constraint type

Public Member Functions

void method (method_pointer m)
 Set method (called before locate() by rest processing).
method_pointer method () const
virtual pointer locate (uri_path_type &path)
virtual void get_method ()
virtual void put_method ()
virtual void post_method ()
virtual void del_method ()
virtual void options_method ()
virtual void head_method ()
int methods_allowed_mask () const
 Get methods-allowed mask.
pointer child () const
 Get child pointer (MUST set chain_children in root).
pointer parent () const
 Get parent pointer (MUST set chain_children in root).
bool chain_children () const
 Chain children?
void child (pointer rthis, pointer cr)
 Set child.

Protected Types

typedef uripp::query uri_query_type
typedef std::pair< uri_path_type,
version_type
id_version_type

Protected Member Functions

 resource (int methods_allowed_mask, bool chain_children=false)
const method_eme () const
 Get method enum, for example method_e::GET (valid after locate()).
const uri_info_typeuri_info () const
version_constraint_type version_constraint () const
 Get version constraint (valid after locate()).
void default_response (bool v)
virtual version_type read (bool veronly)
virtual version_type update (icontent::pointer ic)
virtual pointer create_child ()
virtual id_version_type create (icontent::pointer ic)
virtual void del ()
virtual void on_responding (status_code_e &sc, response_hdr &rh, content_hdr &ch)
virtual void write (ocontent::pointer oc)


Member Typedef Documentation

typedef std::pair<uri_path_type, version_type> restcgi::resource::id_version_type [protected]

id version pair type

typedef uripp::query restcgi::resource::uri_query_type [protected]

uri_query type


Constructor & Destructor Documentation

restcgi::resource::resource ( int  methods_allowed_mask,
bool  chain_children = false 
) [protected]

Construct. The methods that this resource supports must be declared by or'ing the corresponding method_e enums into the mask argument, e.g. method_e::GET | method_e::DEL. This base class has default implementations for OPTIONS and HEAD (if GET is implemented), but they must be put into the given mask by the derived class in order for this class to use them. If chain_children is set in root (MUST be root) all resources located along URI path are linked. This will enable the child() and parent() methods.

Application programming note: THE MASK MUST BE KEPT IN SYNC WITH METHOD IMPLEMENTATIONS!!!

See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7


Member Function Documentation

virtual id_version_type restcgi::resource::create ( icontent::pointer  ic  )  [protected, virtual]

Create the resource from the input content and save to the application store, returning the id and version information. This is called on the child resource for a POST on th parent. The id is a single-segment URI path and is used to set the content-location response hdr field (the parent path plus this child id). If versioning is not being used a null version can be returned.

Default is to throw internal_server_error.

Exceptions:
exception application can throw any exception

virtual pointer restcgi::resource::create_child (  )  [protected, virtual]

Create a child resource. This is called for post().

Default is to throw internal_server_error.

Exceptions:
exception application can throw any exception

void restcgi::resource::default_response ( bool  v  )  [protected]

Set default response. Seting to true causes on_responding() and write() methods to be skipped. This is convenient for resources that handle multiple methods when some do not require a custom response. (See above.)

virtual void restcgi::resource::del (  )  [protected, virtual]

Delete the resource from the application store.

Default is to throw internal_server_error.

Exceptions:
exception application can throw any exception

virtual void restcgi::resource::del_method (  )  [virtual]

Delete this resource.

Unless overridden (usually not necessary), this will call the following methods:

  1. If request contained a version constraint:
    1. read(veronly=true): Read resource version info from application store (required).
    2. If version returned, assert constraint (if any) satisfied.
  2. del(): Delete the resource from the application store (required).
  3. Initialize status code (OK).
  4. on_responding(): Modify status, response hdr (optional). (Not called if default_response set.)
  5. write(): Write to content stream (optional). (Not called if default_response set.)
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7

virtual void restcgi::resource::get_method (  )  [virtual]

Respond with a representation of this resource in the output content.

Unless overridden (usually not necessary), this will call the following methods:

  1. read(): Read resource from application store (required).
  2. If version returned, assert constraint (if any) satisfied.
  3. Initialize status code (OK) and response hdr (with version info).
  4. on_responding(): Modify status, response and content hdrs (optional). (Not called if default_response set.)
  5. write(): Write to content stream (optional but customary). (Not called if default_response set.)
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3

virtual void restcgi::resource::head_method (  )  [virtual]

Respond with header fields the same as if this were a get() but do not return output content. The application may be able to optimize the read for the HEAD case.

Unless overridden (usually not necessary), this will call the following methods:

  1. read(): Read resource from application store (required).
  2. If version returned, assert constraint (if any) satisfied.
  3. Initialize status code (OK) and response hdr (with version info).
  4. on_responding(): Modify status, response and content hdrs (optional).
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

virtual pointer restcgi::resource::locate ( uri_path_type path  )  [virtual]

Locate the resource at the given path relative to this and update path. This method provides the main means for navigating to the resource specified by the client in terms of the URI path. The application should organize its resources in a hierarchy (basic REST architecture) which can be descended by repeated calls to this method on the returned resources and the updated path (typically using pop_front() at each step).

Result possibilities:

  1. a null pointer is returned and the path is empty then method is performed on this,
  2. null pointer is returned and path is not empty then a bad_request exception is automatically thrown,
  3. pointer to another resource returned then that resource's locate() method is called if the (potentially) updated path is not empty otherwise the method is performed on that resource.

The default is to perform the method on this, but will result in bad_request if path is not empty.

Exceptions:
exception restcgi exceptions are rethrown, others wrapped and rethrown as a bad_request

method_pointer restcgi::resource::method (  )  const [inline]

Get method (valid before and after locate()).

virtual void restcgi::resource::on_responding ( status_code_e sc,
response_hdr rh,
content_hdr ch 
) [protected, virtual]

Modify status code and response and content hdrs prior to responding. This class sends the response but calls this before doing it so that the derived class can add header fields, e.g. content-type. The version information, if any has already been added to the hdrs. Note methods that do not write output content, e.g. del(), will ignore any changes to the content_hdr. In other words, the content-type can always be set regardless of method type.

"hello, world" example (see also write()). This is all that is needed here:

 ch.content_type("text/plain");

If needed, the method type can be checked as follows:

 if (me() == method_e::GET) ...

Default is to do nothing (use status code and hdrs as they are).

virtual void restcgi::resource::options_method (  )  [virtual]

Respond with the "options" available for the resource and, optionally, send a representation of the "options" in the output content.

The required, minimum response, and the default behavior of this base class, is to return the Allow header field based on the "methods allowed mask" set at construction and not send content (note that Content-Length must be explicitly set to 0). See RFC. The special case of the URI path "*", essentially a "ping", is handled elsewhere and does not result in a call to any of the resource objects.

Unless overridden (usually not necessary), this will call the following methods:

  1. Initialize status code (OK) and response hdr (allow field is set to methods_allowed_mask).
  2. on_responding(): Modify status, response and content hdrs (optional).
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2

virtual void restcgi::resource::post_method (  )  [virtual]

Create a subordinate (child) resource with the input content and, optionally, send a representation of the child resource in the output content. Note that the URI path points to the containing (parent) resource which is this resource.

Unless overridden (usually not necessary), this will call the following methods:

  1. create_child(): Create child resource (required).
  2. On child resource:
    1. create(): Create the resource from the input content and save to application store (required).
    2. Initialize status code (CREATED) and response hdr (with version info and content-location to this uri path appended with the returned id).
    3. on_responding(): Modify status, response and content hdrs (optional). (Not called if child default_response set.)
    4. write(): Write to content stream (optional). (Not called if child default_response set.)
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5

virtual void restcgi::resource::put_method (  )  [virtual]

Update the resource with the input content and, optionally, send a representation of the updated resource in the output content. The resource must already exist and this is designed to change one or more of its attributes.

Unless overridden (usually not necessary), this will call the following methods:

  1. read(): Read resource from application store (required).
  2. If version returned, assert constraint (if any) satisfied.
  3. update(): Update the resource from the input content and save to application store (required).
  4. Initialize status code (OK) and response hdr (with version info).
  5. on_responding(): Modify status, response and content hdrs (optional). (Not called if default_response set.)
  6. write(): Write to content stream (optional). (Not called if default_response set.)
See also:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6

virtual version_type restcgi::resource::read ( bool  veronly  )  [protected, virtual]

Read the attributes of the resource from the application store and return the current version information. This is called for get(), put(), del(), and head(). But, note that for del() this is only called if there is a version constraint and then veronly is set to true to indicate that all the attributes are not needed. For head() the application may be able to optimize the resource read. If versioning is not being used a null version can be returned and constraints will be ignored.

Default is to throw internal_server_error.

Exceptions:
exception application can throw any exception

virtual version_type restcgi::resource::update ( icontent::pointer  ic  )  [protected, virtual]

Update the attributes of the resource and save to the application store, returning the new version information. This is called for put(). If versioning is not being used a null version can be returned.

Default is to throw internal_server_error.

Exceptions:
exception application can throw any exception

const uri_info_type& restcgi::resource::uri_info (  )  const [inline, protected]

Get URI info (valid after locate()).

virtual void restcgi::resource::write ( ocontent::pointer  oc  )  [protected, virtual]

Write the resouce as an output content stream. Called by get(), post() on the child, and put(). Note that exceptions are ignored here because the response hdr has already been sent.

"hello, world" example, continued:

 *oc << "hello, world!";

Default is to do nothing (send no content).


Generated on Fri May 15 11:27:12 2009 for restcgi by  doxygen 1.4.7