| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.net.MalformedURLException; |
| 4 |
import java.net.URL; |
| 5 |
import java.util.Collection; |
| 6 |
import java.util.HashSet; |
| 7 |
import java.util.Iterator; |
| 8 |
import java.util.List; |
| 9 |
import java.util.Map; |
| 10 |
import org.astrogrid.samp.Message; |
| 11 |
import org.astrogrid.samp.Metadata; |
| 12 |
import org.astrogrid.samp.RegInfo; |
| 13 |
import org.astrogrid.samp.Response; |
| 14 |
import org.astrogrid.samp.Subscriptions; |
| 15 |
import org.astrogrid.samp.client.CallableClient; |
| 16 |
import org.astrogrid.samp.client.HubConnection; |
| 17 |
import org.astrogrid.samp.client.SampException; |
| 18 |
|
| 19 |
/** |
| 20 |
* HubConnection wrapper implementation which intercepts all incoming |
| 21 |
* and outgoing communications, scans them for URLs in the payload, |
| 22 |
* and notifies a supplied UrlTracker object. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 22 Jul 2011 |
| 26 |
*/ |
| 27 |
class UrlTrackerHubConnection implements HubConnection { |
| 28 |
|
| 29 |
private final HubConnection base_; |
| 30 |
private final UrlTracker urlTracker_; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor. |
| 34 |
* |
| 35 |
* @param base connection on which this one is based |
| 36 |
* @param urlTracker tracker for URL usage |
| 37 |
*/ |
| 38 |
public UrlTrackerHubConnection( HubConnection base, |
| 39 |
UrlTracker urlTracker ) { |
| 40 |
base_ = base; |
| 41 |
urlTracker_ = urlTracker; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Recursively scans a SAMP map for items that look like URLs, |
| 46 |
* and notifies the tracker that they are incoming. |
| 47 |
* As a convenience the input map is returned. |
| 48 |
* |
| 49 |
* @param map map to scan |
| 50 |
* @return the input map, unchanged |
| 51 |
*/ |
| 52 |
private Map scanIncoming( Map map ) { |
| 53 |
URL[] urls = scanForUrls( map ); |
| 54 |
for ( int iu = 0; iu < urls.length; iu++ ) { |
| 55 |
urlTracker_.noteIncomingUrl( urls[ iu ] ); |
| 56 |
} |
| 57 |
return map; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Recursively scans a SAMP map for items that look like URLs, |
| 62 |
* and notifies the tracker that they are outgoing. |
| 63 |
* As a convenience the input map is returned. |
| 64 |
* |
| 65 |
* @param map map to scan |
| 66 |
* @return the input map, unchanged |
| 67 |
*/ |
| 68 |
private Map scanOutgoing( Map map ) { |
| 69 |
URL[] urls = scanForUrls( map ); |
| 70 |
for ( int iu = 0; iu < urls.length; iu++ ) { |
| 71 |
urlTracker_.noteOutgoingUrl( urls[ iu ] ); |
| 72 |
} |
| 73 |
return map; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Recursively scans a map for items that look like URLs and |
| 78 |
* returns them as an array. |
| 79 |
* |
| 80 |
* @param map map to scan |
| 81 |
* @return array of URLs found in <code>map</code> |
| 82 |
*/ |
| 83 |
private URL[] scanForUrls( Map map ) { |
| 84 |
Collection urlSet = new HashSet(); |
| 85 |
scanForUrls( map, urlSet ); |
| 86 |
return (URL[]) urlSet.toArray( new URL[ 0 ] ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Recursively scans a SAMP data item for items that look like URLs |
| 91 |
* and appends them into a supplied list. |
| 92 |
* |
| 93 |
* @param item SAMP data item (String, List or Map) |
| 94 |
* @param urlSet list of URL objects to which URLs can be added |
| 95 |
*/ |
| 96 |
private void scanForUrls( Object item, Collection urlSet ) { |
| 97 |
if ( item instanceof String ) { |
| 98 |
if ( isUrl( (String) item ) ) { |
| 99 |
try { |
| 100 |
urlSet.add( new URL( (String) item ) ); |
| 101 |
} |
| 102 |
catch ( MalformedURLException e ) { |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
else if ( item instanceof List ) { |
| 107 |
for ( Iterator it = ((List) item).iterator(); it.hasNext(); ) { |
| 108 |
scanForUrls( it.next(), urlSet ); |
| 109 |
} |
| 110 |
} |
| 111 |
else if ( item instanceof Map ) { |
| 112 |
for ( Iterator it = ((Map) item).values().iterator(); |
| 113 |
it.hasNext(); ) { |
| 114 |
scanForUrls( it.next(), urlSet ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Determines whether a given string is apparently a URL. |
| 121 |
* |
| 122 |
* @param str string to test |
| 123 |
* @return true iff <code>str</code> looks like a URL |
| 124 |
*/ |
| 125 |
private boolean isUrl( String str ) { |
| 126 |
if ( str == null || str.indexOf( ":/" ) <= 0 ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
else { |
| 130 |
try { |
| 131 |
new URL( str ); |
| 132 |
return true; |
| 133 |
} |
| 134 |
catch ( MalformedURLException e ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public void setCallable( CallableClient callable ) throws SampException { |
| 141 |
base_.setCallable( new UrlTrackerCallableClient( callable ) ); |
| 142 |
} |
| 143 |
|
| 144 |
public void notify( String recipientId, Map msg ) throws SampException { |
| 145 |
base_.notify( recipientId, scanOutgoing( msg ) ); |
| 146 |
} |
| 147 |
|
| 148 |
public List notifyAll( Map msg ) throws SampException { |
| 149 |
return base_.notifyAll( scanOutgoing( msg ) ); |
| 150 |
} |
| 151 |
|
| 152 |
public String call( String recipientId, String msgTag, Map msg ) |
| 153 |
throws SampException { |
| 154 |
return base_.call( recipientId, msgTag, scanOutgoing( msg ) ); |
| 155 |
} |
| 156 |
|
| 157 |
public Map callAll( String msgTag, Map msg ) throws SampException { |
| 158 |
return base_.callAll( msgTag, scanOutgoing( msg ) ); |
| 159 |
} |
| 160 |
|
| 161 |
public Response callAndWait( String recipientId, Map msg, int timeout ) |
| 162 |
throws SampException { |
| 163 |
return (Response) |
| 164 |
scanIncoming( base_.callAndWait( recipientId, |
| 165 |
scanOutgoing( msg ), |
| 166 |
timeout ) ); |
| 167 |
} |
| 168 |
|
| 169 |
public void reply( String msgId, Map response ) throws SampException { |
| 170 |
base_.reply( msgId, scanOutgoing( response ) ); |
| 171 |
} |
| 172 |
|
| 173 |
public RegInfo getRegInfo() { |
| 174 |
return (RegInfo) scanIncoming( base_.getRegInfo() ); |
| 175 |
} |
| 176 |
|
| 177 |
public void ping() throws SampException { |
| 178 |
base_.ping(); |
| 179 |
} |
| 180 |
|
| 181 |
public void unregister() throws SampException { |
| 182 |
base_.unregister(); |
| 183 |
} |
| 184 |
|
| 185 |
public void declareMetadata( Map meta ) throws SampException { |
| 186 |
base_.declareMetadata( scanOutgoing( meta ) ); |
| 187 |
} |
| 188 |
|
| 189 |
public Metadata getMetadata( String clientId ) throws SampException { |
| 190 |
return (Metadata) scanIncoming( base_.getMetadata( clientId ) ); |
| 191 |
} |
| 192 |
|
| 193 |
public void declareSubscriptions( Map subs ) throws SampException { |
| 194 |
base_.declareSubscriptions( scanOutgoing( subs ) ); |
| 195 |
} |
| 196 |
|
| 197 |
public Subscriptions getSubscriptions( String clientId ) |
| 198 |
throws SampException { |
| 199 |
return (Subscriptions) |
| 200 |
scanIncoming( base_.getSubscriptions( clientId ) ); |
| 201 |
} |
| 202 |
|
| 203 |
public String[] getRegisteredClients() throws SampException { |
| 204 |
return base_.getRegisteredClients(); |
| 205 |
} |
| 206 |
|
| 207 |
public Map getSubscribedClients( String mtype ) throws SampException { |
| 208 |
return scanIncoming( base_.getSubscribedClients( mtype ) ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* CallableClient wrapper implementation which intercepts |
| 213 |
* communications, scans the payloads for URLs, and informs an |
| 214 |
* associated UrlTracker. |
| 215 |
*/ |
| 216 |
private class UrlTrackerCallableClient implements CallableClient { |
| 217 |
private final CallableClient baseCallable_; |
| 218 |
|
| 219 |
/** |
| 220 |
* Constructor. |
| 221 |
* |
| 222 |
* @param baseCallable object on which this one is based |
| 223 |
*/ |
| 224 |
UrlTrackerCallableClient( CallableClient baseCallable ) { |
| 225 |
baseCallable_ = baseCallable; |
| 226 |
} |
| 227 |
|
| 228 |
public void receiveCall( String senderId, String msgId, Message msg ) |
| 229 |
throws Exception { |
| 230 |
baseCallable_.receiveCall( senderId, msgId, |
| 231 |
(Message) scanIncoming( msg ) ); |
| 232 |
} |
| 233 |
|
| 234 |
public void receiveNotification( String senderId, Message msg ) |
| 235 |
throws Exception { |
| 236 |
baseCallable_.receiveNotification( senderId, |
| 237 |
(Message) scanIncoming( msg ) ); |
| 238 |
} |
| 239 |
|
| 240 |
public void receiveResponse( String responderId, String msgTag, |
| 241 |
Response response ) |
| 242 |
throws Exception { |
| 243 |
baseCallable_.receiveResponse( responderId, msgTag, |
| 244 |
(Response) |
| 245 |
scanIncoming( response ) ); |
| 246 |
} |
| 247 |
} |
| 248 |
} |