001: package net.refractions.udig.catalog.internal.wms.ui;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011:
012: import net.refractions.udig.catalog.CatalogPlugin;
013: import net.refractions.udig.catalog.IResolve;
014: import net.refractions.udig.catalog.IService;
015: import net.refractions.udig.catalog.internal.wms.WMSGeoResourceImpl;
016: import net.refractions.udig.catalog.internal.wms.WMSServiceExtension;
017: import net.refractions.udig.catalog.internal.wms.WMSServiceImpl;
018: import net.refractions.udig.catalog.ui.UDIGConnectionFactory;
019:
020: import org.geotools.data.wms.WebMapServer;
021:
022: public class WMSConnectionFactory extends UDIGConnectionFactory {
023:
024: public boolean canProcess(Object context) {
025: if (context instanceof IResolve) {
026: IResolve resolve = (IResolve) context;
027: return resolve.canResolve(WebMapServer.class);
028: }
029: return toCapabilitiesURL(context) != null;
030: }
031:
032: public Map<String, Serializable> createConnectionParameters(
033: Object context) {
034: if (context instanceof IResolve) {
035: Map params = createParams((IResolve) context);
036: if (!params.isEmpty())
037: return params;
038: }
039: URL url = toCapabilitiesURL(context);
040: if (url == null) {
041: // so we are not sure it is a wms url
042: // lets guess
043: url = CatalogPlugin.locateURL(context);
044: }
045: if (url != null) {
046: // well we have a url - lets try it!
047: List<IResolve> list = CatalogPlugin.getDefault()
048: .getLocalCatalog().find(url, null);
049: for (IResolve resolve : list) {
050: Map params = createParams(resolve);
051: if (!params.isEmpty())
052: return params; // we got the goods!
053: }
054: return createParams(url);
055: }
056: return Collections.EMPTY_MAP;
057: }
058:
059: static public Map<String, Serializable> createParams(IResolve handle) {
060: if (handle instanceof WMSServiceImpl) {
061: // got a hit!
062: WMSServiceImpl wms = (WMSServiceImpl) handle;
063: return wms.getConnectionParams();
064: } else if (handle instanceof WMSGeoResourceImpl) {
065: WMSGeoResourceImpl layer = (WMSGeoResourceImpl) handle;
066: WMSServiceImpl wms;
067: try {
068: wms = layer.parent(null);
069: return wms.getConnectionParams();
070: } catch (IOException e) {
071: checkedURL(layer.getIdentifier());
072: }
073: } else if (handle.canResolve(WebMapServer.class)) {
074: // must be some kind of handle from a search!
075: return createParams(handle.getIdentifier());
076: }
077: return Collections.EMPTY_MAP;
078: }
079:
080: /** 'Create' params given the provided url, no magic occurs */
081: static public Map<String, Serializable> createParams(URL url) {
082: WMSServiceExtension factory = new WMSServiceExtension();
083: Map params = factory.createParams(url);
084: if (params != null)
085: return params;
086:
087: Map<String, Serializable> params2 = new HashMap<String, Serializable>();
088: params2.put(WMSServiceImpl.WMS_URL_KEY, url);
089: return params2;
090: }
091:
092: /**
093: * Convert "data" to a wms capabilities url
094: * <p>
095: * Candidates for conversion are:
096: * <ul>
097: * <li>URL - from browser DnD
098: * <li>URL#layer - from browser DnD
099: * <li>WMSService - from catalog DnD
100: * <li>WMSGeoResource - from catalog DnD
101: * <li>IService - from search DnD
102: * </ul>
103: * </p>
104: * <p>
105: * No external processing should be required here, it is enough to guess and let
106: * the ServiceFactory try a real connect.
107: * </p>
108: * @param data IService, URL, or something else
109: * @return URL considered a possibility for a WMS Capabilities, or null
110: */
111: static URL toCapabilitiesURL(Object data) {
112: if (data instanceof IResolve) {
113: return toCapabilitiesURL((IResolve) data);
114: } else if (data instanceof URL) {
115: return toCapabilitiesURL((URL) data);
116: } else if (CatalogPlugin.locateURL(data) != null) {
117: return toCapabilitiesURL(CatalogPlugin.locateURL(data));
118: } else {
119: return null; // no idea what this should be
120: }
121: }
122:
123: static URL toCapabilitiesURL(IResolve resolve) {
124: if (resolve instanceof IService) {
125: return toCapabilitiesURL((IService) resolve);
126: }
127: return toCapabilitiesURL(resolve.getIdentifier());
128: }
129:
130: static URL toCapabilitiesURL(IService resolve) {
131: if (resolve instanceof WMSServiceImpl) {
132: return toCapabilitiesURL((WMSServiceImpl) resolve);
133: }
134: return toCapabilitiesURL(resolve.getIdentifier());
135: }
136:
137: /** No further QA checks needed - we know this one works */
138: static URL toCapabilitiesURL(WMSServiceImpl wms) {
139: return wms.getIdentifier();
140: }
141:
142: /** Quick sanity check to see if url is a WMS url */
143: static URL toCapabilitiesURL(URL url) {
144: if (url == null)
145: return null;
146:
147: String path = url.getPath() == null ? null : url.getPath()
148: .toLowerCase();
149: String query = url.getQuery() == null ? null : url.getQuery()
150: .toLowerCase();
151: String protocol = url.getProtocol() == null ? null : url
152: .getProtocol().toLowerCase();
153:
154: if (!"http".equals(protocol) //$NON-NLS-1$
155: && !"https".equals(protocol)) { //$NON-NLS-1$
156: return null;
157: }
158: if (query != null && query.indexOf("service=wms") != -1) { //$NON-NLS-1$
159: return checkedURL(url);
160: } else if (query != null
161: && query.indexOf("service=") == -1 && query.indexOf("request=getcapabilities") != -1) { //$NON-NLS-1$ //$NON-NLS-2$
162: try {
163: return new URL(url.toString() + "&SERVICE=WMS"); //$NON-NLS-1$
164: } catch (MalformedURLException e) {
165: return null;
166: }
167: }
168:
169: if (path != null
170: && path.toUpperCase().indexOf("GEOSERVER/WMS") != -1) { //$NON-NLS-1$
171: return checkedURL(url);
172: }
173: if (url.toExternalForm().indexOf("WMS") != -1) { //$NON-NLS-1$
174: return checkedURL(url);
175: }
176: return null;
177: }
178:
179: /** Check that any trailing #layer is removed from the url */
180: static public URL checkedURL(URL url) {
181: String check = url.toExternalForm();
182: int hash = check.indexOf('#');
183: if (hash == -1) {
184: return url;
185: }
186: try {
187: return new URL(check.substring(0, hash));
188: } catch (MalformedURLException e) {
189: return null;
190: }
191: }
192:
193: public URL createConnectionURL(Object context) {
194: // TODO Auto-generated method stub
195: return null;
196: }
197:
198: }
|