Class FileContext
- java.lang.Object
-
- org.simpleframework.http.resource.FileContext
-
- All Implemented Interfaces:
Context
public class FileContext extends java.lang.Object implements Context
TheFileContext
provides an implementation of theContext
object that provides a direct mapping from a request URI as defined in RFC 2616 to an OS specific target. This uses aFile
object to define the mapping for the request URI paths. Using aFile
object allows theFileContext
to be easily used with both DOS and UNIX systems.This
Indexer
implementation uses an MIME database to obtain mappings for thegetContentType
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.- Author:
- Niall Gallagher
- See Also:
FileIndexer
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 theFile
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 theFile
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 thePath
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.
-
-
-
Constructor Detail
-
FileContext
public FileContext()
Constructor for creating an instance that operates from the given current working path. This instance will use the current path to translate the HTTP request URIs into the OS specific path. This will load configuration files from the current working directory.
-
FileContext
public FileContext(java.io.File base)
Constructor for creating an instance that operates from the given OS specific base path. This instance will use the given base path to translate the HTTP request URIs into the OS specific path. This will load configuration files from the specified directory path.- Parameters:
base
- this is the OS specific base path for this
-
-
Method Detail
-
getBasePath
public java.lang.String getBasePath()
This is used to retrieve the base path of the context. The base path of the context is that path that that this will retrieve system information from. This represents a base that the request URI paths are served from on the system. For instance a base of "c:\path" would translate a URI path of "/index.html" into "c:\path\index.html". Every resource request must be relative to the context path this allows theFileEngine
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.- Specified by:
getBasePath
in interfaceContext
- Returns:
- this returns the base path of the context
-
getRealPath
public 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. This will convert the URI to a format that the system can use and also represents the resource path on that system. So if for example the context path was "c:\path" on a DOS system and the HTTP URI given was "/index.html" this returns "c:\path\index.html". If a UNIX system was running the VM and the context base was for example "/home/" then this would return the UNIX path "/home/index.html" for the same request URI.- Specified by:
getRealPath
in interfaceContext
- Parameters:
target
- this is the HTTP request URI path that is to be translated into the OS specific path- Returns:
- this returns the OS specific path name for the translate request URI
-
getRequestPath
public 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. This is used so that the resource requested by the client can be discovered. For example this will convert the HTTP request URI "http://hostname/bin;param=value/../index.html?query" into the relative URI path /index.html. This is useful if a logging mechanism requires the name of the resource that was requested, it can also be used help find the resource.- Specified by:
getRequestPath
in interfaceContext
- Parameters:
target
- this is the HTTP request URI that is to be converted into a normalized relative URI path- Returns:
- the HTTP request URI as a normalized relative path
-
getFile
public java.io.File getFile(java.lang.String target)
This is used to translate the HTTP request URI into theFile
object that it represents. This will convert the URI to a format that the system can use and then create theFile
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 theFile
"c:\path\index.html". This is basically for convenience as the same could be achieved using thegetRealPath
and then creating theFile
from that OS specific path.
-
getDirectory
public java.io.File getDirectory(java.lang.String target)
This is used to translate the HTTP request URI into theFile
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 theFile
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 theFile
"c:\path\". This is basically for convenience as the same could be achieved using the file retrieved fromgetFile
and acquiring the parent.- Specified by:
getDirectory
in interfaceContext
- Parameters:
target
- this is the HTTP request URI path that is used to retrieve theFile
object- Returns:
- returns the
File
for the directory
-
getPath
public Path getPath(java.lang.String target)
This is used to translate the HTTP request URI into thePath
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 thePath
can be used to acquire the relative path. This is useful if links within a HTML page are to be dynamically generated. ThePath.getRelative
provides this functionality.
-
getContentType
public java.lang.String getContentType(java.lang.String target)
This method will extract the type attribute of this URI. The MIME type of the request URI is extracted from the name of the target. The name for theContext
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.- Specified by:
getContentType
in interfaceContext
- Parameters:
target
- the request URI to be parsed for its type- Returns:
- the type of the given request URI path refers to
-
getName
public java.lang.String getName(java.lang.String target)
This will parse and return the file name that this request URI references. The name for theContext
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.
-
getIndex
public 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. The motivation for this method is primarily convenience. However it is also used to increase the performance of theFileEngine
when theContext
implementation is synchronized. This will enable theFileEngine
to gather the information on the target by acquiring the lock for the object instance only once.
-
-