| 1 |
package org.astrogrid.samp.bridge; |
| 2 |
|
| 3 |
import java.awt.image.BufferedImage; |
| 4 |
import java.awt.image.RenderedImage; |
| 5 |
import java.io.FileNotFoundException; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.OutputStream; |
| 8 |
import java.net.MalformedURLException; |
| 9 |
import java.net.URL; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.Map; |
| 12 |
import javax.imageio.ImageIO; |
| 13 |
import org.astrogrid.samp.SampUtils; |
| 14 |
import org.astrogrid.samp.httpd.HttpServer; |
| 15 |
|
| 16 |
/** |
| 17 |
* HttpServer handler for turning the URL of one icon into the URL of |
| 18 |
* another, related icon. |
| 19 |
* |
| 20 |
* @author Mark Taylor |
| 21 |
* @since 23 Jul 2009 |
| 22 |
*/ |
| 23 |
abstract class IconAdjuster implements HttpServer.Handler { |
| 24 |
|
| 25 |
private final URL baseUrl_; |
| 26 |
private static final String OUTPUT_FORMAT_NAME = "png"; |
| 27 |
private static final String OUTPUT_MIME_TYPE = "image/png"; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param server server with which this handler will be used |
| 33 |
* @param basePath path at which the dynamic URLs generated by |
| 34 |
* this server will be rooted |
| 35 |
*/ |
| 36 |
public IconAdjuster( HttpServer server, String basePath ) { |
| 37 |
if ( ! basePath.startsWith( "/" ) ) { |
| 38 |
basePath = "/" + basePath; |
| 39 |
} |
| 40 |
if ( ! basePath.endsWith( "/" ) ) { |
| 41 |
basePath = basePath + "/"; |
| 42 |
} |
| 43 |
try { |
| 44 |
baseUrl_ = new URL( server.getBaseUrl(), basePath ); |
| 45 |
} |
| 46 |
catch ( MalformedURLException e ) { |
| 47 |
throw (AssertionError) new AssertionError().initCause( e ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Produces an adjusted image for serving. |
| 53 |
* |
| 54 |
* @param inImage input image |
| 55 |
* @return adjusted version of <code>inImage</code> |
| 56 |
*/ |
| 57 |
public abstract RenderedImage adjustImage( BufferedImage inImage ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns a URL at which the dynamically adjusted version of the icon |
| 61 |
* at the given URL will be served. |
| 62 |
* |
| 63 |
* @param iconUrl URL of existing icon (GIF, PNG or JPEG) |
| 64 |
*/ |
| 65 |
public URL exportAdjustedIcon( URL iconUrl ) { |
| 66 |
try { |
| 67 |
return new URL( baseUrl_ + "?" |
| 68 |
+ SampUtils.uriEncode( iconUrl.toString() ) ); |
| 69 |
} |
| 70 |
catch ( MalformedURLException e ) { |
| 71 |
throw (AssertionError) new AssertionError().initCause( e ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the URL at which the underlying icon for the one represented |
| 77 |
* by the given server path. The <code>resourcePath</code> should be |
| 78 |
* the path part of a URL returned from an earlier call to |
| 79 |
* {@link #exportAdjustedIcon}. |
| 80 |
* |
| 81 |
* @param resourcePath path part of a URL requesting an adjusted icon |
| 82 |
* @return original icon corresponding to resourcePath, or null |
| 83 |
* if it doesn't look like a path this object dispensed |
| 84 |
*/ |
| 85 |
private URL getOriginalUrl( String resourcePath ) |
| 86 |
throws MalformedURLException { |
| 87 |
|
| 88 |
// If there's no query part, it's not one of ours. |
| 89 |
URL resourceUrl = new URL( baseUrl_, resourcePath ); |
| 90 |
String query = resourceUrl.getQuery(); |
| 91 |
if ( query == null ) { |
| 92 |
return null; |
| 93 |
} |
| 94 |
else { |
| 95 |
|
| 96 |
// If the base does not match our base URL, it's not one of ours. |
| 97 |
String base = resourceUrl.toString(); |
| 98 |
base = base.substring( 0, base.length() - query.length() ); |
| 99 |
if ( ! base.equals( baseUrl_.toString() + "?" ) ) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
// Otherwise, try to interpret the query as a URL. |
| 104 |
// It should be the URL of the original icon. |
| 105 |
// If it's not, an exception will result. |
| 106 |
|
| 107 |
String qurl; |
| 108 |
try { |
| 109 |
qurl = SampUtils.uriDecode( query ); |
| 110 |
} |
| 111 |
catch ( RuntimeException e ) { |
| 112 |
throw (MalformedURLException) |
| 113 |
new MalformedURLException().initCause( e ); |
| 114 |
} |
| 115 |
return new URL( qurl ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public HttpServer.Response serveRequest( HttpServer.Request request ) { |
| 120 |
URL baseIconUrl; |
| 121 |
try { |
| 122 |
baseIconUrl = getOriginalUrl( request.getUrl() ); |
| 123 |
} |
| 124 |
catch ( MalformedURLException e ) { |
| 125 |
return HttpServer.createErrorResponse( 404, "Not found", e ); |
| 126 |
} |
| 127 |
if ( baseIconUrl == null ) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
// Prepare the headers. |
| 132 |
Map hdrMap = new HashMap(); |
| 133 |
hdrMap.put( "Content-Type", OUTPUT_MIME_TYPE ); |
| 134 |
|
| 135 |
// Generate the response object. |
| 136 |
String method = request.getMethod(); |
| 137 |
if ( "HEAD".equals( method ) ) { |
| 138 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 139 |
public void writeBody( OutputStream out ) { |
| 140 |
} |
| 141 |
}; |
| 142 |
} |
| 143 |
else if ( "GET".equals( method ) ) { |
| 144 |
BufferedImage baseImage; |
| 145 |
try { |
| 146 |
baseImage = ImageIO.read( baseIconUrl ); |
| 147 |
if ( baseImage == null ) { |
| 148 |
throw new FileNotFoundException( baseIconUrl.toString() ); |
| 149 |
} |
| 150 |
} |
| 151 |
catch ( IOException e ) { |
| 152 |
return HttpServer |
| 153 |
.createErrorResponse( 500, "Server I/O error", e ); |
| 154 |
} |
| 155 |
final RenderedImage adjustedImage = adjustImage( baseImage ); |
| 156 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 157 |
public void writeBody( OutputStream out ) |
| 158 |
throws IOException { |
| 159 |
ImageIO.write( adjustedImage, OUTPUT_FORMAT_NAME, out ); |
| 160 |
} |
| 161 |
}; |
| 162 |
} |
| 163 |
else { |
| 164 |
return HttpServer |
| 165 |
.create405Response( new String[] { "GET", "HEAD" } ); |
| 166 |
} |
| 167 |
} |
| 168 |
} |