Class URLMapperHandler

  • All Implemented Interfaces:
    HttpServer.Handler

    public class URLMapperHandler
    extends java.lang.Object
    implements HttpServer.Handler
    Handler implementation which allows the server to serve resources which are available to it as URLs. The main use for this is if the URLs are jar:-type ones which are available to the JVM in which the server is running, but not to it's clients. Either a single resource or a whole tree may be served.
    Since:
    8 Jan 2009
    Author:
    Mark Taylor
    • Constructor Detail

      • URLMapperHandler

        public URLMapperHandler​(HttpServer server,
                                java.lang.String basePath,
                                java.net.URL sourceUrl,
                                boolean includeRelatives)
                         throws java.net.MalformedURLException
        Constructor.
        Parameters:
        server - server within which this handler will be used
        basePath - path of served resources relative to the base path of the server itself
        sourceUrl - URL of the resource which is to be made available at the basePath by this handler
        includeRelatives - if true, relative URLs based at basePath may be requested (potentially giving access to for instance the entire tree of classpath resources); if false, only the exact resource named by sourceUrl is served
        Throws:
        java.net.MalformedURLException
    • Method Detail

      • getBaseUrl

        public java.net.URL getBaseUrl()
        Returns the base URL for this handler. If not including relatives, this will be the only URL served.
        Returns:
        URL
      • serveRequest

        public HttpServer.Response serveRequest​(HttpServer.Request request)
        Description copied from interface: HttpServer.Handler
        Provides a response to an HTTP request. A handler which does not recognise the URL should simply return null; in this case there may be another handler which is able to serve the request. If the URL appears to be in this handler's domain but the request cannot be served for some reason, an error response should be returned.
        Specified by:
        serveRequest in interface HttpServer.Handler
        Parameters:
        request - HTTP request
        Returns:
        response response to request, or null
      • mapUrlResponse

        public static HttpServer.Response mapUrlResponse​(java.lang.String method,
                                                         java.net.URL targetUrl)
        Repackages a resource from a given target URL as an HTTP response. The data and relevant headers are copied straight through. GET and HEAD methods are served.
        Parameters:
        method - HTTP method
        targetUrl - URL containing the resource to forward
        Returns:
        response redirecting to the given target URL