001: package net.refractions.udig.catalog.internal.wfs.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.wfs.WFSGeoResourceImpl;
016: import net.refractions.udig.catalog.internal.wfs.WFSServiceExtension;
017: import net.refractions.udig.catalog.internal.wfs.WFSServiceImpl;
018: import net.refractions.udig.catalog.ui.UDIGConnectionFactory;
019:
020: import org.geotools.data.wfs.WFSDataStore;
021: import org.geotools.data.wfs.WFSDataStoreFactory;
022:
023: public class WFSConnectionFactory extends UDIGConnectionFactory {
024:
025: public boolean canProcess(Object data) {
026: if (data instanceof IResolve) {
027: IResolve resolve = (IResolve) data;
028: return resolve.canResolve(WFSDataStore.class);
029: }
030: return toCapabilitiesURL(data) != null;
031: }
032:
033: public Map<String, Serializable> createConnectionParameters(
034: Object data) {
035: if (data == null)
036: return null;
037: if (data instanceof WFSServiceImpl) {
038: WFSServiceImpl wfs = (WFSServiceImpl) data;
039: return wfs.getConnectionParams();
040: }
041: URL url = toCapabilitiesURL(data);
042: if (url == null) {
043: // so we are not sure it is a wms url
044: // lets guess
045: url = CatalogPlugin.locateURL(data);
046: }
047: if (url != null) {
048: // well we have a url - lets try it!
049: List<IResolve> list = CatalogPlugin.getDefault()
050: .getLocalCatalog().find(url, null);
051: for (IResolve resolve : list) {
052: if (resolve instanceof WFSServiceImpl) {
053: // got a hit!
054: WFSServiceImpl wfs = (WFSServiceImpl) resolve;
055: return wfs.getConnectionParams();
056: } else if (resolve instanceof WFSGeoResourceImpl) {
057: WFSGeoResourceImpl layer = (WFSGeoResourceImpl) resolve;
058: WFSServiceImpl wfs;
059: try {
060: wfs = (WFSServiceImpl) layer.parent(null);
061: return wfs.getConnectionParams();
062: } catch (IOException e) {
063: checkedURL(layer.getIdentifier());
064: }
065: }
066: }
067: return createParams(url);
068: }
069: return Collections.emptyMap();
070: }
071:
072: public URL createConnectionURL(Object context) {
073: return null;
074: }
075:
076: /**
077: * Convert "data" to a wfs capabilities url
078: * <p>
079: * Candidates for conversion are:
080: * <ul>
081: * <li>URL - from browser DnD
082: * <li>URL#layer - from browser DnD
083: * <li>WFSService - from catalog DnD
084: * <li>IService - from search DnD
085: * </ul>
086: * </p>
087: * <p>
088: * No external processing should be required here, it is enough to guess and let
089: * the ServiceFactory try a real connect.
090: * </p>
091: * @param data IService, URL, or something else
092: * @return URL considered a possibility for a WFS Capabilities, or null
093: */
094: URL toCapabilitiesURL(Object data) {
095: if (data instanceof IResolve) {
096: return toCapabilitiesURL((IResolve) data);
097: } else if (data instanceof URL) {
098: return toCapabilitiesURL((URL) data);
099: } else if (CatalogPlugin.locateURL(data) != null) {
100: return toCapabilitiesURL(CatalogPlugin.locateURL(data));
101: } else {
102: return null; // no idea what this should be
103: }
104: }
105:
106: protected URL toCapabilitiesURL(IResolve resolve) {
107: if (resolve instanceof IService) {
108: return toCapabilitiesURL((IService) resolve);
109: }
110: return toCapabilitiesURL(resolve.getIdentifier());
111: }
112:
113: protected URL toCapabilitiesURL(IService resolve) {
114: if (resolve instanceof WFSServiceImpl) {
115: return toCapabilitiesURL((WFSServiceImpl) resolve);
116: }
117: return toCapabilitiesURL(resolve.getIdentifier());
118: }
119:
120: protected URL toCapabilitiesURL(WFSServiceImpl wfs) {
121: return wfs.getIdentifier();
122: }
123:
124: protected URL toCapabilitiesURL(URL url) {
125: if (url == null)
126: return null;
127:
128: String path = url.getPath();
129: String query = url.getQuery();
130: String protocol = (url.getProtocol() != null) ? url
131: .getProtocol().toLowerCase() : null;
132:
133: if (!"http".equals(protocol) && !"https".equals(protocol)) { //$NON-NLS-1$ //$NON-NLS-2$
134: return null;
135:
136: }
137: if (query != null && query.indexOf("service=wfs") != -1) { //$NON-NLS-1$
138: return checkedURL(url);
139: }
140: if (path != null && path.indexOf("geoserver/wfs") != -1) { //$NON-NLS-1$
141: return checkedURL(url);
142: }
143: if (url.toExternalForm().indexOf("WFS") != -1) { //$NON-NLS-1$
144: return checkedURL(url);
145: }
146: return null;
147: }
148:
149: /** Check that any trailing #layer is removed from the url */
150: private static final URL checkedURL(URL url) {
151: String check = url.toExternalForm();
152: int hash = check.indexOf('#');
153: if (hash == -1) {
154: return url;
155: }
156: try {
157: return new URL(check.substring(0, hash));
158: } catch (MalformedURLException e) {
159: return null;
160: }
161: }
162:
163: /** 'Create' params given the provided url, no magic occurs */
164: protected Map<String, Serializable> createParams(URL url) {
165: WFSServiceExtension factory = new WFSServiceExtension();
166: Map<String, Serializable> params = factory.createParams(url);
167: if (params != null)
168: return params;
169:
170: Map<String, Serializable> params2 = new HashMap<String, Serializable>();
171: params2.put(WFSDataStoreFactory.URL.key, url);
172:
173: return params2;
174: }
175: }
|