| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.lang.reflect.InvocationTargetException; |
| 4 |
import java.lang.reflect.Method; |
| 5 |
import java.net.MalformedURLException; |
| 6 |
import java.net.URL; |
| 7 |
import java.util.Arrays; |
| 8 |
import java.util.Collections; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.Set; |
| 13 |
import java.util.logging.Logger; |
| 14 |
import org.astrogrid.samp.Metadata; |
| 15 |
import org.astrogrid.samp.RegInfo; |
| 16 |
import org.astrogrid.samp.Subscriptions; |
| 17 |
import org.astrogrid.samp.SampUtils; |
| 18 |
import org.astrogrid.samp.client.ClientProfile; |
| 19 |
import org.astrogrid.samp.client.HubConnection; |
| 20 |
import org.astrogrid.samp.client.SampException; |
| 21 |
import org.astrogrid.samp.httpd.HttpServer; |
| 22 |
import org.astrogrid.samp.httpd.URLMapperHandler; |
| 23 |
import org.astrogrid.samp.hub.KeyGenerator; |
| 24 |
import org.astrogrid.samp.xmlrpc.ActorHandler; |
| 25 |
|
| 26 |
/** |
| 27 |
* SampXmlRpcHandler implementation which passes Web Profile-type XML-RPC calls |
| 28 |
* to a hub connection factory to provide a Web Profile hub server. |
| 29 |
* |
| 30 |
* @author Mark Taylor |
| 31 |
* @since 2 Feb 2011 |
| 32 |
*/ |
| 33 |
public class WebHubXmlRpcHandler extends ActorHandler { |
| 34 |
|
| 35 |
private final WebHubActorImpl impl_; |
| 36 |
private static final Logger logger_ = |
| 37 |
Logger.getLogger( WebHubXmlRpcHandler.class.getName() ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
* |
| 42 |
* @param profile hub connection factory |
| 43 |
* @param auth client authorizer |
| 44 |
* @param keyGen key generator for private keys |
| 45 |
* @param baseUrl base URL of HTTP server, used for URL translation |
| 46 |
* @param urlTracker tracks URLs in messages to restrict use in URL |
| 47 |
* translation service for security reasons; may be null for |
| 48 |
* no restrictions |
| 49 |
*/ |
| 50 |
public WebHubXmlRpcHandler( ClientProfile profile, ClientAuthorizer auth, |
| 51 |
KeyGenerator keyGen, URL baseUrl, |
| 52 |
UrlTracker urlTracker ) { |
| 53 |
super( WebClientProfile.WEBSAMP_HUB_PREFIX, WebHubActor.class, |
| 54 |
new WebHubActorImpl( profile, auth, keyGen, baseUrl, |
| 55 |
urlTracker ) ); |
| 56 |
impl_ = (WebHubActorImpl) getActor(); |
| 57 |
} |
| 58 |
|
| 59 |
public Object handleCall( String fqName, List params, Object reqObj ) |
| 60 |
throws Exception { |
| 61 |
String regMethod = WebClientProfile.WEBSAMP_HUB_PREFIX + "register"; |
| 62 |
if ( regMethod.equals( fqName ) && |
| 63 |
reqObj instanceof HttpServer.Request ) { |
| 64 |
HttpServer.Request req = (HttpServer.Request) reqObj; |
| 65 |
final Map securityMap; |
| 66 |
if ( params.size() == 1 && params.get( 0 ) instanceof Map ) { |
| 67 |
securityMap = (Map) params.get( 0 ); |
| 68 |
} |
| 69 |
else if ( params.size() == 1 && |
| 70 |
params.get( 0 ) instanceof String ) { |
| 71 |
securityMap = new HashMap(); |
| 72 |
securityMap.put( Metadata.NAME_KEY, (String) params.get( 0 ) ); |
| 73 |
logger_.info( "Deprecated register call signature " |
| 74 |
+ "(arg is string appName not map security-info)" ); |
| 75 |
} |
| 76 |
else { |
| 77 |
throw new IllegalArgumentException( "Bad args for " + regMethod |
| 78 |
+ "(map)" ); |
| 79 |
} |
| 80 |
Map result = impl_.register( req, securityMap ); |
| 81 |
assert result != null; |
| 82 |
return result; |
| 83 |
} |
| 84 |
else { |
| 85 |
return super.handleCall( fqName, params, reqObj ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns a handler suitable for performing URL translations on behalf |
| 91 |
* of sandboxed clients as required by the Web Profile. |
| 92 |
* |
| 93 |
* @return url translation handler |
| 94 |
*/ |
| 95 |
public HttpServer.Handler getUrlTranslationHandler() { |
| 96 |
return impl_.getUrlTranslationHandler(); |
| 97 |
} |
| 98 |
|
| 99 |
protected Object invokeMethod( Method method, Object obj, Object[] args ) |
| 100 |
throws IllegalAccessException, InvocationTargetException { |
| 101 |
return method.invoke( obj, args ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* WebHubActor implementation. |
| 106 |
*/ |
| 107 |
private static class WebHubActorImpl implements WebHubActor { |
| 108 |
|
| 109 |
private final ClientProfile profile_; |
| 110 |
private final ClientAuthorizer auth_; |
| 111 |
private final KeyGenerator keyGen_; |
| 112 |
private final Map regMap_; |
| 113 |
private final URLTranslationHandler urlTranslator_; |
| 114 |
private final URL baseUrl_; |
| 115 |
private final UrlTracker urlTracker_; |
| 116 |
|
| 117 |
/** |
| 118 |
* Constructor. |
| 119 |
* |
| 120 |
* @param profile hub connection factory |
| 121 |
* @param auth client authorizer |
| 122 |
* @param keyGen key generator for private keys |
| 123 |
* @param baseUrl HTTP server base URL |
| 124 |
* @param urlTracker controls access to translated URLs, |
| 125 |
* may be null for no control |
| 126 |
*/ |
| 127 |
public WebHubActorImpl( ClientProfile profile, ClientAuthorizer auth, |
| 128 |
KeyGenerator keyGen, URL baseUrl, |
| 129 |
UrlTracker urlTracker ) { |
| 130 |
profile_ = profile; |
| 131 |
auth_ = auth; |
| 132 |
keyGen_ = keyGen; |
| 133 |
baseUrl_ = baseUrl; |
| 134 |
urlTracker_ = urlTracker; |
| 135 |
regMap_ = Collections.synchronizedMap( new HashMap() ); |
| 136 |
urlTranslator_ = |
| 137 |
new URLTranslationHandler( "/proxied/", regMap_.keySet(), |
| 138 |
urlTracker ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns a handler suitable for performing URL translations on behalf |
| 143 |
* of sandboxed clients as required by the Web Profile. |
| 144 |
* |
| 145 |
* @return url translation handler |
| 146 |
*/ |
| 147 |
public HttpServer.Handler getUrlTranslationHandler() { |
| 148 |
return urlTranslator_; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Attempt client registration. An exception is thrown if registration |
| 153 |
* fails for any reason. |
| 154 |
* |
| 155 |
* @param request HTTP request from applicant |
| 156 |
* @param securityMap map of required security information |
| 157 |
* supplied by applicant |
| 158 |
* @return registration information if registration is successful |
| 159 |
*/ |
| 160 |
public RegInfo register( HttpServer.Request request, Map securityMap ) |
| 161 |
throws SampException { |
| 162 |
if ( profile_.isHubRunning() ) { |
| 163 |
auth_.authorize( request, securityMap ); |
| 164 |
HubConnection connection = profile_.register(); |
| 165 |
if ( connection != null ) { |
| 166 |
if ( urlTracker_ != null ) { |
| 167 |
connection = new UrlTrackerHubConnection( connection, |
| 168 |
urlTracker_ ); |
| 169 |
} |
| 170 |
String clientKey = keyGen_.next(); |
| 171 |
regMap_.put( clientKey, new Registration( connection ) ); |
| 172 |
String urlTrans = baseUrl_ |
| 173 |
+ urlTranslator_ |
| 174 |
.getTranslationBasePath( clientKey ); |
| 175 |
RegInfo regInfo = new RegInfo( connection.getRegInfo() ); |
| 176 |
regInfo.put( RegInfo.PRIVATEKEY_KEY, clientKey ); |
| 177 |
regInfo.put( WebClientProfile.URLTRANS_KEY, urlTrans ); |
| 178 |
return regInfo; |
| 179 |
} |
| 180 |
else { |
| 181 |
throw new SampException( "Hub is not running" ); |
| 182 |
} |
| 183 |
} |
| 184 |
else { |
| 185 |
throw new SampException( "Hub not running" ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
public void unregister( String clientKey ) throws SampException { |
| 190 |
HubConnection connection = getConnection( clientKey ); |
| 191 |
regMap_.remove( clientKey ); |
| 192 |
connection.unregister(); |
| 193 |
} |
| 194 |
|
| 195 |
public void allowReverseCallbacks( String clientKey, String allow ) |
| 196 |
throws SampException { |
| 197 |
boolean isAllowed = SampUtils.decodeBoolean( allow ); |
| 198 |
Registration reg = getRegistration( clientKey ); |
| 199 |
synchronized ( reg ) { |
| 200 |
if ( isAllowed == ( reg.callable_ != null ) ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
else if ( isAllowed ) { |
| 204 |
WebCallableClient callable = new WebCallableClient(); |
| 205 |
reg.connection_.setCallable( callable ); |
| 206 |
reg.callable_ = callable; |
| 207 |
} |
| 208 |
else { |
| 209 |
reg.connection_.setCallable( null ); |
| 210 |
reg.callable_.endCallbacks(); |
| 211 |
reg.callable_ = null; |
| 212 |
} |
| 213 |
assert isAllowed == ( reg.callable_ != null ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public List pullCallbacks( String clientKey, String timeout ) |
| 218 |
throws SampException { |
| 219 |
WebCallableClient callable = getRegistration( clientKey ).callable_; |
| 220 |
if ( callable != null ) { |
| 221 |
return callable |
| 222 |
.pullCallbacks( SampUtils.decodeInt( timeout ) ); |
| 223 |
} |
| 224 |
else { |
| 225 |
throw new SampException( "Client is not callable (first invoke" |
| 226 |
+ " allowReverseCallbacks)" ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public void declareMetadata( String clientKey, Map meta ) |
| 231 |
throws SampException { |
| 232 |
getConnection( clientKey ).declareMetadata( meta ); |
| 233 |
} |
| 234 |
|
| 235 |
public Map getMetadata( String clientKey, String clientId ) |
| 236 |
throws SampException { |
| 237 |
return getConnection( clientKey ).getMetadata( clientId ); |
| 238 |
} |
| 239 |
|
| 240 |
public void declareSubscriptions( String clientKey, Map subs ) |
| 241 |
throws SampException { |
| 242 |
getRegistration( clientKey ).subs_ = |
| 243 |
new Subscriptions( subs == null ? new HashMap() : subs ); |
| 244 |
getConnection( clientKey ).declareSubscriptions( subs ); |
| 245 |
} |
| 246 |
|
| 247 |
public Map getSubscriptions( String clientKey, String clientId ) |
| 248 |
throws SampException { |
| 249 |
return getConnection( clientKey ).getSubscriptions( clientId ); |
| 250 |
} |
| 251 |
|
| 252 |
public List getRegisteredClients( String clientKey ) |
| 253 |
throws SampException { |
| 254 |
return Arrays.asList( getConnection( clientKey ) |
| 255 |
.getRegisteredClients() ); |
| 256 |
} |
| 257 |
|
| 258 |
public Map getSubscribedClients( String clientKey, String mtype ) |
| 259 |
throws SampException { |
| 260 |
return getConnection( clientKey ).getSubscribedClients( mtype ); |
| 261 |
} |
| 262 |
|
| 263 |
public void notify( String clientKey, String recipientId, Map msg ) |
| 264 |
throws SampException { |
| 265 |
getConnection( clientKey ).notify( recipientId, msg ); |
| 266 |
} |
| 267 |
|
| 268 |
public List notifyAll( String clientKey, Map msg ) |
| 269 |
throws SampException { |
| 270 |
return getConnection( clientKey ).notifyAll( msg ); |
| 271 |
} |
| 272 |
|
| 273 |
public String call( String clientKey, String recipientId, String msgTag, |
| 274 |
Map msg ) |
| 275 |
throws SampException { |
| 276 |
return getConnection( clientKey ).call( recipientId, msgTag, msg ); |
| 277 |
} |
| 278 |
|
| 279 |
public Map callAll( String clientKey, String msgTag, Map msg ) |
| 280 |
throws SampException { |
| 281 |
return getConnection( clientKey ).callAll( msgTag, msg ); |
| 282 |
} |
| 283 |
|
| 284 |
public Map callAndWait( String clientKey, String recipientId, Map msg, |
| 285 |
String timeout ) |
| 286 |
throws SampException { |
| 287 |
return getConnection( clientKey ) |
| 288 |
.callAndWait( recipientId, msg, |
| 289 |
SampUtils.decodeInt( timeout ) ); |
| 290 |
} |
| 291 |
|
| 292 |
public void reply( String clientKey, String msgId, Map response ) |
| 293 |
throws SampException { |
| 294 |
getConnection( clientKey ).reply( msgId, response ); |
| 295 |
} |
| 296 |
|
| 297 |
public void ping() { |
| 298 |
if ( ! profile_.isHubRunning() ) { |
| 299 |
throw new RuntimeException( "No hub running" ); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
public void ping( String clientKey ) { |
| 304 |
ping(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Returns the registration object associated with a given private key. |
| 309 |
* |
| 310 |
* @param privateKey private key string known by client and hub |
| 311 |
* to identify the connection |
| 312 |
* @return registration object for client with key |
| 313 |
* <code>privateKey</code> |
| 314 |
* @throws SampException if no client is known with that private key |
| 315 |
*/ |
| 316 |
private Registration getRegistration( String privateKey ) |
| 317 |
throws SampException { |
| 318 |
Registration reg = (Registration) regMap_.get( privateKey ); |
| 319 |
if ( reg == null ) { |
| 320 |
throw new SampException( "Unknown client key" ); |
| 321 |
} |
| 322 |
else { |
| 323 |
return reg; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returns the connection object associated with a given private key. |
| 329 |
* |
| 330 |
* @param privateKey private key string known by client and hub |
| 331 |
* to identify the connection |
| 332 |
* @return connection object for client with key |
| 333 |
* <code>privateKey</code> |
| 334 |
* @throws SampException if no client is known with that private key |
| 335 |
*/ |
| 336 |
private HubConnection getConnection( String privateKey ) |
| 337 |
throws SampException { |
| 338 |
return getRegistration( privateKey ).connection_; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* HTTP handler which provides URL translation services for sandboxed |
| 344 |
* clients. |
| 345 |
*/ |
| 346 |
private static class URLTranslationHandler implements HttpServer.Handler { |
| 347 |
private final String basePath_; |
| 348 |
private final Set keySet_; |
| 349 |
private final UrlTracker urlTracker_; |
| 350 |
|
| 351 |
/** |
| 352 |
* Constructor. |
| 353 |
* |
| 354 |
* @param basePath base path for HTTP server |
| 355 |
* @param keySet set of strings which contains keys for all |
| 356 |
* currently registered clients |
| 357 |
* @param urlTracker controls access to translated URLs, |
| 358 |
* may be null for no control |
| 359 |
*/ |
| 360 |
public URLTranslationHandler( String basePath, Set keySet, |
| 361 |
UrlTracker urlTracker ) { |
| 362 |
if ( ! basePath.startsWith( "/" ) ) { |
| 363 |
basePath = "/" + basePath; |
| 364 |
} |
| 365 |
if ( ! basePath.endsWith( "/" ) ) { |
| 366 |
basePath = basePath + "/"; |
| 367 |
} |
| 368 |
basePath_ = basePath; |
| 369 |
keySet_ = keySet; |
| 370 |
urlTracker_ = urlTracker; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Returns the translation base path that can be used by a client |
| 375 |
* with a given private key. |
| 376 |
* |
| 377 |
* @param privateKey client private key |
| 378 |
* @return URL translation base path that can be used by a |
| 379 |
* registered client with the given private key |
| 380 |
*/ |
| 381 |
public String getTranslationBasePath( String privateKey ) { |
| 382 |
return basePath_ + privateKey + "?"; |
| 383 |
} |
| 384 |
|
| 385 |
public HttpServer.Response serveRequest( HttpServer.Request request ) { |
| 386 |
|
| 387 |
// Ignore requests outside this handler's domain. |
| 388 |
String path = request.getUrl(); |
| 389 |
if ( ! path.startsWith( basePath_ ) ) { |
| 390 |
return null; |
| 391 |
} |
| 392 |
|
| 393 |
// Ensure the URL has a query part. |
| 394 |
String relPath = path.substring( basePath_.length() ); |
| 395 |
int qIndex = relPath.indexOf( '?' ); |
| 396 |
if ( qIndex < 0 ) { |
| 397 |
return HttpServer.createErrorResponse( 404, "Not Found" ); |
| 398 |
} |
| 399 |
|
| 400 |
// Ensure a valid key for authorization is present; this makes |
| 401 |
// sure that only registered clients can use this service. |
| 402 |
String authKey = relPath.substring( 0, qIndex ); |
| 403 |
if ( ! keySet_.contains( authKey ) ) { |
| 404 |
return HttpServer.createErrorResponse( 403, "Forbidden" ); |
| 405 |
} |
| 406 |
|
| 407 |
// Extract the URL whose translation is being requested. |
| 408 |
assert path.substring( 0, path.indexOf( '?' ) + 1 ) |
| 409 |
.equals( getTranslationBasePath( authKey ) ); |
| 410 |
String targetString; |
| 411 |
try { |
| 412 |
targetString = |
| 413 |
SampUtils.uriDecode( relPath.substring( qIndex + 1 ) ); |
| 414 |
} |
| 415 |
catch ( RuntimeException e ) { |
| 416 |
return HttpServer.createErrorResponse( 400, "Bad Request" ); |
| 417 |
} |
| 418 |
URL targetUrl; |
| 419 |
try { |
| 420 |
targetUrl = new URL( targetString ); |
| 421 |
} |
| 422 |
catch ( MalformedURLException e ) { |
| 423 |
return HttpServer.createErrorResponse( 400, "Bad Request" ); |
| 424 |
} |
| 425 |
|
| 426 |
// Check permissions. |
| 427 |
if ( urlTracker_ != null && |
| 428 |
! urlTracker_.isUrlPermitted( targetUrl ) ) { |
| 429 |
return HttpServer.createErrorResponse( 403, "Forbidden" ); |
| 430 |
} |
| 431 |
|
| 432 |
// Perform the translation and return the result. |
| 433 |
return URLMapperHandler.mapUrlResponse( request.getMethod(), |
| 434 |
targetUrl ); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Utility class to aggregate information about a registered client. |
| 440 |
*/ |
| 441 |
private static class Registration { |
| 442 |
final HubConnection connection_; |
| 443 |
WebCallableClient callable_; |
| 444 |
Subscriptions subs_; |
| 445 |
|
| 446 |
/** |
| 447 |
* Constructor. |
| 448 |
* |
| 449 |
* @param connection hub connection |
| 450 |
*/ |
| 451 |
Registration( HubConnection connection ) { |
| 452 |
connection_ = connection; |
| 453 |
} |
| 454 |
} |
| 455 |
} |