001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.data.wfs;
017:
018: import java.io.IOException;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.logging.Logger;
023:
024: import org.geotools.data.AbstractDataStoreFactory;
025: import org.geotools.data.DataStore;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * <p>
030: * DOCUMENT ME!
031: * </p>
032: *
033: * @author dzwiers
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/main/java/org/geotools/data/wfs/WFSDataStoreFactory.java $
035: */
036: public class WFSDataStoreFactory extends AbstractDataStoreFactory {
037:
038: /**
039: * url
040: */
041: public static final Param URL = new Param(
042: "WFSDataStoreFactory:GET_CAPABILITIES_URL",
043: URL.class,
044: "Represents a URL to the getCapabilities document or a server instance.",
045: true);
046:
047: /**
048: * boolean
049: */
050: public static final Param PROTOCOL = new Param(
051: "WFSDataStoreFactory:PROTOCOL",
052: Boolean.class,
053: "Sets a preference for the HTTP protocol to use when requesting WFS functionality. Set this value to Boolean.TRUE for POST, Boolean.FALSE for GET or NULL for AUTO",
054: false);
055:
056: // password stuff -- see java.net.Authentication
057: // either both or neither
058: /**
059: * String
060: */
061: public static final Param USERNAME = new Param(
062: "WFSDataStoreFactory:USERNAME",
063: String.class,
064: "This allows the user to specify a username. This param should not be used without the PASSWORD param.",
065: false);
066: /**
067: * String
068: */
069: public static final Param PASSWORD = new Param(
070: "WFSDataStoreFactory:PASSWORD",
071: String.class,
072: "This allows the user to specify a username. This param should not be used without the USERNAME param.",
073: false);
074:
075: /**
076: * String
077: */
078: public static final Param ENCODING = new Param(
079: "WFSDataStoreFactory:ENCODING",
080: String.class,
081: "This allows the user to specify the Encoding of the XML of the XML-Requests sent to the Server.",
082: false);
083:
084: // timeout -- optional
085: /**
086: * Integer
087: */
088: public static final Param TIMEOUT = new Param(
089: "WFSDataStoreFactory:TIMEOUT",
090: Integer.class,
091: "This allows the user to specify a timeout in milliseconds. This param has a default value of 3000ms.",
092: false);
093:
094: // buffer size -- optional
095: /**
096: * Integer
097: */
098: public static final Param BUFFER_SIZE = new Param(
099: "WFSDataStoreFactory:BUFFER_SIZE",
100: Integer.class,
101: "This allows the user to specify a buffer size in features. This param has a default value of 10 features.",
102: false);
103: // use gzip -- optional
104: /**
105: * Boolean
106: */
107: public static final Param TRY_GZIP = new Param(
108: "WFSDataStoreFactory:TRY_GZIP",
109: Boolean.class,
110: "Indicates that datastore should use gzip to transfer data if the server supports it. Default is true",
111: false);
112: // be lenient about parsing data -- optional
113: /**
114: * Boolean
115: */
116: public static final Param LENIENT = new Param(
117: "WFSDataStoreFactory:LENIENT",
118: Boolean.class,
119: "Indicates that datastore should do its best to create features from the provided data even if it does not accurately match the schema. Errors will be logged but the parsing will continue if this is true. Default is false",
120: false);
121:
122: protected Map cache = new HashMap();
123: protected static final Logger logger = logger();
124:
125: private static Logger logger() {
126: Logger r = org.geotools.util.logging.Logging
127: .getLogger("org.geotools.data.wfs");
128: return r;
129: }
130:
131: /**
132: * @see org.geotools.data.DataStoreFactorySpi#createDataStore(java.util.Map)
133: */
134: public DataStore createDataStore(Map params) throws IOException {
135: // TODO check that we can use hashcodes in this manner -- think it's ok, particularily for regular usage
136: if (cache.containsKey(params)) {
137: return (DataStore) cache.get(params);
138: }
139:
140: return createNewDataStore(params);
141: }
142:
143: /**
144: * DOCUMENT ME!
145: *
146: * @param params DOCUMENT ME!
147: *
148: * @return DOCUMENT ME!
149: *
150: * @throws IOException
151: *
152: * @see org.geotools.data.DataStoreFactorySpi#createNewDataStore(java.util.Map)
153: */
154: public DataStore createNewDataStore(Map params) throws IOException {
155: URL host = null;
156:
157: if (params.containsKey(URL.key)) {
158: host = (URL) URL.lookUp(params);
159: }
160:
161: Boolean protocol = null;
162: if (params.containsKey(PROTOCOL.key)) {
163: protocol = (Boolean) PROTOCOL.lookUp(params);
164: }
165:
166: String user, pass;
167: user = pass = null;
168:
169: int timeout = 3000;
170: int buffer = 10;
171: boolean tryGZIP = true;
172: boolean lenient = false;
173: String encoding = null;
174:
175: if (params.containsKey(TIMEOUT.key)) {
176: Integer i = (Integer) TIMEOUT.lookUp(params);
177: if (i != null)
178: timeout = i.intValue();
179: }
180:
181: if (params.containsKey(BUFFER_SIZE.key)) {
182: Integer i = (Integer) BUFFER_SIZE.lookUp(params);
183: if (i != null)
184: buffer = i.intValue();
185: }
186:
187: if (params.containsKey(TRY_GZIP.key)) {
188: Boolean b = (Boolean) TRY_GZIP.lookUp(params);
189: if (b != null)
190: tryGZIP = b.booleanValue();
191: }
192:
193: if (params.containsKey(LENIENT.key)) {
194: Boolean b = (Boolean) LENIENT.lookUp(params);
195: if (b != null)
196: lenient = b.booleanValue();
197: }
198:
199: if (params.containsKey(USERNAME.key)) {
200: user = (String) USERNAME.lookUp(params);
201: }
202:
203: if (params.containsKey(PASSWORD.key)) {
204: pass = (String) PASSWORD.lookUp(params);
205: }
206:
207: if (params.containsKey(ENCODING.key)) {
208: encoding = (String) ENCODING.lookUp(params);
209: }
210:
211: if (((user == null) && (pass != null))
212: || ((pass == null) && (user != null))) {
213: throw new IOException(
214: "Cannot define only one of USERNAME or PASSWORD, muct define both or neither");
215: }
216:
217: DataStore ds = null;
218:
219: try {
220: ds = new WFSDataStore(host, protocol, user, pass, timeout,
221: buffer, tryGZIP, lenient, encoding);
222: cache.put(params, ds);
223: } catch (SAXException e) {
224: logger.warning(e.toString());
225: throw new IOException(e.toString());
226: }
227:
228: return ds;
229: }
230:
231: /**
232: * @see org.geotools.data.DataStoreFactorySpi#getDescription()
233: */
234: public String getDescription() {
235: return "The WFSDataStore represents a connection to a Web Feature Server. This connection provides access to the Features published by the server, and the ability to perform transactions on the server (when supported / allowed).";
236: }
237:
238: /**
239: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
240: */
241: public Param[] getParametersInfo() {
242: return new Param[] { URL, PROTOCOL, USERNAME, PASSWORD,
243: TIMEOUT, BUFFER_SIZE };
244: }
245:
246: /**
247: * @see org.geotools.data.DataStoreFactorySpi#canProcess(java.util.Map)
248: */
249: public boolean canProcess(Map params) {
250: if (params == null) {
251: return false;
252: }
253:
254: // check url
255: if (!params.containsKey(URL.key)) {
256: return false; // cannot have both
257: }
258:
259: // check password / username
260: if (params.containsKey(USERNAME.key)) {
261: if (!params.containsKey(PASSWORD.key)) {
262: return false; // must have both
263: }
264: } else {
265: if (params.containsKey(PASSWORD.key)) {
266: return false; // must have both
267: }
268: }
269:
270: // check for type
271: if (params.containsKey(PROTOCOL.key)) {
272: try {
273: PROTOCOL.lookUp(params);
274: } catch (IOException e) {
275: return false;
276: }
277: }
278:
279: // check for type
280: if (params.containsKey(TIMEOUT.key)) {
281: try {
282: TIMEOUT.lookUp(params);
283: } catch (IOException e) {
284: return false;
285: }
286: }
287:
288: if (params.containsKey(BUFFER_SIZE.key)) {
289: try {
290: BUFFER_SIZE.lookUp(params);
291: } catch (IOException e) {
292: return false;
293: }
294: }
295:
296: return true;
297: }
298:
299: /**
300: * @see org.geotools.data.DataStoreFactorySpi#getDisplayName()
301: */
302: public String getDisplayName() {
303: return "Web Feature Server";
304: }
305:
306: /**
307: * @see org.geotools.data.DataStoreFactorySpi#isAvailable()
308: */
309: public boolean isAvailable() {
310: return true;
311: }
312: }
|