org.klomp.snark.web

## Class BasicServlet

- java.lang.Object
- - javax.servlet.GenericServlet
  - - javax.servlet.http.HttpServlet
    - - org.klomp.snark.web.BasicServlet

- All Implemented Interfaces:
    Serializable, Servlet, ServletConfig
  Direct Known Subclasses:
    I2PSnarkServlet

      class BasicServlet
      extends HttpServlet

  Based on DefaultServlet from Jetty 6.1.26, heavily simplified and modified to remove all dependencies on Jetty libs.

  Supports HEAD and GET only, for resources from the .war and local files. Supports files and resource only. Supports MIME types with local overrides and additions. Supports Last-Modified. Supports single request ranges.

  Does not support directories or "welcome files". Does not support gzip. Does not support multiple request ranges. Does not cache.

  POST returns 405. Directories return 403. Jar resources are sent with a long cache directive.

  ------------------------------------------------------------

  The default servlet. This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context. The following initParameters are supported, these can be set on the servlet itself:

        resourceBase  Set to replace the context resource base
        warBase      Path allowed for resource in war

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `BasicServlet.HttpContent` from Jetty HttpContent.java

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected I2PAppContext` | `_context`
    `protected Log` | `_log`
    `protected File` | `_resourceBase`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BasicServlet()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `addMimeMapping(String extension, String type)` Add a MIME type mapping for an extension.
    `protected static String` | `addPaths(String base, String path)` Simple version of URIUtil.addPaths().
    `protected static String` | `decodePath(String path)` Simple version of URIUtil.decodePath().
    `protected void` | `doDelete(HttpServletRequest request, HttpServletResponse response)`
    `protected void` | `doGet(HttpServletRequest request, HttpServletResponse response)`
    `protected void` | `doOptions(HttpServletRequest request, HttpServletResponse response)`
    `protected void` | `doPost(HttpServletRequest request, HttpServletResponse response)`
    `protected void` | `doTrace(HttpServletRequest request, HttpServletResponse response)`
    `protected static String` | `encodePath(String path)` Simple version of URIUtil.encodePath().
    `BasicServlet.HttpContent` | `getContent(String pathInContext, long limit)` Get Resource to serve.
    `protected String` | `getMimeType(String filename)` Get the MIME type by filename extension.
    `File` | `getResource(String pathInContext)` Get Resource to serve.
    `void` | `init(ServletConfig cfg)`
    `protected void` | `loadMimeMap(String resourcePath)` Load MIME type mappings from a resource file.
    `protected boolean` | `passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content)` Check modification date headers.
    `protected void` | `sendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content)`
    `protected void` | `setResourceBase(File base)` Files are served from here.
    `protected void` | `setWarBase(String base)` Only paths starting with this in the path are served.
    `protected void` | `writeHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count)` Write response headers for the content.

    - ### Methods inherited from class javax.servlet.http.HttpServlet

      `doHead, doPut, getLastModified, service, service`

    - ### Methods inherited from class javax.servlet.GenericServlet

      `destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### _context

          protected final transient I2PAppContext _context

    - #### _log

          protected final transient Log _log

    - #### _resourceBase

          protected File _resourceBase

  - ### Constructor Detail

    - #### BasicServlet

          public BasicServlet()

  - ### Method Detail

    - #### addMimeMapping

          protected void addMimeMapping(String extension,
                                        String type)

      Add a MIME type mapping for an extension.
      Parameters:
        `extension` - the file extension (without the dot)
        `type` - the MIME type to associate with the extension

    - #### addPaths

          protected static String addPaths(String base,
                                           String path)

      Simple version of URIUtil.addPaths().
      Parameters:
        `base` - the base path
        `path` - the path to add (may be null)
      Returns:
        the combined path

    - #### decodePath

          protected static String decodePath(String path)
                                      throws MalformedURLException

      Simple version of URIUtil.decodePath().
      Parameters:
        `path` - the path to decode
      Returns:
        the decoded path
      Throws:
        `MalformedURLException` - if the path contains invalid escape sequences

    - #### doDelete

          protected void doDelete(HttpServletRequest request,
                                  HttpServletResponse response)
                           throws ServletException,
                                  IOException

      Overrides:
        `doDelete` in class `HttpServlet`
      Throws:
        `ServletException`
        `IOException`

    - #### doGet

          protected void doGet(HttpServletRequest request,
                               HttpServletResponse response)
                        throws ServletException,
                               IOException

      Overrides:
        `doGet` in class `HttpServlet`
      Throws:
        `ServletException`
        `IOException`

    - #### doOptions

          protected void doOptions(HttpServletRequest request,
                                   HttpServletResponse response)
                            throws ServletException,
                                   IOException

      Overrides:
        `doOptions` in class `HttpServlet`
      Throws:
        `ServletException`
        `IOException`

    - #### doPost

          protected void doPost(HttpServletRequest request,
                                HttpServletResponse response)
                         throws ServletException,
                                IOException

      Overrides:
        `doPost` in class `HttpServlet`
      Throws:
        `ServletException`
        `IOException`

    - #### doTrace

          protected void doTrace(HttpServletRequest request,
                                 HttpServletResponse response)
                          throws ServletException,
                                 IOException

      Overrides:
        `doTrace` in class `HttpServlet`
      Throws:
        `ServletException`
        `IOException`

    - #### encodePath

          protected static String encodePath(String path)

      Simple version of URIUtil.encodePath().
      Parameters:
        `path` - the path to encode
      Returns:
        the encoded path

    - #### getContent

          public BasicServlet.HttpContent getContent(String pathInContext,
                                                     long limit)

      Get Resource to serve. Map a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.
      Parameters:
        `pathInContext` - The path to find a resource for.
        `limit` - the maximum content length to serve, or 0 for unlimited
      Returns:
        The resource to serve or null. Returns null for directories

    - #### getMimeType

          protected String getMimeType(String filename)

      Get the MIME type by filename extension.
      Parameters:
        `filename` - A file name
      Returns:
        MIME type matching the longest dot extension of the file name.

    - #### getResource

          public File getResource(String pathInContext)

      Get Resource to serve. Map a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.
      Parameters:
        `pathInContext` - The path to find a resource for.
      Returns:
        The resource to serve or null if not existing

    - #### init

          public void init(ServletConfig cfg)
                    throws ServletException

      Specified by:
        `init` in interface `Servlet`
      Overrides:
        `init` in class `GenericServlet`
      Throws:
        `ServletException`

    - #### loadMimeMap

          protected void loadMimeMap(String resourcePath)

      Load MIME type mappings from a resource file.
      Parameters:
        `resourcePath` - the resource path in the classpath, without ".properties" extension

    - #### passConditionalHeaders

          protected boolean passConditionalHeaders(HttpServletRequest request,
                                                   HttpServletResponse response,
                                                   BasicServlet.HttpContent content)
                                            throws IOException

      Check modification date headers.
      Parameters:
        `request` - the HTTP request
        `response` - the HTTP response
        `content` - the content to check
      Returns:
        true to keep going, false if handled here
      Throws:
        `IOException` - if an I/O error occurs

    - #### sendData

          protected void sendData(HttpServletRequest request,
                                  HttpServletResponse response,
                                  BasicServlet.HttpContent content)
                           throws IOException

      Throws:
        `IOException`

    - #### setResourceBase

          protected void setResourceBase(File base)
                                  throws UnavailableException

      Files are served from here.
      Parameters:
        `base` - the directory to serve files from
      Throws:
        `UnavailableException` - if the base directory doesn't exist

    - #### setWarBase

          protected void setWarBase(String base)

      Only paths starting with this in the path are served.
      Parameters:
        `base` - the base path that must be matched

    - #### writeHeaders

          protected void writeHeaders(HttpServletResponse response,
                                      BasicServlet.HttpContent content,
                                      long count)
                               throws IOException

      Write response headers for the content.
      Parameters:
        `response` - the HTTP response
        `content` - the content to write headers for
        `count` - the content length
      Throws:
        `IOException` - if an I/O error occurs