001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.internal.wfs;
018:
019: import java.io.Serializable;
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import net.refractions.udig.catalog.AbstractDataStoreServiceExtension;
025: import net.refractions.udig.catalog.IService;
026: import net.refractions.udig.catalog.ServiceExtension;
027: import net.refractions.udig.catalog.internal.wfs.UDIGWFSDataStoreFactory.UDIGWFSDataStore;
028: import net.refractions.udig.catalog.wfs.internal.Messages;
029:
030: import org.geotools.data.DataStoreFactorySpi;
031: import org.geotools.data.wfs.WFSDataStoreFactory;
032:
033: /**
034: * Service extension for WFS Services
035: *
036: * @author David Zwiers, Refractions Research
037: * @since 0.6
038: */
039: public class WFSServiceExtension extends
040: AbstractDataStoreServiceExtension implements ServiceExtension {
041:
042: private static WFSDataStoreFactory wfsDSFactory;
043:
044: public static WFSDataStoreFactory getWFSDSFactory() {
045: if (wfsDSFactory == null)
046: wfsDSFactory = new UDIGWFSDataStoreFactory();
047: return wfsDSFactory;
048: }
049:
050: public IService createService(URL id,
051: Map<String, Serializable> params) {
052: if (params == null
053: || !params.containsKey(WFSDataStoreFactory.URL.key))
054: return null;
055:
056: if (!params.containsKey(WFSDataStoreFactory.LENIENT.key))
057: params.put(WFSDataStoreFactory.LENIENT.key, true);
058: if (!params.containsKey(WFSDataStoreFactory.TRY_GZIP.key))
059: params.put(WFSDataStoreFactory.TRY_GZIP.key, true);
060: if (id == null) {
061: URL base = (URL) params.get(WFSDataStoreFactory.URL.key);
062: base = base == null ? null : UDIGWFSDataStore
063: .createGetCapabilitiesRequest(base);
064: return new WFSServiceImpl(base, params);
065: }
066: return new WFSServiceImpl(id, params);
067: }
068:
069: public Map<String, Serializable> createParams(URL url) {
070: if (!isWFS(url)) {
071: return null;
072: }
073:
074: // wfs check
075: Map<String, Serializable> params = new HashMap<String, Serializable>();
076: params.put(WFSDataStoreFactory.URL.key, url);
077: params.put(WFSDataStoreFactory.BUFFER_SIZE.key, 100);
078: params.put(WFSDataStoreFactory.LENIENT.key, true);
079: params.put(WFSDataStoreFactory.TRY_GZIP.key, true);
080:
081: // don't check ... it blocks
082: // (XXX: but we are using that to figure out if the service will work?)
083: return params;
084: }
085:
086: /** A couple quick checks on the url */
087: private static final boolean isWFS(URL url) {
088: return processURL(url) == null;
089: }
090:
091: @Override
092: protected String doOtherChecks(Map<String, Serializable> params) {
093: return null;
094: }
095:
096: @Override
097: protected DataStoreFactorySpi getDataStoreFactory() {
098: return getWFSDSFactory();
099: }
100:
101: public String reasonForFailure(URL url) {
102: String result = processURL(url);
103: if (result != null)
104: return result;
105: return reasonForFailure(createParams(url));
106: }
107:
108: private static String processURL(URL url) {
109: String PATH = url.getPath();
110: String QUERY = url.getQuery();
111: String PROTOCOL = url.getProtocol();
112:
113: if (PROTOCOL.indexOf("http") == -1) { //$NON-NLS-1$
114: return Messages.WFSServiceExtension_protocol
115: + "'" + PROTOCOL + "'"; //$NON-NLS-1$//$NON-NLS-2$
116: }
117: if (QUERY != null
118: && QUERY.toUpperCase().indexOf("SERVICE=") != -1) { //$NON-NLS-1$
119: int indexOf = QUERY.toUpperCase().indexOf("SERVICE="); //$NON-NLS-1$
120: // we have a service! it better be wfs
121: if (QUERY.toUpperCase().indexOf("SERVICE=WFS") == -1) { //$NON-NLS-1$
122: int endOfExp = QUERY.indexOf('&', indexOf);
123: if (endOfExp == -1)
124: endOfExp = QUERY.length();
125: if (endOfExp > indexOf + 8)
126: return Messages.WFSServiceExtension_badService
127: + QUERY.substring(indexOf + 8, endOfExp);
128: else {
129: return Messages.WFSServiceExtension_badService + ""; //$NON-NLS-1$
130: }
131: }
132: }
133: if (PATH != null
134: && PATH.toUpperCase().indexOf("GEOSERVER/WFS") != -1) { //$NON-NLS-1$
135: return null;
136: }
137: return null; // try it anyway
138: }
139:
140: }
|