| 1 |
package org.astrogrid.samp.httpd; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.io.OutputStream; |
| 6 |
import java.net.MalformedURLException; |
| 7 |
import java.net.URL; |
| 8 |
import java.net.URLConnection; |
| 9 |
import java.util.LinkedHashMap; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
/** |
| 13 |
* Handler implementation which allows the server to serve resources which |
| 14 |
* are available to it as URLs. The main use for this is if the URLs |
| 15 |
* are jar:-type ones which are available to the JVM in which the server |
| 16 |
* is running, but not to it's clients. |
| 17 |
* Either a single resource or a whole tree may be served. |
| 18 |
* |
| 19 |
* @author Mark Taylor |
| 20 |
* @since 8 Jan 2009 |
| 21 |
*/ |
| 22 |
public class URLMapperHandler implements HttpServer.Handler { |
| 23 |
private final String basePath_; |
| 24 |
private final URL baseUrl_; |
| 25 |
private final URL sourceUrl_; |
| 26 |
private final boolean includeRelatives_; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
* |
| 31 |
* @param server server within which this handler will be used |
| 32 |
* @param basePath path of served resources relative to the base path |
| 33 |
* of the server itself |
| 34 |
* @param sourceUrl URL of the resource which is to be made available |
| 35 |
* at the basePath by this handler |
| 36 |
* @param includeRelatives if true, relative URLs based at |
| 37 |
* <code>basePath</code> |
| 38 |
* may be requested (potentially giving access to |
| 39 |
* for instance the entire tree of classpath resources); |
| 40 |
* if false, only the exact resource named by |
| 41 |
* <code>sourceUrl</code> is served |
| 42 |
*/ |
| 43 |
public URLMapperHandler( HttpServer server, String basePath, URL sourceUrl, |
| 44 |
boolean includeRelatives ) |
| 45 |
throws MalformedURLException { |
| 46 |
if ( ! basePath.startsWith( "/" ) ) { |
| 47 |
basePath = "/" + basePath; |
| 48 |
} |
| 49 |
if ( ! basePath.endsWith( "/" ) && includeRelatives ) { |
| 50 |
basePath = basePath + "/"; |
| 51 |
} |
| 52 |
basePath_ = basePath; |
| 53 |
baseUrl_ = new URL( server.getBaseUrl(), basePath ); |
| 54 |
sourceUrl_ = sourceUrl; |
| 55 |
includeRelatives_ = includeRelatives; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the base URL for this handler. |
| 60 |
* If not including relatives, this will be the only URL served. |
| 61 |
* |
| 62 |
* @return URL |
| 63 |
*/ |
| 64 |
public URL getBaseUrl() { |
| 65 |
return baseUrl_; |
| 66 |
} |
| 67 |
|
| 68 |
public HttpServer.Response serveRequest( HttpServer.Request request ) { |
| 69 |
|
| 70 |
// Determine the source URL from which the data will be obtained. |
| 71 |
String path = request.getUrl(); |
| 72 |
if ( ! path.startsWith( basePath_ ) ) { |
| 73 |
return null; |
| 74 |
} |
| 75 |
String relPath = path.substring( basePath_.length() ); |
| 76 |
final URL srcUrl; |
| 77 |
if ( includeRelatives_ ) { |
| 78 |
try { |
| 79 |
srcUrl = new URL( sourceUrl_, relPath ); |
| 80 |
} |
| 81 |
catch ( MalformedURLException e ) { |
| 82 |
return HttpServer |
| 83 |
.createErrorResponse( 500, "Internal server error", e ); |
| 84 |
} |
| 85 |
} |
| 86 |
else { |
| 87 |
if ( relPath.length() == 0 ) { |
| 88 |
srcUrl = sourceUrl_; |
| 89 |
} |
| 90 |
else { |
| 91 |
return HttpServer.createErrorResponse( 403, "Forbidden" ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Forward header and data from the source URL to the response. |
| 96 |
return mapUrlResponse( request.getMethod(), srcUrl ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Repackages a resource from a given target URL as an HTTP response. |
| 101 |
* The data and relevant headers are copied straight through. |
| 102 |
* GET and HEAD methods are served. |
| 103 |
* |
| 104 |
* @param method HTTP method |
| 105 |
* @param targetUrl URL containing the resource to forward |
| 106 |
* @return response redirecting to the given target URL |
| 107 |
*/ |
| 108 |
public static HttpServer.Response mapUrlResponse( String method, |
| 109 |
URL targetUrl ) { |
| 110 |
final URLConnection conn; |
| 111 |
try { |
| 112 |
conn = targetUrl.openConnection(); |
| 113 |
conn.connect(); |
| 114 |
} |
| 115 |
catch ( IOException e ) { |
| 116 |
return HttpServer.createErrorResponse( 404, "Not found", e ); |
| 117 |
} |
| 118 |
try { |
| 119 |
Map hdrMap = new LinkedHashMap(); |
| 120 |
String contentType = conn.getContentType(); |
| 121 |
if ( contentType != null ) { |
| 122 |
hdrMap.put( "Content-Type", contentType ); |
| 123 |
} |
| 124 |
int contentLength = conn.getContentLength(); |
| 125 |
if ( contentLength >= 0 ) { |
| 126 |
hdrMap.put( "Content-Length", |
| 127 |
Integer.toString( contentLength ) ); |
| 128 |
} |
| 129 |
String contentEncoding = conn.getContentEncoding(); |
| 130 |
if ( contentEncoding != null ) { |
| 131 |
hdrMap.put( "Content-Encoding", contentEncoding ); |
| 132 |
} |
| 133 |
if ( "GET".equals( method ) ) { |
| 134 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 135 |
public void writeBody( OutputStream out ) |
| 136 |
throws IOException { |
| 137 |
UtilServer.copy( conn.getInputStream(), out ); |
| 138 |
} |
| 139 |
}; |
| 140 |
} |
| 141 |
else if ( "HEAD".equals( method ) ) { |
| 142 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 143 |
public void writeBody( OutputStream out ) { |
| 144 |
} |
| 145 |
}; |
| 146 |
} |
| 147 |
else { |
| 148 |
return HttpServer |
| 149 |
.create405Response( new String[] { "HEAD", "GET" } ); |
| 150 |
} |
| 151 |
} |
| 152 |
catch ( Exception e ) { |
| 153 |
return HttpServer |
| 154 |
.createErrorResponse( 500, "Internal server error", e ); |
| 155 |
} |
| 156 |
} |
| 157 |
} |