| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.ConnectException; |
| 5 |
import java.net.ServerSocket; |
| 6 |
import java.net.Socket; |
| 7 |
import java.net.URL; |
| 8 |
import java.net.MalformedURLException; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.Map; |
| 11 |
import java.util.Random; |
| 12 |
import org.astrogrid.samp.SampUtils; |
| 13 |
import org.astrogrid.samp.TestProfile; |
| 14 |
import org.astrogrid.samp.client.HubConnection; |
| 15 |
import org.astrogrid.samp.client.SampException; |
| 16 |
import org.astrogrid.samp.httpd.HttpServer; |
| 17 |
import org.astrogrid.samp.hub.HubProfile; |
| 18 |
import org.astrogrid.samp.hub.KeyGenerator; |
| 19 |
import org.astrogrid.samp.hub.MessageRestriction; |
| 20 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcClientFactory; |
| 21 |
import org.astrogrid.samp.xmlrpc.XmlRpcKit; |
| 22 |
import org.astrogrid.samp.xmlrpc.internal.InternalServer; |
| 23 |
|
| 24 |
public class WebTestProfile extends TestProfile { |
| 25 |
|
| 26 |
private final int port_; |
| 27 |
private final String path_; |
| 28 |
private final URL hubEndpoint_; |
| 29 |
private final SampXmlRpcClientFactory xClientFactory_; |
| 30 |
private final boolean urlControl_; |
| 31 |
private final MessageRestriction mrestrict_; |
| 32 |
private final String baseAppName_; |
| 33 |
private ClientAuthorizer clientAuth_; |
| 34 |
private int regSeq_; |
| 35 |
|
| 36 |
public WebTestProfile( Random random, boolean urlControl, |
| 37 |
MessageRestriction mrestrict ) |
| 38 |
throws IOException { |
| 39 |
this( random, getFreePort(), "/", |
| 40 |
XmlRpcKit.getInstance().getClientFactory(), |
| 41 |
urlControl, mrestrict, "test" ); |
| 42 |
} |
| 43 |
|
| 44 |
public WebTestProfile( Random random, int port, String path, |
| 45 |
SampXmlRpcClientFactory xClientFactory, |
| 46 |
boolean urlControl, MessageRestriction mrestrict, |
| 47 |
String baseAppName ) { |
| 48 |
super( random ); |
| 49 |
port_ = port; |
| 50 |
path_ = path; |
| 51 |
xClientFactory_ = xClientFactory; |
| 52 |
urlControl_ = urlControl; |
| 53 |
mrestrict_ = mrestrict; |
| 54 |
baseAppName_ = baseAppName; |
| 55 |
clientAuth_ = ClientAuthorizers.createFixedClientAuthorizer( true ); |
| 56 |
try { |
| 57 |
hubEndpoint_ = new URL( "http://" + SampUtils.getLocalhost() + ":" |
| 58 |
+ port + path ); |
| 59 |
} |
| 60 |
catch ( MalformedURLException e ) { |
| 61 |
throw new AssertionError(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void setClientAuthorizer( ClientAuthorizer clientAuth ) { |
| 66 |
clientAuth_ = clientAuth; |
| 67 |
} |
| 68 |
|
| 69 |
public HubProfile createHubProfile() throws IOException { |
| 70 |
WebHubProfile.ServerFactory sxfact = new WebHubProfile.ServerFactory(); |
| 71 |
sxfact.setPort( port_ ); |
| 72 |
sxfact.setXmlrpcPath( path_ ); |
| 73 |
sxfact.setOriginAuthorizer( OriginAuthorizers.TRUE ); |
| 74 |
sxfact.setAllowFlash( false ); |
| 75 |
sxfact.setAllowSilverlight( false ); |
| 76 |
ClientAuthorizer copyAuth = new ClientAuthorizer() { |
| 77 |
public void authorize( HttpServer.Request request, Map securityMap ) |
| 78 |
throws SampException { |
| 79 |
clientAuth_.authorize( request, securityMap ); |
| 80 |
} |
| 81 |
}; |
| 82 |
return new WebHubProfile( sxfact, copyAuth, mrestrict_, |
| 83 |
new KeyGenerator( "wk:", 24, |
| 84 |
createRandom() ), |
| 85 |
urlControl_ ); |
| 86 |
} |
| 87 |
|
| 88 |
public boolean isHubRunning() { |
| 89 |
try { |
| 90 |
Socket sock = new Socket( hubEndpoint_.getHost(), port_ ); |
| 91 |
sock.close(); |
| 92 |
return true; |
| 93 |
} |
| 94 |
catch ( IOException e ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public HubConnection register() throws SampException { |
| 100 |
int regSeq; |
| 101 |
synchronized ( this ) { |
| 102 |
regSeq = ++regSeq_; |
| 103 |
} |
| 104 |
Map secMap = new HashMap(); |
| 105 |
secMap.put( "samp.name", baseAppName_ + "-" + regSeq ); |
| 106 |
try { |
| 107 |
return new WebHubConnection( xClientFactory_ |
| 108 |
.createClient( hubEndpoint_ ), |
| 109 |
secMap ); |
| 110 |
} |
| 111 |
catch ( SampException e ) { |
| 112 |
for ( Throwable ex = e; ex != null; ex = ex.getCause() ) { |
| 113 |
if ( ex instanceof ConnectException ) { |
| 114 |
return null; |
| 115 |
} |
| 116 |
} |
| 117 |
throw e; |
| 118 |
} |
| 119 |
catch ( ConnectException e ) { |
| 120 |
return null; |
| 121 |
} |
| 122 |
catch ( IOException e ) { |
| 123 |
throw new SampException( e ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private static int getFreePort() throws IOException { |
| 128 |
ServerSocket sock = new ServerSocket( 0 ); |
| 129 |
int port = sock.getLocalPort(); |
| 130 |
sock.close(); |
| 131 |
return port; |
| 132 |
} |
| 133 |
} |