diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 873b44d..9223844 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -3212,13 +3212,6 @@ public class GeoServerRESTPublisher { * @author Carl Schroedl - cschroedl@usgs.gov */ public enum BBoxRecalculationMode { - /** - * Do not calculate any fields, regardless of the projection, projection - * policy, etc. This might be useful to avoid slow recalculation when - * operating against large datasets. - */ - NONE(""), - /** * Recalculate the native bounding box, but do not recalculate the * lat/long bounding box. @@ -3229,7 +3222,14 @@ public class GeoServerRESTPublisher { * Recalculate both the native bounding box and the lat/long bounding * box. */ - NATIVE_AND_LAT_LON_BBOX("nativebbox,latlonbbox") + NATIVE_AND_LAT_LON_BBOX("nativebbox,latlonbbox"), + + /** + * Do not calculate any fields, regardless of the projection, projection + * policy, etc. This might be useful to avoid slow recalculation when + * operating against large datasets. + */ + NONE(""), ; private final String paramValue; @@ -3256,24 +3256,36 @@ public class GeoServerRESTPublisher { * Recalculate the bounding box for a feature type or a coverage * * @param type + * @param xmlElementName - either featureType or coverage * @param workspace * @param storeName * @param layerName * @param calculationMode * @return true if recalculation succeeded, false otherwise. */ - private boolean recalculateBBox(StoreType type, String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode){ + private boolean recalculateBBox(StoreType type, String xmlElementName, String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode, boolean enabled){ - String sUrl = restURL + "/rest/workspaces/" + workspace + "/" + - type.getType() +"s/" + storeName + "/" + - type.getTypeName() + "/" + - layerName + "." + Format.XML.toString() + "?recalculate=" + - calculationMode.getParamValue(); + String baseUrl = restURL + "/rest/workspaces/" + workspace + "/" + + type.getType().toLowerCase() +"s/" + storeName + "/" + + type.getTypeName().toLowerCase() + "/" + + layerName + "." + Format.XML.toString(); - LOGGER.debug("Constructed the following url for bounding box recalculation: " + sUrl); - String sendResult = HTTPUtils.put(sUrl, "", "text/plain", gsuser, - gspass); +// LOGGER.debug("Retrieving current state of item from "+ baseUrl); +// String getResult = HTTPUtils.get(baseUrl, gsuser, gspass); + +// LOGGER.debug("Current state of item is:\n" + getResult); + + String sUrl = baseUrl + "?recalculate=" + calculationMode.getParamValue(); + LOGGER.debug("Constructed the following url for bounding box recalculation: " + sUrl); + +// String body = getResult +// GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace); + +// String body = wsenc.toString(); + String body = "<" + xmlElementName +">" + layerName + "" + + "" + enabled + ""; + String sendResult = HTTPUtils.putXml(sUrl, body, gsuser, gspass); boolean success = sendResult != null; return success; } @@ -3284,10 +3296,10 @@ public class GeoServerRESTPublisher { * @param storeName * @param layerName * @param calculationMode - * @return + * @return true if successful, false otherwise */ - public boolean recalculateFeatureTypeBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode){ - return recalculateBBox(StoreType.DATASTORES, workspace, storeName, layerName, calculationMode); + public boolean recalculateFeatureTypeBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode, boolean enabled){ + return recalculateBBox(StoreType.DATASTORES, "featureType", workspace, storeName, layerName, calculationMode, enabled); } /** @@ -3296,9 +3308,9 @@ public class GeoServerRESTPublisher { * @param storeName * @param layerName * @param calculationMode - * @return + * @return true if successful, false otherwise */ - public boolean recalculateCoverageBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode){ - return recalculateBBox(StoreType.COVERAGESTORES, workspace, storeName, layerName, calculationMode); + public boolean recalculateCoverageBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode, boolean enabled){ + return recalculateBBox(StoreType.COVERAGESTORES, "coverage", workspace, storeName, layerName, calculationMode, enabled); } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTRecalculateTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTRecalculateTest.java new file mode 100644 index 0000000..1f94f2d --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTRecalculateTest.java @@ -0,0 +1,107 @@ +/* + * 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.publisher; + +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.StoreType; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.UploadMethod; +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy; +import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoderTest; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.commons.httpclient.NameValuePair; +import org.junit.After; +import org.junit.Test; +import static org.junit.Assert.*; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author Carl Schroedl - cschroedl@usgs.gov + */ +public class GeoserverRESTRecalculateTest extends GeoserverRESTTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTRecalculateTest.class); + + @Before + @Override + public void before(){ + super.before(); + deleteAll(); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + } + + @After + public void cleanUp(){ + deleteAll(); + } + + @Test + public void testRecalculateFeatureTypeBBox() throws FileNotFoundException, IOException { + if (!enabled()) + return; + + String storeName = "resttestshp"; + String layerName = "cities"; + + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + assertTrue(publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile)); + + for(GeoServerRESTPublisher.BBoxRecalculationMode recalcMode : GeoServerRESTPublisher.BBoxRecalculationMode.values()){ + boolean recalculated = publisher.recalculateFeatureTypeBBox(DEFAULT_WS,storeName, layerName, recalcMode, true); + assertTrue("recalculateBBox failed with recalculation mode '" +recalcMode.toString() + "'.", recalculated); + } + } + + @Test + public void testRecalculateCoverageBBox() throws FileNotFoundException, IOException { + if (!enabled()) + return; + + String storeName = "testRESTStoreArcGrid"; + String layerName = "resttestdem"; + + File arcgrid = new ClassPathResource("testdata/resttestdem.asc").getFile(); + + assertTrue(publisher.publishArcGrid(DEFAULT_WS, storeName, layerName, arcgrid)); + + for(GeoServerRESTPublisher.BBoxRecalculationMode recalcMode : GeoServerRESTPublisher.BBoxRecalculationMode.values()){ + boolean recalculated = publisher.recalculateCoverageBBox(DEFAULT_WS,storeName, layerName, recalcMode, true); + assertTrue("recalculateBBox failed with recalculation mode '" +recalcMode.toString() + "'.", recalculated); + } + } +}