com.sun.grizzly.tcp
Class StaticResourcesAdapter

java.lang.Object
  extended by com.sun.grizzly.tcp.StaticResourcesAdapter
All Implemented Interfaces:
Adapter
Direct Known Subclasses:
DynamicContentAdapter, GrizzlyAdapter

public class StaticResourcesAdapter
extends Object
implements Adapter

Simple Adapter that map the Request URI to a local file. The file is send synchronously using the NIO send file mechanism (@link File#transfertTo}. This class doesn't not decode the Request uri and just do basic security check. If you need more protection, use the GrizzlyAdapter class instead or extend the service(com.sun.grizzly.tcp.Request, com.sun.grizzly.tcp.Response) and use HttpRequestURIDecoder to protect against security attack.

Author:
Jeanfrancois Arcand

Field Summary
protected  ConcurrentHashMap<String,File> cache
           
protected  boolean commitErrorResponse
          Commit the 404 response automatically.
protected  Queue<File> fileFolders
           
protected  Logger logger
           
protected  String resourcesContextPath
           
 
Constructor Summary
StaticResourcesAdapter()
           
StaticResourcesAdapter(String rootFolder)
           
 
Method Summary
 boolean addRootFolder(String rootFolder)
          Add a folder to the list of folders this Adapter can serve file from.
 void afterService(Request req, Response res)
          Finish the Response and recycle the Request and the Response.
protected  void customizedErrorPage(Request req, Response res)
          Customize the error pahe
 String getDefaultContentType()
          If the content-type of the request cannot be determined, used the default value.
 String getResourcesContextPath()
          Return the context path used for servicing resources.
 String getRootFolder()
          Deprecated. - use getRootFolders()
 Queue<String> getRootFolders()
          Return the list of folders the adapter can serve file from.
protected  void initWebDir()
          Initialize.
 boolean isUseSendFile()
           
 void service(Request req, Response res)
          Based on the Request URI, try to map the file from the rootFolder, and send it synchronously using send file.
protected  void service(String uri, Request req, Response res)
          Lookup a resource based on the request URI, and send it using send file.
 void setDefaultContentType(String defaultContentType)
          Set the default content-type if we can't determine it.
 void setLogger(Logger logger)
           
 void setResourcesContextPath(String resourcesContextPath)
          Set the context path used for servicing resource.
 void setRootFolder(String rootFolder)
          Deprecated. - use addRootFolder(java.lang.String)
 void setUseSendFile(boolean useSendFile)
          true if FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send a static resources, false if the File needs to be loaded in memory and flushed using ByteBuffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resourcesContextPath

protected String resourcesContextPath

fileFolders

protected final Queue<File> fileFolders

cache

protected final ConcurrentHashMap<String,File> cache

logger

protected Logger logger

commitErrorResponse

protected boolean commitErrorResponse
Commit the 404 response automatically.

Constructor Detail

StaticResourcesAdapter

public StaticResourcesAdapter()

StaticResourcesAdapter

public StaticResourcesAdapter(String rootFolder)
Method Detail

service

public void service(Request req,
                    Response res)
             throws Exception
Based on the Request URI, try to map the file from the rootFolder, and send it synchronously using send file.

Specified by:
service in interface Adapter
Parameters:
req - the Request
res - the Response
Throws:
Exception

service

protected void service(String uri,
                       Request req,
                       Response res)
                throws Exception
Lookup a resource based on the request URI, and send it using send file.

Parameters:
uri - The request URI
req - the Request
res - the Response
Throws:
Exception

customizedErrorPage

protected void customizedErrorPage(Request req,
                                   Response res)
                            throws Exception
Customize the error pahe

Parameters:
req - The Request object
res - The Response object
Throws:
Exception

afterService

public void afterService(Request req,
                         Response res)
                  throws Exception
Finish the Response and recycle the Request and the Response. If the commitErrorResponse is set to false, this method does nothing.

Specified by:
afterService in interface Adapter
Parameters:
req - Request
res - Response
Throws:
Exception

getRootFolder

public String getRootFolder()
Deprecated. - use getRootFolders()

Return the directory from where files will be serviced.

Returns:
the directory from where file will be serviced.

setRootFolder

public void setRootFolder(String rootFolder)
Deprecated. - use addRootFolder(java.lang.String)

Set the directory from where files will be serviced. NOTE: For backward compatibility, invoking that method will clear all previous values added using addRootFolder(java.lang.String).

Parameters:
rootFolder - the directory from where files will be serviced.

getRootFolders

public Queue<String> getRootFolders()
Return the list of folders the adapter can serve file from.

Returns:
a Queue of the folders this Adapter can serve file from.

addRootFolder

public boolean addRootFolder(String rootFolder)
Add a folder to the list of folders this Adapter can serve file from.

Parameters:
rootFolder -
Returns:

initWebDir

protected void initWebDir()
                   throws IOException
Initialize.

Throws:
IOException

setLogger

public void setLogger(Logger logger)

isUseSendFile

public boolean isUseSendFile()
Returns:
true if FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send a static resources.

setUseSendFile

public void setUseSendFile(boolean useSendFile)
true if FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send a static resources, false if the File needs to be loaded in memory and flushed using ByteBuffer.

Parameters:
useSendFile - True if FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) to send a static resources, false if the File needs to be loaded in memory and flushed using ByteBuffer

getResourcesContextPath

public String getResourcesContextPath()
Return the context path used for servicing resources. By default, "" is used so request taking the form of http://host:port/index.html are serviced directly. If set, the resource will be available under http://host:port/context-path/index.html

Returns:
the context path.

setResourcesContextPath

public void setResourcesContextPath(String resourcesContextPath)
Set the context path used for servicing resource. By default, "" is used so request taking the form of http://host:port/index.html are serviced directly. If set, the resource will be available under http://host:port/context-path/index.html

Parameters:
resourcesContextPath - the context path

getDefaultContentType

public String getDefaultContentType()
If the content-type of the request cannot be determined, used the default value. Current default is text/html

Returns:
the defaultContentType

setDefaultContentType

public void setDefaultContentType(String defaultContentType)
Set the default content-type if we can't determine it. Default was text/html

Parameters:
defaultContentType - the defaultContentType to set


Copyright © 2013 Oracle Corporation. All Rights Reserved.