Debian Codegraph @

src/java/org/astrogrid/samp/httpd/MultiURLMapperHandler.java

raw
1 package org.astrogrid.samp.httpd;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 /**
11 * Handler implementation which allows the server to serve multiple
12 * separate resources which are available to it, but not necessarily to
13 * external clients, as URLs. The main use for this is if the URLs
14 * are jar:-type ones (not available to clients outside the current JVM)
15 * or file:-type ones (not available to clients on different hosts).
16 * Only single resources, not whole trees, can be exported in this way.
17 *
18 * <p>The functionality of this class overlaps with that of
19 * {@link URLMapperHandler}. They may be merged at some point.
20 *
21 * @author Mark Taylor
22 * @since 21 Jul 2009
23 */
24 public class MultiURLMapperHandler implements HttpServer.Handler {
25
26 private final HttpServer server_;
27 private final String basePath_;
28 private final URL baseUrl_;
29 private final Map urlMap_;
30 private int resourceCount_;
31
32 /**
33 * Constructor.
34 *
35 * @param server server within which this handler will be used
36 * @param basePath path of served resources relative to the base
37 * URL of the server itself
38 */
39 public MultiURLMapperHandler( HttpServer server, String basePath )
40 throws MalformedURLException {
41 server_ = server;
42 if ( ! basePath.startsWith( "/" ) ) {
43 basePath = "/" + basePath;
44 }
45 if ( ! basePath.endsWith( "/" ) ) {
46 basePath = basePath + "/";
47 }
48 basePath_ = basePath;
49 baseUrl_ = new URL( server.getBaseUrl(), basePath );
50 urlMap_ = Collections.synchronizedMap( new HashMap() );
51 }
52
53 /**
54 * Returns the base URL for resources served by this handler.
55 *
56 * @return base URL for output
57 */
58 public URL getBaseUrl() {
59 return baseUrl_;
60 }
61
62 /**
63 * Adds a local URL to the list of those which can be served by this
64 * handler, and returns the public URL at which it will be available.
65 *
66 * @param localUrl URL readable within this JVM
67 * @return URL readable in principle by external agents with the same
68 * content as <code>localUrl</code>
69 */
70 public synchronized URL addLocalUrl( URL localUrl ) {
71
72 // Get a name for the publicly visible URL, using the same as the
73 // local URL if possible. This is just for cosmetic purposes.
74 String path = localUrl.getPath();
75 int lastSlash = path.lastIndexOf( "/" );
76 String name = lastSlash >= 0 && lastSlash < path.length() - 1
77 ? path.substring( lastSlash + 1 )
78 : "f";
79
80 // Construct a new unique public URL at which this resource will
81 // be made available.
82 String relPath;
83 URL mappedUrl;
84 try {
85 relPath = ++resourceCount_ + "/" + name;
86 mappedUrl = new URL( baseUrl_, relPath );
87 }
88 catch ( MalformedURLException e ) {
89 try {
90 relPath = resourceCount_ + "/" + "f";
91 mappedUrl = new URL( baseUrl_, relPath );
92 }
93 catch ( MalformedURLException e2 ) {
94 throw (AssertionError) new AssertionError().initCause( e2 );
95 }
96 }
97
98 // Add the fragment part to the mapped URL if there is one.
99 String frag = localUrl.getRef();
100 if ( frag != null ) {
101 try {
102 mappedUrl = new URL( mappedUrl.toString() + "#" + frag );
103 }
104 catch ( MalformedURLException e ) {
105 // shouldn't happen, but if it does, just use the non-frag one.
106 assert false;
107 }
108 }
109
110 // Remember the mapping between the local URL and the public one.
111 urlMap_.put( relPath, localUrl );
112
113 // Return the public URL.
114 return mappedUrl;
115 }
116
117 /**
118 * Removes access to a resource which was publicised by a previous call
119 * to {@link #addLocalUrl}.
120 *
121 * @param url result of previous call to <code>addLocalUrl</code>
122 */
123 public synchronized void removeServerUrl( URL url ) {
124 String surl = url.toString();
125 String sbase = baseUrl_.toString();
126 if ( ! surl.startsWith( sbase ) ) {
127 return;
128 }
129 String relPath = surl.substring( sbase.length() );
130 urlMap_.remove( relPath );
131 }
132
133 public HttpServer.Response serveRequest( HttpServer.Request request ) {
134
135 // Determine the source URL from which the data will be obtained.
136 String path = request.getUrl();
137 if ( ! path.startsWith( basePath_ ) ) {
138 return null;
139 }
140 String relPath = path.substring( basePath_.length() );
141 if ( ! urlMap_.containsKey( relPath ) ) {
142 return HttpServer.createErrorResponse( 404, "Not found" );
143 }
144 URL srcUrl = (URL) urlMap_.get( relPath );
145
146 // Forward header and data from the source URL to the response.
147 return URLMapperHandler.mapUrlResponse( request.getMethod(), srcUrl );
148 }
149 }