| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.ConnectException; |
| 5 |
import java.net.MalformedURLException; |
| 6 |
import java.net.URL; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Map; |
| 10 |
import org.astrogrid.samp.Metadata; |
| 11 |
import org.astrogrid.samp.Platform; |
| 12 |
import org.astrogrid.samp.SampUtils; |
| 13 |
import org.astrogrid.samp.client.ClientProfile; |
| 14 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 15 |
import org.astrogrid.samp.client.HubConnection; |
| 16 |
import org.astrogrid.samp.client.SampException; |
| 17 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcClient; |
| 18 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcClientFactory; |
| 19 |
import org.astrogrid.samp.xmlrpc.XmlRpcKit; |
| 20 |
|
| 21 |
/** |
| 22 |
* ClientProfile implementation for Web Profile. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 3 Feb 2011 |
| 26 |
*/ |
| 27 |
public class WebClientProfile implements ClientProfile { |
| 28 |
|
| 29 |
private final SampXmlRpcClientFactory xClientFactory_; |
| 30 |
private final Map securityMap_; |
| 31 |
private final URL hubEndpoint_; |
| 32 |
|
| 33 |
/** Web Profile hub port number ({@value}). */ |
| 34 |
public static final int WEBSAMP_PORT = 21012; |
| 35 |
|
| 36 |
/** |
| 37 |
* Path on WEBSAMP_PORT web server at which XML-RPC server lives |
| 38 |
* ({@value}). |
| 39 |
*/ |
| 40 |
public static final String WEBSAMP_PATH = "/"; |
| 41 |
|
| 42 |
/** |
| 43 |
* Prefix to hub interface operation names for XML-RPC method names |
| 44 |
* ({@value}). |
| 45 |
*/ |
| 46 |
public static final String WEBSAMP_HUB_PREFIX = "samp.webhub."; |
| 47 |
|
| 48 |
/** |
| 49 |
* Prefix to client interface opeation names for XML-RPC method names |
| 50 |
* ({@value}). |
| 51 |
*/ |
| 52 |
public static final String WEBSAMP_CLIENT_PREFIX = ""; |
| 53 |
|
| 54 |
/** |
| 55 |
* RegInfo map key for URL translation service base URL |
| 56 |
* ({@value}). |
| 57 |
*/ |
| 58 |
public static final String URLTRANS_KEY = "samp.url-translator"; |
| 59 |
|
| 60 |
/** |
| 61 |
* Prefix in SAMP_HUB value indicating web profile application name |
| 62 |
* ({@value}). |
| 63 |
*/ |
| 64 |
public static final String WEBPROFILE_HUB_PREFIX = "web-appname:"; |
| 65 |
|
| 66 |
/** |
| 67 |
* Constructor with configuration options. |
| 68 |
* |
| 69 |
* @param securityMap map containing security information for registration |
| 70 |
* @param xClientFactory XML-RPC client factory |
| 71 |
* @param hubEndpoint XML-RPC endpoint for hub server |
| 72 |
*/ |
| 73 |
public WebClientProfile( Map securityMap, |
| 74 |
SampXmlRpcClientFactory xClientFactory, |
| 75 |
URL hubEndpoint ) { |
| 76 |
securityMap_ = securityMap; |
| 77 |
xClientFactory_ = xClientFactory; |
| 78 |
hubEndpoint_ = hubEndpoint; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Constructor with declared client name. |
| 83 |
* |
| 84 |
* @param appName client's declared application name |
| 85 |
* (samp.name entry in security-info map) |
| 86 |
*/ |
| 87 |
public WebClientProfile( String appName ) { |
| 88 |
this( createSecurityMap( appName ), |
| 89 |
XmlRpcKit.getInstance().getClientFactory(), |
| 90 |
getDefaultHubEndpoint() ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Constructor with no arguments. The client's declared application |
| 95 |
* name will be as given by {@link #getDefaultAppName}. |
| 96 |
*/ |
| 97 |
public WebClientProfile() { |
| 98 |
this( getDefaultAppName() ); |
| 99 |
} |
| 100 |
|
| 101 |
public boolean isHubRunning() { |
| 102 |
try { |
| 103 |
SampXmlRpcClient xClient = |
| 104 |
xClientFactory_.createClient( hubEndpoint_ ); |
| 105 |
xClient.callAndWait( WEBSAMP_HUB_PREFIX + "ping", new ArrayList() ); |
| 106 |
return true; |
| 107 |
} |
| 108 |
catch ( IOException e ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public HubConnection register() throws SampException { |
| 114 |
try { |
| 115 |
return new WebHubConnection( xClientFactory_ |
| 116 |
.createClient( hubEndpoint_ ), |
| 117 |
securityMap_ ); |
| 118 |
} |
| 119 |
catch ( SampException e ) { |
| 120 |
for ( Throwable ex = e; ex != null; ex = ex.getCause() ) { |
| 121 |
if ( ex instanceof ConnectException ) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
} |
| 125 |
throw e; |
| 126 |
} |
| 127 |
catch ( ConnectException e ) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
catch ( IOException e ) { |
| 131 |
throw new SampException( e ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns the hub XML-RPC endpoint used by this profile. |
| 137 |
* |
| 138 |
* @return hub endpoint URL |
| 139 |
*/ |
| 140 |
public URL getHubEndpoint() { |
| 141 |
return hubEndpoint_; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the hub XML-RPC endpoint defined by the Web Profile. |
| 146 |
* |
| 147 |
* @return Web Profile hub endpoint URL |
| 148 |
*/ |
| 149 |
public static URL getDefaultHubEndpoint() { |
| 150 |
String surl = "http://" + SampUtils.getLocalhost() + ":" |
| 151 |
+ WEBSAMP_PORT + WEBSAMP_PATH; |
| 152 |
try { |
| 153 |
return new URL( surl ); |
| 154 |
} |
| 155 |
catch ( MalformedURLException e ) { |
| 156 |
throw new AssertionError( "http scheme not supported?? " + surl ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns a default instance of this profile. |
| 162 |
* |
| 163 |
* @return default web client profile instance |
| 164 |
*/ |
| 165 |
public static WebClientProfile getInstance() { |
| 166 |
return new WebClientProfile(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the default application name used by this profile if none |
| 171 |
* is supplied explicitly. |
| 172 |
* If the SAMP_HUB environment variable has the form |
| 173 |
* "web-appname:<appname>" it is taken from there; |
| 174 |
* otherwise it's something like "Unknown". |
| 175 |
* |
| 176 |
* @return default declared client name |
| 177 |
*/ |
| 178 |
public static String getDefaultAppName() { |
| 179 |
String hubloc = Platform.getPlatform() |
| 180 |
.getEnv( DefaultClientProfile.HUBLOC_ENV ); |
| 181 |
return hubloc != null && hubloc.startsWith( WEBPROFILE_HUB_PREFIX ) |
| 182 |
? hubloc.substring( WEBPROFILE_HUB_PREFIX.length() ) |
| 183 |
: "Unknown Application"; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Constructs a security-info map suitable for presentation at |
| 188 |
* registration time, containing the mandatory samp.name entry. |
| 189 |
* |
| 190 |
* @param appName samp.name entry |
| 191 |
* @return security map |
| 192 |
*/ |
| 193 |
private static Map createSecurityMap( String appName ) { |
| 194 |
Map map = new HashMap(); |
| 195 |
map.put( Metadata.NAME_KEY, appName ); |
| 196 |
return map; |
| 197 |
} |
| 198 |
} |