org.abstracthorizon.danube.http.util
Class ReadOnlyFileConnectionHandler

java.lang.Object
  extended by org.abstracthorizon.danube.http.BaseReflectionHTTPConnectionHandler
      extended by org.abstracthorizon.danube.http.util.ReadOnlyFileConnectionHandler
All Implemented Interfaces:
ConnectionHandler
Direct Known Subclasses:
FileConnectionHandler, ReadWriteFileConnectionHandler

public class ReadOnlyFileConnectionHandler
extends BaseReflectionHTTPConnectionHandler

Connection handler that handles static files (and directories)

Author:
Daniel Sendula

Field Summary
protected  int bufferSize
          Buffer size
static SimpleDateFormat dateFormat
           
static int DEFAULT_BUFFER_SIZE
           
protected  File filePath
          Path of directory files are going to be displayed from
protected  javax.activation.FileTypeMap fileTypeMap
          File type map
static long GIGABYTE
           
static long KILOBYTE
           
static long MEGABYTE
           
static long TERABYTE
           
 
Fields inherited from class org.abstracthorizon.danube.http.BaseReflectionHTTPConnectionHandler
cachedMethods, errorResponse, noDefaultHead, noDefaultTrace
 
Constructor Summary
ReadOnlyFileConnectionHandler()
          Constructor
 
Method Summary
protected  String encode(String string)
          URL encodes given string
 int getBufferSize()
          Returns buffer size
protected  File getFileHandle(HTTPConnection httpConnection)
          Obtains real file system's file from the given connection
 File getFilePath()
          Sets path where files are stored
 javax.activation.FileTypeMap getFileTypeMap()
          Returns file type map that is used with this object
protected  String getLengthAsString(File f)
          Returns file length as a string
protected  void handleDirectory(File dir, HTTPConnection httpConnection)
          Handles request of directory.
protected  void handleFile(File file, HTTPConnection httpConnection)
          Handles file request.
 void methodGET(HTTPConnection httpConnection)
          This method retruns list of files from give directory or returns a file, using FileTypeMap to convert file extension to mime type
 void methodHEAD(HTTPConnection httpConnection)
          This method retruns headers for for GET method but no body
protected  ByteBuffer obtainBuffer()
          Obtains new buffer
protected  void releaseBuffer(ByteBuffer buffer)
          Releases used direct buffer
 void setBufferSize(int bufferSize)
          Sets buffer size
 void setFilePath(File filePath)
          Sets path where files are stored
 void setFileTypeMap(javax.activation.FileTypeMap fileTypeMap)
          Sets file type map to be used with this object
protected  void transferFromFileChannel(FileChannel inputChannel, WritableByteChannel outputChannel, long length)
          Transfer file channel to given output channel
protected  void transferStreams(InputStream inputStream, OutputStream outputStream, long length)
          Transfer file input stream to output stream
protected  void updateHeadersForFile(HTTPConnection httpConnection, File resource)
          Updates http headers for given resource
 
Methods inherited from class org.abstracthorizon.danube.http.BaseReflectionHTTPConnectionHandler
cacheMethods, getErrorResponse, getNoDefaultHead, getNoDefaultTrace, handleConnection, invokeMethod, methodOPTIONS, methodTRACE, returnError, returnSimpleContent, setErrorResponse, setNoDefaultHead, setNoDefaultTrace, updateDefaultHeadMethod, updateDefaultTraceMethod
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dateFormat

public static final SimpleDateFormat dateFormat

KILOBYTE

public static final long KILOBYTE
See Also:
Constant Field Values

MEGABYTE

public static final long MEGABYTE
See Also:
Constant Field Values

GIGABYTE

public static final long GIGABYTE
See Also:
Constant Field Values

TERABYTE

public static final long TERABYTE
See Also:
Constant Field Values

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values

filePath

protected File filePath
Path of directory files are going to be displayed from


bufferSize

protected int bufferSize
Buffer size


fileTypeMap

protected javax.activation.FileTypeMap fileTypeMap
File type map

Constructor Detail

ReadOnlyFileConnectionHandler

public ReadOnlyFileConnectionHandler()
Constructor

Method Detail

setFilePath

public void setFilePath(File filePath)
Sets path where files are stored

Parameters:
filePath - file path

getFilePath

public File getFilePath()
Sets path where files are stored

Returns:
file path

getBufferSize

public int getBufferSize()
Returns buffer size

Returns:
buffer size

setBufferSize

public void setBufferSize(int bufferSize)
Sets buffer size

Parameters:
bufferSize - buffer size

getFileTypeMap

public javax.activation.FileTypeMap getFileTypeMap()
Returns file type map that is used with this object

Returns:
file type map that is used with this object

setFileTypeMap

public void setFileTypeMap(javax.activation.FileTypeMap fileTypeMap)
Sets file type map to be used with this object

Parameters:
fileTypeMap - file type map to be used with this object

methodGET

public void methodGET(HTTPConnection httpConnection)
This method retruns list of files from give directory or returns a file, using FileTypeMap to convert file extension to mime type

Parameters:
connection - http connection

methodHEAD

public void methodHEAD(HTTPConnection httpConnection)
This method retruns headers for for GET method but no body

Overrides:
methodHEAD in class BaseReflectionHTTPConnectionHandler
Parameters:
connection - http connection

updateHeadersForFile

protected void updateHeadersForFile(HTTPConnection httpConnection,
                                    File resource)
Updates http headers for given resource

Parameters:
httpConnection - http connection
file - resource

getFileHandle

protected File getFileHandle(HTTPConnection httpConnection)
Obtains real file system's file from the given connection

Parameters:
httpConnection - connection
Returns:
file handle

handleDirectory

protected void handleDirectory(File dir,
                               HTTPConnection httpConnection)
Handles request of directory. It displays simple list (a table) of file names and sizes. Also it renders link to parent directory if not at the top (given URI).

Parameters:
dir - directory
httpConnection - http connection
Throws:
IOException

handleFile

protected void handleFile(File file,
                          HTTPConnection httpConnection)
Handles file request. It converts file extension to mime type using FileTypeMap.

Parameters:
file - requested file
httpConnection - http conneciton

getLengthAsString

protected String getLengthAsString(File f)
Returns file length as a string

Parameters:
f - file
Returns:
file length as a string

encode

protected String encode(String string)
URL encodes given string

Parameters:
string - string to be encoded
Returns:
encoded string

transferFromFileChannel

protected void transferFromFileChannel(FileChannel inputChannel,
                                       WritableByteChannel outputChannel,
                                       long length)
                                throws IOException
Transfer file channel to given output channel

Parameters:
inputChannel - file channel
outputChannel - output channel
length - length
Throws:
IOException - IO exception

obtainBuffer

protected ByteBuffer obtainBuffer()
Obtains new buffer

Returns:
new buffer

releaseBuffer

protected void releaseBuffer(ByteBuffer buffer)
Releases used direct buffer


transferStreams

protected void transferStreams(InputStream inputStream,
                               OutputStream outputStream,
                               long length)
                        throws IOException
Transfer file input stream to output stream

Parameters:
inputStream - file input stream
outputStream - output stream
length - length
Throws:
IOException - IO exception


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.