01: package net.refractions.catalog.util.internal;
02:
03: import java.net.MalformedURLException;
04: import java.net.URL;
05: import java.util.ArrayList;
06: import java.util.Collections;
07: import java.util.List;
08:
09: import org.eclipse.core.runtime.IProgressMonitor;
10:
11: import net.refractions.udig.catalog.CatalogPlugin;
12: import net.refractions.udig.catalog.ICatalog;
13: import net.refractions.udig.catalog.IGeoResource;
14: import net.refractions.udig.catalog.IResolve;
15: import net.refractions.udig.catalog.internal.wms.WMSGeoResourceImpl;
16: import net.refractions.udig.catalog.util.IFriend;
17:
18: /**
19: * Used to handle URL recognisable as coming from geoserver.
20: *
21: * @author Jody
22: */
23: public class GeoServerFriend extends IFriend {
24: public List<IResolve> friendly(IResolve handle,
25: IProgressMonitor monitor) {
26: if (!(handle instanceof IGeoResource)) {
27: return Collections.emptyList();
28: }
29:
30: WMSGeoResourceImpl layer = (WMSGeoResourceImpl) handle;
31: URL url = layer.getIdentifier();
32:
33: String uri = url.toString();
34: String file = url.getFile();
35: String host = url.getHost();
36: int port = url.getPort();
37: String ref = url.getRef();
38: String protocol = url.getProtocol();
39:
40: if (!uri.contains("geoserver")) { //$NON-NLS-1$
41: return Collections.emptyList();
42: }
43: String associate;
44: associate = file.replace("wms", "wfs"); //$NON-NLS-1$ //$NON-NLS-2$
45: int split = associate.indexOf('?');
46: if (split != -1)
47: associate = associate.substring(0, split + 1);
48: associate += "service=WFS&request=GetCapabilities&VERSION=1.0.0"; //$NON-NLS-1$
49: associate += "#"; //$NON-NLS-1$
50: associate += ref;
51:
52: URL target;
53: try {
54: target = new URL(protocol, host, port, associate);
55: } catch (MalformedURLException e) {
56: return Collections.emptyList();
57: }
58: ICatalog local = CatalogPlugin.getDefault().getLocalCatalog();
59:
60: // look up frendly wfs entry from local catalog (if present)
61: IGeoResource friend = local.getById(IGeoResource.class, target,
62: monitor);
63: if (friend == null) {
64: return Collections.emptyList();
65: } else {
66: return new ArrayList<IResolve>(Collections
67: .singletonList(friend));
68: }
69: }
70: }
|