| 1 |
package org.astrogrid.samp.httpd; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.io.UnsupportedEncodingException; |
| 6 |
import java.net.MalformedURLException; |
| 7 |
import java.net.URL; |
| 8 |
import java.net.URLEncoder; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.LinkedHashMap; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.logging.Logger; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handler implementation which implements dynamic resource provision. |
| 16 |
* HTTP HEAD and GET methods are implemented. |
| 17 |
* |
| 18 |
* @author Mark Taylor |
| 19 |
* @since 7 Jan 2009 |
| 20 |
*/ |
| 21 |
public class ResourceHandler implements HttpServer.Handler { |
| 22 |
private final String basePath_; |
| 23 |
private final URL serverUrl_; |
| 24 |
private final Map resourceMap_; |
| 25 |
private int iRes_; |
| 26 |
|
| 27 |
private static Logger logger_ = |
| 28 |
Logger.getLogger( ResourceHandler.class.getName() ); |
| 29 |
|
| 30 |
/** Dummy resource indicating a withdrawn item. */ |
| 31 |
private static final ServerResource EXPIRED = new ServerResource() { |
| 32 |
public String getContentType() { |
| 33 |
throw new AssertionError(); |
| 34 |
} |
| 35 |
public long getContentLength() { |
| 36 |
throw new AssertionError(); |
| 37 |
} |
| 38 |
public void writeBody( OutputStream out ) { |
| 39 |
throw new AssertionError(); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @param server HTTP server |
| 47 |
* @param basePath path from server root beneath which all resources |
| 48 |
* provided by this handler will appear |
| 49 |
*/ |
| 50 |
public ResourceHandler( HttpServer server, String basePath ) { |
| 51 |
if ( ! basePath.startsWith( "/" ) ) { |
| 52 |
basePath = "/" + basePath; |
| 53 |
} |
| 54 |
if ( ! basePath.endsWith( "/" ) ) { |
| 55 |
basePath = basePath + "/"; |
| 56 |
} |
| 57 |
basePath_ = basePath; |
| 58 |
serverUrl_ = server.getBaseUrl(); |
| 59 |
resourceMap_ = new HashMap(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Adds a resource to this server. |
| 64 |
* |
| 65 |
* @param name resource name, for cosmetic purposes only |
| 66 |
* @param resource resource to make available |
| 67 |
* @return URL at which resource can be found |
| 68 |
*/ |
| 69 |
public synchronized URL addResource( String name, |
| 70 |
ServerResource resource ) { |
| 71 |
String path = basePath_ + Integer.toString( ++iRes_ ) + "/"; |
| 72 |
if ( name != null ) { |
| 73 |
try { |
| 74 |
path += URLEncoder.encode( name, "utf-8" ); |
| 75 |
} |
| 76 |
catch ( UnsupportedEncodingException e ) { |
| 77 |
logger_.warning( "No utf-8?? No cosmetic path name then" ); |
| 78 |
} |
| 79 |
} |
| 80 |
resourceMap_.put( path, resource ); |
| 81 |
try { |
| 82 |
URL url = new URL( serverUrl_, path ); |
| 83 |
logger_.info( "Resource added: " + url ); |
| 84 |
return new URL( serverUrl_, path ); |
| 85 |
} |
| 86 |
catch ( MalformedURLException e ) { |
| 87 |
throw new AssertionError( "Unknown protocol http??" ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Removes a resource from this server. |
| 93 |
* |
| 94 |
* @param url URL returned by a previous addResource call |
| 95 |
*/ |
| 96 |
public synchronized void removeResource( URL url ) { |
| 97 |
String path = url.getPath(); |
| 98 |
if ( resourceMap_.containsKey( path ) ) { |
| 99 |
logger_.info( "Resource expired: " + url ); |
| 100 |
resourceMap_.put( path, EXPIRED ); |
| 101 |
} |
| 102 |
else { |
| 103 |
throw new IllegalArgumentException( "Unknown URL to expire: " |
| 104 |
+ url ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public HttpServer.Response serveRequest( HttpServer.Request request ) { |
| 109 |
String path = request.getUrl(); |
| 110 |
if ( ! path.startsWith( basePath_ ) ) { |
| 111 |
return null; |
| 112 |
} |
| 113 |
final ServerResource resource = |
| 114 |
(ServerResource) resourceMap_.get( path ); |
| 115 |
if ( resource == EXPIRED ) { |
| 116 |
return HttpServer.createErrorResponse( 410, "Gone" ); |
| 117 |
} |
| 118 |
else if ( resource != null ) { |
| 119 |
Map hdrMap = new LinkedHashMap(); |
| 120 |
hdrMap.put( "Content-Type", resource.getContentType() ); |
| 121 |
long contentLength = resource.getContentLength(); |
| 122 |
if ( contentLength >= 0 ) { |
| 123 |
hdrMap.put( "Content-Length", Long.toString( contentLength ) ); |
| 124 |
} |
| 125 |
String method = request.getMethod(); |
| 126 |
if ( method.equals( "HEAD" ) ) { |
| 127 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 128 |
public void writeBody( OutputStream out ) { |
| 129 |
} |
| 130 |
}; |
| 131 |
} |
| 132 |
else if ( method.equals( "GET" ) ) { |
| 133 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 134 |
public void writeBody( OutputStream out ) |
| 135 |
throws IOException { |
| 136 |
resource.writeBody( out ); |
| 137 |
} |
| 138 |
}; |
| 139 |
} |
| 140 |
else { |
| 141 |
return HttpServer |
| 142 |
.create405Response( new String[] { "HEAD", "GET" } ); |
| 143 |
} |
| 144 |
} |
| 145 |
else { |
| 146 |
return HttpServer.createErrorResponse( 404, "Not found" ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |