eu.webtoolkit.jwt
Class WResource

java.lang.Object
  extended by eu.webtoolkit.jwt.WObject
      extended by eu.webtoolkit.jwt.WResource
Direct Known Subclasses:
WFileResource, WMemoryResource, WRasterPaintDevice, WSvgImage

public abstract class WResource
extends WObject

An object which can be rendered in the HTTP protocol.

Usage

Besides the main page, other objects may be rendered as additional resources, for example documents or images. Classes such as WAnchor or WImage can use a resource instead of a URL to provide their contents. Whenever the resource has changed, you should call the setChanged() method. setChanged() will make sure that the browser will use a new version of the resource by generating a new URL, and emits the dataChanged() signal to make those that refer to the resource aware that they should update their references to the new URL.

You can help the browser to start a suitable helper application to handle the resource, or suggest to the user a suitable filename for saving the resource, by setting an appropriate file name using suggestFileName(String).

To serve resources that you create on the fly, you need to specialize this class and reimplement handleRequest(WebRequest, WebResponse).

Concurrency issues

Because of the nature of the web, a resource may be requested one time or multiple times at the discretion of the browser, and therefore your resource should in general not have any side-effects except for what is needed to render its own contents. Unlike event notifications to a JWt application, resource requests are not serialized, but are handled concurrently. Therefore you are not allowed to access or modify widget state from within the resource, unless you provide your own locking mechanism for it.

See Also:
WAnchor, WImage

Constructor Summary
WResource()
          Constructor.
 
Method Summary
 Signal dataChanged()
          Signal triggered when the data presented in this resource has changed.
 java.lang.String generateUrl()
          Generates a URL for this resource.
 java.lang.String getInternalPath()
          Returns the internal path.
 java.lang.String getSuggestedFileName()
          Returns the suggested file name.
 java.lang.String getUrl()
          Returns the current URL for this resource.
protected abstract  void handleRequest(WebRequest request, WebResponse response)
          Handles a request.
 void setChanged()
          Generates a new URL for this resource and emits the changed signal
 void setInternalPath(java.lang.String internalPath)
          Sets an internal path for this resource.
 void suggestFileName(java.lang.String name)
          Suggests a filename to the user for the data streamed by this resource.
 void write(java.io.OutputStream out)
          Writes the resource to an output stream.
 void write(java.io.OutputStream out, java.util.Map<java.lang.String,java.lang.String[]> parameterMap, java.util.Map<java.lang.String,UploadedFile> uploadedFiles)
          Writes the resource to an output stream.
 
Methods inherited from class eu.webtoolkit.jwt.WObject
getId, getObjectName, remove, setObjectName, tr
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WResource

public WResource()
Constructor.

Method Detail

generateUrl

public java.lang.String generateUrl()
Generates a URL for 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 triggering the dataChanged() signal. The old urls are not invalidated by calling this method.

Returns:
the url.

handleRequest

protected abstract void handleRequest(WebRequest request,
                                      WebResponse response)
                               throws java.io.IOException
Handles a request.

Reimplement this method so that a proper response is generated for the given request. From the request object you can access request parameters and whether the request is a continuation request. In the response object, you should set the mime type and stream the output data.

Parameters:
request - The request information
response - The response object
Throws:
java.io.IOException

dataChanged

public Signal dataChanged()
Signal triggered when the data presented in this resource has changed.

Widgets that reference the resource (such as anchors and images) will make sure the new data is rendered. It is better to call setChanged() than to trigger this signal since that method generates a new URL for this resource to avoid caching problems before emitting the signal.


write

public void write(java.io.OutputStream out,
                  java.util.Map<java.lang.String,java.lang.String[]> parameterMap,
                  java.util.Map<java.lang.String,UploadedFile> uploadedFiles)
           throws java.io.IOException
Writes the resource to an output stream.

This is a utility method that allows you to write the resource to an output stream, by using handleRequest(WebRequest, WebResponse).

Parameters:
out - The output stream.
parameterMap - A map with parameters that are made available in the WebRequest.
uploadedFiles - A list of uploaded files that are made available in the WebRequest.
Throws:
java.io.IOException

write

public void write(java.io.OutputStream out)
           throws java.io.IOException
Writes the resource to an output stream.

This is a utility method that allows you to write the resource to an output stream, by using handleRequest(WebRequest, WebResponse).

Parameters:
out - The output stream.
Throws:
java.io.IOException

suggestFileName

public void suggestFileName(java.lang.String name)
Suggests 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.


getSuggestedFileName

public java.lang.String getSuggestedFileName()
Returns the suggested file name.

See Also:
suggestFileName(String)

setChanged

public void setChanged()
Generates a new URL for this resource and emits the changed signal


getUrl

public java.lang.String getUrl()
Returns the current URL for this resource. Returns the url that refers to this resource.


getInternalPath

public java.lang.String getInternalPath()
Returns the internal path.

See Also:
setInternalPath(String)

setInternalPath

public void setInternalPath(java.lang.String internalPath)
Sets an internal path for this resource. Using this method you can deploy the resource at a fixed path. Unless you deploy using cookies for session tracking (not recommended), a session identifier will be appended to the path. You should use internal paths that are different from internal paths handled by your application (WApplication.setInternalPath(String)), if you do not want a conflict between these two when the browser does not use AJAX (and thus url fragments for its internal paths). The default value is empty. By default the URL for a resource is unspecified and a URL will be generated by the library.