From 715fb71508ba14af4dc5932ecd56608fe69ec92a Mon Sep 17 00:00:00 2001 From: Simone Giannecchini Date: Tue, 7 May 2013 11:00:04 +0200 Subject: [PATCH 1/3] first commit on work for StructuredCoverage --- .../rest/GeoServerRESTPublisher.java | 95 ++++- .../geoserver/rest/GeoServerRESTReader.java | 103 ++++- .../geoserver/rest/HTTPUtils.java | 12 +- .../RESTStructuredCoverageGranulesList.java | 230 ++++++++++++ .../RESTStructuredCoverageIndexSchema.java | 212 +++++++++++ .../manager/GeoServerRESTStoreManager.java | 2 +- ...STStructuredCoverageGridReaderManager.java | 354 ++++++++++++++++++ ...ructuredCoverageGridReaderManagerTest.java | 136 +++++++ 8 files changed, 1135 insertions(+), 9 deletions(-) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageGranulesList.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageIndexSchema.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java create mode 100644 src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 9b703ce..2691b12 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -26,6 +26,7 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.decoder.RESTCoverageList; import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import it.geosolutions.geoserver.rest.encoder.GSBackupEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; @@ -37,9 +38,11 @@ import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy import it.geosolutions.geoserver.rest.encoder.GSWorkspaceEncoder; import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredCoverageGridReaderManager; import java.io.File; import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; @@ -2537,7 +2540,7 @@ public class GeoServerRESTPublisher { * * configured - Only setup or configured feature types are returned. This is the default value. available - Only unconfigured feature types * (not yet setup) but are available from the specified datastore will be returned. available_with_geom - Same as available but only - * includes feature types that have a geometry attribute. all - The union of configured and available. + * includes feature types that have a geometry granule. all - The union of configured and available. * * * @return true if success @@ -2647,4 +2650,94 @@ public class GeoServerRESTPublisher { return URLEncoder.encode(s); // } } + + // ==> StructuredCoverageGridReader + + /** + * Create a store or harvest the coverage from the provided external path. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param format the format of the file to upload + * @param the absolut path to the file to upload + * + * @return true if the call succeeds or false otherwise. + */ + public boolean createOrHarvestExternal(String workspace, String coverageStore, String format, String path) { + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + return manager.createOrHarvestExternal(workspace, coverageStore, format, path); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return false; + } + + /** + * Remove a granule from a structured coverage by id. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage from which we are going to remove + * @param filter the absolute path to the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + public boolean removeGranuleById(final String workspace, String coverageStore, String coverage, String granuleId) { + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + return manager.removeGranuleById(workspace, coverageStore, coverage, granuleId); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return false; + } + /** + * Remove granules from a structured coverage, by providing a CQL filter. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage from which we are going to remove + * @param filter the absolute path to the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + public boolean removeGranulesByCQL(final String workspace, String coverageStore,String coverage, String filter) throws UnsupportedEncodingException{ + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + return manager.removeGranulesByCQL(workspace, coverageStore, coverage, filter); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return false; + + } + } diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java index 0cf9846..95654ba 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java @@ -39,16 +39,20 @@ import it.geosolutions.geoserver.rest.decoder.RESTLayerList; import it.geosolutions.geoserver.rest.decoder.RESTNamespace; import it.geosolutions.geoserver.rest.decoder.RESTNamespaceList; import it.geosolutions.geoserver.rest.decoder.RESTResource; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; import it.geosolutions.geoserver.rest.decoder.RESTStyleList; import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredCoverageGridReaderManager; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -575,4 +579,101 @@ public class GeoServerRESTReader { return names; } + /** + * Get information about a granule for a structured coverage. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param format the format of the file to upload + * @param the absolute path to the file to upload + * @param id the ID of the granule to get information for + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + public RESTStructuredCoverageGranulesList getGranuleById(final String workspace, + String coverageStore, String coverage, String id) throws MalformedURLException, + UnsupportedEncodingException { + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + return manager.getGranuleById(workspace, coverageStore, coverage, id); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return null; + } + + /** + * Get information about the schema of the index for a structured coverage. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param format the format of the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + @Test + public RESTStructuredCoverageIndexSchema getGranuleIndexSchema(final String workspace, String coverageStore, String coverage) throws MalformedURLException { + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + return manager.getGranuleIndexSchema(workspace, coverageStore, coverage); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return null; + } + + /** + * Get information about the granules for a coverage with optional filter and paging. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage + * @param filter the format of the file to upload, can be null to include all the granules + * @param offset the start page, can be null or an integer + * @param limit the dimension of the page, can be null or a positive integer + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + @Test + public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) + throws MalformedURLException, UnsupportedEncodingException { + try { + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + return manager.getGranules(workspace, coverageStore, coverage, filter, offset, limit); + } catch (IllegalArgumentException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } catch (MalformedURLException e) { + if(LOGGER.isInfoEnabled()){ + LOGGER.info(e.getLocalizedMessage(),e); + } + } + return null; + } + } diff --git a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java index a08da22..ad8e2d0 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java +++ b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java @@ -336,7 +336,7 @@ public class HTTPUtils { InputStream is = httpMethod.getResponseBodyAsStream(); response = IOUtils.toString(is); IOUtils.closeQuietly(is); - if (response.trim().equals("")) { // sometimes gs rest fails + if (response.trim().equals("")) { if (LOGGER.isDebugEnabled()) LOGGER .debug("ResponseBody is empty (this may be not an error since we just performed a DELETE call)"); @@ -467,14 +467,14 @@ public class HTTPUtils { /** * @param str a string array - * @return create a StringBuffer appending all the passed arguments + * @return create a StringBuilder appending all the passed arguments */ - public static StringBuffer append(String ... str){ + public static StringBuilder append(String ... str){ if (str==null){ return null; } - StringBuffer buf=new StringBuffer(); + StringBuilder buf=new StringBuilder(); for (String s: str){ if (s!=null) buf.append(s); @@ -488,12 +488,12 @@ public class HTTPUtils { * @param str strings to append * @return the base URL with parameters attached */ - public static StringBuffer append(URL base, String ... str){ + public static StringBuilder append(URL base, String ... str){ if (str==null){ return append(base.toString()); } - StringBuffer buf=new StringBuffer(base.toString()); + StringBuilder buf=new StringBuilder(base.toString()); for (String s: str){ if (s!=null) buf.append(s); diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageGranulesList.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageGranulesList.java new file mode 100644 index 0000000..80d612a --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageGranulesList.java @@ -0,0 +1,230 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package it.geosolutions.geoserver.rest.decoder; + +import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.jdom.Element; +import org.jdom.Namespace; + +/** + * This decode turns index format for a GeoServer StructuredGridCoverageReader into something + * useful, giving access to the definition of the single attributes. + * + *

This is the XML REST representation: + *

+  {@code
+
+
+
+  
+    
+      
+        5.0
+        45.0
+      
+      
+        14.875
+        50.9375
+      
+    
+  
+  
+    
+      
+        
+          
+            
+              5.0,45.0 5.0,50.9375 14.875,50.9375 14.875,45.0 5.0,45.0
+            
+          
+        
+      
+      ..\\polyphemus\\polyphemus_20130302.nc
+      672
+      2013-03-01T23:00:00Z
+      10.0
+      2013-03-01T23:00:00Z
+      2013-04-08T05:40:29.061Z
+    
+  
+
+
+}
+ * @author Simone Giannecchini, GeoSolutions SAS + * + */ +public class RESTStructuredCoverageGranulesList implements Iterable { + + /** GML_NAMESPACE */ + private static final Namespace GML_NAMESPACE = Namespace.getNamespace("gml", "http://www.opengis.net/gml"); + + private final List granulesList; + + private final Element bbox; + + /** + * @return the bbox + */ + public Element getBbox() { + return bbox; + } + + /** + * @param list + */ + @SuppressWarnings("unchecked") + protected RESTStructuredCoverageGranulesList(Element featureCollection) { + + // check ordering of elements + if(!featureCollection.getName().equals("FeatureCollection")){ + throw new IllegalStateException("Root element should be wfs:FeatureCollection"); + } + Element boundedBy = featureCollection.getChild("boundedBy",GML_NAMESPACE); + if(boundedBy==null){ + throw new IllegalStateException("Unable to find boundedBy element"); + } + // save bbox + bbox=boundedBy.getChild("Box",GML_NAMESPACE); + + // now get the feature members + List tmpList = new ArrayList(); + for(Element el : (List)featureCollection.getChildren("featureMember",GML_NAMESPACE)){ + tmpList.add(new RESTStructuredCoverageGranule(el)); + } + granulesList = Collections.unmodifiableList(tmpList); + } + + public static RESTStructuredCoverageGranulesList build(String response) { + if(response == null) + return null; + + Element pb = JDOMBuilder.buildElement(response); + if(pb != null){ + return new RESTStructuredCoverageGranulesList(pb); + } else { + return null; + } + } + + public int size() { + return granulesList.size(); + } + + public boolean isEmpty() { + return granulesList.isEmpty(); + } + + public RESTStructuredCoverageGranule get(int index) { + return granulesList.get(index); + } + + /* (non-Javadoc) + * @see java.lang.Iterable#iterator() + */ + @Override + public Iterator iterator() { + return granulesList.iterator(); + } + + /** + * Generic granule of the index. + * + *

This is the XML REST representation: + *

+  {@code
+  
+    
+      
+        
+          
+            
+              5.0,45.0 5.0,50.9375 14.875,50.9375 14.875,45.0 5.0,45.0
+            
+          
+        
+      
+      polyphemus_20130301.nc
+      672
+      2013-02-28T23:00:00Z
+      10.0
+      2013-02-28T23:00:00Z
+      2013-04-08T06:18:41.597Z
+    
+  
+
+     * @author Simone Giannecchini, GeoSolutions SAS
+     *
+     */
+    public static class RESTStructuredCoverageGranule {
+
+        protected final Element granule;
+        
+        private final String fid;
+
+        private final List children;
+
+        @SuppressWarnings("unchecked")
+        public RESTStructuredCoverageGranule(Element elem) {
+            if(!elem.getName().equals("featureMember")){
+                throw new IllegalStateException("Root element should be gml:featureMember for a granule");
+            }
+            Element feature = (Element) elem.getChildren().get(0);
+            if(feature==null){
+                throw new IllegalStateException("Unable to find feature element for this granule");
+            }    
+            this.granule = feature;
+            this.fid=granule.getAttribute("fid").getValue();
+            this.children=granule.getChildren();
+            
+        }
+
+        public String getAttributeByName(String name) {
+            return granule.getChildTextTrim(name,null);
+        }   
+
+        public String getAttributeByIndex(int index) {
+            return children.get(index).getValue();
+        }   
+        
+        @SuppressWarnings("unchecked")
+        public Iterator getAttributesIterator() {
+            return granule.getChildren().iterator();
+        }  
+        
+        /**
+         * @return the fid
+         */
+        public String getFid() {
+            return fid;
+        }
+    }
+
+}
diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageIndexSchema.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageIndexSchema.java
new file mode 100644
index 0000000..b19e0e6
--- /dev/null
+++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStructuredCoverageIndexSchema.java
@@ -0,0 +1,212 @@
+/*
+ *  GeoServer-Manager - Simple Manager Library for GeoServer
+ *  
+ *  Copyright (C) 2007,2011 GeoSolutions S.A.S.
+ *  http://www.geo-solutions.it
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package it.geosolutions.geoserver.rest.decoder;
+
+import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.jdom.Element;
+
+/**
+ * This decode turns index format for a GeoServer StructuredGridCoverageReader into something
+ * useful, giving access to the definition of the single attributes.
+ * 
+ * 

This is the XML REST representation: + *

+  {@code
+
+
+  
+    
+      the_geom
+      0
+      1
+      true
+      com.vividsolutions.jts.geom.Polygon
+    
+    
+      location
+      0
+      1
+      true
+      java.lang.String
+    
+    
+      imageindex
+      0
+      1
+      true
+      java.lang.Integer
+    
+    
+      time
+      0
+      1
+      true
+      java.sql.Timestamp
+    
+    
+      elevation
+      0
+      1
+      true
+      java.lang.Double
+    
+    
+      fileDate
+      0
+      1
+      true
+      java.sql.Timestamp
+    
+    
+      updated
+      0
+      1
+      true
+      java.sql.Timestamp
+    
+  
+  
+
+}
+ * @author Simone Giannecchini, GeoSolutions SAS + * + */ +public class RESTStructuredCoverageIndexSchema implements Iterable { + + private final List attributeList; + + /** + * @param list + */ + @SuppressWarnings("unchecked") + protected RESTStructuredCoverageIndexSchema(Element schema) { + + // check ordering of elements + if(!schema.getName().equals("Schema")){ + throw new IllegalStateException("Root element should be Schema"); + } + Element attributes = schema.getChild("attributes"); + if(attributes==null){ + throw new IllegalStateException("Root element should be Schema"); + } + + List tmpList = new ArrayList(); + for(Element el : (List)attributes.getChildren()){ + tmpList.add(new RESTStructuredCoverageIndexAttribute(el)); + } + attributeList = Collections.unmodifiableList(tmpList); + } + + public static RESTStructuredCoverageIndexSchema build(String response) { + if(response == null) + return null; + + Element pb = JDOMBuilder.buildElement(response); + if(pb != null){ + return new RESTStructuredCoverageIndexSchema(pb); + } else { + return null; + } + } + + public int size() { + return attributeList.size(); + } + + public boolean isEmpty() { + return attributeList.isEmpty(); + } + + public RESTStructuredCoverageIndexAttribute get(int index) { + return attributeList.get(index); + } + + /* (non-Javadoc) + * @see java.lang.Iterable#iterator() + */ + @Override + public Iterator iterator() { + return attributeList.iterator(); + } + + /** + * Generic granule of the index. + * + *

This is the XML REST representation: + *

+  {@code
+    
+      the_geom
+      0
+      1
+      true
+      com.vividsolutions.jts.geom.Polygon
+    
+     * @author Simone Giannecchini, GeoSolutions SAS
+     *
+     */
+    public static class RESTStructuredCoverageIndexAttribute {
+        protected final Element attribute;
+
+        public RESTStructuredCoverageIndexAttribute(Element elem) {
+            this.attribute = elem;
+        }
+
+        public String getName() {
+            return attribute.getChildTextTrim("name");
+        }
+        
+        public String getMinOccurs() {
+            return attribute.getChildTextTrim("minOccurs");
+        }
+        
+        public String getMaxOccurs() {
+            return attribute.getChildTextTrim("maxOccurs");
+        }
+        
+        public String getNillable() {
+            return attribute.getChildTextTrim("nillable");
+        }
+        
+        public String getBinding() {
+            return attribute.getChildTextTrim("binding");
+        }
+
+        @Override
+        public String toString() {
+            return "RESTStructuredCoverageGranule [getName()=" + getName()
+                    + ", getMinOccurs()=" + getMinOccurs() + ", getMaxOccurs()=" + getMaxOccurs()
+                    + ", getNillable()=" + getNillable() + ", getBinding()=" + getBinding() + "]";
+        }
+        
+    }
+
+}
diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java
index 34dad05..5aad7be 100644
--- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java
+++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java
@@ -107,7 +107,7 @@ public class GeoServerRESTStoreManager extends GeoServerRESTAbstractManager {
 //            if (workspace.isEmpty() || storename.isEmpty())
 //                throw new IllegalArgumentException("Arguments may not be empty!");
 
-            final StringBuffer url=HTTPUtils.append(restURL,"/rest/workspaces/",workspace,"/", store.getStoreType().toString(), "/",store.getName());
+            final StringBuilder url=HTTPUtils.append(restURL,"/rest/workspaces/",workspace,"/", store.getStoreType().toString(), "/",store.getName());
             if (recurse)
                 url.append("?recurse=true");
             final URL deleteStore = new URL(url.toString());
diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java
new file mode 100644
index 0000000..44b3ccc
--- /dev/null
+++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java
@@ -0,0 +1,354 @@
+/*
+ *  GeoServer-Manager - Simple Manager Library for GeoServer
+ *  
+ *  Copyright (C) 2007,2012 GeoSolutions S.A.S.
+ *  http://www.geo-solutions.it
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package it.geosolutions.geoserver.rest.manager;
+
+import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.UploadMethod;
+import it.geosolutions.geoserver.rest.HTTPUtils;
+import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList;
+import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Manage GeoTools StructuredCoverageGridReader. It allows to create a store from a file or harvest
+ * the coverages contained in a file, to delete granules from an existing coverage and eventually
+ * to get information about the granules insi a StructuredCoverageGridReader.
+ * 
+ * @author Simone Giannecchini, GeoSolutions
+ */
+public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerRESTAbstractManager {
+
+    /**
+     * Default logger
+     */
+    private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStructuredCoverageGridReaderManager.class);
+
+    /**
+     * Default constructor.
+     * 
+     * @param restURL GeoServer REST API endpoint
+     * @param username GeoServer REST API authorized username
+     * @param password GeoServer REST API password for the former username
+     * @throws IllegalArgumentException
+     */
+    public GeoServerRESTStructuredCoverageGridReaderManager(URL restURL, String username, String password) throws IllegalArgumentException, MalformedURLException{
+        super(restURL, username, password);
+    }
+
+    /**
+     * Create a store or harvest the coverage from the provided external path.
+     * 
+     * @param workspace the GeoServer workspace
+     * @param coverageStore the GeoServer coverageStore
+     * @param format the format of the file to upload
+     * @param the absolut path to the file to upload
+     * 
+     * @return true if the call succeeds or false otherwise.
+     */
+    public boolean createOrHarvestExternal(String workspace, String coverageStore, String format, String path) {
+        // checks
+        checkString(workspace);
+        checkString(coverageStore);
+        checkString(format);
+        checkString(path);
+        
+        // create URL
+        String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/",coverageStore,"/",UploadMethod.EXTERNAL.toString(),".", format).toString();
+        
+        // POST request
+        String result = HTTPUtils.post(sUrl, "file:/"+path, "text/plain", gsuser, gspass);
+        return result != null;
+    }
+
+    /**
+     * Check the provided string for not being null or empty.
+     * 
+     * 

+ * It throws an exception in case the string is either null or empty. + * + * @param string the {@link String} to be checked + */ + private static void checkString(String string) { + if(string==null){ + throw new NullPointerException("Provided string is is null!"); + } + if(string.length()<=0){ + throw new IllegalArgumentException("Provided string is is empty!"); + } + + } + + /** + * Remove granules from a structured coverage, by providing a CQL filter. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage from which we are going to remove + * @param filter the absolute path to the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + public boolean removeGranulesByCQL(final String workspace, String coverageStore,String coverage, String filter) throws UnsupportedEncodingException{ + // checks + checkString(workspace); + checkString(coverage); + checkString(filter); + checkString(coverageStore); + + // does it exist? + RESTStructuredCoverageGranulesList granulesList=null; + try { + granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, "1"); + } catch (MalformedURLException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } catch (UnsupportedEncodingException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } + if (granulesList == null||granulesList.isEmpty()) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace("Granules for filter: "+filter+ " does not exist for coverage "+coverage); + } + return true; // nothing to remove + } + + // method + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores", "/",coverageStore,"/coverages/",coverage,"/index/granules?filter=",URLEncoder.encode(filter, "UTF-8")).toString(); + if(!HTTPUtils.delete(sUrl, gsuser, gspass)){ + return false; + } + + // does it exist? + granulesList=null; + try { + granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, "1"); + } catch (MalformedURLException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } catch (UnsupportedEncodingException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } + if (granulesList == null||granulesList.isEmpty()) { + return true; // nothing to remove + } + return false; + } + + /** + * Remove a granule from a structured coverage by id. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage from which we are going to remove + * @param filter the absolute path to the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + public boolean removeGranuleById(final String workspace, String coverageStore, String coverage, String granuleId) { + // checks + checkString(workspace); + checkString(coverage); + checkString(granuleId); + checkString(coverageStore); + + // does it exist? + RESTStructuredCoverageGranulesList granule=null; + try { + granule = getGranuleById(workspace, coverageStore, coverage, granuleId); + } catch (MalformedURLException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } catch (UnsupportedEncodingException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } + if (granule == null) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace("Granule for id: "+granuleId+ " does not exist for coverage "+coverage); + } + return true; // nothing to remove + } + + // delete + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores", + "/", coverageStore, "/coverages/", coverage, "/index/granules/", granuleId) + .toString(); + if(!HTTPUtils.delete(sUrl, gsuser, gspass)){ + return false; + } + + // has it been canceled? + // does it exist? + granule=null; + try { + granule = getGranuleById(workspace, coverageStore, coverage, granuleId); + } catch (MalformedURLException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } catch (UnsupportedEncodingException e) { + if(LOGGER.isTraceEnabled()){ + LOGGER.trace(e.getMessage(), e); + } + } + if (granule == null) { + return true; + } + + return false; + } + + /** + * Get information about the schema of the index for a structured coverage. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param format the format of the file to upload + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + @Test + public RESTStructuredCoverageIndexSchema getGranuleIndexSchema(final String workspace, String coverageStore, String coverage) throws MalformedURLException { + // checks + checkString(workspace); + checkString(coverage); + checkString(coverageStore); + + // create URL and then call it + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index.xml").toString(); + String result = HTTPUtils.get(sUrl, gsuser, gspass); + if (result != null) { + return RESTStructuredCoverageIndexSchema.build(result); + } + return null; + } + + /** + * Get information about the granules for a coverage with optional filter and paging. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param coverage the name of the target coverage + * @param filter the format of the file to upload, can be null to include all the granules + * @param offset the start page, can be null or an integer + * @param limit the dimension of the page, can be null or a positive integer + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + @Test + public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) + throws MalformedURLException, UnsupportedEncodingException { + // checks + checkString(workspace); + checkString(coverage); + checkString(coverageStore); + + // method + boolean append = false; + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index/granules.xml").toString(); + if (filter != null && !filter.isEmpty()) { + append = true; + sUrl = HTTPUtils.append(sUrl, "?filter=", URLEncoder.encode(filter, "UTF-8")).toString(); + } + if (offset != null && !offset.isEmpty()) { + sUrl = HTTPUtils.append(sUrl, append ? "&offset=" : "?offset=", offset).toString(); + append = true; + } + if (limit != null && !limit.isEmpty()) { + sUrl = HTTPUtils.append(sUrl, append ? "&limit=" : "?limit=", limit).toString(); + append = true; + } + String result = HTTPUtils.get(sUrl, gsuser, gspass); + if (result != null) { + return RESTStructuredCoverageGranulesList.build(result); + } + return null; + } + + /** + * Get information about a granule for a structured coverage. + * + * @param workspace the GeoServer workspace + * @param coverageStore the GeoServer coverageStore + * @param format the format of the file to upload + * @param the absolute path to the file to upload + * @param id the ID of the granule to get information for + * + * @return null in case the call does not succeed, or an instance of {@link RESTStructuredCoverageGranulesList}. + * + * @throws MalformedURLException + * @throws UnsupportedEncodingException + */ + @Test + public RESTStructuredCoverageGranulesList getGranuleById(final String workspace, + String coverageStore, String coverage, String id) throws MalformedURLException, + UnsupportedEncodingException { + // checks + checkString(workspace); + checkString(coverage); + checkString(coverageStore); + checkString(id); + try{ + Integer.parseInt(id); + }catch (NumberFormatException e) { + throw new IllegalArgumentException(e); + } + + // method + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index/granules/", id, ".xml").toString(); + String result = HTTPUtils.get(sUrl, gsuser, gspass); + if (result != null) { + return RESTStructuredCoverageGranulesList.build(result); + } + return null; + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java new file mode 100644 index 0000000..e71f2f8 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java @@ -0,0 +1,136 @@ +/* + * GeoTools - The Open Source Java GIS Toolkit + * http://geotools.org + * + * (C) 2002-2011, Open Source Geospatial Foundation (OSGeo) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package it.geosolutions.geoserver.rest.manager; + +import it.geosolutions.geoserver.rest.datastore.StoreIntegrationTest; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList.RESTStructuredCoverageGranule; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema.RESTStructuredCoverageIndexAttribute; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; + +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Iterator; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * @author Simone Giannecchini, simone.giannecchini@geo-solutions.it + * + */ +public class GeoServerRESTStructuredCoverageGridReaderManagerTest extends StoreIntegrationTest { + + /** + * @param ignore + * @throws IllegalArgumentException + * @throws MalformedURLException + */ + public GeoServerRESTStructuredCoverageGridReaderManagerTest() + throws IllegalArgumentException, MalformedURLException { + super(true); + } + + @Override + public GSAbstractStoreEncoder getStoreEncoderTest() { + // TODO Auto-generated method stub + return null; + } + + @Test + public void createAndDelete() throws IllegalArgumentException, MalformedURLException, UnsupportedEncodingException{ + GeoServerRESTStructuredCoverageGridReaderManager manager = + new GeoServerRESTStructuredCoverageGridReaderManager(new URL("http://localhost:8080/geoserver"), "admin", "geoserver"); +// boolean result=manager.createOrHarvestExternal("it.geosolutions", "polyphemus", "imagemosaic", "D:\\DLR\\Geoserver-MD\\polyphemus\\polyphemus_20130301.nc"); +// Assert.assertTrue(result); + + // check index format + RESTStructuredCoverageIndexSchema indexFormat = manager.getGranuleIndexSchema("it.geosolutions", "polyphemus","V"); + assertNotNull(indexFormat); + assertFalse(indexFormat.isEmpty()); + assertEquals(7, indexFormat.size()); + Iterator iterator = indexFormat.iterator(); + while(iterator.hasNext()){ + final RESTStructuredCoverageIndexAttribute element = iterator.next(); + if(element.getName().equals("location")){ + assertEquals("0", element.getMinOccurs()); + assertEquals("1", element.getMaxOccurs()); + assertEquals("true", element.getNillable()); + assertEquals("java.lang.String", element.getBinding()); + break; + } + } + + // get some granules by id + RESTStructuredCoverageGranulesList granulesList = manager.getGranuleById("it.geosolutions", "polyphemus","V","348"); + assertNotNull(granulesList); + assertSame(1, granulesList.size()); + assertFalse(granulesList.isEmpty()); + RESTStructuredCoverageGranule granule = granulesList.get(0); + assertNotNull(granule); + assertEquals(granule.getAttributeByIndex(4), "1250.0"); + assertEquals(granule.getAttributeByName("elevation"), "1250.0"); + + + // get with paging + granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,"0","10"); + assertNotNull(granulesList); + assertEquals(10, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,null,"10"); + assertNotNull(granulesList); + assertEquals(10, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,null,null); + assertNotNull(granulesList); + assertEquals(1007, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "polyphemus","V","elevation = 10",null,null); + assertNotNull(granulesList); + assertEquals(72, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "polyphemus","V","elevation = 10","0","10"); + assertNotNull(granulesList); + assertEquals(10, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + // remove by id + boolean result = manager.removeGranuleById("it.geosolutions", "polyphemus","V", "349"); + assertTrue(result); + + // remove by filter + result = manager.removeGranulesByCQL("it.geosolutions", "polyphemus","V", "location = 'polyphemus_20130301.nc'"); + Assert.assertTrue(result); + } +} From c3a437de20fc6492f4acacb3fbd08a6ffa9f2145 Mon Sep 17 00:00:00 2001 From: Daniele Romagnoli Date: Thu, 9 May 2013 11:36:33 +0200 Subject: [PATCH 2/3] Adding REST test to delete and re-harvest tiff mosaic granules --- .../geoserver/rest/GeoServerRESTManager.java | 8 + .../rest/GeoServerRESTPublisher.java | 14 +- .../geoserver/rest/GeoServerRESTReader.java | 19 +- .../rest/decoder/RESTCoverageStore.java | 22 +- ...TStructuredGridCoverageReaderManager.java} | 14 +- .../GeoServerRESTImageMosaicManagerTest.java | 265 ++++++++++++++++++ ...ructuredCoverageGridReaderManagerTest.java | 136 --------- .../resources/testdata/granules/mosaic.zip | Bin 0 -> 4397 bytes 8 files changed, 308 insertions(+), 170 deletions(-) rename src/main/java/it/geosolutions/geoserver/rest/manager/{GeoServerRESTStructuredCoverageGridReaderManager.java => GeoServerRESTStructuredGridCoverageReaderManager.java} (97%) create mode 100644 src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java delete mode 100644 src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java create mode 100644 src/test/resources/testdata/granules/mosaic.zip diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java index f18e4bc..97255fa 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java @@ -26,6 +26,7 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; import java.net.MalformedURLException; import java.net.URL; @@ -50,6 +51,8 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { private final GeoServerRESTReader reader; private final GeoServerRESTStoreManager store; + + private final GeoServerRESTStructuredGridCoverageReaderManager structuredGridCoverageReader; /** * Default constructor. @@ -69,6 +72,7 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { // Internal publisher and reader, provide simple access methods. publisher = new GeoServerRESTPublisher(restURL.toString(), username, password); reader = new GeoServerRESTReader(restURL, username, password); + structuredGridCoverageReader = new GeoServerRESTStructuredGridCoverageReaderManager(restURL, username, password); store = new GeoServerRESTStoreManager(restURL, gsuser, gspass); } @@ -84,4 +88,8 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { return store; } + public GeoServerRESTStructuredGridCoverageReaderManager getStructuredGridCoverageReader() { + return structuredGridCoverageReader; + } + } diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 2691b12..5eb3dfc 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -38,7 +38,7 @@ import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy import it.geosolutions.geoserver.rest.encoder.GSWorkspaceEncoder; import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; -import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredCoverageGridReaderManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; import java.io.File; import java.io.FileNotFoundException; @@ -2665,8 +2665,8 @@ public class GeoServerRESTPublisher { */ public boolean createOrHarvestExternal(String workspace, String coverageStore, String format, String path) { try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(restURL), gsuser, gspass); return manager.createOrHarvestExternal(workspace, coverageStore, format, path); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ @@ -2695,8 +2695,8 @@ public class GeoServerRESTPublisher { */ public boolean removeGranuleById(final String workspace, String coverageStore, String coverage, String granuleId) { try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(restURL), gsuser, gspass); return manager.removeGranuleById(workspace, coverageStore, coverage, granuleId); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ @@ -2724,8 +2724,8 @@ public class GeoServerRESTPublisher { */ public boolean removeGranulesByCQL(final String workspace, String coverageStore,String coverage, String filter) throws UnsupportedEncodingException{ try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(restURL), gsuser, gspass); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(restURL), gsuser, gspass); return manager.removeGranulesByCQL(workspace, coverageStore, coverage, filter); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java index 95654ba..a6737b0 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java @@ -43,7 +43,7 @@ import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; import it.geosolutions.geoserver.rest.decoder.RESTStyleList; import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList; -import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredCoverageGridReaderManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -52,7 +52,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -597,8 +596,8 @@ public class GeoServerRESTReader { String coverageStore, String coverage, String id) throws MalformedURLException, UnsupportedEncodingException { try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(baseurl), username, password); return manager.getGranuleById(workspace, coverageStore, coverage, id); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ @@ -624,11 +623,10 @@ public class GeoServerRESTReader { * @throws MalformedURLException * @throws UnsupportedEncodingException */ - @Test public RESTStructuredCoverageIndexSchema getGranuleIndexSchema(final String workspace, String coverageStore, String coverage) throws MalformedURLException { try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(baseurl), username, password); return manager.getGranuleIndexSchema(workspace, coverageStore, coverage); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ @@ -657,12 +655,11 @@ public class GeoServerRESTReader { * @throws MalformedURLException * @throws UnsupportedEncodingException */ - @Test - public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) + public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) throws MalformedURLException, UnsupportedEncodingException { try { - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL(baseurl), username, password); + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(baseurl), username, password); return manager.getGranules(workspace, coverageStore, coverage, filter, offset, limit); } catch (IllegalArgumentException e) { if(LOGGER.isInfoEnabled()){ diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTCoverageStore.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTCoverageStore.java index 04b8316..2e14e65 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTCoverageStore.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTCoverageStore.java @@ -76,15 +76,23 @@ public class RESTCoverageStore { return new RESTCoverageStore(pb); else return null; - } + } - public String getName() { - return cs.getChildText("name"); - } + public String getName() { + return cs.getChildText("name"); + } - public String getWorkspaceName() { - return cs.getChild("workspace").getChildText("name"); - } + public String getWorkspaceName() { + return cs.getChild("workspace").getChildText("name"); + } + + public String getURL() { + return cs.getChildText("url"); + } + + public String getType() { + return cs.getChildText("type"); + } public String toString() { StringBuilder sb = new StringBuilder(getClass().getSimpleName()) diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java similarity index 97% rename from src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java rename to src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java index 44b3ccc..b960165 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java @@ -34,23 +34,22 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Manage GeoTools StructuredCoverageGridReader. It allows to create a store from a file or harvest + * Manage GeoTools StructuredGridCoverageReader. It allows to create a store from a file or harvest * the coverages contained in a file, to delete granules from an existing coverage and eventually - * to get information about the granules insi a StructuredCoverageGridReader. + * to get information about the granules inside a StructuredGridCoverageReader. * * @author Simone Giannecchini, GeoSolutions */ -public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerRESTAbstractManager { +public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerRESTAbstractManager { /** * Default logger */ - private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStructuredCoverageGridReaderManager.class); + private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStructuredGridCoverageReaderManager.class); /** * Default constructor. @@ -60,7 +59,7 @@ public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerR * @param password GeoServer REST API password for the former username * @throws IllegalArgumentException */ - public GeoServerRESTStructuredCoverageGridReaderManager(URL restURL, String username, String password) throws IllegalArgumentException, MalformedURLException{ + public GeoServerRESTStructuredGridCoverageReaderManager(URL restURL, String username, String password) throws IllegalArgumentException, MalformedURLException{ super(restURL, username, password); } @@ -253,7 +252,6 @@ public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerR * @throws MalformedURLException * @throws UnsupportedEncodingException */ - @Test public RESTStructuredCoverageIndexSchema getGranuleIndexSchema(final String workspace, String coverageStore, String coverage) throws MalformedURLException { // checks checkString(workspace); @@ -284,7 +282,6 @@ public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerR * @throws MalformedURLException * @throws UnsupportedEncodingException */ - @Test public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) throws MalformedURLException, UnsupportedEncodingException { // checks @@ -328,7 +325,6 @@ public class GeoServerRESTStructuredCoverageGridReaderManager extends GeoServerR * @throws MalformedURLException * @throws UnsupportedEncodingException */ - @Test public RESTStructuredCoverageGranulesList getGranuleById(final String workspace, String coverageStore, String coverage, String id) throws MalformedURLException, UnsupportedEncodingException { diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java new file mode 100644 index 0000000..ca84f8c --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java @@ -0,0 +1,265 @@ +/* + * GeoTools - The Open Source Java GIS Toolkit + * http://geotools.org + * + * (C) 2002-2011, Open Source Geospatial Foundation (OSGeo) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package it.geosolutions.geoserver.rest.manager; + +import it.geosolutions.geoserver.rest.GeoServerRESTReader; +import it.geosolutions.geoserver.rest.datastore.StoreIntegrationTest; +import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList.RESTStructuredCoverageGranule; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; +import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema.RESTStructuredCoverageIndexAttribute; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLDecoder; +import java.util.Iterator; + +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * In order to test that class, make sure to configure a geoserver with a "mosaic" store. + * + * 1) take the mosaic.zip archive contained on src/test/resources/testdata.granules + * 2) extract it on disk + * 3) configure an ImageMosaic store on geoserver (name the store as "mosaic"), use the "it.geosolutions" workspace + * 4) configure a layer on that store (name the coverage as "mosaic" again). + * 5) on dimensions configuration tab, make sure to enable custom depth and date dimensions. + * 6) publish it. + * + * + * @author Simone Giannecchini, simone.giannecchini@geo-solutions.it + * @author Daniele Romagnoli, GeoSolutions SAS + * + */ +public class GeoServerRESTImageMosaicManagerTest extends StoreIntegrationTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTImageMosaicManagerTest.class); + + /** + * @param ignore + * @throws IllegalArgumentException + * @throws MalformedURLException + */ + public GeoServerRESTImageMosaicManagerTest() + throws IllegalArgumentException, MalformedURLException { + super(true); + } + + @Override + public GSAbstractStoreEncoder getStoreEncoderTest() { + return null; + } + + @Test + public void createAndDelete() throws IllegalArgumentException, MalformedURLException, UnsupportedEncodingException{ + GeoServerRESTStructuredGridCoverageReaderManager manager = + new GeoServerRESTStructuredGridCoverageReaderManager(new URL(RESTURL), RESTUSER, RESTPW); + GeoServerRESTReader reader = new GeoServerRESTReader(new URL(RESTURL), RESTUSER, RESTPW); + + // check index format + RESTStructuredCoverageIndexSchema indexFormat = manager.getGranuleIndexSchema("it.geosolutions", "mosaic","mosaic"); + if (indexFormat == null) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("sample coverage hasn't been found. Make sure to configure the layer before running this test"); + return; + } + } + assertNotNull(indexFormat); + assertFalse(indexFormat.isEmpty()); + assertEquals(5, indexFormat.size()); + Iterator iterator = indexFormat.iterator(); + while (iterator.hasNext()) { + final RESTStructuredCoverageIndexAttribute element = iterator.next(); + final String elementName = element.getName(); + if (elementName.equals("location")) { + assertEquals("0", element.getMinOccurs()); + assertEquals("1", element.getMaxOccurs()); + assertEquals("true", element.getNillable()); + assertEquals("java.lang.String", element.getBinding()); + } else if (elementName.equals("time")) { + assertEquals("0", element.getMinOccurs()); + assertEquals("1", element.getMaxOccurs()); + assertEquals("true", element.getNillable()); + assertEquals("java.sql.Timestamp", element.getBinding()); + } else if (elementName.equals("date")) { + assertEquals("0", element.getMinOccurs()); + assertEquals("1", element.getMaxOccurs()); + assertEquals("true", element.getNillable()); + assertEquals("java.lang.String", element.getBinding()); + } else if (elementName.equals("depth")) { + assertEquals("0", element.getMinOccurs()); + assertEquals("1", element.getMaxOccurs()); + assertEquals("true", element.getNillable()); + assertEquals("java.lang.Integer", element.getBinding()); + } + } + + RESTStructuredCoverageGranulesList granulesList = null; + RESTStructuredCoverageGranule granule = null; + // get some granules by id +// manager.getGranuleById("it.geosolutions", "mosaic","mosaic","2"); +// assertNotNull(granulesList); +// assertSame(1, granulesList.size()); +// assertFalse(granulesList.isEmpty()); +// RESTStructuredCoverageGranule granule = granulesList.get(0); +// assertNotNull(granule); +// assertEquals(granule.getAttributeByIndex(4), "1250.0"); +// assertEquals(granule.getAttributeByName("elevation"), "1250.0"); + + + // get with paging + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic" , null, "0", "1"); + assertNotNull(granulesList); + assertEquals(1, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, "2"); + assertNotNull(granulesList); + assertEquals(2, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, null); + assertNotNull(granulesList); + assertEquals(4, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", "depth = 100", null, null); + assertNotNull(granulesList); + assertEquals(2, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", "depth = 100 AND date='20081101T0000000'", null, null); + assertNotNull(granulesList); + assertEquals(1, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + // remove by filter + final String fileLocation = "NCOM_wattemp_100_20081101T0000000_12.tiff"; + boolean result = manager.removeGranulesByCQL("it.geosolutions", "mosaic", "mosaic", "location = '" + fileLocation + "'"); + Assert.assertTrue(result); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, null); + assertNotNull(granulesList); + assertEquals(3, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + // Readding that granule + RESTCoverageStore store = reader.getCoverageStore("it.geosolutions", "mosaic"); + final String urlString = store.getURL(); + final URL url = new URL(urlString); + final File file = urlToFile(url); + final String filePath = file.getAbsolutePath(); + + int i=0; + i++; + + + // use reflection to get the store URL since coveragestore only returns name and workspace + result = manager.createOrHarvestExternal("it.geosolutions", "mosaic", "imagemosaic", filePath + File.separatorChar + fileLocation ); + Assert.assertTrue(result); + + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, null); + assertNotNull(granulesList); + assertEquals(4, granulesList.size()); + assertFalse(granulesList.isEmpty()); + granule = granulesList.get(0); + assertNotNull(granule); + + } + + + /** + * This method has been copied from org.geotools.data.DataUtilities + * + * Takes a URL and converts it to a File. The attempts to deal with Windows UNC format specific + * problems, specifically files located on network shares and different drives. + * + * If the URL.getAuthority() returns null or is empty, then only the url's path property is used + * to construct the file. Otherwise, the authority is prefixed before the path. + * + * It is assumed that url.getProtocol returns "file". + * + * Authority is the drive or network share the file is located on. Such as "C:", "E:", + * "\\fooServer" + * + * @param url + * a URL object that uses protocol "file" + * @return a File that corresponds to the URL's location + */ + private static File urlToFile(URL url) { + if (!"file".equals(url.getProtocol())) { + return null; // not a File URL + } + String string = url.toExternalForm(); + if (string.contains("+")) { + // this represents an invalid URL created using either + // file.toURL(); or + // file.toURI().toURL() on a specific version of Java 5 on Mac + string = string.replace("+", "%2B"); + } + try { + string = URLDecoder.decode(string, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Could not decode the URL to UTF-8 format", e); + } + + String path3; + + String simplePrefix = "file:/"; + String standardPrefix = "file://"; + String os = System.getProperty("os.name"); + + if (os.toUpperCase().contains("WINDOWS") && string.startsWith(standardPrefix)) { + // win32: host/share reference + path3 = string.substring(standardPrefix.length() - 2); + } else if (string.startsWith(standardPrefix)) { + path3 = string.substring(standardPrefix.length()); + } else if (string.startsWith(simplePrefix)) { + path3 = string.substring(simplePrefix.length() - 1); + } else { + String auth = url.getAuthority(); + String path2 = url.getPath().replace("%20", " "); + if (auth != null && !auth.equals("")) { + path3 = "//" + auth + path2; + } else { + path3 = path2; + } + } + + return new File(path3); + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java deleted file mode 100644 index e71f2f8..0000000 --- a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredCoverageGridReaderManagerTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * GeoTools - The Open Source Java GIS Toolkit - * http://geotools.org - * - * (C) 2002-2011, Open Source Geospatial Foundation (OSGeo) - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; - * version 2.1 of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ -package it.geosolutions.geoserver.rest.manager; - -import it.geosolutions.geoserver.rest.datastore.StoreIntegrationTest; -import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; -import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList.RESTStructuredCoverageGranule; -import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; -import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema.RESTStructuredCoverageIndexAttribute; -import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; - -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Iterator; - -import junit.framework.Assert; - -import org.junit.Test; - -/** - * @author Simone Giannecchini, simone.giannecchini@geo-solutions.it - * - */ -public class GeoServerRESTStructuredCoverageGridReaderManagerTest extends StoreIntegrationTest { - - /** - * @param ignore - * @throws IllegalArgumentException - * @throws MalformedURLException - */ - public GeoServerRESTStructuredCoverageGridReaderManagerTest() - throws IllegalArgumentException, MalformedURLException { - super(true); - } - - @Override - public GSAbstractStoreEncoder getStoreEncoderTest() { - // TODO Auto-generated method stub - return null; - } - - @Test - public void createAndDelete() throws IllegalArgumentException, MalformedURLException, UnsupportedEncodingException{ - GeoServerRESTStructuredCoverageGridReaderManager manager = - new GeoServerRESTStructuredCoverageGridReaderManager(new URL("http://localhost:8080/geoserver"), "admin", "geoserver"); -// boolean result=manager.createOrHarvestExternal("it.geosolutions", "polyphemus", "imagemosaic", "D:\\DLR\\Geoserver-MD\\polyphemus\\polyphemus_20130301.nc"); -// Assert.assertTrue(result); - - // check index format - RESTStructuredCoverageIndexSchema indexFormat = manager.getGranuleIndexSchema("it.geosolutions", "polyphemus","V"); - assertNotNull(indexFormat); - assertFalse(indexFormat.isEmpty()); - assertEquals(7, indexFormat.size()); - Iterator iterator = indexFormat.iterator(); - while(iterator.hasNext()){ - final RESTStructuredCoverageIndexAttribute element = iterator.next(); - if(element.getName().equals("location")){ - assertEquals("0", element.getMinOccurs()); - assertEquals("1", element.getMaxOccurs()); - assertEquals("true", element.getNillable()); - assertEquals("java.lang.String", element.getBinding()); - break; - } - } - - // get some granules by id - RESTStructuredCoverageGranulesList granulesList = manager.getGranuleById("it.geosolutions", "polyphemus","V","348"); - assertNotNull(granulesList); - assertSame(1, granulesList.size()); - assertFalse(granulesList.isEmpty()); - RESTStructuredCoverageGranule granule = granulesList.get(0); - assertNotNull(granule); - assertEquals(granule.getAttributeByIndex(4), "1250.0"); - assertEquals(granule.getAttributeByName("elevation"), "1250.0"); - - - // get with paging - granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,"0","10"); - assertNotNull(granulesList); - assertEquals(10, granulesList.size()); - assertFalse(granulesList.isEmpty()); - granule = granulesList.get(0); - assertNotNull(granule); - - granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,null,"10"); - assertNotNull(granulesList); - assertEquals(10, granulesList.size()); - assertFalse(granulesList.isEmpty()); - granule = granulesList.get(0); - assertNotNull(granule); - - granulesList = manager.getGranules("it.geosolutions", "polyphemus","V",null,null,null); - assertNotNull(granulesList); - assertEquals(1007, granulesList.size()); - assertFalse(granulesList.isEmpty()); - granule = granulesList.get(0); - assertNotNull(granule); - - granulesList = manager.getGranules("it.geosolutions", "polyphemus","V","elevation = 10",null,null); - assertNotNull(granulesList); - assertEquals(72, granulesList.size()); - assertFalse(granulesList.isEmpty()); - granule = granulesList.get(0); - assertNotNull(granule); - - granulesList = manager.getGranules("it.geosolutions", "polyphemus","V","elevation = 10","0","10"); - assertNotNull(granulesList); - assertEquals(10, granulesList.size()); - assertFalse(granulesList.isEmpty()); - granule = granulesList.get(0); - assertNotNull(granule); - - // remove by id - boolean result = manager.removeGranuleById("it.geosolutions", "polyphemus","V", "349"); - assertTrue(result); - - // remove by filter - result = manager.removeGranulesByCQL("it.geosolutions", "polyphemus","V", "location = 'polyphemus_20130301.nc'"); - Assert.assertTrue(result); - } -} diff --git a/src/test/resources/testdata/granules/mosaic.zip b/src/test/resources/testdata/granules/mosaic.zip new file mode 100644 index 0000000000000000000000000000000000000000..138af2656cccfa77c6fba0b4591ddcb041479e2b GIT binary patch literal 4397 zcma)92{@E%*q%XlNhDc{kcMK+SVy*zU3SSjwxO|&EyqswWEW*AWy>}SDO-pnOEY6% zvXo>Sr4!kgf8_Yh`RhODymNi?eD8OC*L~mbJJ0jXd+TbFQ?LN`hP`a9L3!4!A6|zT^xrm*;#ubJ&+DaA7M8Sv>VdH3x)LjIhQp9 zUzE1+mkKo8zmW{&-UHlYr3UN)Ubji;swU1vfB?X$LqL?P9nuHsK>`;TuSgfFa4ceR zCq|YvT3+EBx5)>7iaO=jd10;#U*Ezqb~e4mWt8t)N3M=bE}j)H=(;LL`6hgfKlE7{ zzb51%q=);R@nQ-)TMN64tFrFZi;ij(FMQC6Ugn7z9FRvjv;lI$7Fe-IkKgF9@vvVj zK5xe68nB#tawf7|Q7E)oKPqj8929XP#wq|_b*{OwO`yv ztbXj+p1Tn&`NVA39|3k9&!4oUKp!r*&$I{*XJ!cqCIbKjC;$NP;c~T=bgm%&u=eso zy0{_05HJD)21|*8p`wQ1{ecjL2z#OI?c)+%)C~04+Oq4`E3|T&7P!Kb&w_Kd@KKky zZPs4p(2K`f!_{I$j6G6s^{#tlqMbDFN@sm6gYrlqxZlCW?3d=j6INA@?H)|a{Gg2w zQS;J+7_40q71lQhw;PPz&GI)eNC>3|#v`@DIKY$V{!l!+X4~J7fF6v67-aR$l1@Anv z>0wj+^%zHXV)E4BsbmdTdc`*)z#P>1yQxToTkwU3pvkEw(t;keu-$e7_w%cv9Qpt!IhhIag}>XQz__gzMCi`0E)_ukcq+ z@qkeIrd6+opi(rZ41rSt_t&lcjiOMr5A|i#P7SAGT3|*N1kMn6Td#_`-r|y9eh@I& zmchlmL2&SB`usU(wpK@#9d_x}GDCuj%uE4ro-TuOXBOmZ5p(==^%w|Hc-sl1v#p|{ zr!y?>-L=vRj9XnY3f;Bvtzk&}KAH1;{Zv%?nG!rMUMr?n4qE4%mm60Fv(#Q~w&^Uz z3tV`JHL?_R&bt5f=#^na(|PLgKjP)AX&4${gvg$Cy{C1Lori=QEP|j5ZoF|q9%$5< z=Yi)z@eq@9snW@Z*;LRck&anTQ z%%b35lR3pn&A^(N%$2?-;yFz*uG}n4`b^h}$xL@)K{G!zQe3>_t=T)xm{sw?;XdD@ ztj^CD`V7+22X8PfO0;DOp}g2^^)+KRdRFD^aQ<0;3bU>^Z(eF#-3`v33B-NO8X5v8 z0u-nLyp)tE8SO}$Zd$s!*FqP%xML)tK)8hU5PZ(8NbY^T|J2zKF0F@6jc#9;J&r|M z7=8(g`2-p-yc2qF`AsWE5JbUq1|IY(hKI!@av;lrmhhD>0Ovbby^HWBUlAmzZqk@$n=&B#E?}wMV zpAzAdeMCcRtYje}KbA9f+{+lFkA2kr?G6Ou0ijZL?6@YQpqQTo(HF#WMAmgssRA@S z#gzIwow}~hMoJOPLOL!O&76^yYrQ6j3xrCeTNzjUOxlKu-KRDDyZfCYK`)MsESo5& z1wFA$G;akBcimh~@f>CL%pZ^tyEt1+hswD5RppCjCwPM9x#{inOus0Y8PnaIAc={7 zd8jC@t(B$MNKH56^-KC4-#gYKZh>Z`r2AlmNrbfd6e6)xqr*3$ zHXzRRyS_?Q@4I00XENPuZ~W65cD5Sow>S>F^ABQo?&H#i+x$yHi-P~5J0~ydBDZU?CFzO9mow6oZVrwY3GPNjJg}D>z+FW4lRwsxYj+&Q zZ)OK#bL8oZ1I`Rou-&HO`$A~f`T@Dnr5-*miGwbyi9uM3g|i}tTmu6(HUg6@5Z{lF zFZNL3s;RcNUgk42T7MSpb$fAZ{u%scDf5TvOTE|o)t2HuZza2ibfXHo`N}3c=<731 zhupN?rq9)TIK^_WwZJ+c&6Mg>@SJA^r)A2>B+#ws%U$o~s<4_h6^5z-P>Ak!_lJ1* zEBq`h9s*>y6VuYvFpqnTv4$w@q+P&q!1{lVl8vFQQ8XH`&mZU z#e202JEFJ`Urso;Naq$U#B5wqhS~VMkMe_YUQy}Za9X?OYpd&6>#I^=4*O^2sYr?N zX?BX-?#Ry@wy%^h10yzs)^+^oh+k#fLYB~QYWH{N#-+`y#nr5d#nHOlLCp}d5L!U! z5sI9+^VF0noy4TQ2<397T#6=x3E&Oe@g;0GB}gfbrNrei-smS<$-3cAAK=xN+Rk^? zy8-tWOQKmGqz?|U-)^L+brAoS%>=o2)}=@~Zd~AbJBQh`U@tP`RxY|@xg1Qx^eotk zYKjv}EFaO22YE9G_4F9!3__tjQ5TLjy7j~pP)cpF{tUG%BV!%{rS2EDm3F0fK#fT? zD}qnty3s-R2i2Y*eQEYK7dJj6o|TTZ%`>AIz|EQekht`+ZD#$XXPv-t#hLpqND-b| z)72=;OM)^JeAhw}O1$pYttP*yF0ibkz{!CbI@@C~g2E1+?tMb-VzC<;`c=`z1hQ|J ztk_`>>eajwF;B^)w7er971-J3YZsmYNw75nd>l|K=hj0%`XO;Sg|Skxah5p9|t6;_Z!N z+&{yCC!z)H&w)fA#Oi)IT0 zV8+K3gatPE?votE!&UswRgsc;ACmp*u}G-`w)d&95BI4KA}Q6cj)s&9Nwjo-t?NJg z8&U=RkNO(Zh&yQq(EYii2a#04ug6eQ1rUmVQSh6dNUFeBigImFL4@|cf`dq^;Ma2l zsRDAMlm6Wv{L?9dQ~^)5#^*f+sWJV-K*6v#`ycyc>v{kH literal 0 HcmV?d00001 From eeea363c8784ddd5ac4e888f362bf51334714330 Mon Sep 17 00:00:00 2001 From: Daniele Romagnoli Date: Fri, 10 May 2013 15:48:32 +0200 Subject: [PATCH 3/3] - changing limit and offset to Integers --- .../geoserver/rest/GeoServerRESTReader.java | 2 +- ...verRESTStructuredGridCoverageReaderManager.java | 14 +++++++------- .../GeoServerRESTImageMosaicManagerTest.java | 11 +++++------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java index a6737b0..05c1dcf 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java @@ -655,7 +655,7 @@ public class GeoServerRESTReader { * @throws MalformedURLException * @throws UnsupportedEncodingException */ - public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) + public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, Integer offset, Integer limit) throws MalformedURLException, UnsupportedEncodingException { try { GeoServerRESTStructuredGridCoverageReaderManager manager = diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java index b960165..5440f2b 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java @@ -129,7 +129,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR // does it exist? RESTStructuredCoverageGranulesList granulesList=null; try { - granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, "1"); + granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, 1); } catch (MalformedURLException e) { if(LOGGER.isTraceEnabled()){ LOGGER.trace(e.getMessage(), e); @@ -155,7 +155,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR // does it exist? granulesList=null; try { - granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, "1"); + granulesList = getGranules(workspace, coverageStore, coverageStore, filter, null, 1); } catch (MalformedURLException e) { if(LOGGER.isTraceEnabled()){ LOGGER.trace(e.getMessage(), e); @@ -282,7 +282,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR * @throws MalformedURLException * @throws UnsupportedEncodingException */ - public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, String offset, String limit) + public RESTStructuredCoverageGranulesList getGranules(final String workspace, String coverageStore, String coverage, String filter, Integer offset, Integer limit) throws MalformedURLException, UnsupportedEncodingException { // checks checkString(workspace); @@ -296,12 +296,12 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR append = true; sUrl = HTTPUtils.append(sUrl, "?filter=", URLEncoder.encode(filter, "UTF-8")).toString(); } - if (offset != null && !offset.isEmpty()) { - sUrl = HTTPUtils.append(sUrl, append ? "&offset=" : "?offset=", offset).toString(); + if (offset != null) { + sUrl = HTTPUtils.append(sUrl, append ? "&offset=" : "?offset=", offset.toString()).toString(); append = true; } - if (limit != null && !limit.isEmpty()) { - sUrl = HTTPUtils.append(sUrl, append ? "&limit=" : "?limit=", limit).toString(); + if (limit != null) { + sUrl = HTTPUtils.append(sUrl, append ? "&limit=" : "?limit=", limit.toString()).toString(); append = true; } String result = HTTPUtils.get(sUrl, gsuser, gspass); diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java index ca84f8c..1634102 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java @@ -73,6 +73,9 @@ public class GeoServerRESTImageMosaicManagerTest extends StoreIntegrationTest { @Test public void createAndDelete() throws IllegalArgumentException, MalformedURLException, UnsupportedEncodingException{ + if (!enabled()) { + return; + } GeoServerRESTStructuredGridCoverageReaderManager manager = new GeoServerRESTStructuredGridCoverageReaderManager(new URL(RESTURL), RESTUSER, RESTPW); GeoServerRESTReader reader = new GeoServerRESTReader(new URL(RESTURL), RESTUSER, RESTPW); @@ -129,14 +132,14 @@ public class GeoServerRESTImageMosaicManagerTest extends StoreIntegrationTest { // get with paging - granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic" , null, "0", "1"); + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic" , null, 0, 1); assertNotNull(granulesList); assertEquals(1, granulesList.size()); assertFalse(granulesList.isEmpty()); granule = granulesList.get(0); assertNotNull(granule); - granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, "2"); + granulesList = manager.getGranules("it.geosolutions", "mosaic", "mosaic", null, null, 2); assertNotNull(granulesList); assertEquals(2, granulesList.size()); assertFalse(granulesList.isEmpty()); @@ -183,10 +186,6 @@ public class GeoServerRESTImageMosaicManagerTest extends StoreIntegrationTest { final File file = urlToFile(url); final String filePath = file.getAbsolutePath(); - int i=0; - i++; - - // use reflection to get the store URL since coveragestore only returns name and workspace result = manager.createOrHarvestExternal("it.geosolutions", "mosaic", "imagemosaic", filePath + File.separatorChar + fileLocation ); Assert.assertTrue(result);