public class FileContext extends java.lang.Object implements Context
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 acquired from the class path as a mapping from
file extension to MIME type. This file can be modified if any
additional types are required. However it is more advisable to
simple extend this object and override the content type method.
FileIndexer
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getBasePath()
This is used to retrieve the base path of the context.
|
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. |
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.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.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.
|
public FileContext()
public FileContext(java.io.File base)
base
- this is the OS specific base path for thispublic 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 pathpublic 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 pathpublic 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.public 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
objectFile
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.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.getContentType
in interface Context
target
- the request URI to be parsed for its typepublic 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.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.