| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.io.ByteArrayOutputStream; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.OutputStream; |
| 7 |
import java.net.URL; |
| 8 |
import java.util.LinkedHashMap; |
| 9 |
import java.util.Map; |
| 10 |
import org.astrogrid.samp.httpd.HttpServer; |
| 11 |
import org.astrogrid.samp.httpd.ServerResource; |
| 12 |
|
| 13 |
/** |
| 14 |
* HTTP resource handler suitable for serving static cross-origin policy files. |
| 15 |
* |
| 16 |
* @author Mark Taylor |
| 17 |
* @since 2 Feb 2011 |
| 18 |
*/ |
| 19 |
public class OpenPolicyResourceHandler implements HttpServer.Handler { |
| 20 |
|
| 21 |
private final String policyPath_; |
| 22 |
private final ServerResource policyResource_; |
| 23 |
private final OriginAuthorizer authorizer_; |
| 24 |
private final HttpServer.Response response405_; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param policyPath path at which the policy file will reside on |
| 30 |
* this handler's server |
| 31 |
* @param policyResource content of policy file |
| 32 |
* @param authorizer controls who is permitted to view the policy file |
| 33 |
*/ |
| 34 |
public OpenPolicyResourceHandler( String policyPath, |
| 35 |
ServerResource policyResource, |
| 36 |
OriginAuthorizer authorizer ) { |
| 37 |
policyPath_ = policyPath; |
| 38 |
policyResource_ = policyResource; |
| 39 |
authorizer_ = authorizer; |
| 40 |
response405_ = |
| 41 |
HttpServer.create405Response( new String[] { "GET", "HEAD", } ); |
| 42 |
} |
| 43 |
|
| 44 |
public HttpServer.Response serveRequest( HttpServer.Request request ) { |
| 45 |
if ( request.getUrl().equals( policyPath_ ) ) { |
| 46 |
String method = request.getMethod(); |
| 47 |
if ( ! method.equals( "HEAD" ) && |
| 48 |
! method.equals( "GET" ) ) { |
| 49 |
return response405_; |
| 50 |
} |
| 51 |
else if ( authorizer_.authorizeAll() ) { |
| 52 |
Map hdrMap = new LinkedHashMap(); |
| 53 |
hdrMap.put( "Content-Type", policyResource_.getContentType() ); |
| 54 |
long contentLength = policyResource_.getContentLength(); |
| 55 |
if ( contentLength >= 0 ) { |
| 56 |
hdrMap.put( "Content-Length", |
| 57 |
Long.toString( contentLength ) ); |
| 58 |
} |
| 59 |
if ( method.equals( "HEAD" ) ) { |
| 60 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 61 |
public void writeBody( OutputStream out ) { |
| 62 |
} |
| 63 |
}; |
| 64 |
} |
| 65 |
else if ( method.equals( "GET" ) ) { |
| 66 |
return new HttpServer.Response( 200, "OK", hdrMap ) { |
| 67 |
public void writeBody( OutputStream out ) |
| 68 |
throws IOException { |
| 69 |
policyResource_.writeBody( out ); |
| 70 |
} |
| 71 |
}; |
| 72 |
} |
| 73 |
else { |
| 74 |
assert false; |
| 75 |
return response405_; |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
return HttpServer.createErrorResponse( 404, "Not found" ); |
| 80 |
} |
| 81 |
} |
| 82 |
else { |
| 83 |
return null; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Creates a handler suitable for serving static cross-origin policy files. |
| 89 |
* |
| 90 |
* @param path path at which the policy file will reside on the |
| 91 |
* handler's HTTP server |
| 92 |
* @param contentUrl external URL at which the resource contents |
| 93 |
* can be found; this will be retrieved once and |
| 94 |
* cached |
| 95 |
* @param oAuth controls who is permitted to retrieve the policy file |
| 96 |
*/ |
| 97 |
public static HttpServer.Handler |
| 98 |
createPolicyHandler( String path, URL contentUrl, |
| 99 |
String contentType, |
| 100 |
OriginAuthorizer oAuth ) |
| 101 |
throws IOException { |
| 102 |
ServerResource resource = |
| 103 |
createCachedResource( contentUrl, contentType ); |
| 104 |
return new OpenPolicyResourceHandler( path, resource, oAuth ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns a handler which can serve the /crossdomain.xml file |
| 109 |
* used by Adobe Flash. The policy file permits access from anywhere. |
| 110 |
* |
| 111 |
* @param oAuth controls who is permitted to retrieve the policy file |
| 112 |
* @return policy file handler |
| 113 |
* @see <a href="http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html" |
| 114 |
* >Adobe Flash cross-origin policy</a> |
| 115 |
*/ |
| 116 |
public static HttpServer.Handler |
| 117 |
createFlashPolicyHandler( OriginAuthorizer oAuth ) |
| 118 |
throws IOException { |
| 119 |
return createPolicyHandler( "/crossdomain.xml", |
| 120 |
OpenPolicyResourceHandler.class |
| 121 |
.getResource( "crossdomain.xml" ), |
| 122 |
"text/x-cross-domain-policy", oAuth ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns a handler which can serve the /clientaccesspolicy.xml file |
| 127 |
* used by Microsoft Silverlight. The policy file permits access |
| 128 |
* from anywhere. |
| 129 |
* |
| 130 |
* @param oAuth controls who is permitted to retrieve the policy file |
| 131 |
* @return policy file handler |
| 132 |
* @see <a href="http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx" |
| 133 |
* >MS Silverlight cross-origin policy</a> |
| 134 |
*/ |
| 135 |
public static HttpServer.Handler |
| 136 |
createSilverlightPolicyHandler( OriginAuthorizer oAuth ) |
| 137 |
throws IOException { |
| 138 |
return createPolicyHandler( "/clientaccesspolicy.xml", |
| 139 |
OpenPolicyResourceHandler.class |
| 140 |
.getResource( "clientaccesspolicy.xml" ), |
| 141 |
"text/xml", oAuth ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns a ServerResource which caches the contents of a given |
| 146 |
* (presumably smallish and unchanging) external resource. |
| 147 |
* |
| 148 |
* @param dataUrl location of external resource |
| 149 |
* @param contentType MIME type for content of dataUrl |
| 150 |
* @return new cached resource representing content of |
| 151 |
* <code>dataUrl</code> |
| 152 |
*/ |
| 153 |
private static ServerResource |
| 154 |
createCachedResource( URL dataUrl, final String contentType ) |
| 155 |
throws IOException { |
| 156 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 157 |
InputStream uin = dataUrl.openStream(); |
| 158 |
byte[] buf = new byte[ 1024 ]; |
| 159 |
for ( int count; ( count = uin.read( buf ) ) >= 0; ) { |
| 160 |
bout.write( buf, 0, count ); |
| 161 |
} |
| 162 |
bout.close(); |
| 163 |
final byte[] data = bout.toByteArray(); |
| 164 |
return new ServerResource() { |
| 165 |
public long getContentLength() { |
| 166 |
return data.length; |
| 167 |
} |
| 168 |
public String getContentType() { |
| 169 |
return contentType; |
| 170 |
} |
| 171 |
public void writeBody( OutputStream out ) throws IOException { |
| 172 |
out.write( data ); |
| 173 |
} |
| 174 |
}; |
| 175 |
} |
| 176 |
} |