diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java index 9e3d191..578412c 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.GeoServerRESTDatastoreManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; import java.net.MalformedURLException; import java.net.URL; @@ -39,48 +40,57 @@ import java.net.URL; *
  • getPublisher() simple, high-level pubhish methods. *
  • getFooManager, full-fledged management of catalog objects. * + * * @author Oscar Fonts * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ 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 - * @throws MalformedURLException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)} - * @throws IllegalArgumentException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)} - */ - public GeoServerRESTManager(URL restURL, String username, String password) throws IllegalArgumentException, MalformedURLException { - 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; - } - + + private final GeoServerRESTPublisher publisher; + + private final GeoServerRESTReader reader; + + private final GeoServerRESTStoreManager store; + + /** + * 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 + * @throws MalformedURLException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)} + * @throws IllegalArgumentException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)} + */ + public GeoServerRESTManager(URL restURL, String username, String password) + throws IllegalArgumentException, MalformedURLException { + 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); + store = new GeoServerRESTStoreManager(restURL, gsuser, gspass); + } + + public GeoServerRESTPublisher getPublisher() { + return publisher; + } + + public GeoServerRESTReader getReader() { + return reader; + } + + public GeoServerRESTStoreManager getStoreManager() { + return store; + } + + /** + * + * @Deprecated use {GeoServerRESTManager#getStoreManager()} + */ + public GeoServerRESTDatastoreManager getDatastoreManager() { + return (GeoServerRESTDatastoreManager) store; + } + } diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index ae9fa57..35bddec 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -30,13 +30,13 @@ import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import it.geosolutions.geoserver.rest.encoder.GSBackupEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSNamespaceEncoder; -import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; 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.GSPostGISDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; -import it.geosolutions.geoserver.rest.manager.GeoServerRESTDatastoreManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; import java.io.File; import java.io.FileNotFoundException; @@ -371,7 +371,7 @@ public class GeoServerRESTPublisher { } LOGGER.debug("POSTing new style " + name + " to " + sUrl); String result = HTTPUtils - .post(sUrl, sldFile, "application/vnd.ogc.sld+xml", gsuser, gspass); + .post(sUrl, sldFile, Format.SLD.getContentType(), gsuser, gspass); return result != null; } @@ -583,14 +583,40 @@ public class GeoServerRESTPublisher { } /** - * Get the type name of a DataStoreType. + * Get the type name of a StoreType. * - * @param type the DataStoreType. - * @return "featuretypes.xml" for DATASTORES, "coverages.xml" otherwise. + * @param type the StoreType. + * @return "dataStore" for DATASTORES, "coverageStore" otherwise. + */ + public static String getType(StoreType type) { + switch (type) { + case COVERAGESTORES: + return "coverageStore"; // Format + case DATASTORES: + return "dataStore"; + default: + return "coverageStore"; + } + } + + /** + * Get the type name of a StoreType. + * + * @return "featuretypes" for DATASTORES, "coverages" otherwise. */ public String getTypeName() { return getTypeName(this); } + + /** + * Get the type of a StoreType. + * + * @param type the StoreType. + * @return "dataStore" for DATASTORES, "coverageStore" otherwise. + */ + public String getType() { + return getType(this); + } /** * Returns a lowercase representation of the parameter value, suitable to construct the rest call. @@ -849,7 +875,7 @@ public class GeoServerRESTPublisher { /** * Create a PostGIS datastore. * - * @deprecated Will be deleted in next version 1.5.x, use {@link GeoServerRESTDatastoreManager} instead. + * @deprecated Will be deleted in next version 1.6.x, use {@link GeoServerRESTStoreManager} instead. * * @param workspace Name of the workspace to contain the database. This will also be the prefix of any layer names created from tables in the * database. @@ -858,7 +884,7 @@ public class GeoServerRESTPublisher { * @return true if the PostGIS datastore has been successfully created, false otherwise */ public boolean createPostGISDatastore(String workspace, - GSPostGISDatastoreEncoder datastoreEncoder) { + it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder datastoreEncoder) { String sUrl = restURL + "/rest/workspaces/" + workspace + "/datastores/"; String xml = datastoreEncoder.toString(); String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); @@ -1342,6 +1368,16 @@ public class GeoServerRESTPublisher { } } + /** + * Gets the mime type from a format. + * + * @param f the format key. + * @return The content-type (mime), or {@code null} if not in the enum. + */ + public String getContentType() { + return getContentType(this); + } + /** * Returns a lowercase representation of the parameter. Useful when constructing the REST request. */ diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSAbstractStoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSAbstractStoreEncoder.java new file mode 100644 index 0000000..86c9d08 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSAbstractStoreEncoder.java @@ -0,0 +1,121 @@ +/* + * 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.encoder; + +import org.jdom.Element; + +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.StoreType; +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; + +/** + * Generic Store encoder. + * + * Provides getters and setters for parameters common to all CoverageStore. + * + * @author Carlo Cancellieri - GeoSolutions + */ +public abstract class GSAbstractStoreEncoder extends PropertyXMLEncoder { + + private final GeoServerRESTPublisher.StoreType type; + + protected GSAbstractStoreEncoder(GeoServerRESTPublisher.StoreType type, String storeName) { + super(type.getType()); + this.type=type; + } + + public StoreType getStoreType() { + return this.type; + } + + public void setType(String type) { + set("type", type); + } + + public String getType() { + return ElementUtils.contains(getRoot(), "name").getTextTrim(); + } + + public void setName(String name) { + ensureValidName(name); + set("name", name); + } + + public String getName() { + Element e = ElementUtils.contains(getRoot(), "name"); + return e!=null?e.getTextTrim():null; + } + + public void setDescription(String description) { + set("description", description); + } + + public String getDescription() { + Element e = ElementUtils.contains(getRoot(), "description"); + return e!=null?e.getTextTrim():null; + } + + public void setEnabled(boolean enabled) { + set("enabled", Boolean.toString(enabled)); + } + + public boolean getEnabled() { + Element e = ElementUtils.contains(getRoot(), "name"); + if (e!=null) + return Boolean.parseBoolean(e.getTextTrim()); + else + return false; + } + + /** + * Check name validity. + * + * @param name the name + * @throws IllegalArgumentException if name is null or empty + */ + protected void ensureValidName(String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Store name cannot be null or empty"); + } + } + + /** + * Check type validity. + * + * @param type the type. + * @throws IllegalArgumentException if type is not {@value #TYPE} + */ + protected void ensureValidType(String type) { + if (!type.equals(getValidType())) { + throw new IllegalArgumentException("The store type '" + type + "' is not valid"); + } + } + + /** + * The type of the implementing store. + */ + protected abstract String getValidType(); +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSPostGISDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSPostGISDatastoreEncoder.java index 0d85ff0..63766a2 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSPostGISDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSPostGISDatastoreEncoder.java @@ -35,7 +35,7 @@ import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; * @author ETj * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * - * @deprecated Will be removed in next version 1.5.x. + * @deprecated Will be removed in next version 1.6.x. * Use {@link it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder} instead. */ public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder { diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/coverage/GSAbstractCoveragestoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/coverage/GSAbstractCoveragestoreEncoder.java new file mode 100644 index 0000000..9eff844 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/coverage/GSAbstractCoveragestoreEncoder.java @@ -0,0 +1,49 @@ +/* + * 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.encoder.coverage; + +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.StoreType; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; + +/** + * Generic CoverageStore encoder. + * + * Provides getters and setters for parameters common to all CoverageStore. + * + * @author Carlo Cancellieri - GeoSolutions + * + * @deprecated TODO complete specialization + */ +public abstract class GSAbstractCoveragestoreEncoder extends GSAbstractStoreEncoder { + + protected GSAbstractCoveragestoreEncoder(String storeName) { + super(StoreType.COVERAGESTORES,storeName); + // Add mandatory parameter + ensureValidName(storeName); + setName(storeName); + + } + +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSAbstractDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSAbstractDatastoreEncoder.java index f957810..e866fc6 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSAbstractDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSAbstractDatastoreEncoder.java @@ -24,38 +24,37 @@ */ package it.geosolutions.geoserver.rest.encoder.datastore; -import java.util.Map; - +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; import it.geosolutions.geoserver.rest.decoder.RESTDataStore; -import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; -import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; + +import java.util.Map; /** * Generic Datastore encoder. * - * Provides getters and setters for parameters common to all Datastores, - * an internal placeholder for specific connection parameters, and - * a constructor to read parameters from a {@link RESTDataStore}. + * Provides getters and setters for parameters common to all Datastores, an internal placeholder for specific connection parameters, and a constructor + * to read parameters from a {@link RESTDataStore}. * * @author Oscar Fonts */ -public abstract class GSAbstractDatastoreEncoder extends PropertyXMLEncoder { +public abstract class GSAbstractDatastoreEncoder extends GSAbstractStoreEncoder { - final static String ROOT = "dataStore"; - - NestedElementEncoder connectionParameters = new NestedElementEncoder("connectionParameters"); - - GSAbstractDatastoreEncoder(String storeName) { - super(ROOT); + final static String ROOT = "dataStore"; + + NestedElementEncoder connectionParameters = new NestedElementEncoder("connectionParameters"); + + GSAbstractDatastoreEncoder(String storeName) { + super(GeoServerRESTPublisher.StoreType.DATASTORES, ROOT); // Add mandatory parameter - ensureValidName(storeName); - setName(storeName); - + ensureValidName(storeName); + setName(storeName); + // Add connection parameters - addContent(connectionParameters.getRoot()); - } - + addContent(connectionParameters.getRoot()); + } + /** * Create a {@value #TYPE} datastore encoder from a store read from server. * @@ -63,84 +62,25 @@ public abstract class GSAbstractDatastoreEncoder extends PropertyXMLEncoder { * @throws IllegalArgumentException if store type or mandatory parameters are not valid */ GSAbstractDatastoreEncoder(RESTDataStore store) { - this(store.getName()); - - // Match datastore type - ensureValidType(store.getStoreType()); - setType(store.getStoreType()); - - // Copy store parameters - setDescription(store.getDescription()); - setEnabled(store.isEnabled()); - - // Copy connection parameters - bulk - Map params = store.getConnectionParameters(); - for(String key : params.keySet()) { - connectionParameters.set(key, params.get(key)); - } - } - - void setType(String type) { - set("type", type); - } - - public String getType() { - return ElementUtils.contains(getRoot(), "type").getTextTrim(); - } - - public void setName(String name) { - ensureValidName(name); - set("name", name); - } - - public String getName() { - return ElementUtils.contains(getRoot(), "name").getTextTrim(); + this(store.getName()); + + // Match datastore type + ensureValidType(store.getStoreType()); + + // Copy store parameters + setDescription(store.getDescription()); + setEnabled(store.isEnabled()); + + // Copy connection parameters - bulk + Map params = store.getConnectionParameters(); + for (String key : params.keySet()) { + connectionParameters.set(key, params.get(key)); + } } - public void setDescription(String description) { - set("description", description); - } - - public String getDescription() { - return ElementUtils.contains(getRoot(), "description").getTextTrim(); - } - - public void setEnabled(boolean enabled) { - set("enabled", Boolean.toString(enabled)); - } - - public boolean getEnabled() { - return Boolean.parseBoolean(ElementUtils.contains(getRoot(), "enabled").getTextTrim()); - } - - /** - * Check name validity. - * - * @param name the name - * @throws IllegalArgumentException if name is null or empty - */ - void ensureValidName(String name) { - if (name == null || name.isEmpty()) { - throw new IllegalArgumentException( - "Store name cannot be null or empty"); - } - } - - /** - * Check type validity. - * - * @param type the type. - * @throws IllegalArgumentException if type is not {@value #TYPE} - */ - void ensureValidType(String type) { - if (!type.equals(getValidType())) { - throw new IllegalArgumentException( - "The store type '"+ type +"' is not valid"); - } - } - /** * The type of the implementing datastore. */ - abstract String getValidType(); + protected abstract String getValidType(); + } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSArcSDEDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSArcSDEDatastoreEncoder.java index 3d64a1c..8779f49 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSArcSDEDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSArcSDEDatastoreEncoder.java @@ -66,7 +66,6 @@ public class GSArcSDEDatastoreEncoder extends GSAbstractDatastoreEncoder { super(name); // Set mandatory parameters - setType(TYPE); setServer(server); setUser(user); @@ -212,7 +211,7 @@ public class GSArcSDEDatastoreEncoder extends GSAbstractDatastoreEncoder { /** * @return {@value #TYPE} */ - String getValidType() { + protected String getValidType() { return TYPE; } } \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSDirectoryOfShapefilesDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSDirectoryOfShapefilesDatastoreEncoder.java index eae422e..ab46886 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSDirectoryOfShapefilesDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSDirectoryOfShapefilesDatastoreEncoder.java @@ -35,19 +35,18 @@ import java.net.URL; */ public class GSDirectoryOfShapefilesDatastoreEncoder extends GSShapefileDatastoreEncoder { - static final String TYPE = "Directory of spatial files (shapefiles)"; - + static final String TYPE = "Directory of spatial files (shapefiles)"; + /** - * Create a {@value #TYPE} datastore with default connection parameters, - * given a store name and a url (the store location). + * Create a {@value #TYPE} datastore with default connection parameters, given a store name and a url (the store location). * * @param name New datastore name * @param url The shapefile location in the server, relative to $GEOSERVER_DATA_DIR. */ - public GSDirectoryOfShapefilesDatastoreEncoder(String name, URL url) { - super(name, url); - setType(TYPE); - } + public GSDirectoryOfShapefilesDatastoreEncoder(String name, URL url) { + super(name, url); + setType(TYPE); + } /** * Create a {@value #TYPE} datastore encoder from an existing store read from server. @@ -55,14 +54,14 @@ public class GSDirectoryOfShapefilesDatastoreEncoder extends GSShapefileDatastor * @param store The existing store. * @throws IllegalArgumentException if store type or mandatory parameters are not valid */ - public GSDirectoryOfShapefilesDatastoreEncoder(RESTDataStore store) { - super(store); - } - + public GSDirectoryOfShapefilesDatastoreEncoder(RESTDataStore store) { + super(store); + } + /** * @return {@value #TYPE} */ - String getValidType() { - return TYPE; + protected String getValidType() { + return TYPE; } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSOracleNGDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSOracleNGDatastoreEncoder.java index 1368633..2cc3f27 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSOracleNGDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSOracleNGDatastoreEncoder.java @@ -181,7 +181,7 @@ public class GSOracleNGDatastoreEncoder extends GSAbstractDatastoreEncoder { /** * @return {@value #TYPE} */ - String getValidType() { + protected String getValidType() { return TYPE; } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSPostGISDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSPostGISDatastoreEncoder.java index 19978ee..f4fd813 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSPostGISDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSPostGISDatastoreEncoder.java @@ -44,6 +44,10 @@ public class GSPostGISDatastoreEncoder extends GSAbstractDatastoreEncoder { static final boolean DEFAULT_PREPARED_STATEMENTS = false; static final int DEFAULT_MAX_OPEN_PREPARED_STATEMENTS = 50; + /** + * + * @param name DataStore name + */ public GSPostGISDatastoreEncoder(String name) { super(name); @@ -141,7 +145,7 @@ public class GSPostGISDatastoreEncoder extends GSAbstractDatastoreEncoder { /** * @return {@value #TYPE} */ - String getValidType() { + protected String getValidType() { return TYPE; } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSShapefileDatastoreEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSShapefileDatastoreEncoder.java index 3a9d949..cc6794a 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSShapefileDatastoreEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/datastore/GSShapefileDatastoreEncoder.java @@ -157,7 +157,7 @@ public class GSShapefileDatastoreEncoder extends GSAbstractDatastoreEncoder { /** * @return {@value #TYPE} */ - String getValidType() { + protected String getValidType() { return TYPE; } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDataStoreManager.java similarity index 55% rename from src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java rename to src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDataStoreManager.java index 06390f8..ffe8ee3 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDataStoreManager.java @@ -24,7 +24,7 @@ */ package it.geosolutions.geoserver.rest.manager; -import it.geosolutions.geoserver.rest.HTTPUtils; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; import java.net.MalformedURLException; @@ -33,13 +33,14 @@ import java.net.URL; /** * Manage datastores. * - * To pass connection parameters, use the encoders derived from - * {@link GSAbstractDatastoreEncoder}. + * To pass connection parameters, use the encoders derived from {@link GSAbstractDatastoreEncoder}. * * @author Oscar Fonts * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + * + * @deprecated currently this is only a {@link GeoServerRESTStoreManager} wrapper for datastore */ -public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager { +public class GeoServerRESTDatastoreManager extends GeoServerRESTStoreManager { /** * Default constructor. @@ -51,43 +52,47 @@ public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager * @throws IllegalArgumentException */ public GeoServerRESTDatastoreManager(URL restURL, String username, String password) - throws IllegalArgumentException, MalformedURLException { + throws IllegalArgumentException, MalformedURLException { super(restURL, username, password); } /** - * Create a datastore. + * Create a store. * - * @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 + * @param workspace Name of the workspace to contain the store. This will also be the prefix of any layer names contained in the store. + * @param datastore the set of parameters to be set to the store (including connection parameters). + * @return true if the store has been successfully created, false otherwise + * + * @deprecated use {@link GeoServerRESTStoreManager#create(String, GSAbstractStoreEncoder)} */ - - public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) { - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/").toString(); - String xml = datastore.toString(); - String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); - return result != null; + public boolean create(String workspace, GSAbstractDatastoreEncoder store) { + return super.create(workspace, store); } /** - * Update a datastore. + * Update a store. * - * @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 + * @param workspace Name of the workspace that contains the store. + * @param datastore the set of parameters to be set to the store (including connection parameters). + * @return true if the store has been successfully updated, false otherwise + * @deprecated use {@link GeoServerRESTStoreManager#update(String, GSAbstractStoreEncoder)} */ - public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) { - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/", - datastore.getName()).toString(); - String xml = datastore.toString(); - String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); - return result != null; + public boolean update(String workspace, GSAbstractDatastoreEncoder store) { + return super.update(workspace, store); + } + + /** + * Remove a given CoverageStore in a given Workspace. + * + * @param workspace The name of the workspace + * @param storename The name of the CoverageStore to remove. + * @param recurse if remove should be performed recursively + * @return true if the CoverageStore was successfully removed. + * @throws MalformedURLException + */ + public boolean remove(final String workspace, final GSAbstractStoreEncoder store, + final boolean recurse) throws IllegalArgumentException, MalformedURLException { + + return super.remove(workspace, store, recurse); } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java new file mode 100644 index 0000000..34dad05 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java @@ -0,0 +1,123 @@ +/* + * 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.Format; +import it.geosolutions.geoserver.rest.HTTPUtils; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; + +import java.net.MalformedURLException; +import java.net.URL; + +/** + * Manage stores. + * + * To pass connection parameters, use the encoders derived from + * {@link GSAbstractDatastoreEncoder}. + * + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoServerRESTStoreManager 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 + * @throws MalformedURLException + * @throws IllegalArgumentException + */ + public GeoServerRESTStoreManager(URL restURL, String username, String password) + throws IllegalArgumentException, MalformedURLException { + super(restURL, username, password); + } + + /** + * Create a store. + * + * @param workspace Name of the workspace to contain the store. This + * will also be the prefix of any layer names contained in the + * store. + * @param datastore the set of parameters to be set to the store + * (including connection parameters). + * @return true if the store has been successfully created, + * false otherwise + */ + public boolean create(String workspace, GSAbstractStoreEncoder store) { + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/", store.getStoreType().toString(),".",Format.XML.toString()).toString(); + String xml = store.toString(); + String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); + return result != null; + } + + /** + * Update a store. + * + * @param workspace Name of the workspace that contains the store. + * @param datastore the set of parameters to be set to the store + * (including connection parameters). + * @return true if the store has been successfully updated, + * false otherwise + */ + public boolean update(String workspace, GSAbstractStoreEncoder store) { + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace,"/", store.getStoreType().toString(),"/", + store.getName(),".",Format.XML.toString()).toString(); + String xml = store.toString(); + String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); + return result != null; + } + + /** + * Remove a given CoverageStore in a given Workspace. + * + * @param workspace The name of the workspace + * @param storename The name of the CoverageStore to remove. + * @param recurse if remove should be performed recursively + * @return true if the CoverageStore was successfully removed. + * @throws MalformedURLException + */ + public boolean remove(final String workspace, final GSAbstractStoreEncoder store, + final boolean recurse) throws IllegalArgumentException, MalformedURLException { +// if (workspace == null || storename == null) +// throw new IllegalArgumentException("Arguments may not be null!"); +// 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()); + if (recurse) + url.append("?recurse=true"); + final URL deleteStore = new URL(url.toString()); + + boolean deleted = HTTPUtils.delete(deleteStore.toExternalForm(), gsuser, gspass); +// if (!deleted) { +// LOGGER.warn("Could not delete CoverageStore " + workspace + ":" + storename); +// } else { +// LOGGER.info("CoverageStore successfully deleted " + workspace + ":" + storename); +// } + return deleted; + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/datastore/GSArcSDEDatastoreEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/datastore/GSArcSDEDatastoreEncoderTest.java index 12e343f..5da386c 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/datastore/GSArcSDEDatastoreEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/datastore/GSArcSDEDatastoreEncoderTest.java @@ -24,25 +24,24 @@ */ package it.geosolutions.geoserver.rest.datastore; -import it.geosolutions.geoserver.rest.GeoserverRESTTest; -import it.geosolutions.geoserver.rest.decoder.RESTDataStore; -import it.geosolutions.geoserver.rest.encoder.datastore.GSArcSDEDatastoreEncoder; +import java.net.MalformedURLException; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSArcSDEDatastoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSOracleNGDatastoreEncoder; /** * Testcase for creating arcsde-based resources on geoserver. *

    * Since these tests require a running arcsde instance, this is more like integration tests.
    * You may skip them by defining

    - *        -DpgIgnore=true 
    - * When pgIgnore is defined that way, failing tests will not break - * the build: they will be logged as errors instead. - * + * -DpgIgnore=true When pgIgnore is defined that way, failing tests will not break the build: they will be logged as + * errors instead. + * *

    - * The target arcsde instance can be customized by defining the following env vars:

      + * The target arcsde instance can be customized by defining the following env vars: + *
        *
      • pgHost (default localhost)
      • *
      • pgPort (default: 5432)
      • *
      • pgDatabase (default: test)
      • @@ -50,76 +49,30 @@ import org.slf4j.LoggerFactory; *
      • pgUser (default: utest)
      • *
      • pgPassword (default: ptest)
      • *
      - * + * * @author etj * @author Eric Grosso * @author Gianni Barrotta - * + * @author carlo cancellieri - GeoSolutions + * * @see GeoserverRESTTest */ -public class GSArcSDEDatastoreEncoderTest extends GeoserverRESTTest { +public class GSArcSDEDatastoreEncoderTest extends StoreIntegrationTest { - private final static Logger LOGGER = LoggerFactory.getLogger(GSArcSDEDatastoreEncoderTest.class); - private static final String DEFAULT_WS = "it.geosolutions"; + public GSArcSDEDatastoreEncoderTest() throws IllegalArgumentException, MalformedURLException { - private final boolean pgIgnore; - private final String pgServer; - private final int pgPort; - private final String pgInstance; - private final String pgUser; - private final String pgPassword; - - public GSArcSDEDatastoreEncoderTest() { - - pgIgnore = System.getProperty("pgIgnore", "false").equalsIgnoreCase("true"); - pgServer = System.getProperty("pgServer", "localhost"); - pgPort = Integer.parseInt(System.getProperty("pgPort", "5151")); - pgInstance = System.getProperty("pgInstance", "test"); - pgUser = System.getProperty("pgUser", "utest"); - pgPassword = System.getProperty("pgPassword", "ptest"); + super(System.getProperty("Ignore", "false").equalsIgnoreCase("true")); } - @Test - public void testCreateDeleteArcSDEDatastore() { - if (!enabled()) { - return; - } - deleteAll(); - - String wsName = DEFAULT_WS; - String datastoreName = "resttestarcsde"; - String description = "description"; - String dsNamespace = "http://www.geo-solutions.it"; + @Override + public GSAbstractStoreEncoder getStoreEncoderTest() { + GSArcSDEDatastoreEncoder datastoreEncoder = new GSArcSDEDatastoreEncoder( + System.getProperty("arcsdeHost", "localhost"), + System.getProperty("arcsdeSchema", "public"), System.getProperty("arcsdePassword", "ptest")); + datastoreEncoder.setNamespace(DEFAULT_WS); + datastoreEncoder.setPort(Integer.parseInt(System.getProperty("arcsdePort", "5432"))); - GSArcSDEDatastoreEncoder datastoreEncoder = new GSArcSDEDatastoreEncoder(datastoreName, pgServer, pgUser); - datastoreEncoder.setDescription(description); - datastoreEncoder.setNamespace(dsNamespace); - datastoreEncoder.setPort(pgPort); - datastoreEncoder.setInstance(pgInstance); - datastoreEncoder.setPassword(pgPassword); - - assertTrue(publisher.createWorkspace(wsName)); - - // creation test - boolean created = manager.getDatastoreManager().create(wsName, datastoreEncoder); - - if( ! pgIgnore ) - assertTrue("arcsde datastore not created", created); - else if( ! created) - LOGGER.error("*** Datastore " + datastoreName + " has not been created."); - - - RESTDataStore datastore = reader.getDatastore(wsName, datastoreName); - LOGGER.info("The type of the created datastore is: " + datastore.getStoreType()); - - // removing test - boolean removed = publisher.removeDatastore(wsName, datastoreName, true); - if( ! pgIgnore ) - assertTrue("arcsde datastore not removed", removed); - else if( ! removed ) - LOGGER.error("*** Datastore " + datastoreName + " has not been removed."); - - assertTrue(publisher.removeWorkspace(wsName, false)); + return datastoreEncoder; } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/datastore/GSOracleNGDatastoreEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/datastore/GSOracleNGDatastoreEncoderTest.java index b470860..263fcee 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/datastore/GSOracleNGDatastoreEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/datastore/GSOracleNGDatastoreEncoderTest.java @@ -24,8 +24,11 @@ */ package it.geosolutions.geoserver.rest.datastore; +import java.net.MalformedURLException; + import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTDataStore; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSOracleNGDatastoreEncoder; import org.junit.Test; @@ -54,82 +57,37 @@ import org.slf4j.LoggerFactory; * @author etj * @author Eric Grosso * @author Gianni Barrotta + * @author carlo cancellieri - GeoSolutions * * @see GeoserverRESTTest */ -public class GSOracleNGDatastoreEncoderTest extends GeoserverRESTTest { +public class GSOracleNGDatastoreEncoderTest extends StoreIntegrationTest { - private final static Logger LOGGER = LoggerFactory.getLogger(GSOracleNGDatastoreEncoderTest.class); - private static final String DEFAULT_WS = "it.geosolutions"; - - private final boolean pgIgnore; - private final String pgHost; - private final int pgPort; - private final String pgDatabase; - private final String pgSchema; - private final String pgUser; - private final String pgPassword; - - public GSOracleNGDatastoreEncoderTest() { - - pgIgnore = System.getProperty("pgIgnore", "false").equalsIgnoreCase("true"); - pgHost = System.getProperty("pgHost", "localhost"); - pgPort = Integer.parseInt(System.getProperty("pgPort", "5432")); - pgDatabase = System.getProperty("pgDatabase", "test"); - pgSchema = System.getProperty("pgSchema", "public"); - pgUser = System.getProperty("pgUser", "utest"); - pgPassword = System.getProperty("pgPassword", "ptest"); +// private final static Logger LOGGER = LoggerFactory.getLogger(GSOracleNGDatastoreEncoderTest.class); + + public GSOracleNGDatastoreEncoderTest() throws IllegalArgumentException, MalformedURLException { + super(System.getProperty("pgIgnore", "false").equalsIgnoreCase("true")); + } - @Test - public void testCreateDeleteOracleNGDatastore() { - if (!enabled()) { - return; - } - deleteAll(); - - String wsName = DEFAULT_WS; - String datastoreName = "resttestOracleNG"; - String description = "description"; - String dsNamespace = "http://www.geo-solutions.it"; + @Override + public GSAbstractStoreEncoder getStoreEncoderTest() { + GSOracleNGDatastoreEncoder datastoreEncoder = new GSOracleNGDatastoreEncoder(System.getProperty("oDataStoreName", "test"), System.getProperty("pgDatabase", "test")); + datastoreEncoder.setNamespace(DEFAULT_WS); + datastoreEncoder.setHost(System.getProperty("pgHost", "localhost")); + datastoreEncoder.setPort(Integer.parseInt(System.getProperty("pgPort", "5432"))); + datastoreEncoder.setSchema(System.getProperty("pgUser", "postgres")); + datastoreEncoder.setUser(System.getProperty("pgSchema", "public")); + datastoreEncoder.setPassword(System.getProperty("pgPassword", "postgres")); + boolean exposePrimaryKeys = true; boolean validateConnections = false; String primaryKeyMetadataTable = "test"; - - GSOracleNGDatastoreEncoder datastoreEncoder = new GSOracleNGDatastoreEncoder(datastoreName, pgDatabase); - datastoreEncoder.setDescription(description); - datastoreEncoder.setNamespace(dsNamespace); - datastoreEncoder.setHost(pgHost); - datastoreEncoder.setPort(pgPort); - datastoreEncoder.setSchema(pgSchema); - datastoreEncoder.setUser(pgUser); - datastoreEncoder.setPassword(pgPassword); datastoreEncoder.setExposePrimaryKeys(exposePrimaryKeys); datastoreEncoder.setValidateConnections(validateConnections); datastoreEncoder.setPrimaryKeyMetadataTable(primaryKeyMetadataTable); - - assertTrue(publisher.createWorkspace(wsName)); - - // creation test - - boolean created = manager.getDatastoreManager().create(wsName, datastoreEncoder); - if( ! pgIgnore ) - assertTrue("OracleNG datastore not created", created); - else if( ! created) - LOGGER.error("*** Datastore " + datastoreName + " has not been created."); - - - RESTDataStore datastore = reader.getDatastore(wsName, datastoreName); - LOGGER.info("The type of the created datastore is: " + datastore.getStoreType()); - - // removing test - boolean removed = publisher.removeDatastore(wsName, datastoreName, true); - if( ! pgIgnore ) - assertTrue("OracleNG datastore not removed", removed); - else if( ! removed ) - LOGGER.error("*** Datastore " + datastoreName + " has not been removed."); - - assertTrue(publisher.removeWorkspace(wsName, false)); + return datastoreEncoder; } + } diff --git a/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java b/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java new file mode 100644 index 0000000..fab7cb7 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java @@ -0,0 +1,114 @@ +/* + * 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.datastore; + +import java.net.MalformedURLException; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTDataStore; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSOracleNGDatastoreEncoder; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; + +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *

      + * Since these tests require a running Store instance, this is more like integration tests.
      + * + * @author carlo cancellieri - GeoSolutions + * + * @see GeoserverRESTTest + */ +public abstract class StoreIntegrationTest extends GeoserverRESTTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(StoreIntegrationTest.class); + + private final GeoServerRESTStoreManager storeManager; + + /** + * ignore integration tests + */ + private final boolean ignore; + + public boolean isIgnore() { + return ignore; + } + + /** + * + * @param ignore false if this test shoudl be disabled + * @throws IllegalArgumentException + * @throws MalformedURLException + */ + public StoreIntegrationTest(boolean ignore) throws IllegalArgumentException, MalformedURLException { + super(); + this.storeManager = new GeoServerRESTStoreManager(URL, RESTUSER, RESTPW); + this.ignore=ignore; + } + + public abstract GSAbstractStoreEncoder getStoreEncoderTest(); + + @Test + public void testCreateDeleteDatastore() throws IllegalArgumentException, MalformedURLException { + if (!enabled()) { + return; + } + deleteAll(); + + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + // creation test + GSAbstractStoreEncoder storeEncoder=getStoreEncoderTest(); + + String storeName = storeEncoder.getName(); +// String description = storeEncoder.getDescription(); + + boolean created = storeManager.create(DEFAULT_WS, storeEncoder); + + if( ! ignore ) + assertTrue("Datastore not created", created); + else if( ! created) + LOGGER.error("*** store " + storeName + " has not been created."); + + + RESTDataStore datastore = reader.getDatastore(DEFAULT_WS, storeName); + assertNotNull(datastore); + LOGGER.info("The type of the created datastore is: " + datastore.getStoreType()); + + // removing test + boolean removed = storeManager.remove(DEFAULT_WS, storeEncoder, true); + if( ! ignore ) + assertTrue("Datastore not removed", removed); + else if( ! removed ) + LOGGER.error("*** Datastore " + storeName + " has not been removed."); + + assertTrue(publisher.removeWorkspace(DEFAULT_WS, false)); + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java index 82273a6..8d121d3 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java @@ -24,8 +24,9 @@ */ package it.geosolutions.geoserver.rest.manager; -import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.datastore.StoreIntegrationTest; import it.geosolutions.geoserver.rest.decoder.RESTDataStore; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSDirectoryOfShapefilesDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder; @@ -62,70 +63,135 @@ import org.junit.Test; * * @author Oscar Fonts */ -public class GeoserverRESTDatastoreManagerTest extends GeoserverRESTTest { - - 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 GeoserverRESTDatastoreManagerTest() throws Exception { - LOCATION_1 = new URL("file:data/1"); - LOCATION_2 = new URL("file:data/2"); - } +/* + * 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. + */ - @Test - public void test() throws Exception { +/** + * Test datastore handling (create, read and update): + * + *

        + *
      • Tests all the constructors and setters from {@link GSDirectoryOfShapefilesDatastoreEncoder} and parent classes ( + * {@link GSShapefileDatastoreEncoder}, {@link GSAbstractDatastoreEncoder}). + * + *
      • Tests constructors and getters from {@link RESTDataStore} (reader). + * + *
      • Tests {@link GeoServerRESTDatastoreManager} create and update methods. + *
      + * + *

      + * The sequence is: + *

        + *
      1. Create a DirectoryOfShapefilesDatastoreEncoder, with default parameters. + *
      2. Publish via GeoServerRESTDatastoreManager.create. + *
      3. Read the datastore from server. + *
      4. Test all parameter values. + *
      5. Create a new Encoder from it. + *
      6. Change all datastore parameter to non-default ones. + *
      7. Update via GeoServerRESTDatastoreManager.update. + *
      8. Read again. + *
      9. Test all new values. + *
      + * + * @author Oscar Fonts + */ +public class GeoserverRESTDatastoreManagerTest extends StoreIntegrationTest { + + 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 GeoserverRESTDatastoreManagerTest() throws Exception { + super(true); + LOCATION_1 = new URL("file:data/1"); + LOCATION_2 = new URL("file:data/2"); + } + + @Override + public GSAbstractStoreEncoder getStoreEncoderTest() { + return new GSDirectoryOfShapefilesDatastoreEncoder(DS_NAME, LOCATION_1); + } + + @Test + public void test() throws Exception { if (!enabled()) { return; } - - // Delete all resources except styles - deleteAllWorkspacesRecursively(); - + + // Delete all resources except styles + deleteAllWorkspacesRecursively(); + // Create workspace assertTrue(publisher.createWorkspace(WS_NAME)); - - // Create a directory of spatial files with default parameters - GSDirectoryOfShapefilesDatastoreEncoder create = new GSDirectoryOfShapefilesDatastoreEncoder(DS_NAME, LOCATION_1); - assertTrue(manager.getDatastoreManager().create(WS_NAME, create)); - - // Read the store from server; check all parameter values - RESTDataStore read = reader.getDatastore(WS_NAME, DS_NAME); - assertEquals(read.getName(), DS_NAME); - assertEquals(read.getWorkspaceName(), WS_NAME); - assertEquals(read.isEnabled(), true); - - Map connParams = read.getConnectionParameters(); - assertEquals(connParams.get("url"), LOCATION_1.toString()); - assertEquals(connParams.get("charset"), "ISO-8859-1"); - assertEquals(connParams.get("create spatial index"), "true"); - assertEquals(connParams.get("memory mapped buffer"), "false"); - assertEquals(connParams.get("cache and reuse memory maps"), "true"); - // Change all parameter to non-default values - GSDirectoryOfShapefilesDatastoreEncoder update = new GSDirectoryOfShapefilesDatastoreEncoder(read); - update.setDescription(DS_DESCRIPTION); - update.setEnabled(false); - update.setUrl(LOCATION_2); - update.setCharset(Charset.forName("UTF-8")); - update.setCreateSpatialIndex(false); - update.setMemoryMappedBuffer(true); - update.setCacheAndReuseMemoryMaps(false); - - //update the store - assertTrue(manager.getDatastoreManager().update(WS_NAME, update)); - - // Read again, check that all parameters have changed - read = reader.getDatastore(WS_NAME, DS_NAME); - assertEquals(read.getWorkspaceName(), WS_NAME); - assertEquals(read.isEnabled(), false); - connParams = read.getConnectionParameters(); - assertEquals(connParams.get("url"), LOCATION_2.toString()); - assertEquals(connParams.get("charset"), "UTF-8"); - assertEquals(connParams.get("create spatial index"), "false"); - assertEquals(connParams.get("memory mapped buffer"), "true"); - assertEquals(connParams.get("cache and reuse memory maps"), "false"); - } + // Create a directory of spatial files with default parameters + GSDirectoryOfShapefilesDatastoreEncoder create = new GSDirectoryOfShapefilesDatastoreEncoder( + DS_NAME, LOCATION_1); + assertTrue(manager.getStoreManager().create(WS_NAME, create)); + + // Read the store from server; check all parameter values + RESTDataStore read = reader.getDatastore(WS_NAME, DS_NAME); + assertEquals(read.getName(), DS_NAME); + assertEquals(read.getWorkspaceName(), WS_NAME); + assertEquals(read.isEnabled(), true); + + Map connParams = read.getConnectionParameters(); + assertEquals(connParams.get("url"), LOCATION_1.toString()); + assertEquals(connParams.get("charset"), "ISO-8859-1"); + assertEquals(connParams.get("create spatial index"), "true"); + assertEquals(connParams.get("memory mapped buffer"), "false"); + assertEquals(connParams.get("cache and reuse memory maps"), "true"); + + // Change all parameter to non-default values + GSDirectoryOfShapefilesDatastoreEncoder update = new GSDirectoryOfShapefilesDatastoreEncoder( + read); + update.setDescription(DS_DESCRIPTION); + update.setEnabled(false); + update.setUrl(LOCATION_2); + update.setCharset(Charset.forName("UTF-8")); + update.setCreateSpatialIndex(false); + update.setMemoryMappedBuffer(true); + update.setCacheAndReuseMemoryMaps(false); + + // update the store + assertTrue(manager.getStoreManager().update(WS_NAME, update)); + + // Read again, check that all parameters have changed + read = reader.getDatastore(WS_NAME, DS_NAME); + assertEquals(read.getWorkspaceName(), WS_NAME); + assertEquals(read.isEnabled(), false); + connParams = read.getConnectionParameters(); + assertEquals(connParams.get("url"), LOCATION_2.toString()); + assertEquals(connParams.get("charset"), "UTF-8"); + assertEquals(connParams.get("create spatial index"), "false"); + assertEquals(connParams.get("memory mapped buffer"), "true"); + assertEquals(connParams.get("cache and reuse memory maps"), "false"); + } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java index c809d42..92fc273 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java @@ -27,10 +27,13 @@ package it.geosolutions.geoserver.rest.publisher; import it.geosolutions.geoserver.rest.GeoserverRESTTest; -import it.geosolutions.geoserver.rest.decoder.RESTDataStore; -import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; +import it.geosolutions.geoserver.rest.datastore.StoreIntegrationTest; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; + +import java.net.MalformedURLException; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,53 +58,39 @@ import org.slf4j.LoggerFactory; * * @author etj * @author Eric Grosso + * @author carlo cancellieri - GeoSolutions * * @see GeoserverRESTTest */ -public class GeoserverRESTPostgisDatastoreTest extends GeoserverRESTTest { +public class GeoserverRESTPostgisDatastoreTest extends StoreIntegrationTest { - private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTPostgisDatastoreTest.class); - private static final String DEFAULT_WS = "it.geosolutions"; - - private final boolean pgIgnore; - private final String pgHost; - private final int pgPort; - private final String pgDatabase; - private final String pgSchema; - private final String pgUser; - private final String pgPassword; - - public GeoserverRESTPostgisDatastoreTest() { - - pgIgnore = System.getProperty("pgIgnore", "false").equalsIgnoreCase("true"); - pgHost = System.getProperty("pgHost", "localhost"); - pgPort = Integer.parseInt(System.getProperty("pgPort", "5432")); - pgDatabase = System.getProperty("pgDatabase", "test"); - pgSchema = System.getProperty("pgSchema", "public"); - pgUser = System.getProperty("pgUser", "utest"); - pgPassword = System.getProperty("pgPassword", "ptest"); + public GeoserverRESTPostgisDatastoreTest() + throws IllegalArgumentException, MalformedURLException { + super(System.getProperty("pgIgnore", "false").equalsIgnoreCase("true")); } - @Test - public void testCreateDeletePostGISDatastore() { - if (!enabled()) { - return; - } - deleteAll(); - String wsName = DEFAULT_WS; - String datastoreName = "resttestpostgis"; - String description = "description"; - String dsNamespace = "http://www.geo-solutions.it"; + @Override + public GSAbstractStoreEncoder getStoreEncoderTest(){ boolean exposePrimaryKeys = true; boolean validateConnections = false; String primaryKeyMetadataTable = "test"; - GSPostGISDatastoreEncoder datastoreEncoder = new GSPostGISDatastoreEncoder(); - datastoreEncoder.defaultInit(); - datastoreEncoder.setName(datastoreName); + String datastoreName = "resttestpostgis"; + String description = "description"; + String dsNamespace = "http://www.geo-solutions.it"; + + GSPostGISDatastoreEncoder datastoreEncoder = new GSPostGISDatastoreEncoder(datastoreName); datastoreEncoder.setDescription(description); datastoreEncoder.setNamespace(dsNamespace); + + String pgHost = System.getProperty("pgHost", "localhost"); + int pgPort = Integer.parseInt(System.getProperty("pgPort", "5432")); + String pgDatabase = System.getProperty("pgDatabase", "test"); + String pgSchema = System.getProperty("pgSchema", "public"); + String pgUser = System.getProperty("pgUser", "utest"); + String pgPassword = System.getProperty("pgPassword", "ptest"); + datastoreEncoder.setHost(pgHost); datastoreEncoder.setPort(pgPort); datastoreEncoder.setDatabase(pgDatabase); @@ -111,29 +100,7 @@ public class GeoserverRESTPostgisDatastoreTest extends GeoserverRESTTest { datastoreEncoder.setExposePrimaryKeys(exposePrimaryKeys); datastoreEncoder.setValidateConnections(validateConnections); datastoreEncoder.setPrimaryKeyMetadataTable(primaryKeyMetadataTable); - - assertTrue(publisher.createWorkspace(wsName)); - - // creation test - boolean created = publisher.createPostGISDatastore(wsName, datastoreEncoder); - - if( ! pgIgnore ) - assertTrue("PostGIS datastore not created", created); - else if( ! created) - LOGGER.error("*** Datastore " + datastoreName + " has not been created."); - - - RESTDataStore datastore = reader.getDatastore(wsName, datastoreName); - LOGGER.info("The type of the created datastore is: " + datastore.getType()); - - // removing test - boolean removed = publisher.removeDatastore(wsName, datastoreName); - if( ! pgIgnore ) - assertTrue("PostGIS datastore not removed", removed); - else if( ! removed ) - LOGGER.error("*** Datastore " + datastoreName + " has not been removed."); - - assertTrue(publisher.removeWorkspace(wsName)); + return datastoreEncoder; } - + } diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java index 3778449..2491f12 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java @@ -60,16 +60,14 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { @After public void cleanUp(){ - if(enabled()){ - deleteAllWorkspaces(); - } } @Test public void testReloadDataStore() throws FileNotFoundException, IOException { if (!enabled()) return; - + deleteAllWorkspacesRecursively(); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); String storeName = "resttestshp"; @@ -91,7 +89,8 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { if (!enabled()) { return; } - + deleteAllWorkspacesRecursively(); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); String storeName = "resttestshp"; @@ -126,6 +125,7 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { if (!enabled()) { return; } + deleteAllWorkspacesRecursively(); // Assume.assumeTrue(enabled); assertTrue(publisher.createWorkspace(DEFAULT_WS)); @@ -159,6 +159,7 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { if (!enabled()) { return; } + deleteAllWorkspacesRecursively(); // Assume.assumeTrue(enabled); assertTrue(publisher.createWorkspace(DEFAULT_WS)); @@ -192,6 +193,7 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { if (!enabled()) { return; } + deleteAllWorkspacesRecursively(); // Assume.assumeTrue(enabled); assertTrue(publisher.createWorkspace(DEFAULT_WS)); @@ -236,6 +238,7 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { if (!enabled()) { return; } + deleteAllWorkspacesRecursively(); // Assume.assumeTrue(enabled); assertTrue(publisher.createWorkspace(DEFAULT_WS)); @@ -278,7 +281,8 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { public void testPublishShpUsingDeclaredNativeCRS() throws Exception { if (!enabled()) return; - + deleteAllWorkspacesRecursively(); + // layer publication params String workspace = DEFAULT_WS; String storename = "resttestshp"; @@ -315,7 +319,8 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { public void testPublishShpUsingWKTNativeCRS() throws Exception { if (!enabled()) return; - + deleteAllWorkspacesRecursively(); + // layer publication params String workspace = DEFAULT_WS; String storename = "resttestshp";