|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsimple.http.serve.FileContext
public class FileContext
The FileContext
provides an implementation of the
Context
object that provides a direct mapping from
a request URI as defined in RFC 2616 to an OS specific target.
This uses a File
object to define the mapping
for the request URI paths. Using a File
object
allows the FileContext
to be easily used with both
DOS and UNIX systems.
This Indexer
implementation uses an MIME database
to obtain mappings for the getContentType
method.
The file used is "Content.properties", which is packaged within
simple.http.serve
. This determines the MIME type
of the request URI by matching file extension of the resource
with the MIME type as defined in the "Content.properties" file.
The mappings in the "Content.properties" file can be overridden
by any XML file named "Content.xml" within reach of the
Locator
object, this configuration file requires
the mappings to be in the form of wild card patterns.
For example, taking the XML configuration file described above,
this will match all files ending with the ".jpg" extenstion to
the MIME type "image/jpeg". Also, all files within the "/gif/"
folder will be considered GIF images with a MIME type of
"image/gif". By default most of the common file extensions
already have mappings, however overriding these can be useful.
<?xml version="1.0" encoding="UTF-8"?>
<content>
<resolve match="*.jpg" type="image/jpeg">
<resolve match="/gif/*" type="image/gif">
</content>
FileIndexer
Field Summary | |
---|---|
protected java.io.File |
base
This will be used to fetch the real OS system paths. |
protected Format |
format
This is the format instance used by this instance. |
protected simple.http.serve.FileIndexer |
indexer
This is used to extract any user specified MIME types. |
protected Locator |
locator
This is used to locate the configuration information. |
Constructor Summary | |
---|---|
FileContext()
Constructor for creating an instance that operates from the given current working path. |
|
FileContext(java.io.File base)
Constructor for creating an instance that operates from the given OS specific base path. |
|
FileContext(java.io.File base,
java.io.File path)
Constructor for creating an instance that operates from the given OS specific base path. |
|
FileContext(java.io.File base,
java.io.File[] list)
Constructor for creating an instance that operates from the given OS specific base path. |
|
FileContext(java.io.File base,
Locator locator)
Constructor for creating an instance that operates from the given OS specific base path. |
Method Summary | |
---|---|
java.lang.String |
getBasePath()
This is used to retrieve the base path of the context. |
Content |
getContent(java.lang.String target)
This retrieves a Content instance that wraps the
specified resource. |
java.lang.String |
getContentType(java.lang.String target)
This method will extract the type attribute of this URI. |
java.io.File |
getDirectory(java.lang.String target)
This is used to translate the HTTP request URI into the File object that it represent the parent directory
of the URI. |
java.io.File |
getFile(java.lang.String target)
This is used to translate the HTTP request URI into the File object that it represents. |
Format |
getFormat()
Each Context object must be coupled with an instance
of the Format object. |
Index |
getIndex(java.lang.String target)
This is an all in one method that allows all the information on the target URI to be gathered at once. |
java.util.Locale |
getLocale(java.lang.String target)
This will parse the HTTP request URI specified and return the Locale for that resource. |
Locator |
getLocator()
Each Context must supply a Locator to
enable the system to locate configuration information and other
resources that reside outside the context path. |
java.lang.String |
getName(java.lang.String target)
This will parse and return the file name that this request URI references. |
Path |
getPath(java.lang.String target)
This is used to translate the HTTP request URI into the Path object that it represents. |
java.util.Properties |
getProperties(java.lang.String target)
This provides a convenient way for an XML configuration file to be loaded. |
java.lang.String |
getRealPath(java.lang.String target)
This is used to translate the HTTP request URI into the OS specific path that it represents. |
java.lang.String |
getRequestPath(java.lang.String target)
This is used to translate the HTTP request URI into the URI path normalized and without query or parameter parts. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected simple.http.serve.FileIndexer indexer
protected Locator locator
protected Format format
protected java.io.File base
Constructor Detail |
---|
public FileContext()
public FileContext(java.io.File base)
base
- this is the OS specific base path for thispublic FileContext(java.io.File base, java.io.File path)
base
- this is the OS specific base path for thispath
- this is the path used to load configurationpublic FileContext(java.io.File base, java.io.File[] list)
base
- this is the OS specific base path for thislist
- this path list used to load configurationpublic FileContext(java.io.File base, Locator locator)
base
- this is the OS specific base path for thislocator
- this is the configuration file locatorMethod Detail |
---|
public java.lang.String getBasePath()
FileEngine
to map the URIs
onto the specific OS. The base path is the OS file system
specific path. So on UNIX it could be "/home/user/" and
on a DOS system it could be "c:\web\html" for example.
getBasePath
in interface Context
public java.lang.String getRealPath(java.lang.String target)
getRealPath
in interface Context
target
- this is the HTTP request URI path that is to
be translated into the OS specific path
public java.lang.String getRequestPath(java.lang.String target)
getRequestPath
in interface Context
target
- this is the HTTP request URI that is to be
converted into a normalized relative URI path
public java.io.File getFile(java.lang.String target)
File
object that it represents. This will convert
the URI to a format that the system can use and then create
the File
object for that path. So if for example
the context path was "c:\path" on a DOS system and the HTTP
URI given was "/index.html" this returns the File
"c:\path\index.html". This is basically for convenience as the
same could be achieved using the getRealPath
and
then creating the File
from that OS specific path.
getFile
in interface Context
target
- this is the HTTP request URI path that is used
to retrieve the File
object
File
for the given pathpublic java.io.File getDirectory(java.lang.String target)
File
object that it represent the parent directory
of the URI. This will convert the URI to a format that the host
system can use and then create the File
object for
that path. So if for example the context path was "c:\path" on
a DOS system and the HTTP URI given was "/index.html" this
returns the File
"c:\path\". This is basically
for convenience as the same could be achieved using the file
retrieved from getFile
and acquiring the parent.
getDirectory
in interface Context
target
- this is the HTTP request URI path that is used
to retrieve the File
object
File
for the directorypublic Path getPath(java.lang.String target)
Path
object that it represents. This enables the
HTTP request URI to be examined thoroughly an allows various
other files to be examined relative to it. For example if the
URI referenced a path "/usr/bin/file" and some resource
in the same directory is required then the Path
can be used to acquire the relative path. This is useful if
links within a HTML page are to be dynamically generated. The
Path.getRelative
provides this functionality.
getPath
in interface Context
target
- this is the HTTP request URI path that is used
to retrieve the Path
object
Path
for the given pathpublic java.util.Locale getLocale(java.lang.String target)
Locale
for that resource. The Locale
is extracted from the target by examining the path segment of
the HTTP request URI. The path segment is the abs_path token
defined in RFC 2396. It is extracted from a second extension
in the file name. So for example if the HTTP request URI was
"http://some.host/usr;param=value/index.en_US.html" then the
file name "index.en_US.html" would have the second file
extension en_US converted into a Locale
. This
will not interfere if the file name was "compressed.tar.gz",
it will simply ignore the "tar" second file extension and
return Locale.getDefault
.
getLocale
in interface Context
target
- the request URI to be parsed for its locale
public java.lang.String getContentType(java.lang.String target)
Context
is the last path
segment in the token defined by RFC 2396 as path_segments. So
for example if the target was "some.host:8080/bin/index.html"
then the name for that resource would be "index.html". Once
the name has been extracted the MIME is defined by the file
extension, which for the example is text/html. The MIME type
mappings can be directly specified in a configuration file
named either "Content.xml" or "content.xml".
getContentType
in interface Context
target
- the request URI to be parsed for its type
public java.lang.String getName(java.lang.String target)
Context
is the last
path segment is the token defined by RFC 2396 as path_segments.
So for example if the target was "some.host:8080/home/user/"
then the name for that resource would be "user". If the path
references the root path "/" then null should be returned.
getName
in interface Context
target
- the request URI to be parsed for its name
public Index getIndex(java.lang.String target)
FileEngine
when the Context
implementation is synchronized.
This will enable the FileEngine
to gather the
information on the target by acquiring the lock for the object
instance only once.
getIndex
in interface Context
target
- this is the request URI that is to be parsedpublic Content getContent(java.lang.String target) throws java.io.IOException
Content
instance that wraps the
specified resource. This returns a Content
instance
that transfers the contents of the referenced file in one
kilobyte chunks. This does not cache the file, however if the
files need to be cached the FileContentFactory
can
be used to act as a hot spot cache for heavly accessed files.
getContent
in interface Context
target
- this is the request URI that identifies the file
Content
interface
java.io.IOException
- this is thrown if the file resource does
not exist or cannot be accessedpublic java.util.Properties getProperties(java.lang.String target) throws java.io.IOException
getFile
method would return. Once the file has been acquired the Java
properties file is loaded, each time, there is no caching of
the loaded properties. This ensures that changes to a loaded
object does not affect other users of the properties file.
getProperties
in interface Context
target
- the request URI that refers to the properties
Properties
object
using the specified Java properties file
java.io.IOException
- this is thrown if the resource does not
exist or cannot be accessedpublic Locator getLocator()
Context
must supply a Locator
to
enable the system to locate configuration information and other
resources that reside outside the context path. This is useful
when there are Java properties and XML configuration files
required by objects interacting with a Context
.
The Locator
employs a search to locate resources,
which are identified either by name or using aliases.
getLocator
in interface Context
public Format getFormat()
Context
object must be coupled with an instance
of the Format
object. This is required because each
FileEngine
needs to serve the directory listing and
the error messages in a consistent format. The resources of the
instances can thus be pooled by comparing the equality of the
various Context
objects. When there is an object
that requires a FileEngine
it can create an instance
of the Context
and using the static factory method
FileEngine.getInstance
with the context object there
is a search for an active instance of the FileEngine
.
If one is found that uses a similar context object then it is
returned to the caller. This enables instances and thus resources
to be shared transparently.
getFormat
in interface Context
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |