org.glassfish.grizzly.web.container
Class StaticResourcesAdapter

java.lang.Object
  extended by org.glassfish.grizzly.web.container.StaticResourcesAdapter
All Implemented Interfaces:
Adapter
Direct Known Subclasses:
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 StaticResourcesAdapter#service() 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  Logger logger
           
protected  String resourcesContextPath
           
protected  String rootFolder
           
protected  File webDir
           
 
Constructor Summary
StaticResourcesAdapter()
           
StaticResourcesAdapter(String rootFolder)
           
 
Method Summary
 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 getResourcesContextPath()
          Return the context path used for servicing resources.
 String getRootFolder()
          Return the directory from where files will be serviced.
protected  void initWebDir()
          Initialize.
 boolean isUseSendFile()
          True if File#transfertTo to send a static resources.
 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 setLogger(Logger logger)
           
 void setResourcesContextPath(String resourcesContextPath)
          Set the context path used for servicing resource.
 void setRootFolder(String rootFolder)
          Set the directory from where files will be serviced.
 void setUseSendFile(boolean useSendFile)
          True if File#transfertTo 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

rootFolder

protected String rootFolder

resourcesContextPath

protected String resourcesContextPath

webDir

protected File webDir

cache

protected 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()
Return the directory from where files will be serviced.

Returns:
the directory from where file will be serviced.

setRootFolder

public void setRootFolder(String rootFolder)
Set the directory from where files will be serviced.

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

initWebDir

protected void initWebDir()
Initialize.


setLogger

public void setLogger(Logger logger)

isUseSendFile

public boolean isUseSendFile()
True if File#transfertTo to send a static resources.

Returns:
True if File#transfertTo to send a static resources.

setUseSendFile

public void setUseSendFile(boolean useSendFile)
True if File#transfertTo to send a static resources, false if the File needs to be loaded in memory and flushed using ByteBuffer

Parameters:
useSendFile - True if File#transfertTo 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


Copyright © 2009 SUN Microsystems. All Rights Reserved.