com.ibm.wbi.protocol.http.beans
Class FileGenerator

com.ibm.wbi.Meg
  |
  +--com.ibm.wbi.Generator
        |
        +--com.ibm.wbi.protocol.http.HttpGenerator
              |
              +--com.ibm.wbi.protocol.http.beans.FourStepHttpGenerator
                    |
                    +--com.ibm.wbi.protocol.http.beans.FileGenerator

public class FileGenerator
extends FourStepHttpGenerator

A Generator for delivering the contents of a file to the client. This class has several properties (some of which are defined by the superclass). For normal operation, the most used properties are Filename and ContentType. These specify the name of the file to be served (either absolute filepath, or relative to the working directory) and the MIME type to be reported back to the browser.

The ContentType property is set automatically based on the filename extension, if the extension is known. It can always be set to a different value with the setContentType(...) method.

The DefaultFile property is used to redirect a browser to a default file within a directory if the specified filename is itself a directory. If DefaultFile is null, a null string, or points to a non-existent file, this property is ignored.

The ShowDirectories property, when set to true (the default is false), means that Filenames which point to directories will be presented as HTML renditions of directories, with links to the subdirectories and files within the directories. Note that this may be security issue (which is why the property defaults to false).

The LinkToParentDirectory property, when set to true (the default is false), means that HTML renditions of directories will contain a link to the ".." directory, the parent of the current directory. When set to false, this link will not be shown.

By default, the browser is allowed to cache the document. This property can be adjusted with the setCache(boolean) and isCache() methods of the superclass.


Field Summary
static java.lang.String COPYRIGHT
          IBM copyright information
 
Fields inherited from class com.ibm.wbi.protocol.http.HttpGenerator
http
 
Fields inherited from class com.ibm.wbi.Meg
EDITOR, GENERATOR, MONITOR, REQUEST_EDITOR, UNDEFINED
 
Constructor Summary
FileGenerator()
          Construct an instance with a null Filename, a default "text/html" ContentType, a DefaultFile of "index.html", and not to show directory contents.
FileGenerator(java.lang.String filename)
          Construct an instance with the given Filename and not to show directory contents.
FileGenerator(java.lang.String filename, java.lang.String type)
          Construct an instance with the given Filename and ContentType.
 
Method Summary
 java.lang.String getDefaultFile()
          Access the DefaultFile property.
 java.lang.String getFilename()
          Access the Filename property
 boolean isLinkToParentDirectory()
          Access the LinkToParentDirectory property.
 boolean isShowDirectories()
          Access the ShowDirectories property.
 void setContentType(java.lang.String ct)
          Override the inherited method.
 void setDefaultFile(java.lang.String df)
          Set the DefaultFile property.
 void setFilename(java.lang.String fn)
          set the Filename property with the name of the file to be served, (either an absolute filepath, or relative to the WBI working directory).
 void setLinkToParentDirectory(boolean ltpd)
          Set the LinkToParentDirectory property.
 void setShowDirectories(boolean sd)
          Set the ShowDirectories property.
protected  boolean verify(RequestEvent e)
          Determine if file exists.
protected  void writeContent(RequestEvent e)
          Write the file's contents to the stream.
 
Methods inherited from class com.ibm.wbi.protocol.http.beans.FourStepHttpGenerator
getHeader, handleRequest, initialize
 
Methods inherited from class com.ibm.wbi.protocol.http.HttpGenerator
add, addCookie, getContentType, getHttpResponse, getHttpResponseString, getResponseCode, getResponseText, isCache, produceHeader, set, setCache, setContentLength, setResponseCode, setResponseText, setServer, writeHeader
 
Methods inherited from class com.ibm.wbi.Generator
getType
 
Methods inherited from class com.ibm.wbi.Meg
forwardRequest, getCondition, getEnabled, getMegProperty, getMegProperty, getMegPropertyKeys, getName, getPlugin, getPriority, getSystemContext, initialize, initialize, isEnabled, isMegApplicable, loadMegResources, run, setCondition, setEnabled, setMegProperty, setName, setPriority, setSystemContext, setup, setup, setup, setup
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
IBM copyright information
Constructor Detail

FileGenerator

public FileGenerator()
Construct an instance with a null Filename, a default "text/html" ContentType, a DefaultFile of "index.html", and not to show directory contents.

FileGenerator

public FileGenerator(java.lang.String filename)
Construct an instance with the given Filename and not to show directory contents. The DefaultFile property is set to "index.html". The ContentType is automatically determined from the filename extension (with a default of text/html for unknown extensions). The ContentType property can be overridden with the setContentType(...) method.
Parameters:
filename - The name of the file to be served.
See Also:
setFilename(String)

FileGenerator

public FileGenerator(java.lang.String filename,
                     java.lang.String type)
Construct an instance with the given Filename and ContentType. If the ContentType is null, the ContentType is automatically determined from the filename extension (with a default of text/html for unknown extensions).
Parameters:
filename - The name of the file to be served
type - The MIME type to be reported back to the browser. If null, the MIME type is derived from the filename extension (e.g. ".jpg" becomes "image/jpeg")
See Also:
setFilename(String)
Method Detail

setFilename

public void setFilename(java.lang.String fn)
set the Filename property with the name of the file to be served, (either an absolute filepath, or relative to the WBI working directory). If the current WBI base directory is needed in order to construct an appropriate filename, it can be found by calling WBIInstPackage.locateWBIDirectory().

The ContentType is automatically determined from the filename extension, but may be overridden with the setContentType(...) method. If the filename extension is unknown, the ContentType property is left unchanged.

Parameters:
fn - The Filename

getFilename

public java.lang.String getFilename()
Access the Filename property
Returns:
fn The Filename

setShowDirectories

public void setShowDirectories(boolean sd)
Set the ShowDirectories property. If true, directories will be rendered as an HTML page of links. If false, an error page will be produced.

setContentType

public void setContentType(java.lang.String ct)
Override the inherited method. If the specified content-type is null or the null string, automatically derive the content type from the filename, defaulting to "text/html".
Overrides:
setContentType in class HttpGenerator
Following copied from class: com.ibm.wbi.protocol.http.HttpGenerator
Parameters:
ct - The ContentType.
See Also:
HttpBase.setContentType(java.lang.String)

isShowDirectories

public boolean isShowDirectories()
Access the ShowDirectories property.

setLinkToParentDirectory

public void setLinkToParentDirectory(boolean ltpd)
Set the LinkToParentDirectory property. If true, there will be a link to the parent directory (i.e. "..")

isLinkToParentDirectory

public boolean isLinkToParentDirectory()
Access the LinkToParentDirectory property.

setDefaultFile

public void setDefaultFile(java.lang.String df)
Set the DefaultFile property. If pointing to a valid file within the directory referred to by the Filename property, then the browser will be redirected to this file within the given directory.

getDefaultFile

public java.lang.String getDefaultFile()
Access the DefaultFile property.

verify

protected boolean verify(RequestEvent e)
Determine if file exists. If does, then accept request.
Overrides:
verify in class FourStepHttpGenerator
Parameters:
e - The request event.
Returns:
Accept request?

writeContent

protected void writeContent(RequestEvent e)
                     throws RequestRejectedException,
                            java.io.IOException
Write the file's contents to the stream. If the file is a directory, the DefaultDocument property is checked to see if the browser should be redirected to that document. If DefaultFile is null (or if the file does not exist) the ShowDirectories property is checked before displaying an HTML-rendering of the directory contents. Note: we count on no content being written before this method gets called, because we may forward the request to another Generator. Also, we count on no other content being written after this method returns!
Overrides:
writeContent in class FourStepHttpGenerator