Wt::WResource Class Reference

An object which can be rendered in the HTTP protocol. More...

#include <Wt/WResource>

Inheritance diagram for Wt::WResource:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 WResource (WObject *parent=0)
 Create a new resource.
 ~WResource ()
 Destroy the resource.
void suggestFileName (const std::string &name)
 Suggest a filename to the user for the data streamed by this resource.
const std::string generateUrl () const
 Generate an URL for this resource.
bool reentrant () const
 Can this resource be streamed reentrantly ?
void setReentrant (bool reentrant)
 Specify if this resource may be streamed reentrantly.
void write (std::ostream &out)
 Stream the resource to a stream.

Public Attributes

Signal< void > dataChanged
 Emit this signal if the data presented in this resource has changed.

Protected Types

typedef std::vector< std::string > ArgumentValues
 Values corresponding to a single argument name.
typedef std::map< std::string,
ArgumentValues
ArgumentMap
 A map for request arguments.

Protected Member Functions

virtual const std::string resourceMimeType () const =0
 Return the mimetype.
virtual bool streamResourceData (std::ostream &stream, const ArgumentMap &arguments)=0
 Stream the data for this resource.
void flush ()
 Flush data for this resource.
virtual void setArguments (const ArgumentMap &arguments)
 Handle the request arguments.
void addHeader (const std::string &name, const std::string &value)
 Add a HTTP header.


Detailed Description

An object which can be rendered in the HTTP protocol.

Besides the main page, other objects may be rendered as additional resources, such as frames or dynamic images.

To serve resources that you create on the fly, you need to specialize this class and reimplement:

In addition, you may want to help the browser to start a suitable helper application, or the user to save the file with a suitable name, by setting an appropriate file name using suggestFileName().

For small resources, or resources that you do not want to generate on the fly, you can also use the WMemoryResource or WFileResource which stream data from respectively memory or a file.

A resource can be used directly in conjunction with WAnchor or a WImage, which has the benefit that these classes are aware of resource changes that you can indicate by emitting the dataChanged signal.


Constructor & Destructor Documentation

Wt::WResource::~WResource (  ) 

Destroy the resource.

You must make sure that the resource is nog longer in use (no longer referred within the application) when deleting it.


Member Function Documentation

void Wt::WResource::suggestFileName ( const std::string &  name  ) 

Suggest a filename to the user for the data streamed by this resource.

For resources, intended to be downloaded by the user, suggest a name used for saving. The filename extension may also help the browser to identify the correct program for opening the resource.

const std::string Wt::WResource::generateUrl (  )  const

Generate an URL for this resource.

Generates a new url that refers to this resource. The url is unique to assure that it is not cached by the web browser, and can thus be used to refer to a new "version" of the resource, which can be indicated by emitting the dataChanged signal.

bool Wt::WResource::reentrant (  )  const [inline]

Can this resource be streamed reentrantly ?

Reentrant resources may be streamed concurrently to the user. Thus, its resourceMimeType() and streamResourceData() functions must be implemented in a thread-safe way.

See also:
setReentrant()

void Wt::WResource::setReentrant ( bool  reentrant  ) 

Specify if this resource may be streamed reentrantly.

A reentrant resource may stream its data concurrently with other resources (and with the main event handling ? perhaps this is not necessary...)

See also:
reentrant()

void Wt::WResource::write ( std::ostream &  out  ) 

Stream the resource to a stream.

This is a convenience method to serialize to a stream (for example a files stream).

virtual const std::string Wt::WResource::resourceMimeType (  )  const [protected, pure virtual]

Return the mimetype.

Implement this method to return the correct mime type for your resource, e.g. "text/html".

Implemented in Wt::Ext::DataStore, Wt::WFileResource, Wt::WMemoryResource, Wt::WSvgImage, and Wt::WWidget.

virtual bool Wt::WResource::streamResourceData ( std::ostream &  stream,
const ArgumentMap arguments 
) [protected, pure virtual]

Stream the data for this resource.

Implement this method to output the data for this resource.

Returns whether all data has been streamed. If not, call flush() from outside the mean event loop to indicate that more data is available. This is how "server-push" is implemented. The stream is not closed until this function returns true.

See also:
flush()

void Wt::WResource::flush (  )  [protected]

Flush data for this resource.

This is only valid when a previous call to streamResourceData() returned false.

This will trigger a call to streamResourceData() to retrieve more data to be transmitted.

See also:
streamResourceData()

void Wt::WResource::setArguments ( const ArgumentMap arguments  )  [protected, virtual]

Handle the request arguments.

This method is called before serving the resource, and provides the request arguments. You may want to reimplement this method if you want to tailor how the resource is served based on request arguments.

The default implementation does nothing.

void Wt::WResource::addHeader ( const std::string &  name,
const std::string &  value 
) [protected]

Add a HTTP header.

Headers may be added only before the content-type is set, which is done before streaming the resource data. This method can only be used from within a reimplemented setArguments() method.


Generated on Mon Jan 26 14:14:10 2009 for Wt by doxygen 1.5.6