org.glassfish.grizzly.http.server
Class StaticHttpHandler

java.lang.Object
  extended by org.glassfish.grizzly.http.server.HttpHandler
      extended by org.glassfish.grizzly.http.server.StaticHttpHandler

public class StaticHttpHandler
extends HttpHandler

HttpHandler, which processes requests to a static resources.

Author:
Jeanfrancois Arcand, Alexey Stashok

Field Summary
protected  ArraySet<java.io.File> docRoots
           
 
Constructor Summary
StaticHttpHandler()
          Create HttpHandler, which, by default, will handle requests to the static resources located in the current directory.
StaticHttpHandler(java.util.Set<java.lang.String> docRoots)
          Create a new instance which will look for static pages located under the docRoot.
StaticHttpHandler(java.lang.String... docRoots)
          Create a new instance which will look for static pages located under the docRoot.
 
Method Summary
 void addDocRoot(java.io.File docRoot)
          Add the directory to the list of directories where files will be serviced from.
 java.io.File addDocRoot(java.lang.String docRoot)
          Add the directory to the list of directories where files will be serviced from.
 boolean addToFileCache(Request req, java.io.File resource)
           
 java.io.File getDefaultDocRoot()
          Return the default directory from where files will be serviced.
 ArraySet<java.io.File> getDocRoots()
          Return the list of directories where files will be serviced from.
protected  java.lang.String getRelativeURI(Request request)
           
protected  boolean handle(java.lang.String uri, Request req, Response res)
          Lookup a resource based on the request URI, and send it using send file.
protected  void onMissingResource(Request request, Response response)
          The method will be called, if the static resource requested by the Request wasn't found, so StaticHttpHandler implementation may try to workaround this situation.
 void removeDocRoot(java.io.File docRoot)
          Removes the directory from the list of directories where static files will be serviced from.
static void sendFile(Response response, java.io.File file)
           
 void service(Request request, Response response)
          Based on the Request URI, try to map the file from the getDocRoots(), and send it back to a client.
 
Methods inherited from class org.glassfish.grizzly.http.server.HttpHandler
customizedErrorPage, destroy, doHandle, getName, getRequestURIEncoding, isAllowCustomStatusMessage, isAllowEncodedSlash, sendAcknowledgment, setAllowCustomStatusMessage, setAllowEncodedSlash, setDecodeUrl, setDispatcherHelper, setRequestURIEncoding, setRequestURIEncoding, start, updateContextPath
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

docRoots

protected final ArraySet<java.io.File> docRoots
Constructor Detail

StaticHttpHandler

public StaticHttpHandler()
Create HttpHandler, which, by default, will handle requests to the static resources located in the current directory.


StaticHttpHandler

public StaticHttpHandler(java.lang.String... docRoots)
Create a new instance which will look for static pages located under the docRoot. If the docRoot is null - static pages won't be served by this HttpHandler

Parameters:
docRoots - the folder(s) where the static resource are located. If the docRoot is null - static pages won't be served by this HttpHandler

StaticHttpHandler

public StaticHttpHandler(java.util.Set<java.lang.String> docRoots)
Create a new instance which will look for static pages located under the docRoot. If the docRoot is null - static pages won't be served by this HttpHandler

Parameters:
docRoots - the folders where the static resource are located. If the docRoot is empty - static pages won't be served by this HttpHandler
Method Detail

getDefaultDocRoot

public java.io.File getDefaultDocRoot()
Return the default directory from where files will be serviced.

Returns:
the default directory from where file will be serviced.

getDocRoots

public ArraySet<java.io.File> getDocRoots()
Return the list of directories where files will be serviced from.

Returns:
the list of directories where files will be serviced from.

addDocRoot

public final java.io.File addDocRoot(java.lang.String docRoot)
Add the directory to the list of directories where files will be serviced from.

Parameters:
docRoot - the directory to be added to the list of directories where files will be serviced from.
Returns:
return the File representation of the passed docRoot.

addDocRoot

public final void addDocRoot(java.io.File docRoot)
Add the directory to the list of directories where files will be serviced from.

Parameters:
docRoot - the directory to be added to the list of directories where files will be serviced from.

removeDocRoot

public void removeDocRoot(java.io.File docRoot)
Removes the directory from the list of directories where static files will be serviced from.

Parameters:
docRoot - the directory to remove.

service

public void service(Request request,
                    Response response)
             throws java.lang.Exception
Based on the Request URI, try to map the file from the getDocRoots(), and send it back to a client.

Specified by:
service in class HttpHandler
Parameters:
request - the Request
response - the Response
Throws:
java.lang.Exception

getRelativeURI

protected java.lang.String getRelativeURI(Request request)

onMissingResource

protected void onMissingResource(Request request,
                                 Response response)
                          throws java.lang.Exception
The method will be called, if the static resource requested by the Request wasn't found, so StaticHttpHandler implementation may try to workaround this situation. The default implementation - sends a 404 response page by calling HttpHandler.customizedErrorPage(Request, Response).

Parameters:
request - the Request
response - the Response
Throws:
java.lang.Exception

handle

protected boolean handle(java.lang.String uri,
                         Request req,
                         Response res)
                  throws java.lang.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:
java.lang.Exception

sendFile

public static void sendFile(Response response,
                            java.io.File file)
                     throws java.io.IOException
Throws:
java.io.IOException

addToFileCache

public final boolean addToFileCache(Request req,
                                    java.io.File resource)


Copyright © 2011 Oracle Corpration. All Rights Reserved.