diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java new file mode 100644 index 0000000..1d4fc32 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java @@ -0,0 +1,82 @@ +/* + * 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; + +import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTDatastoreManager; + +import java.net.URL; + +/** + * The single entry point to all of geoserver-manager functionality. + * + * Instance this one, and use getters to use different components. These are: + * + * @author Oscar Fonts + */ +public class GeoServerRESTManager extends GeoServerRESTAbstractManager { + + private final GeoServerRESTPublisher publisher; + private final GeoServerRESTReader reader; + + private final GeoServerRESTDatastoreManager datastoreManager; + + /** + * Default constructor. + * + * Indicates connection parameters to remote GeoServer instance. + * + * @param restURL GeoServer REST API endpoint + * @param username GeoServer REST API authorized username + * @param password GeoServer REST API password for the former username + */ + public GeoServerRESTManager(URL restURL, String username, String password) { + super(restURL, username, password); + + // Internal publisher and reader, provide simple access methods. + publisher = new GeoServerRESTPublisher(restURL.toString(), username, password); + reader = new GeoServerRESTReader(restURL, username, password); + + // Classes for fine-grained management of catalog components. + datastoreManager = new GeoServerRESTDatastoreManager(restURL, username, password); + } + + public GeoServerRESTPublisher getPublisher() { + return publisher; + } + + public GeoServerRESTReader getReader() { + return reader; + } + + public GeoServerRESTDatastoreManager getDatastoreManager() { + return datastoreManager; + } + +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 67e24eb..d2b0d87 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -34,7 +34,6 @@ import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; 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.datastore.GSAbstractDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; @@ -695,7 +694,7 @@ public class GeoServerRESTPublisher { * @return true if the PostGIS datastore has been successfully * created, false otherwise * @deprecated Will be deleted in next version 1.5.x. - * Use {@link #createDatastore(String, GSAbstractDatastoreEncoder)} instead. + * Use {@link GeoServerRESTDatastoreManager} instead. */ public boolean createPostGISDatastore(String workspace, GSPostGISDatastoreEncoder datastoreEncoder) { @@ -705,47 +704,6 @@ public class GeoServerRESTPublisher { String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); return result != null; } - - /** - * Create a datastore (any datastore extending {@link GSAbstractDatastoreEncoder}). - * - * @param workspace - * Name of the workspace to contain the datastore. This will also - * be the prefix of any layer names contained in the datastore. - * @param datastore - * the set of parameters to be set to the datastore (including - * connection parameters). - * @return true if the datastore has been successfully - * created, false otherwise - */ - public boolean createDatastore(String workspace, - GSAbstractDatastoreEncoder datastore) { - String sUrl = restURL + "/rest/workspaces/" + workspace - + "/datastores/"; - String xml = datastore.toString(); - String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); - return result != null; - } - - /** - * Update a datastore (any datastore extending {@link GSAbstractDatastoreEncoder}). - * - * @param workspace - * Name of the workspace that contains the datastore. - * @param datastore - * the set of parameters to be set to the datastore (including - * connection parameters). - * @return true if the datastore has been successfully - * updated, false otherwise - */ - public boolean updateDatastore(String workspace, - GSAbstractDatastoreEncoder datastore) { - String sUrl = restURL + "/rest/workspaces/" + workspace - + "/datastores/" + datastore.getName(); - String xml = datastore.toString(); - String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); - return result != null; - } // ========================================================================== // === SHAPEFILES diff --git a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java index 143418a..dd7d527 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java +++ b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java @@ -54,7 +54,7 @@ import org.slf4j.LoggerFactory; /** * Low level HTTP utilities. */ -class HTTPUtils { +public class HTTPUtils { private static final Logger LOGGER = LoggerFactory.getLogger(HTTPUtils.class); /** diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java new file mode 100644 index 0000000..6fc3464 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java @@ -0,0 +1,55 @@ +/* + * 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 java.net.URL; + +/** + * Abstract manager, common functionality and interface + * for all GeoServerRESTFooManager classes. + * + * @author Oscar Fonts + */ +public abstract class GeoServerRESTAbstractManager { + + protected final URL restURL; + protected final String gsuser; + protected final String gspass; + + /** + * Default constructor. + * + * Indicates connection parameters to remote GeoServer instance. + * + * @param restURL GeoServer REST API endpoint + * @param username GeoServer REST API authorized username + * @param password GeoServer REST API password for the former username + */ + public GeoServerRESTAbstractManager(URL restURL, String username, String password) { + this.restURL = restURL; + this.gsuser = username; + this.gspass = password; + } +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java new file mode 100644 index 0000000..36572be --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java @@ -0,0 +1,92 @@ +/* + * 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.HTTPUtils; +import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; + +import java.net.URL; + +/** + * Manage datastores. + * + * To pass connection parameters, use the encoders derived from + * {@link GSAbstractDatastoreEncoder}. + * + * @author Oscar Fonts + */ +public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager { + + /** + * 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 + */ + public GeoServerRESTDatastoreManager(URL restURL, String username, String password) { + super(restURL, username, password); + } + + /** + * Create a datastore. + * + * @param workspace + * Name of the workspace to contain the datastore. This will also + * be the prefix of any layer names contained in the datastore. + * @param datastore + * the set of parameters to be set to the datastore (including + * connection parameters). + * @return true if the datastore has been successfully + * created, false otherwise + */ + + public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) { + String sUrl = restURL + "/rest/workspaces/" + workspace + + "/datastores/"; + String xml = datastore.toString(); + String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); + return result != null; + } + + /** + * Update a datastore. + * + * @param workspace + * Name of the workspace that contains the datastore. + * @param datastore + * the set of parameters to be set to the datastore (including + * connection parameters). + * @return true if the datastore has been successfully + * updated, false otherwise + */ + public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) { + String sUrl = restURL + "/rest/workspaces/" + workspace + + "/datastores/" + datastore.getName(); + String xml = datastore.toString(); + String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); + return result != null; + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTCreateReadUpdateDatastoreTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java similarity index 86% rename from src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTCreateReadUpdateDatastoreTest.java rename to src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java index d56a885..98af5f6 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTCreateReadUpdateDatastoreTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java @@ -22,14 +22,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package it.geosolutions.geoserver.rest.publisher; +package it.geosolutions.geoserver.rest.manager; import org.junit.Test; + import java.net.URL; import java.nio.charset.Charset; import java.util.Map; -import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.GeoServerRESTManager; import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTDataStore; import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; @@ -45,41 +46,46 @@ import it.geosolutions.geoserver.rest.encoder.datastore.GSDirectoryOfShapefilesD * *
  • Tests constructors and getters from {@link RESTDataStore} (reader). * - *
  • Tests {@link GeoServerRESTPublisher#createDatastore} and - * {@link GeoServerRESTPublisher#updateDatastore} methods. + *
  • Tests {@link GeoServerRESTDatastoreManager} create and update methods. * *

    The sequence is: *

      *
    1. Create a DirectoryOfShapefilesDatastoreEncoder, with default parameters. - *
    2. Publish via createDatastore. + *
    3. Publish via GeoServerRESTDatastoreManager.create. *
    4. Read the datastore from server. *
    5. Test all parameter values. *
    6. Create a new Encoder from it. *
    7. Change all datastore parameter to non-default ones. - *
    8. Update via updateDatastore. + *
    9. Update via GeoServerRESTDatastoreManager.update. *
    10. Read again. *
    11. Test all new values. *
    * * @author Oscar Fonts */ -public class GeoserverRESTCreateReadUpdateDatastoreTest extends GeoserverRESTTest { - +public class GeoserverRESTDatastoreManagerTest extends GeoserverRESTTest { + + public final GeoServerRESTManager manager; + private static final String WS_NAME = DEFAULT_WS; private static final String DS_NAME = "testCreateDatastore"; private static final String DS_DESCRIPTION = "A description"; private static URL LOCATION_1; private static URL LOCATION_2; - public GeoserverRESTCreateReadUpdateDatastoreTest(String testName) throws Exception { + public GeoserverRESTDatastoreManagerTest(String testName) throws Exception { super(testName); + manager = new GeoServerRESTManager(new URL(RESTURL), RESTUSER, RESTPW); + LOCATION_1 = new URL("file:data/1"); LOCATION_2 = new URL("file:data/2"); } @Test public void test() throws Exception { - assertTrue(enabled()); + if (!enabled()) { + return; + } // Delete all resources except styles deleteAllWorkspacesRecursively(); @@ -89,7 +95,7 @@ public class GeoserverRESTCreateReadUpdateDatastoreTest extends GeoserverRESTTes // Create a directory of spatial files with default parameters GSDirectoryOfShapefilesDatastoreEncoder create = new GSDirectoryOfShapefilesDatastoreEncoder(DS_NAME, LOCATION_1); - assertTrue(publisher.createDatastore(WS_NAME, create)); + assertTrue(manager.getDatastoreManager().create(WS_NAME, create)); // Read the store from server; check all parameter values RESTDataStore read = reader.getDatastore(WS_NAME, DS_NAME); @@ -115,7 +121,7 @@ public class GeoserverRESTCreateReadUpdateDatastoreTest extends GeoserverRESTTes update.setCacheAndReuseMemoryMaps(false); //update the store - assertTrue(publisher.updateDatastore(WS_NAME, update)); + assertTrue(manager.getDatastoreManager().update(WS_NAME, update)); // Read again, check that all parameters have changed read = reader.getDatastore(WS_NAME, DS_NAME); diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java index e3c5554..8463bbe 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java @@ -45,7 +45,9 @@ public class GeoserverRESTPublishShpCollectionTest extends GeoserverRESTTest { @Test public void testLocalZip() throws Exception { - assertTrue(enabled()); + if (!enabled()) { + return; + } URI location = new ClassPathResource("testdata/multipleshp.zip").getFile().toURI(); test(location); @@ -53,7 +55,9 @@ public class GeoserverRESTPublishShpCollectionTest extends GeoserverRESTTest { @Test public void testExternalDir() throws Exception { - assertTrue(enabled()); + if (!enabled()) { + return; + } URI location = new ClassPathResource("testdata/multipleshapefiles").getFile().toURI(); test(location);