From: <svn...@os...> - 2012-03-27 09:50:52
|
Author: aaime Date: 2012-03-27 02:50:39 -0700 (Tue, 27 Mar 2012) New Revision: 38648 Added: branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/ConnectionFactoryWrapper.java branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/HttpProtocolWrapper.java Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/WFSDataStore.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/MapServerWFSStrategy.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/NonStrictWFSStrategy.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/StrictWFSStrategy.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS100ProtocolHandler.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS_1_0_0_DataStore.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/GetFeatureQueryAdapter.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_DataStore.java branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_Protocol.java branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/GeoServerOnlineTest.java branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/GeoServerOnlineTest.java Log: [GEOT-4092] Add support for vendor parameters in WFS data store Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/WFSDataStore.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/WFSDataStore.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/WFSDataStore.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -18,12 +18,14 @@ import java.net.URI; import java.net.URL; +import java.util.Map; import java.util.Set; import javax.xml.namespace.QName; import org.geotools.data.DataAccess; import org.geotools.data.DataStore; +import org.geotools.factory.Hints.ClassKey; import org.geotools.geometry.jts.ReferencedEnvelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; @@ -39,7 +41,16 @@ * /data/wfs/WFSDataStore.java $ */ public interface WFSDataStore extends DataStore { + /** + * Provides the vendor parameters to be used in a query + * + * @since 2.7.5 + */ + public static final ClassKey WFS_VENDOR_PARAMETERS = new ClassKey(Map.class); + + + /** * Overrides {@link DataAccess#getInfo()} so it type narrows to a {@link WFSServiceInfo} * * @return service information Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/MapServerWFSStrategy.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/MapServerWFSStrategy.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/MapServerWFSStrategy.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -94,8 +94,8 @@ BBOX newFilter = fac.bbox(attName, bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY(), "EPSG:4326"); - query2=new DefaultQuery(query.getTypeName(), query.getNamespace(), newFilter, - query.getMaxFeatures(), query.getPropertyNames(), query.getHandle()); + query2 = new DefaultQuery(query); + query2.setFilter(newFilter); } } catch (IllegalFilterException e) { query2=query; Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/NonStrictWFSStrategy.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/NonStrictWFSStrategy.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/NonStrictWFSStrategy.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -27,6 +27,7 @@ import org.geotools.data.crs.ForceCoordinateSystemFeatureReader; import org.geotools.data.ows.FeatureSetDescription; import org.geotools.data.ows.WFSCapabilities; +import org.geotools.data.wfs.protocol.http.HttpMethod; import org.geotools.feature.SchemaException; import org.geotools.filter.Filters; import org.geotools.filter.visitor.WFSBBoxFilterVisitor; @@ -95,10 +96,18 @@ protected FeatureReader<SimpleFeatureType, SimpleFeature> createFeatureReader(Transaction transaction, Query query) throws IOException { Data data; - data = createFeatureReaderPOST(query, transaction); - - if (data.reader == null) + + if(store.preferredProtocol == HttpMethod.POST) { + data = createFeatureReaderPOST(query, transaction); + + if (data.reader == null) + data = createFeatureReaderGET(query, transaction); + } else { data = createFeatureReaderGET(query, transaction); + + if (data.reader == null) + data = createFeatureReaderPOST(query, transaction); + } if (data.reader == null && data.saxException != null) throw (IOException) new IOException(data.saxException.toString()).initCause(data.saxException); Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/StrictWFSStrategy.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/StrictWFSStrategy.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/StrictWFSStrategy.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -153,12 +153,13 @@ if( visitor.requiresPostProcessing() && query.getPropertyNames()!=Query.ALL_NAMES){ FilterAttributeExtractor attributeExtractor=new FilterAttributeExtractor(); query.getFilter().accept( attributeExtractor, null ); - Set properties=new HashSet(attributeExtractor.getAttributeNameSet()); + Set properties = new HashSet(attributeExtractor.getAttributeNameSet()); properties.addAll(Arrays.asList(query.getPropertyNames())); - this.query=new DefaultQuery(query.getTypeName(), query.getFilter(), query.getMaxFeatures(), - (String[]) properties.toArray(new String[0]), query.getHandle()); - }else + this.query=new DefaultQuery(query); + this.query.setPropertyNames((String[]) properties.toArray(new String[0])); + } else { this.query=query; + } this.filter=visitor.getFilter(); Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS100ProtocolHandler.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS100ProtocolHandler.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS100ProtocolHandler.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -56,7 +56,7 @@ } @SuppressWarnings("unchecked") - private WFSCapabilities parseCapabilities(InputStream capabilitiesReader) throws IOException { + protected WFSCapabilities parseCapabilities(InputStream capabilitiesReader) throws IOException { // TODO: move to some 1.0.0 specific class Map hints = new HashMap(); hints.put(DocumentFactory.VALIDATION_HINT, Boolean.FALSE); Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS_1_0_0_DataStore.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS_1_0_0_DataStore.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_0_0/WFS_1_0_0_DataStore.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -515,6 +515,15 @@ } } } + + // inject vendor params, if any + if(request.getHints() != null && request.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS) != null) { + Map<String, String> vendorParams = (Map<String, String>) request.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS); + for (Map.Entry<String, String> entry : vendorParams.entrySet()) { + url += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), protocolHandler + .getEncoding()); + } + } } } @@ -669,6 +678,30 @@ return null; } + // inject vendor params, if any + if(query != null && query.getHints() != null && query.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS) != null) { + String url = postUrl.toString(); + if ((url == null) || !url.endsWith("?")) { + url += "?"; + } + + boolean first = true; + if(query.getHints() != null && query.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS) != null) { + Map<String, String> vendorParams = (Map<String, String>) query.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS); + for (Map.Entry<String, String> entry : vendorParams.entrySet()) { + if(first) { + first = false; + } else { + url += "&"; + } + url += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), protocolHandler + .getEncoding()); + } + } + + postUrl = new URL(url); + } + HttpURLConnection hc = protocolHandler.getConnectionFactory().getConnection(postUrl, POST); Writer w = getOutputStream(hc); Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/GetFeatureQueryAdapter.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/GetFeatureQueryAdapter.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/GetFeatureQueryAdapter.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -16,7 +16,10 @@ */ package org.geotools.data.wfs.v1_1_0; +import java.util.Map; + import org.geotools.data.Query; +import org.geotools.data.wfs.WFSDataStore; import org.geotools.data.wfs.protocol.wfs.GetFeature; import org.opengis.filter.Filter; import org.opengis.filter.sort.SortBy; @@ -71,5 +74,13 @@ public SortBy[] getSortBy() { return query.getSortBy(); } + + public Map<String, String> getVendorParameter() { + if(query.getHints() != null) { + return (Map<String, String>) query.getHints().get(WFSDataStore.WFS_VENDOR_PARAMETERS); + } else { + return null; + } + } } Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_DataStore.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_DataStore.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_DataStore.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -109,7 +109,7 @@ */ private static final boolean DEFAULT_HTTP_METHOD = true; - private final WFSProtocol wfs; + protected WFSProtocol wfs; private Map<String, SimpleFeatureType> byTypeNameTypes; @@ -319,8 +319,8 @@ Filter[] filters = wfs.splitFilters(query.getFilter()); Filter supportedFilter = filters[0]; Filter postFilter = filters[1]; - System.out.println("Supported filter: " + supportedFilter); - System.out.println("Unupported filter: " + postFilter); + LOGGER.fine("Supported filter: " + supportedFilter); + LOGGER.fine("Unupported filter: " + postFilter); ((DefaultQuery) query).setFilter(supportedFilter); ((DefaultQuery) query).setMaxFeatures(getMaxFeatures(query)); Modified: branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_Protocol.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_Protocol.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/main/java/org/geotools/data/wfs/v1_1_0/WFS_1_1_0_Protocol.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -29,6 +29,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLEncoder; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; @@ -70,6 +71,7 @@ import org.apache.commons.io.IOUtils; import org.eclipse.emf.ecore.EObject; import org.geotools.data.DataSourceException; +import org.geotools.data.wfs.WFSDataStore; import org.geotools.data.wfs.protocol.http.HTTPProtocol; import org.geotools.data.wfs.protocol.http.HTTPResponse; import org.geotools.data.wfs.protocol.http.HttpMethod; @@ -135,9 +137,9 @@ */ private final Map<String, FeatureTypeType> typeInfos; - private HTTPProtocol http; + protected HTTPProtocol http; - private final Charset defaultEncoding; + protected final Charset defaultEncoding; public WFS_1_1_0_Protocol(InputStream capabilitiesReader, HTTPProtocol http, Charset defaultEncoding) throws IOException { @@ -468,11 +470,18 @@ URL url = getOperationURL(WFSOperationType.GET_FEATURE, false); RequestComponents reqParts = strategy.createGetFeatureRequest(this, request); + GetFeatureType requestType = reqParts.getServerRequest(); + + // build the kvp taking into account eventual vendor params Map<String, String> getFeatureKvp = reqParts.getKvpParameters(); - GetFeatureType requestType = reqParts.getServerRequest(); + if(request instanceof GetFeatureQueryAdapter) { + GetFeatureQueryAdapter adapter = (GetFeatureQueryAdapter) request; + if(adapter.getVendorParameter() != null) { + getFeatureKvp.putAll(adapter.getVendorParameter()); + } + } + - System.out.println(" > getFeatureGET: Request url: " + url + ". Parameters: " - + getFeatureKvp); WFSResponse response = issueGetRequest(requestType, url, getFeatureKvp); return response; @@ -486,7 +495,30 @@ throw new UnsupportedOperationException( "The server does not support GetFeature for HTTP method POST"); } - URL url = getOperationURL(WFSOperationType.GET_FEATURE, true); + URL postURL = getOperationURL(WFSOperationType.GET_FEATURE, true); + + // support vendor parameters, GeoServer way + if(request instanceof GetFeatureQueryAdapter) { + GetFeatureQueryAdapter adapter = (GetFeatureQueryAdapter) request; + if(adapter.getVendorParameter() != null) { + String url = postURL.toString(); + if ((url == null) || !url.endsWith("?")) { + url += "?"; + } + + boolean first = true; + for (Map.Entry<String, String> entry : adapter.getVendorParameter().entrySet()) { + if(first) { + first = false; + } else { + url += "&"; + } + url += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); + } + + postURL = new URL(url); + } + } RequestComponents reqParts = strategy.createGetFeatureRequest(this, request); GetFeatureType serverRequest = reqParts.getServerRequest(); @@ -502,7 +534,7 @@ if (!XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) { encoder.getNamespaces().declarePrefix(prefix, namespace); } - WFSResponse response = issuePostRequest(serverRequest, url, encoder); + WFSResponse response = issuePostRequest(serverRequest, postURL, encoder); return response; } @@ -536,7 +568,7 @@ return typeInfos.get(typeName); } - private WFSCapabilitiesType parseCapabilities(InputStream capabilitiesReader) + protected WFSCapabilitiesType parseCapabilities(InputStream capabilitiesReader) throws IOException { final Configuration wfsConfig = strategy.getWfsConfiguration(); final Parser parser = new Parser(wfsConfig); Added: branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/ConnectionFactoryWrapper.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/ConnectionFactoryWrapper.java (rev 0) +++ branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/ConnectionFactoryWrapper.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -0,0 +1,54 @@ +package org.geotools.data.wfs.v1_0_0; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; + +import org.geotools.data.wfs.protocol.http.HttpMethod; +import org.geotools.wfs.protocol.ConnectionFactory; + +/** + * Helper class for the tests + * @author aaime + * + */ +public class ConnectionFactoryWrapper implements ConnectionFactory { + + public ConnectionFactory delegate; + + public ConnectionFactoryWrapper(ConnectionFactory delegate) { + this.delegate = delegate; + } + + public String getAuthUsername() { + return delegate.getAuthUsername(); + } + + public String getAuthPassword() { + return delegate.getAuthPassword(); + } + + public boolean isTryGzip() { + return delegate.isTryGzip(); + } + + public Charset getEncoding() { + return delegate.getEncoding(); + } + + public HttpURLConnection getConnection(URL query, HttpMethod method) throws IOException { + return delegate.getConnection(query, method); + } + + public InputStream getInputStream(HttpURLConnection hc) throws IOException { + return delegate.getInputStream(hc); + } + + public InputStream getInputStream(URL query, HttpMethod method) throws IOException { + return delegate.getInputStream(query, method); + } + + +} Modified: branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/GeoServerOnlineTest.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/GeoServerOnlineTest.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_0_0/GeoServerOnlineTest.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -17,13 +17,12 @@ */ package org.geotools.data.wfs.v1_0_0; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.NoRouteToHostException; import java.net.URL; @@ -45,17 +44,22 @@ import org.geotools.data.FeatureReader; import org.geotools.data.Query; import org.geotools.data.Transaction; +import org.geotools.data.ows.WFSCapabilities; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; +import org.geotools.data.wfs.WFSDataStore; import org.geotools.data.wfs.WFSDataStoreFactory; +import org.geotools.data.wfs.protocol.http.HttpMethod; import org.geotools.factory.CommonFactoryFinder; import org.geotools.factory.GeoTools; +import org.geotools.factory.Hints; import org.geotools.feature.IllegalAttributeException; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.filter.IllegalFilterException; import org.geotools.geometry.jts.ReferencedEnvelope; +import org.geotools.wfs.protocol.ConnectionFactory; import org.junit.Before; import org.junit.Test; import org.opengis.feature.simple.SimpleFeature; @@ -478,7 +482,75 @@ } } } + + @Test + public void testVendorParametersGet() throws Exception { + testVendorParameters(Boolean.FALSE); + } + + @Test + public void testVendorParametersPost() throws Exception { + testVendorParameters(Boolean.TRUE); + } + private void testVendorParameters(Boolean usePost) throws IOException { + if (url == null) + return; + + Map m = new HashMap(); + m.put(WFSDataStoreFactory.URL.key, url); + m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(100000)); + m.put(WFSDataStoreFactory.PROTOCOL.key, usePost); + WFS_1_0_0_DataStore wfs = (WFS_1_0_0_DataStore) (new WFSDataStoreFactory()) + .createDataStore(m); + + final WFS100ProtocolHandler originalHandler = wfs.protocolHandler; + wfs.protocolHandler = new WFS100ProtocolHandler(null, originalHandler.getConnectionFactory()) { + @Override + protected WFSCapabilities parseCapabilities(InputStream capabilitiesReader) + throws IOException { + return originalHandler.getCapabilities(); + } + + @Override + public ConnectionFactory getConnectionFactory() { + return new ConnectionFactoryWrapper(super.getConnectionFactory()) { + + @Override + public HttpURLConnection getConnection(URL query, HttpMethod method) + throws IOException { + String[] keyValueArray = query.getQuery().split("&"); + Map<String, String> kvp = new HashMap<String, String>(); + for (String keyValue : keyValueArray) { + String[] skv = keyValue.split("="); + kvp.put(skv[0], skv[1]); + } + + // check the vendor params actually made it into the url + assertEquals("true", kvp.get("strict")); + assertEquals("mysecret", kvp.get("authkey")); + assertEquals("low%3A2000000%3Bhigh%3A5000000", kvp.get("viewparams")); + + return super.getConnection(query, method); + } + }; + } + }; + + Map<String, String> vparams = new HashMap<String, String>(); + vparams.put("authkey", "mysecret"); + vparams.put("viewparams", "low:2000000;high:5000000"); + vparams.put("strict", "true"); + Hints hints = new Hints(WFSDataStore.WFS_VENDOR_PARAMETERS, vparams); + Query q = new Query("topp:states"); + q.setHints(hints); + + // try with + FeatureReader fr = wfs.getFeatureReader(q, Transaction.AUTO_COMMIT); + assertTrue(fr.hasNext()); + fr.close(); + } + private Id createFidFilter(SimpleFeatureSource fs) throws IOException { SimpleFeatureIterator iter = fs.getFeatures().features(); @@ -496,4 +568,6 @@ iter.close(); } } + + } Modified: branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/GeoServerOnlineTest.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/GeoServerOnlineTest.java 2012-03-26 08:51:38 UTC (rev 38647) +++ branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/GeoServerOnlineTest.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -18,18 +18,42 @@ package org.geotools.data.wfs.v1_1_0; import static org.geotools.data.wfs.v1_1_0.DataTestSupport.GEOS_STATES; +import static org.junit.Assert.*; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import net.opengis.wfs.WFSCapabilitiesType; + import org.geotools.data.DefaultQuery; +import org.geotools.data.FeatureReader; +import org.geotools.data.Query; +import org.geotools.data.Transaction; +import org.geotools.data.ows.WFSCapabilities; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; +import org.geotools.data.wfs.WFSDataStore; +import org.geotools.data.wfs.WFSDataStoreFactory; +import org.geotools.data.wfs.protocol.http.HTTPProtocol; +import org.geotools.data.wfs.protocol.http.HTTPResponse; +import org.geotools.data.wfs.protocol.http.HttpMethod; +import org.geotools.data.wfs.protocol.wfs.WFSProtocol; +import org.geotools.data.wfs.v1_0_0.ConnectionFactoryWrapper; +import org.geotools.data.wfs.v1_0_0.WFS100ProtocolHandler; +import org.geotools.data.wfs.v1_0_0.WFS_1_0_0_DataStore; import org.geotools.factory.CommonFactoryFinder; +import org.geotools.factory.Hints; +import org.geotools.wfs.protocol.ConnectionFactory; import org.junit.Test; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; @@ -44,7 +68,7 @@ public class GeoServerOnlineTest extends AbstractWfsDataStoreOnlineTest { - public static final String SERVER_URL = "http://sigma.openplans.org:8080/geoserver/wfs?service=WFS&request=GetCapabilities&version=1.1.0"; //$NON-NLS-1$ + public static final String SERVER_URL = "http://localhost:8080/geoserver/wfs?service=WFS&request=GetCapabilities&version=1.1.0"; //$NON-NLS-1$ public GeoServerOnlineTest() { super(SERVER_URL, GEOS_STATES, "the_geom", MultiPolygon.class, 49, ff.id(Collections @@ -93,5 +117,76 @@ iterator.close(); } } + + @Test + public void testVendorParametersGet() throws Exception { + testVendorParameters(Boolean.FALSE); + } + + @Test + public void testVendorParametersPost() throws Exception { + testVendorParameters(Boolean.TRUE); + } + private void testVendorParameters(Boolean usePost) throws IOException { + if (Boolean.FALSE.equals(serviceAvailable)) { + return; + } + + // we alter the data store, thus we cannot use the static "wfs" field, we need to create a new one + Map<String, Serializable> params = new HashMap<String, Serializable>(); + params.put(WFSDataStoreFactory.URL.key, SERVER_URL); + params.put(WFSDataStoreFactory.PROTOCOL.key, usePost); + params.put("USE_PULL_PARSER", Boolean.TRUE); + WFSDataStoreFactory dataStoreFactory = new WFSDataStoreFactory(); + WFSDataStore wfs = dataStoreFactory.createDataStore(params); + + final WFS_1_1_0_Protocol originalHandler = (WFS_1_1_0_Protocol) ((WFS_1_1_0_DataStore) wfs).wfs; + originalHandler.http = new HttpProtocolWrapper(originalHandler.http) { + + @Override + public HTTPResponse issueGet(URL baseUrl, Map<String, String> kvp) throws IOException { + // check the vendor params actually made it into the url (at this stage they are not url encoded) + assertEquals("true", kvp.get("strict")); + assertEquals("mysecret", kvp.get("authkey")); + assertEquals("low:2000000;high:5000000", kvp.get("viewparams")); + + return super.issueGet(baseUrl, kvp); + } + + @Override + public HTTPResponse issuePost(URL targetUrl, POSTCallBack callback) throws IOException { + String[] keyValueArray = targetUrl.getQuery().split("&"); + Map<String, String> kvp = new HashMap<String, String>(); + for (String keyValue : keyValueArray) { + String[] skv = keyValue.split("="); + kvp.put(skv[0], skv[1]); + } + + // check the vendor params actually made it into the url + assertEquals("true", kvp.get("strict")); + assertEquals("mysecret", kvp.get("authkey")); + assertEquals("low%3A2000000%3Bhigh%3A5000000", kvp.get("viewparams")); + + return super.issuePost(targetUrl, callback); + } + + }; + + + Map<String, String> vparams = new HashMap<String, String>(); + vparams.put("authkey", "mysecret"); + vparams.put("viewparams", "low:2000000;high:5000000"); + vparams.put("strict", "true"); + Hints hints = new Hints(WFSDataStore.WFS_VENDOR_PARAMETERS, vparams); + Query q = new Query("topp:states"); + q.setHints(hints); + + // read some features, check + FeatureReader fr = wfs.getFeatureReader(q, Transaction.AUTO_COMMIT); + assertTrue(fr.hasNext()); + fr.close(); + } + + } Added: branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/HttpProtocolWrapper.java =================================================================== --- branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/HttpProtocolWrapper.java (rev 0) +++ branches/2.7.x/modules/unsupported/wfs/src/test/java/org/geotools/data/wfs/v1_1_0/HttpProtocolWrapper.java 2012-03-27 09:50:39 UTC (rev 38648) @@ -0,0 +1,56 @@ +package org.geotools.data.wfs.v1_1_0; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; + +import org.geotools.data.wfs.protocol.http.HTTPProtocol; +import org.geotools.data.wfs.protocol.http.HTTPResponse; + +public class HttpProtocolWrapper implements HTTPProtocol { + + HTTPProtocol delegate; + + public HttpProtocolWrapper(HTTPProtocol delegate) { + this.delegate = delegate; + } + + public void setTryGzip(boolean tryGzip) { + delegate.setTryGzip(tryGzip); + } + + public boolean isTryGzip() { + return delegate.isTryGzip(); + } + + public void setAuth(String username, String password) { + delegate.setAuth(username, password); + } + + public boolean isAuthenticating() { + return delegate.isAuthenticating(); + } + + public void setTimeoutMillis(int milliseconds) { + delegate.setTimeoutMillis(milliseconds); + } + + public int getTimeoutMillis() { + return delegate.getTimeoutMillis(); + } + + public HTTPResponse issueGet(URL baseUrl, Map<String, String> kvp) throws IOException { + return delegate.issueGet(baseUrl, kvp); + } + + public HTTPResponse issuePost(URL targetUrl, POSTCallBack callback) throws IOException { + return delegate.issuePost(targetUrl, callback); + } + + public URL createUrl(URL baseUrl, Map<String, String> queryStringKvp) + throws MalformedURLException { + return delegate.createUrl(baseUrl, queryStringKvp); + } + +} |