Class ResourceHandler

  • All Implemented Interfaces:
    Handler

    public class ResourceHandler
    extends java.lang.Object
    implements Handler
    Handler for serving documents out of the jar file. Look for url's as resources, presumably in the same "jar" file as the class files. This allows an entire web site to be included in the jar file. A typical way to use this handler (with java 1.2+) is as follows:
    • Add an existing web site to the jar file with:
      jar uf [jar file] [web site]
    • Create a config file, and add it to the jar file as: sunlabs/brazil/main/config. See Main.java for more info.
    • Create a startup file containing:
      Main-Class: sunlabs.brazil.server.Main
      , and add it to the manifest with:
      jar ufm [jar file] [startup file]
    • Start the server with:
      java -jar [jar file] [optional server options....]

    if no suffix is provided, and the "directory" exists, a redirect is issued, to add-on the trailing slash. The following server properties are used:

    root
    The document root path within the jar file
    prefix, suffix, glob, match
    Specify the URL that triggers this handler. (See MatchString).
    default
    The default file name for url references ending in /
    mime.xxx
    The mime type for suffix xxx. See FileHandler for a description of how to set mime types for url suffixes.
    Version:
    %V% ResourceHandler.java
    Author:
    Stephen Uhler
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceHandler()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] getResourceBytes​(java.util.Properties props, java.lang.String prefix, java.lang.String file)
      Find a file blob as a resource in our jar file (experimental).
      static java.io.InputStream getResourceStream​(java.util.Properties props, java.lang.String prefix, java.lang.String file)
      Look for a file in the filesystem.
      static java.lang.String getResourceString​(java.util.Properties props, java.lang.String prefix, java.lang.String file)  
      boolean init​(Server server, java.lang.String prefix)
      Initializes the handler.
      boolean respond​(Request request)
      Responds to an HTTP request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ResourceHandler

        public ResourceHandler()
    • Method Detail

      • init

        public boolean init​(Server server,
                            java.lang.String prefix)
        Description copied from interface: Handler
        Initializes the handler.
        Specified by:
        init in interface Handler
        Parameters:
        server - The HTTP server that created this Handler. Typical Handlers will use Server.props to obtain run-time configuration information.
        prefix - The handlers name. The string this Handler may prepend to all of the keys that it uses to extract configuration information from Server.props. This is set (by the Server and ChainHandler) to help avoid configuration parameter namespace collisions.
        Returns:
        true if this Handler initialized successfully, false otherwise. If false is returned, this Handler should not be used.
      • respond

        public boolean respond​(Request request)
                        throws java.io.IOException
        Description copied from interface: Handler
        Responds to an HTTP request.
        Specified by:
        respond in interface Handler
        Parameters:
        request - The Request object that represents the HTTP request.
        Returns:
        true if the request was handled. A request was handled if a response was supplied to the client, typically by calling Request.sendResponse() or Request.sendError.
        Throws:
        java.io.IOException - if there was an I/O error while sending the response to the client. Typically, in that case, the Server will (try to) send an error message to the client and then close the client's connection.

        The IOException should not be used to silently ignore problems such as being unable to access some server-side resource (for example getting a FileNotFoundException due to not being able to open a file). In that case, the Handler's duty is to turn that IOException into a HTTP response indicating, in this case, that a file could not be found.

      • getResourceStream

        public static java.io.InputStream getResourceStream​(java.util.Properties props,
                                                            java.lang.String prefix,
                                                            java.lang.String file)
                                                     throws java.io.IOException
        Look for a file in the filesystem. If its not there, see if we can find a resource in our jar file. Relative paths are resolved with respect to the document root.
        Parameters:
        props - where to look for server root property
        prefix - "
        file - The pseudo file to find as a resource
        Returns:
        The input stream (or null)
        Throws:
        java.io.IOException
      • getResourceString

        public static java.lang.String getResourceString​(java.util.Properties props,
                                                         java.lang.String prefix,
                                                         java.lang.String file)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getResourceBytes

        public static byte[] getResourceBytes​(java.util.Properties props,
                                              java.lang.String prefix,
                                              java.lang.String file)
                                       throws java.io.IOException
        Find a file blob as a resource in our jar file (experimental).
        Parameters:
        props - where to look for server root property
        prefix - "
        file - The pseudo file to find as a resource
        Returns:
        The data, if available, or raises an exception.
        Throws:
        java.io.IOException