| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.net.InetSocketAddress; |
| 6 |
import java.net.ServerSocket; |
| 7 |
import java.util.logging.Logger; |
| 8 |
import javax.swing.JToggleButton; |
| 9 |
import javax.swing.SwingUtilities; |
| 10 |
import org.astrogrid.samp.client.ClientProfile; |
| 11 |
import org.astrogrid.samp.httpd.HttpServer; |
| 12 |
import org.astrogrid.samp.hub.ConfigHubProfile; |
| 13 |
import org.astrogrid.samp.hub.HubProfile; |
| 14 |
import org.astrogrid.samp.hub.KeyGenerator; |
| 15 |
import org.astrogrid.samp.hub.MessageRestriction; |
| 16 |
import org.astrogrid.samp.xmlrpc.internal.InternalServer; |
| 17 |
import org.astrogrid.samp.xmlrpc.internal.RpcLoggingInternalServer; |
| 18 |
import org.astrogrid.samp.xmlrpc.internal.XmlLoggingInternalServer; |
| 19 |
|
| 20 |
/** |
| 21 |
* HubProfile implementation for Web Profile. |
| 22 |
* |
| 23 |
* @author Mark Taylor |
| 24 |
* @author Laurent Bourges |
| 25 |
* @since 2 Feb 2011 |
| 26 |
*/ |
| 27 |
public class WebHubProfile implements HubProfile, ConfigHubProfile { |
| 28 |
|
| 29 |
private final ServerFactory serverFactory_; |
| 30 |
private final ClientAuthorizer auth_; |
| 31 |
private final KeyGenerator keyGen_; |
| 32 |
private final ConfigEnabler configEnabler_; |
| 33 |
private final ConfigEnabler configDisabler_; |
| 34 |
private MessageRestriction mrestrict_; |
| 35 |
private boolean controlUrls_; |
| 36 |
private InternalServer xServer_; |
| 37 |
private JToggleButton.ToggleButtonModel[] configModels_; |
| 38 |
private static final Logger logger_ = |
| 39 |
Logger.getLogger( WebHubProfile.class.getName() ); |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructs a profile with configuration options. |
| 43 |
* |
| 44 |
* @param serverFactory factory for server providing HTTP |
| 45 |
* and XML-RPC implementation |
| 46 |
* @param auth client authorizer implementation |
| 47 |
* @param mrestrict restriction for permitted outward MTypes |
| 48 |
* @param keyGen key generator for private keys |
| 49 |
* @param controlUrls true iff access to local URLs is to be restricted |
| 50 |
*/ |
| 51 |
public WebHubProfile( ServerFactory serverFactory, ClientAuthorizer auth, |
| 52 |
MessageRestriction mrestrict, |
| 53 |
KeyGenerator keyGen, boolean controlUrls ) { |
| 54 |
serverFactory_ = serverFactory; |
| 55 |
auth_ = auth; |
| 56 |
mrestrict_ = mrestrict; |
| 57 |
keyGen_ = keyGen; |
| 58 |
controlUrls_ = controlUrls; |
| 59 |
|
| 60 |
/* These Runnables are set up here rather than being defined as |
| 61 |
* anonymous classes where they are used to work round an obscure |
| 62 |
* JNLP bug related to classloading and JVM shutdown. */ |
| 63 |
configEnabler_ = new ConfigEnabler( true ); |
| 64 |
configDisabler_ = new ConfigEnabler( false ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Constructs a profile with default configuration. |
| 69 |
*/ |
| 70 |
public WebHubProfile() throws IOException { |
| 71 |
this( new ServerFactory(), |
| 72 |
new HubSwingClientAuthorizer( null, |
| 73 |
WebCredentialPresenter.INSTANCE ), |
| 74 |
ListMessageRestriction.DEFAULT, createKeyGenerator(), true ); |
| 75 |
} |
| 76 |
|
| 77 |
public String getProfileName() { |
| 78 |
return "Web"; |
| 79 |
} |
| 80 |
|
| 81 |
public MessageRestriction getMessageRestriction() { |
| 82 |
return mrestrict_; |
| 83 |
} |
| 84 |
|
| 85 |
public synchronized void start( ClientProfile profile ) throws IOException { |
| 86 |
if ( isRunning() ) { |
| 87 |
logger_.info( "Profile already running" ); |
| 88 |
return; |
| 89 |
} |
| 90 |
xServer_ = serverFactory_.createSampXmlRpcServer(); |
| 91 |
HttpServer hServer = xServer_.getHttpServer(); |
| 92 |
WebHubXmlRpcHandler wxHandler = |
| 93 |
new WebHubXmlRpcHandler( profile, auth_, keyGen_, |
| 94 |
hServer.getBaseUrl(), |
| 95 |
controlUrls_ ? new UrlTracker() : null ); |
| 96 |
logger_.info( "Web Profile URL controls: " |
| 97 |
+ ( controlUrls_ ? "on" : "off" ) ); |
| 98 |
logger_.info( "Web Profile MType restrictions: " |
| 99 |
+ mrestrict_ ); |
| 100 |
xServer_.addHandler( wxHandler ); |
| 101 |
hServer.addHandler( wxHandler.getUrlTranslationHandler() ); |
| 102 |
hServer.start(); |
| 103 |
if ( configModels_ != null ) { |
| 104 |
SwingUtilities.invokeLater( configDisabler_ ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public synchronized boolean isRunning() { |
| 109 |
return xServer_ != null; |
| 110 |
} |
| 111 |
|
| 112 |
public synchronized void stop() { |
| 113 |
if ( ! isRunning() ) { |
| 114 |
logger_.info( "Profile already stopped" ); |
| 115 |
return; |
| 116 |
} |
| 117 |
xServer_.getHttpServer().stop(); |
| 118 |
xServer_ = null; |
| 119 |
if ( configModels_ != null ) { |
| 120 |
SwingUtilities.invokeLater( configEnabler_ ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public synchronized JToggleButton.ToggleButtonModel[] getConfigModels() { |
| 125 |
if ( configModels_ == null ) { |
| 126 |
configModels_ = createConfigModels(); |
| 127 |
} |
| 128 |
return configModels_; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Creates and returns some toggle models for configuration. |
| 133 |
* They are only enabled when the profile is not running. |
| 134 |
*/ |
| 135 |
private JToggleButton.ToggleButtonModel[] createConfigModels() { |
| 136 |
ConfigModel[] models = new ConfigModel[] { |
| 137 |
new ConfigModel( "CORS cross-domain access" ) { |
| 138 |
void setOn( boolean on ) { |
| 139 |
serverFactory_ |
| 140 |
.setOriginAuthorizer( on ? OriginAuthorizers.TRUE |
| 141 |
: OriginAuthorizers.FALSE ); |
| 142 |
} |
| 143 |
boolean isOn() { |
| 144 |
return serverFactory_.getOriginAuthorizer().authorize( "" ); |
| 145 |
} |
| 146 |
}, |
| 147 |
new ConfigModel( "Flash cross-domain access" ) { |
| 148 |
void setOn( boolean on ) { |
| 149 |
serverFactory_.setAllowFlash( on ); |
| 150 |
} |
| 151 |
boolean isOn() { |
| 152 |
return serverFactory_.isAllowFlash(); |
| 153 |
} |
| 154 |
}, |
| 155 |
new ConfigModel( "Silverlight cross-domain access" ) { |
| 156 |
void setOn( boolean on ) { |
| 157 |
serverFactory_.setAllowSilverlight( on ); |
| 158 |
} |
| 159 |
boolean isOn() { |
| 160 |
return serverFactory_.isAllowSilverlight(); |
| 161 |
} |
| 162 |
}, |
| 163 |
new ConfigModel( "URL Controls" ) { |
| 164 |
void setOn( boolean on ) { |
| 165 |
controlUrls_ = on; |
| 166 |
} |
| 167 |
boolean isOn() { |
| 168 |
return controlUrls_; |
| 169 |
} |
| 170 |
}, |
| 171 |
new ConfigModel( "MType Restrictions" ) { |
| 172 |
void setOn( boolean on ) { |
| 173 |
mrestrict_ = on ? ListMessageRestriction.DEFAULT |
| 174 |
: null; |
| 175 |
} |
| 176 |
boolean isOn() { |
| 177 |
return mrestrict_ != null; |
| 178 |
} |
| 179 |
}, |
| 180 |
}; |
| 181 |
boolean enabled = ! isRunning(); |
| 182 |
for ( int i = 0; i < models.length; i++ ) { |
| 183 |
models[ i ].setEnabled( enabled ); |
| 184 |
} |
| 185 |
return models; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Convenience method to return a new key generator |
| 190 |
* suitable for use with a WebHubProfile. |
| 191 |
* |
| 192 |
* @return new key generator for web hub private keys |
| 193 |
*/ |
| 194 |
public static KeyGenerator createKeyGenerator() { |
| 195 |
return new KeyGenerator( "wk:", 24, KeyGenerator.createRandom() ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Runnable to be called on the Event Dispatch Thread which sets the |
| 200 |
* enabledness of the user controls for configuration of this profile. |
| 201 |
*/ |
| 202 |
private class ConfigEnabler implements Runnable { |
| 203 |
private final boolean isEnabled_; |
| 204 |
|
| 205 |
/** |
| 206 |
* Constructor. |
| 207 |
* |
| 208 |
* @param isEnabled status assigned to config controls by calling |
| 209 |
* this object's run() method |
| 210 |
*/ |
| 211 |
ConfigEnabler( boolean isEnabled ) { |
| 212 |
isEnabled_ = isEnabled; |
| 213 |
} |
| 214 |
|
| 215 |
public void run() { |
| 216 |
JToggleButton.ToggleButtonModel[] configModels = configModels_; |
| 217 |
if ( configModels != null ) { |
| 218 |
for ( int i = 0; i < configModels.length; i++ ) { |
| 219 |
configModels[ i ].setEnabled( isEnabled_ ); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Helper class to generate toggle button models for hub configuration. |
| 227 |
*/ |
| 228 |
private static abstract class ConfigModel |
| 229 |
extends JToggleButton.ToggleButtonModel { |
| 230 |
private final String name_; |
| 231 |
|
| 232 |
/** |
| 233 |
* Constructor. |
| 234 |
* |
| 235 |
* @param name control name |
| 236 |
*/ |
| 237 |
public ConfigModel( String name ) { |
| 238 |
name_ = name; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Indicates whether this toggle is on. |
| 243 |
* |
| 244 |
* @return true iff selected |
| 245 |
*/ |
| 246 |
abstract boolean isOn(); |
| 247 |
|
| 248 |
/** |
| 249 |
* Sets whether this toggle is on. |
| 250 |
* |
| 251 |
* @param on new selected value |
| 252 |
*/ |
| 253 |
abstract void setOn( boolean on ); |
| 254 |
|
| 255 |
public boolean isSelected() { |
| 256 |
return isOn(); |
| 257 |
} |
| 258 |
|
| 259 |
public void setSelected( boolean on ) { |
| 260 |
setOn( on ); |
| 261 |
super.setSelected( on ); |
| 262 |
} |
| 263 |
|
| 264 |
public String toString() { |
| 265 |
return name_; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Creates and configures the HTTP server on which the Web Profile resides. |
| 271 |
*/ |
| 272 |
public static class ServerFactory { |
| 273 |
private String logType_; |
| 274 |
private int port_; |
| 275 |
private String xmlrpcPath_; |
| 276 |
private boolean allowFlash_; |
| 277 |
private boolean allowSilverlight_; |
| 278 |
private OriginAuthorizer oAuth_; |
| 279 |
|
| 280 |
/** |
| 281 |
* Constructs a ServerFactory with default properties. |
| 282 |
*/ |
| 283 |
public ServerFactory() { |
| 284 |
logType_ = null; |
| 285 |
port_ = WebClientProfile.WEBSAMP_PORT; |
| 286 |
xmlrpcPath_ = WebClientProfile.WEBSAMP_PATH; |
| 287 |
allowFlash_ = true; |
| 288 |
allowSilverlight_ = false; |
| 289 |
oAuth_ = OriginAuthorizers.TRUE; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Returns a new internal server. |
| 294 |
* |
| 295 |
* @return new server for use with WebHubProfile |
| 296 |
*/ |
| 297 |
public InternalServer createSampXmlRpcServer() throws IOException { |
| 298 |
String path = getXmlrpcPath(); |
| 299 |
ServerSocket socket = createServerSocket( getPort() ); |
| 300 |
String logType = getLogType(); |
| 301 |
OriginAuthorizer oAuth = getOriginAuthorizer(); |
| 302 |
PrintStream logOut = System.err; |
| 303 |
CorsHttpServer hServer = "http".equals( logType ) |
| 304 |
? new LoggingCorsHttpServer( socket, oAuth, |
| 305 |
logOut ) |
| 306 |
: new CorsHttpServer( socket, oAuth ); |
| 307 |
if ( isAllowFlash() ) { |
| 308 |
hServer.addHandler( OpenPolicyResourceHandler |
| 309 |
.createFlashPolicyHandler( oAuth ) ); |
| 310 |
logger_.info( "Web Profile HTTP server permits " |
| 311 |
+ "Flash-style cross-domain access" ); |
| 312 |
} |
| 313 |
else { |
| 314 |
logger_.info( "Web Profile HTTP server does not permit " |
| 315 |
+ "Flash-style cross-domain access" ); |
| 316 |
} |
| 317 |
if ( isAllowSilverlight() ) { |
| 318 |
hServer.addHandler( OpenPolicyResourceHandler |
| 319 |
.createSilverlightPolicyHandler( oAuth ) ); |
| 320 |
logger_.info( "Web Profile HTTP server permits " |
| 321 |
+ "Silverlight-style cross-domain access" ); |
| 322 |
} |
| 323 |
else { |
| 324 |
logger_.info( "Web Profile HTTP server does not permit " |
| 325 |
+ "Silverlight-style cross-domain access" ); |
| 326 |
} |
| 327 |
hServer.setDaemon( true ); |
| 328 |
if ( "rpc".equals( logType ) ) { |
| 329 |
return new RpcLoggingInternalServer( hServer, path, logOut ); |
| 330 |
} |
| 331 |
else if ( "xml".equals( logType ) ) { |
| 332 |
return new XmlLoggingInternalServer( hServer, path, logOut ); |
| 333 |
} |
| 334 |
else if ( "none".equals( logType ) || "http".equals( logType ) || |
| 335 |
logType == null || logType.length() == 0 ) { |
| 336 |
return new InternalServer( hServer, path ); |
| 337 |
} |
| 338 |
else { |
| 339 |
throw new IllegalArgumentException( "Unknown logType " |
| 340 |
+ logType ); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Sets the type of logging to use. |
| 346 |
* |
| 347 |
* @param logType logging type; |
| 348 |
* may be "http", "rpc", "xml", "none" or null |
| 349 |
*/ |
| 350 |
public void setLogType( String logType ) { |
| 351 |
if ( logType == null || |
| 352 |
logType.equals( "http" ) || |
| 353 |
logType.equals( "rpc" ) || |
| 354 |
logType.equals( "xml" ) || |
| 355 |
logType.equals( "none" ) ) { |
| 356 |
logType_ = logType; |
| 357 |
} |
| 358 |
else { |
| 359 |
throw new IllegalArgumentException( "Unknown log type " |
| 360 |
+ logType ); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Returns the type of logging to use. |
| 366 |
* |
| 367 |
* @return logging type; may be "http", "rpc", "xml", "none" or null |
| 368 |
*/ |
| 369 |
public String getLogType() { |
| 370 |
return logType_; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Sets the port number the server will run on. |
| 375 |
* If port=0, then an unused port will be used at run time. |
| 376 |
* |
| 377 |
* @param port port number |
| 378 |
*/ |
| 379 |
public void setPort( int port ) { |
| 380 |
port_ = port; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Returns the port number the server will run on. |
| 385 |
* |
| 386 |
* @return port number |
| 387 |
*/ |
| 388 |
public int getPort() { |
| 389 |
return port_; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Sets the path on the HTTP server at which the XML-RPC server |
| 394 |
* will reside. |
| 395 |
* |
| 396 |
* @param xmlrpcPath server path for XML-RPC server |
| 397 |
*/ |
| 398 |
public void setXmlrpcPath( String xmlrpcPath ) { |
| 399 |
xmlrpcPath_ = xmlrpcPath; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Returns the path on the HTTP server at which the XML-RPC server |
| 404 |
* will reside. |
| 405 |
* |
| 406 |
* @return XML-RPC path on server |
| 407 |
*/ |
| 408 |
public String getXmlrpcPath() { |
| 409 |
return xmlrpcPath_; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Sets whether Adobe Flash cross-domain workaround will be supported. |
| 414 |
* |
| 415 |
* @param allowFlash true iff supported |
| 416 |
*/ |
| 417 |
public void setAllowFlash( boolean allowFlash ) { |
| 418 |
allowFlash_ = allowFlash; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Indicates whether Adobe Flash cross-domain workaround |
| 423 |
* will be supported. |
| 424 |
* |
| 425 |
* @return true iff supported |
| 426 |
*/ |
| 427 |
public boolean isAllowFlash() { |
| 428 |
return allowFlash_; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Sets whether Microsoft Silverlight cross-domain workaround |
| 433 |
* will be supported. |
| 434 |
* |
| 435 |
* @param allowSilverlight true iff supported |
| 436 |
*/ |
| 437 |
public void setAllowSilverlight( boolean allowSilverlight ) { |
| 438 |
allowSilverlight_ = allowSilverlight; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Indicates whether Microsoft Silverlight cross-domain workaround |
| 443 |
* will be supported. |
| 444 |
* |
| 445 |
* @return true iff supported |
| 446 |
*/ |
| 447 |
public boolean isAllowSilverlight() { |
| 448 |
return allowSilverlight_; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Sets the authorization policy for external origins. |
| 453 |
* |
| 454 |
* @param oAuth authorizer |
| 455 |
*/ |
| 456 |
public void setOriginAuthorizer( OriginAuthorizer oAuth ) { |
| 457 |
oAuth_ = oAuth; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Returns the authorization policy for external origins. |
| 462 |
* |
| 463 |
* @return authorizer |
| 464 |
*/ |
| 465 |
public OriginAuthorizer getOriginAuthorizer() { |
| 466 |
return oAuth_; |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Creates a socket on a given port to be used by the server this |
| 471 |
* object produces. |
| 472 |
* |
| 473 |
* @param port port number |
| 474 |
* @return new server socket |
| 475 |
*/ |
| 476 |
protected ServerSocket createServerSocket( int port ) |
| 477 |
throws IOException { |
| 478 |
ServerSocket sock = new ServerSocket(); |
| 479 |
sock.setReuseAddress( true ); |
| 480 |
sock.bind( new InetSocketAddress( port ) ); |
| 481 |
return sock; |
| 482 |
} |
| 483 |
} |
| 484 |
} |