Package org.astrogrid.samp.httpd

# Class URLMapperHandler

java.lang.Object
org.astrogrid.samp.httpd.URLMapperHandler

All Implemented Interfaces:
  `HttpServer.Handler`

public class URLMapperHandler extends 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 Summary

Constructors
Constructor
Description
`URLMapperHandler(HttpServer server, String basePath, URL sourceUrl, boolean includeRelatives)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`URL`
`getBaseUrl()`
Returns the base URL for this handler.
`static HttpServer.Response`
`mapUrlResponse(String method, URL targetUrl)`
Repackages a resource from a given target URL as an HTTP response.
`HttpServer.Response`
`serveRequest(HttpServer.Request request)`
Provides a response to an HTTP request.

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

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

## Constructor Details

### URLMapperHandler

public URLMapperHandler(HttpServer server, String basePath, URL sourceUrl, boolean includeRelatives) throws 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:
  `MalformedURLException`

## Method Details

### getBaseUrl

public 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(String method, 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