diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java index 97255fa..ba48a58 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2012 GeoSolutions S.A.S. + * Copyright (C) 2007,2013 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager; import java.net.MalformedURLException; import java.net.URL; @@ -47,10 +48,10 @@ import java.net.URL; public class GeoServerRESTManager extends GeoServerRESTAbstractManager { private final GeoServerRESTPublisher publisher; - private final GeoServerRESTReader reader; - private final GeoServerRESTStoreManager store; + private final GeoServerRESTStoreManager storeManager; + private final GeoServerRESTStyleManager styleManager; private final GeoServerRESTStructuredGridCoverageReaderManager structuredGridCoverageReader; @@ -66,14 +67,15 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { * @throws IllegalArgumentException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)} */ public GeoServerRESTManager(URL restURL, String username, String password) - throws IllegalArgumentException, MalformedURLException { + throws IllegalArgumentException { 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); structuredGridCoverageReader = new GeoServerRESTStructuredGridCoverageReaderManager(restURL, username, password); - store = new GeoServerRESTStoreManager(restURL, gsuser, gspass); + storeManager = new GeoServerRESTStoreManager(restURL, gsuser, gspass); + styleManager = new GeoServerRESTStyleManager(restURL, gsuser, gspass); } public GeoServerRESTPublisher getPublisher() { @@ -85,7 +87,11 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { } public GeoServerRESTStoreManager getStoreManager() { - return store; + return storeManager; + } + + public GeoServerRESTStyleManager getStyleManager() { + return styleManager; } public GeoServerRESTStructuredGridCoverageReaderManager getStructuredGridCoverageReader() { diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 0069fcc..a37644c 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * Copyright (C) 2007,2013 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,10 +25,8 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.decoder.RESTCoverage; -import it.geosolutions.geoserver.rest.decoder.RESTCoverageList; import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; -import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import it.geosolutions.geoserver.rest.encoder.GSBackupEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder; @@ -41,6 +39,7 @@ import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager.ConfigureCoveragesOption; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager; import java.io.File; import java.io.FileNotFoundException; @@ -50,7 +49,6 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLEncoder; -import java.util.Iterator; import java.util.zip.ZipFile; import org.apache.commons.httpclient.NameValuePair; @@ -88,6 +86,8 @@ public class GeoServerRESTPublisher { */ private final String gspass; + + private final GeoServerRESTStyleManager styleManager; /** * Creates a GeoServerRESTPublisher to connect against a GeoServer instance with the given URL and user credentials. * @@ -99,6 +99,14 @@ public class GeoServerRESTPublisher { this.restURL = HTTPUtils.decurtSlash(restURL); this.gsuser = username; this.gspass = password; + + URL url = null; + try { + url = new URL(restURL); + } catch (MalformedURLException ex) { + LOGGER.error("Bad URL: Calls to GeoServer are going to fail" , ex); + } + styleManager = new GeoServerRESTStyleManager(url, username, password); } // ========================================================================== @@ -306,20 +314,7 @@ public class GeoServerRESTPublisher { * @return true if the operation completed successfully. */ public boolean publishStyle(String sldBody) { - /* - * This is the equivalent call with cUrl: - * - * {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ - * http://$GSIP:$GSPORT/$SERVLET/rest/styles} - */ - try { - return publishStyle(sldBody, null); - } catch (IllegalArgumentException e) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(e.getLocalizedMessage(), e); - } - } - return false; + return styleManager.publishStyle(sldBody); } /** @@ -333,23 +328,7 @@ public class GeoServerRESTPublisher { */ public boolean publishStyle(final String sldBody, final String name) throws IllegalArgumentException { - /* - * This is the equivalent call with cUrl: - * - * {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ - * http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=name} - */ - if (sldBody == null || sldBody.isEmpty()) { - throw new IllegalArgumentException("The style body may not be null or empty"); - } - StringBuilder sUrl = new StringBuilder(restURL); - sUrl.append("/rest/styles"); - if (name != null && !name.isEmpty()) { - sUrl.append("?name=").append(name); - } - final String result = HTTPUtils.post(sUrl.toString(), sldBody, - "application/vnd.ogc.sld+xml", gsuser, gspass); - return result != null; + return styleManager.publishStyle(sldBody, name); } /** @@ -360,7 +339,7 @@ public class GeoServerRESTPublisher { * @return true if the operation completed successfully. */ public boolean publishStyle(File sldFile) { - return publishStyle(sldFile, null); + return styleManager.publishStyle(sldFile); } /** @@ -372,13 +351,7 @@ public class GeoServerRESTPublisher { * @return true if the operation completed successfully. */ public boolean publishStyle(File sldFile, String name) { - String sUrl = restURL + "/rest/styles"; - if (name != null && !name.isEmpty()) { - sUrl += "?name=" + encode(name); - } - LOGGER.debug("POSTing new style " + name + " to " + sUrl); - String result = HTTPUtils.post(sUrl, sldFile, Format.SLD.getContentType(), gsuser, gspass); - return result != null; + return styleManager.publishStyle(sldFile, name); } /** @@ -392,24 +365,7 @@ public class GeoServerRESTPublisher { */ public boolean updateStyle(final String sldBody, final String name) throws IllegalArgumentException { - /* - * This is the equivalent call with cUrl: - * - * {@code curl -u admin:geoserver -XPUT \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ - * http://$GSIP:$GSPORT/$SERVLET/rest/styles/$NAME} - */ - if (sldBody == null || sldBody.isEmpty()) { - throw new IllegalArgumentException("The style body may not be null or empty"); - } else if (name == null || name.isEmpty()) { - throw new IllegalArgumentException("The style name may not be null or empty"); - } - - final StringBuilder sUrl = new StringBuilder(restURL); - sUrl.append("/rest/styles/").append(encode(name)); - - final String result = HTTPUtils.put(sUrl.toString(), sldBody, - "application/vnd.ogc.sld+xml", gsuser, gspass); - return result != null; + return styleManager.updateStyle(sldBody, name); } /** @@ -424,19 +380,7 @@ public class GeoServerRESTPublisher { public boolean updateStyle(final File sldFile, final String name) throws IllegalArgumentException { - if (sldFile == null) { - throw new IllegalArgumentException("Unable to updateStyle using a null parameter file"); - } else if (name == null || name.isEmpty()) { - throw new IllegalArgumentException("The style name may not be null or empty"); - } - - final StringBuilder sUrl = new StringBuilder(restURL); - sUrl.append("/rest/styles/").append(encode(name)); - - final String result = HTTPUtils.put(sUrl.toString(), sldFile, - "application/vnd.ogc.sld+xml", gsuser, gspass); - return result != null; - + return styleManager.updateStyle(sldFile, name); } /** @@ -452,24 +396,8 @@ public class GeoServerRESTPublisher { */ public boolean removeStyle(String styleName, final boolean purge) throws IllegalArgumentException { - if (styleName == null || styleName.isEmpty()) - throw new IllegalArgumentException( - "Check styleName parameter, it may never be null or empty"); - final StringBuffer sUrl = new StringBuffer(restURL); - - // check style name - // TODO may we whant to throw an exception instead of - // change style name? - styleName = styleName.replaceAll(":", "_"); - styleName = encode(styleName); - - sUrl.append("/rest/styles/").append(styleName); - if (purge) { - sUrl.append("?purge=true"); - } - - return HTTPUtils.delete(sUrl.toString(), gsuser, gspass); + return styleManager.removeStyle(styleName, purge); } /** @@ -482,16 +410,74 @@ public class GeoServerRESTPublisher { * @return true if the operation completed successfully. */ public boolean removeStyle(String styleName) { - try { - return removeStyle(styleName, true); - } catch (IllegalArgumentException e) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(e.getLocalizedMessage(), e); - } - } - return false; + return styleManager.removeStyle(styleName); } + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager# + */ + public boolean publishStyleInWorkspace(String workspace, String sldBody) { + return styleManager.publishStyleInWorkspace(workspace, sldBody); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager# + */ + public boolean publishStyleInWorkspace(String workspace, String sldBody, String name) throws IllegalArgumentException { + return styleManager.publishStyleInWorkspace(workspace, sldBody, name); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#publishStyleInWorkspace(java.lang.String, java.io.File) + */ + public boolean publishStyleInWorkspace(String workspace, File sldFile) { + return styleManager.publishStyleInWorkspace(workspace, sldFile); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#publishStyleInWorkspace(java.lang.String, java.io.File, java.lang.String) + */ + public boolean publishStyleInWorkspace(String workspace, File sldFile, String name) { + return styleManager.publishStyleInWorkspace(workspace, sldFile, name); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#updateStyleInWorkspace(java.lang.String, java.lang.String, java.lang.String) + */ + public boolean updateStyleInWorkspace(String workspace, String sldBody, String name) throws IllegalArgumentException { + return styleManager.updateStyleInWorkspace(workspace, sldBody, name); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#updateStyleInWorkspace(java.lang.String, java.io.File, java.lang.String) + */ + public boolean updateStyleInWorkspace(String workspace, File sldFile, String name) throws IllegalArgumentException { + return styleManager.updateStyleInWorkspace(workspace, sldFile, name); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#removeStyleInWorkspace(java.lang.String, java.lang.String, boolean) + */ + public boolean removeStyleInWorkspace(String workspace, String styleName, boolean purge) throws IllegalArgumentException { + return styleManager.removeStyleInWorkspace(workspace, styleName, purge); + } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager#removeStyleInWorkspace(java.lang.String, java.lang.String) + */ + public boolean removeStyleInWorkspace(String workspace, String styleName) { + return styleManager.removeStyleInWorkspace(workspace, styleName); + } + + // ========================================================================== // === DATASTORE PUBLISHING // ========================================================================== @@ -524,6 +510,7 @@ public class GeoServerRESTPublisher { /** * @deprecated use {@link StoreType#toString()} */ + @Override public String toString() { return this.name().toLowerCase(); } @@ -958,7 +945,7 @@ public class GeoServerRESTPublisher { if (layerEncoder == null) { if (LOGGER.isErrorEnabled()) - LOGGER.error("GSLayerEncoder is null: Unable to find the defauldStyle for this layer"); + LOGGER.error("GSLayerEncoder is null: Unable to find the defaultStyle for this layer"); return false; } diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java index ca8949f..9e38078 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * Copyright (C) 2007,2013 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -41,9 +41,11 @@ import it.geosolutions.geoserver.rest.decoder.RESTNamespaceList; import it.geosolutions.geoserver.rest.decoder.RESTResource; import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageGranulesList; import it.geosolutions.geoserver.rest.decoder.RESTStructuredCoverageIndexSchema; +import it.geosolutions.geoserver.rest.decoder.RESTStyle; import it.geosolutions.geoserver.rest.decoder.RESTStyleList; import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList; import it.geosolutions.geoserver.rest.manager.GeoServerRESTStructuredGridCoverageReaderManager; +import it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -68,23 +70,23 @@ import org.slf4j.LoggerFactory; public class GeoServerRESTReader { private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTReader.class); + private final String baseurl; private String username; private String password; + private GeoServerRESTStyleManager styleManager; + /** * Creates a GeoServerRESTReader for a given GeoServer instance and * no auth credentials. *
Note that GeoServer 2.0 REST interface requires username/password credentials by * default, if not otherwise configured. . * - * @param restUrl the base GeoServer URL(e.g.: http://localhost:8080/geoserver) + * @param gsUrl the base GeoServer URL(e.g.: http://localhost:8080/geoserver) */ - public GeoServerRESTReader(URL restUrl) { - String extForm = restUrl.toExternalForm(); - this.baseurl = extForm.endsWith("/") ? - extForm.substring(0, extForm.length()-1) : - extForm; + public GeoServerRESTReader(URL gsUrl) { + baseurl = init(gsUrl, null, null); } /** @@ -93,65 +95,63 @@ public class GeoServerRESTReader { *
Note that GeoServer 2.0 REST interface requires username/password credentials by * default, if not otherwise configured. . * - * @param restUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) + * @param gsUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) */ - public GeoServerRESTReader(String restUrl) - throws MalformedURLException { - new URL(restUrl); // check URL correctness - this.baseurl = restUrl.endsWith("/") ? - restUrl.substring(0, restUrl.length()-1) : - restUrl; + public GeoServerRESTReader(String gsUrl) throws MalformedURLException { + baseurl = init(gsUrl, null, null); } /** * Creates a GeoServerRESTReader for a given GeoServer instance * with the given auth credentials. * - * @param restUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) + * @param gsUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) * @param username username auth credential * @param password password auth credential */ - public GeoServerRESTReader(String restUrl, String username, String password) throws MalformedURLException { - this(restUrl); - this.username = username; - this.password = password; + public GeoServerRESTReader(String gsUrl, String username, String password) throws MalformedURLException { + baseurl = init(gsUrl, username, password); } /** * Creates a GeoServerRESTReader for a given GeoServer instance * with the given auth credentials. * - * @param restUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) + * @param gsUrl the base GeoServer URL (e.g.: http://localhost:8080/geoserver) * @param username username auth credential * @param password password auth credential */ - public GeoServerRESTReader(URL restUrl, String username, String password) { - this(restUrl); + public GeoServerRESTReader(URL gsUrl, String username, String password) { + baseurl = init(gsUrl, username, password); + } + + private String init(String gsUrl, String username, String password) throws MalformedURLException { + return init(new URL(gsUrl), username, password); + } + + private String init(URL gsUrl, String username, String password) { + String restUrl = gsUrl.toExternalForm(); + String cleanUrl = restUrl.endsWith("/") ? + restUrl.substring(0, restUrl.length()-1) : + restUrl; this.username = username; this.password = password; + + styleManager = new GeoServerRESTStyleManager(gsUrl, username, password); + + return cleanUrl; } private String load(String url) { LOGGER.info("Loading from REST path " + url); - try { - String response = HTTPUtils.get(baseurl + url, username, password); - return response; - } catch (MalformedURLException ex) { - LOGGER.warn("Bad URL", ex); - } - - return null; + String response = HTTPUtils.get(baseurl + url, username, password); + return response; } private String loadFullURL(String url) { LOGGER.info("Loading from REST path " + url); - try { - String response = HTTPUtils.get(url, username, password); - return response; - } catch (MalformedURLException ex) { - LOGGER.warn("Bad URL", ex); - } - return null; + String response = HTTPUtils.get(url, username, password); + return response; } /** @@ -181,8 +181,31 @@ public class GeoServerRESTReader { * @throws RuntimeException if any other HTTP code than 200 or 404 was retrieved. */ public boolean existsStyle(String styleName) throws RuntimeException { - String url = baseurl + "/rest/styles/" + styleName + ".xml"; - return HTTPUtils.exists(url, username, password); + return styleManager.existsStyle(styleName); + } + + /** + * @see GeoServerRESTStyleManager#existsStyle(java.lang.String, java.lang.String) + * @since GeoServer 2.2 + */ + public boolean existsStyle(String workspace, String styleName) throws RuntimeException { + return styleManager.existsStyle(workspace, styleName); + } + + /** + * @see GeoServerRESTStyleManager#getStyle(java.lang.String) + * @since GeoServer 2.2 + */ + public RESTStyle getStyle(String name) { + return styleManager.getStyle(name); + } + + /** + * @see GeoServerRESTStyleManager#getStyle(java.lang.String, java.lang.String) + * @since GeoServer 2.2 + */ + public RESTStyle getStyle(String workspace, String name) { + return styleManager.getStyle(workspace, name); } /** @@ -191,22 +214,30 @@ public class GeoServerRESTReader { * @return summary info about Styles as a {@link RESTStyleList} */ public RESTStyleList getStyles() { - String url = "/rest/styles.xml"; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("### Retrieving Styles list from " + url); - } - return RESTStyleList.build(load(url)); + return styleManager.getStyles(); + } + + /** + * @see GeoServerRESTStyleManager#getStyles(java.lang.String) + * @since GeoServer 2.2 + */ + public RESTStyleList getStyles(String workspace) { + return styleManager.getStyles(workspace); } /** * Get the SLD body of a Style. */ public String getSLD(String styleName) { - String url = "/rest/styles/"+styleName+".sld"; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("### Retrieving SLD body from " + url); - } - return load(url); + return styleManager.getSLD(styleName); + } + + /** + * @see GeoServerRESTStyleManager#getSLD(java.lang.String, java.lang.String) + * @since GeoServer 2.2 + */ + public String getSLD(String workspace, String styleName) { + return styleManager.getSLD(workspace, styleName); } //========================================================================== diff --git a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java index ad8e2d0..ae1fc7a 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java +++ b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java @@ -81,7 +81,7 @@ public class HTTPUtils { * (OK). * @throws MalformedURLException */ - public static String get(String url, String username, String pw) throws MalformedURLException { + public static String get(String url, String username, String pw) { GetMethod httpMethod = null; HttpClient client = new HttpClient(); diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStyle.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStyle.java new file mode 100644 index 0000000..20e57ad --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTStyle.java @@ -0,0 +1,70 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2013 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package it.geosolutions.geoserver.rest.decoder; + +import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import org.jdom.Element; + +/** + * + * @author ETj (etj at geo-solutions.it) + */ +public class RESTStyle { + + private final Element elem; + + public static RESTStyle build(String xml) { + if (xml == null) { + return null; + } + + Element e = JDOMBuilder.buildElement(xml); + if (e != null) { + return new RESTStyle(e); + } else { + return null; + } + } + + protected RESTStyle(Element elem) { + this.elem = elem; + } + + public String getName() { + return elem.getChildText("name"); + } + + public String getFileName() { + return elem.getChildText("filename"); + } + + public String getWorkspace() { + if(elem.getChild("workspace") != null) + return elem.getChild("workspace").getChildText("name"); + else + return null; + } + +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java index dffa489..0745679 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java @@ -38,7 +38,7 @@ import java.net.URL; */ public abstract class GeoServerRESTAbstractManager { - protected final URL restURL; + protected final URL gsBaseUrl; protected final String gsuser; protected final String gspass; @@ -50,17 +50,21 @@ public abstract class GeoServerRESTAbstractManager { * @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 if restURL is malformed */ public GeoServerRESTAbstractManager(URL restURL, String username, String password) - throws IllegalArgumentException, MalformedURLException { - if (restURL == null || username == null || password == null) - throw new IllegalArgumentException("Unable to create the manager using a null argument"); + throws IllegalArgumentException { + try { + if (restURL == null || username == null || password == null) + throw new IllegalArgumentException("Unable to create the manager using a null argument"); - this.restURL = new URL(restURL.getProtocol(), restURL.getHost(), restURL.getPort(), - HTTPUtils.decurtSlash(restURL.getPath()), null); - - this.gsuser = username; - this.gspass = password; + this.gsBaseUrl = new URL(restURL.getProtocol(), restURL.getHost(), restURL.getPort(), + HTTPUtils.decurtSlash(restURL.getPath()), null); + + this.gsuser = username; + this.gspass = password; + + } catch (MalformedURLException ex) { + throw new IllegalArgumentException("URL can't be parsed properly", ex); + } } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java index 758c375..915e1a6 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStoreManager.java @@ -52,7 +52,7 @@ public class GeoServerRESTStoreManager extends GeoServerRESTAbstractManager { * @throws IllegalArgumentException */ public GeoServerRESTStoreManager(URL restURL, String username, String password) - throws IllegalArgumentException, MalformedURLException { + throws IllegalArgumentException { super(restURL, username, password); } @@ -68,7 +68,7 @@ public class GeoServerRESTStoreManager extends GeoServerRESTAbstractManager { * 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 sUrl = HTTPUtils.append(gsBaseUrl, "/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; @@ -84,7 +84,7 @@ public class GeoServerRESTStoreManager extends GeoServerRESTAbstractManager { * false otherwise */ public boolean update(String workspace, GSAbstractStoreEncoder store) { - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace,"/", store.getStoreType().toString(),"/", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace,"/", store.getStoreType().toString(),"/", store.getName(),".",Format.XML.toString()).toString(); String xml = store.toString(); String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); @@ -107,7 +107,7 @@ public class GeoServerRESTStoreManager extends GeoServerRESTAbstractManager { // if (workspace.isEmpty() || storename.isEmpty()) // throw new IllegalArgumentException("Arguments may not be empty!"); - final StringBuilder url=HTTPUtils.append(restURL,"/rest/workspaces/",workspace,"/", store.getStoreType().toString(), "/",store.getName()); + final StringBuilder url=HTTPUtils.append(gsBaseUrl,"/rest/workspaces/",workspace,"/", store.getStoreType().toString(), "/",store.getName()); if (recurse) url.append("?recurse=true"); final URL deleteStore = new URL(url.toString()); diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java index fa49b42..5da0c02 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStructuredGridCoverageReaderManager.java @@ -78,7 +78,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR * @throws IllegalArgumentException */ public GeoServerRESTStructuredGridCoverageReaderManager(URL restURL, String username, - String password) throws IllegalArgumentException, MalformedURLException { + String password) throws IllegalArgumentException { super(restURL, username, password); } @@ -142,7 +142,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR } // create URL - StringBuilder ss=HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", + StringBuilder ss=HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/file.imagemosaic"); switch(configureOpt){ case ALL: @@ -180,7 +180,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR checkString(path); // create URL - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/", UploadMethod.EXTERNAL.toString(), ".", format).toString(); // POST request @@ -251,7 +251,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR } // method - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores", "/", coverageStore, "/coverages/", coverage, "/index/granules?filter=", URLEncoder.encode(filter, "UTF-8")).toString(); if (!HTTPUtils.delete(sUrl, gsuser, gspass)) { @@ -322,7 +322,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR } // delete - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores", "/", coverageStore, "/coverages/", coverage, "/index/granules/", granuleId) .toString(); if (!HTTPUtils.delete(sUrl, gsuser, gspass)) { @@ -372,7 +372,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR checkString(coverageStore); // create URL and then call it - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index.xml").toString(); String result = HTTPUtils.get(sUrl, gsuser, gspass); if (result != null) { @@ -428,7 +428,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR // method boolean append = false; - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index/granules.xml").toString(); if (filter != null && !filter.isEmpty()) { append = true; @@ -488,7 +488,7 @@ public class GeoServerRESTStructuredGridCoverageReaderManager extends GeoServerR } // method - String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/", + String sUrl = HTTPUtils.append(gsBaseUrl, "/rest/workspaces/", workspace, "/coveragestores/", coverageStore, "/coverages/", coverage, "/index/granules/", id, ".xml").toString(); String result = HTTPUtils.get(sUrl, gsuser, gspass); if (result != null) { diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java new file mode 100644 index 0000000..d06492a --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java @@ -0,0 +1,575 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2013 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; +import it.geosolutions.geoserver.rest.HTTPUtils; +import it.geosolutions.geoserver.rest.decoder.RESTStyle; +import it.geosolutions.geoserver.rest.decoder.RESTStyleList; +import java.io.File; +import java.net.URL; +import java.net.URLEncoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author ETj (etj at geo-solutions.it) + */ +public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager { + + private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStyleManager.class); + + /** + * Default constructor. + * + * @param restURL GeoServer REST API endpoint + * @param username GeoServer REST API authorized username + * @param password GeoServer REST API password for the former username + */ + public GeoServerRESTStyleManager(URL restURL, String username, String password) + throws IllegalArgumentException { + super(restURL, username, password); + } + + /** + * Check if a Style exists in the configured GeoServer instance. + * @param name the name of the style to check for. + * @return true on HTTP 200, false on HTTP 404 + * @throws RuntimeException if any other HTTP code than 200 or 404 was retrieved. + */ + public boolean existsStyle(String name) throws RuntimeException { + String url = buildXmlUrl(null, name); + return HTTPUtils.exists(url, gsuser, gspass); + } + + /** + * Get summary info about all Styles. + * + * @return summary info about Styles as a {@link RESTStyleList} + */ + public RESTStyleList getStyles() { + String url = "/rest/styles.xml"; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving Styles list from " + url); + } + + String response = HTTPUtils.get(gsBaseUrl + url, gsuser, gspass); + return RESTStyleList.build(response); + } + + public RESTStyle getStyle(String name) { + String url = buildXmlUrl(null, name); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving Style " + name + " from " + url); + } + + String response = HTTPUtils.get(url, gsuser, gspass); + return RESTStyle.build(response); + } + + /** + * Get the SLD body of a Style. + */ + public String getSLD(String styleName) { + String url = buildUrl(null, styleName, ".sld"); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving SLD body from " + url); + } + return HTTPUtils.get( url, gsuser, gspass); + } + + + //========================================================================= + // Workspaces + //========================================================================= + + /** + * + * @since GeoServer 2.2 + */ + public boolean existsStyle(String workspace, String name) { + String url = buildXmlUrl(workspace, name); + return HTTPUtils.exists(url, gsuser, gspass); + } + + /** + * Get summary info about Styles in a workspace. + * + * @return summary info about Styles as a {@link RESTStyleList} + * @since GeoServer 2.2 + */ + public RESTStyleList getStyles(String workspace) { + String url = "/rest/workspaces/"+workspace+"/styles.xml"; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving Styles list from " + url); + } + + String response = HTTPUtils.get(gsBaseUrl + url, gsuser, gspass); + return RESTStyleList.build(response); + } + + /** + * + * @since GeoServer 2.2 + */ + public RESTStyle getStyle(String workspace, String name) { + String url = buildXmlUrl(workspace, name); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving Style " + name + " from " + url); + } + + String response = HTTPUtils.get(url, gsuser, gspass); + return RESTStyle.build(response); + } + + /** + * Get the SLD body of a Style. + * @since GeoServer 2.2 + */ + public String getSLD(String workspace, String name) { + String url = buildUrl(workspace, name, ".sld"); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("### Retrieving SLD body from " + url); + } + return HTTPUtils.get(url, gsuser, gspass); + } + + //========================================================================= + // Publishing + //========================================================================= + + /** + * Store and publish a Style. + * + * @param sldBody the full SLD document as a String. + * + * @return true if the operation completed successfully. + */ + public boolean publishStyle(String sldBody) { + /* + * This is the equivalent call with cUrl: + * + * {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ + * http://$GSIP:$GSPORT/$SERVLET/rest/styles} + */ + try { + return publishStyle(sldBody, null); + } catch (IllegalArgumentException e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getLocalizedMessage(), e); + } + } + return false; + } + + /** + * Store and publish a Style, assigning it a name. + * + * @param sldBody the full SLD document as a String. + * @param name the Style name. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the style body is null or empty. + */ + public boolean publishStyle(final String sldBody, final String name) + throws IllegalArgumentException { + /* + * This is the equivalent call with cUrl: + * + * {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ + * http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=name} + */ + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } + + String sUrl = buildPostUrl(null, name); + + final String result = HTTPUtils.post(sUrl, sldBody, "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } + + /** + * Store and publish a Style. + * + * @param sldFile the File containing the SLD document. + * + * @return true if the operation completed successfully. + */ + public boolean publishStyle(File sldFile) { + return publishStyle(sldFile, null); + } + + /** + * Store and publish a Style, assigning it a name. + * + * @param sldFile the File containing the SLD document. + * @param name the Style name. + * + * @return true if the operation completed successfully. + */ + public boolean publishStyle(File sldFile, String name) { + String sUrl = buildPostUrl(null, name); + LOGGER.debug("POSTing new style " + name + " to " + sUrl); + String result = HTTPUtils.post(sUrl, sldFile, GeoServerRESTPublisher.Format.SLD.getContentType(), gsuser, gspass); + return result != null; + } + + /** + * Update a Style. + * + * @param sldBody the new SLD document as a String. + * @param name the Style name to update. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the style body or name are null or empty. + */ + public boolean updateStyle(final String sldBody, final String name) + throws IllegalArgumentException { + /* + * This is the equivalent call with cUrl: + * + * {@code curl -u admin:geoserver -XPUT \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \ + * http://$GSIP:$GSPORT/$SERVLET/rest/styles/$NAME} + */ + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } else if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("The style name may not be null or empty"); + } + + final String sUrl = buildUrl(null, name, null); + + final String result = HTTPUtils.put(sUrl, sldBody, "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } + + /** + * Update a Style. + * + * @param sldFile the File containing the SLD document. + * @param name the Style name. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the sldFile file or name are null or name is empty. + */ + public boolean updateStyle(final File sldFile, final String name) + throws IllegalArgumentException { + + if (sldFile == null) { + throw new IllegalArgumentException("Unable to updateStyle using a null parameter file"); + } else if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("The style name may not be null or empty"); + } + + final String sUrl = buildUrl(null, name, null); + + final String result = HTTPUtils.put(sUrl, sldFile, + "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + + } + + /** + * Remove a Style. + *
+ * The Style will be unpublished, and (optionally) the SLD file will be removed. + * + * @param styleName the name of the Style to remove. + * @param purge remove the related SLD file from disk. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if styleName is null or empty. + */ + public boolean removeStyle(String styleName, final boolean purge) + throws IllegalArgumentException { + if (styleName == null || styleName.isEmpty()) + throw new IllegalArgumentException( + "Check styleName parameter, it may never be null or empty"); + + // check style name + // TODO may we want to throw an exception instead of + // change style name? + if(styleName.contains(":")) + LOGGER.warn("Style name is going to be changed ["+styleName+"]"); + styleName = styleName.replaceAll(":", "_"); + styleName = URLEncoder.encode(styleName); + + String sUrl = buildUrl(null, styleName, null); + if (purge) { + sUrl += "?purge=true"; + } + + return HTTPUtils.delete(sUrl, gsuser, gspass); + } + + /** + * Remove a Style. + *
+ * The Style will be unpublished and the related SLD file will be removed. + * + * @param styleName the name of the Style to remove. + * + * @return true if the operation completed successfully. + */ + public boolean removeStyle(String styleName) { + try { + return removeStyle(styleName, true); + } catch (IllegalArgumentException e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getLocalizedMessage(), e); + } + } + return false; + } + + //========================================================================= + // Publishing in workspace + //========================================================================= + + /** + * Store and publish a Style. + * + * @param sldBody the full SLD document as a String. + * + * @return true if the operation completed successfully. + * @since GeoServer 2.2 + */ + public boolean publishStyleInWorkspace(final String workspace, String sldBody) { + try { + return publishStyleInWorkspace(workspace, sldBody); + } catch (IllegalArgumentException e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getLocalizedMessage(), e); + } + } + return false; + } + + /** + * Store and publish a Style, assigning it a name. + * + * @param sldBody the full SLD document as a String. + * @param name the Style name. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the style body is null or empty. + * @since GeoServer 2.2 + */ + public boolean publishStyleInWorkspace(final String workspace, final String sldBody, final String name) + throws IllegalArgumentException { + + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } + String sUrl = buildPostUrl(workspace, name); + final String result = HTTPUtils.post(sUrl, sldBody, "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } + + /** + * Store and publish a Style. + * + * @param sldFile the File containing the SLD document. + * + * @return true if the operation completed successfully. + * @since GeoServer 2.2 + */ + public boolean publishStyleInWorkspace(final String workspace, File sldFile) { + return publishStyleInWorkspace(workspace, sldFile, null); + } + + /** + * Store and publish a Style, assigning it a name. + * + * @param sldFile the File containing the SLD document. + * @param name the Style name. + * + * @return true if the operation completed successfully. + * @since GeoServer 2.2 + */ + public boolean publishStyleInWorkspace(final String workspace, File sldFile, String name) { + String sUrl = buildPostUrl(workspace, name); + LOGGER.debug("POSTing new style " + name + " to " + sUrl); + String result = HTTPUtils.post(sUrl, sldFile, GeoServerRESTPublisher.Format.SLD.getContentType(), gsuser, gspass); + return result != null; + } + + /** + * Update a Style. + * + * @param sldBody the new SLD document as a String. + * @param name the Style name to update. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the style body or name are null or empty. + * @since GeoServer 2.2 + */ + public boolean updateStyleInWorkspace(final String workspace, final String sldBody, final String name) + throws IllegalArgumentException { + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } else if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("The style name may not be null or empty"); + } + + final String sUrl = buildUrl(workspace, name, null); + + final String result = HTTPUtils.put(sUrl, sldBody, + "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } + + /** + * Update a Style. + * + * @param sldFile the File containing the SLD document. + * @param name the Style name. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the sldFile file or name are null or name is empty. + * @since GeoServer 2.2 + */ + public boolean updateStyleInWorkspace(final String workspace, final File sldFile, final String name) + throws IllegalArgumentException { + + if (sldFile == null) { + throw new IllegalArgumentException("Unable to updateStyle using a null parameter file"); + } else if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("The style name may not be null or empty"); + } + + final String sUrl = buildUrl(workspace, name, null); + + final String result = HTTPUtils.put(sUrl, sldFile, + "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } + + /** + * Remove a Style. + *
+ * The Style will be unpublished, and (optionally) the SLD file will be removed. + * + * @param styleName the name of the Style to remove. + * @param purge remove the related SLD file from disk. + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if styleName is null or empty. + * @since GeoServer 2.2 + */ + public boolean removeStyleInWorkspace(final String workspace, String styleName, final boolean purge) + throws IllegalArgumentException { + if (styleName == null || styleName.isEmpty()) + throw new IllegalArgumentException( + "Check styleName parameter, it may never be null or empty"); + + // check style name + // TODO may we want to throw an exception instead of change style name? + if(styleName.contains(":")) + LOGGER.warn("Style name is going to be changed ["+styleName+"]"); + styleName = styleName.replaceAll(":", "_"); + styleName = URLEncoder.encode(styleName); + + String sUrl = buildUrl(workspace, styleName, null); + + if (purge) { + sUrl += "?purge=true"; + } + + return HTTPUtils.delete(sUrl, gsuser, gspass); + } + + /** + * Remove a Style. + *
+ * The Style will be unpublished and the related SLD file will be removed. + * + * @param styleName the name of the Style to remove. + * + * @return true if the operation completed successfully. + * @since GeoServer 2.2 + */ + public boolean removeStyleInWorkspace(final String workspace, String styleName) { + try { + return removeStyleInWorkspace(workspace, styleName, true); + } catch (IllegalArgumentException e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getLocalizedMessage(), e); + } + } + return false; + } + + //========================================================================= + // Util methods + //========================================================================= + + /** + * Creates a URL for the given stylename with the name in querystring + * @param workspace nullable workspace name + * @param name style name + * @return + */ + protected String buildPostUrl(final String workspace, String name) { + StringBuilder sUrl = new StringBuilder(gsBaseUrl.toString()).append("/rest"); + + if(workspace != null) + sUrl.append("/workspaces/").append(workspace); + + sUrl.append("/styles"); + if ( name != null && !name.isEmpty()) { + sUrl.append("?name=").append(URLEncoder.encode(name)); + } + return sUrl.toString(); + } + + + protected String buildXmlUrl(final String workspace, final String name) { + return buildUrl(workspace, name, ".xml"); + } + + /** + * Creates a URL for the given stylename with the name in the REST path + * @param workspace nullable workspace name + * @param name style name + * @param ext nullable output extension (e.g. ".xml" ".sld") + */ + protected String buildUrl(final String workspace, final String name, final String ext) { + StringBuilder sUrl = new StringBuilder(gsBaseUrl.toString()).append("/rest"); + + if(workspace != null) + sUrl.append("/workspaces/").append(workspace); + + sUrl.append("/styles/").append(URLEncoder.encode(name)); + + if(ext != null) + sUrl.append(ext); + + return sUrl.toString(); + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java b/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java index 01d2a8a..ade73bf 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java @@ -36,16 +36,13 @@ import java.io.IOException; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; import org.junit.Test; +import static org.junit.Assert.*; 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. - * Login credentials are hardcoded at the moment (localhost:8080 admin/geoserver). - * If such geoserver instance cannot be contacted, tests will be skipped. - * * * @author etj */ @@ -54,25 +51,6 @@ public class ConfigTest extends GeoserverRESTTest { private static final String DEFAULT_WS = "geosolutions"; -// @Test -// public void testEtj() throws FileNotFoundException, IOException { -// if(!enabled()){ -// LOGGER.info("Skipping test "+"testEtj"+"for class:"+this.getClass().getSimpleName()); -// return; -// } -// deleteAll(); -// -// assertTrue(reader.getWorkspaces().isEmpty()); -// assertTrue(publisher.createWorkspace(DEFAULT_WS)); -// -// insertStyles(); -// insertExternalGeotiff(); -// insertExternalShape(); -// -// boolean ok = publisher.publishDBLayer(DEFAULT_WS, "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon"); -//// assertTrue(ok); -// } - @Test public void insertStyles() throws FileNotFoundException, IOException { if(!enabled()){ diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTReaderTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTReaderTest.java index b3d2717..0656f24 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTReaderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTReaderTest.java @@ -35,6 +35,7 @@ import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import java.util.List; import org.junit.Test; +import static org.junit.Assert.*; /** diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java index 1d3902e..59d769d 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java @@ -37,8 +37,12 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.List; -import org.junit.Assert; +import static org.junit.Assert.*; +import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.rules.TestName; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,9 +61,12 @@ import org.slf4j.LoggerFactory; * @author etj * @author carlo cancellieri - GeoSolutions */ -public abstract class GeoserverRESTTest extends Assert { +public abstract class GeoserverRESTTest { private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTTest.class); + @Rule + public TestName _testName = new TestName(); + public static final String DEFAULT_WS = "geosolutions"; public static final String RESTURL; @@ -102,9 +109,9 @@ public abstract class GeoserverRESTTest extends Assert { private static String getenv(String envName, String envDefault) { String env = System.getenv(envName); - String ret = System.getProperty(envName, env); - LOGGER.debug("env var " + envName + " is " + ret); - return ret != null ? ret : envDefault; + String prop = System.getProperty(envName, env); + LOGGER.debug("varname " + envName + " --> env:" + env + " prop:"+prop); + return prop != null ? prop : envDefault; } @BeforeClass @@ -128,9 +135,19 @@ public abstract class GeoserverRESTTest extends Assert { } } else { System.out.println("Skipping tests "); + LOGGER.warn("Tests are disabled. Please read the documentation to enable them."); } } + @Before + public void before(){ + String testName = _testName.getMethodName(); + LOGGER.warn(""); + LOGGER.warn("============================================================"); + LOGGER.warn("=== RUNNING TEST " + testName); + LOGGER.warn(""); + } + protected boolean enabled() { return enabled; } diff --git a/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java b/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java index 73148b5..42b0442 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/datastore/StoreIntegrationTest.java @@ -35,6 +35,8 @@ import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; import java.net.MalformedURLException; import org.junit.Test; +import static org.junit.Assert.*; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerGroupEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerGroupEncoderTest.java index 52e22fa..f626bff 100755 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerGroupEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerGroupEncoderTest.java @@ -37,6 +37,7 @@ import java.util.List; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.*; import org.springframework.core.io.ClassPathResource; /** diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java index 4c592bd..491bf0a 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java @@ -20,6 +20,7 @@ package it.geosolutions.geoserver.rest.encoder.feature; import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTLayer; import it.geosolutions.geoserver.rest.decoder.RESTResource; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; @@ -32,7 +33,6 @@ import it.geosolutions.geoserver.rest.encoder.metadata.virtualtable.VTGeometryEn import it.geosolutions.geoserver.rest.encoder.metadata.virtualtable.VTParameterEncoder; import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder; import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; -import it.geosolutions.geoserver.rest.publisher.GeoserverRESTPublisherTest; import java.io.File; import java.io.IOException; @@ -40,8 +40,8 @@ import java.math.BigDecimal; import java.util.List; import org.jdom.Element; -import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; @@ -57,7 +57,7 @@ import org.springframework.core.io.ClassPathResource; * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | * emmanuel.blondel@fao.org */ -public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { +public class GSFeatureEncoderTest extends GeoserverRESTTest { protected final static Logger LOGGER = LoggerFactory.getLogger(GSFeatureEncoderTest.class); @Test @@ -157,33 +157,33 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { encoder.setMetadataDimension("elevation", dim2); dim2.setPresentation(Presentation.DISCRETE_INTERVAL, BigDecimal.valueOf(10)); Element el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); - Assert.assertNotNull(el); + assertNotNull(el); LOGGER.info("contains_key:" + el.toString()); dim2.setPresentation(Presentation.DISCRETE_INTERVAL, BigDecimal.valueOf(12)); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); - Assert.assertNotNull(el); - Assert.assertEquals("12", el.getText()); + assertNotNull(el); + assertEquals("12", el.getText()); dim2.setPresentation(Presentation.CONTINUOUS_INTERVAL); encoder.setMetadataDimension("time", new GSFeatureDimensionInfoEncoder("time")); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); - Assert.assertNotNull(el); + assertNotNull(el); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); - Assert.assertNull(el); + assertNull(el); el = ElementUtils.contains(encoder.getRoot(), GSResourceEncoder.METADATA); - Assert.assertNotNull(el); + assertNotNull(el); LOGGER.info("contains_key:" + el.toString()); final boolean removed = ElementUtils.remove(encoder.getRoot(), el); LOGGER.info("remove:" + removed); - Assert.assertTrue(removed); + assertTrue(removed); el = ElementUtils.contains(encoder.getRoot(), "metadata"); - Assert.assertNull(el); + assertNull(el); if (el == null) LOGGER.info("REMOVED"); @@ -201,15 +201,15 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { encoder.addKeyword("..."); encoder.addKeyword("KEYWORD_N"); - Assert.assertTrue(encoder.delKeyword("KEYWORD_2")); - Assert.assertFalse(encoder.delKeyword("KEYWORD_M")); + assertTrue(encoder.delKeyword("KEYWORD_2")); + assertFalse(encoder.delKeyword("KEYWORD_M")); //metadataLinkInfo encoder.addMetadataLinkInfo("text/xml", "ISO19115:2003","http://www.organization.org/metadata1"); encoder.addMetadataLinkInfo("text/html", "ISO19115:2003","http://www.organization.org/metadata2"); - Assert.assertTrue(encoder.delMetadataLinkInfo("http://www.organization.org/metadata2")); - Assert.assertFalse(encoder.delMetadataLinkInfo("http://www.organization.org/metadata3")); + assertTrue(encoder.delMetadataLinkInfo("http://www.organization.org/metadata2")); + assertFalse(encoder.delMetadataLinkInfo("http://www.organization.org/metadata3")); //dimensions final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder( @@ -227,14 +227,14 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { if (LOGGER.isInfoEnabled()) LOGGER.info(encoder.toString()); - Assert.assertTrue(encoder.delMetadata(metadata)); + assertTrue(encoder.delMetadata(metadata)); if (LOGGER.isInfoEnabled()) LOGGER.info(encoder.toString()); final Element el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.DIMENSIONINFO); - Assert.assertNull(el); + assertNull(el); if (el == null) LOGGER.info("REMOVED"); @@ -315,8 +315,8 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { //------------ publisher.createWorkspace(DEFAULT_WS); boolean published = publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder); - assertTrue("Successfull publication", published); - assertTrue(existsLayer(layerName)); + assertTrue("Publication unsuccessful", published); + assertTrue("Layer does not exist", existsLayer(layerName)); } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java index d46987b..a94759b 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTImageMosaicManagerTest.java @@ -16,6 +16,7 @@ import java.util.Map; import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; 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 0d7b750..a7f916b 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/manager/GeoserverRESTDatastoreManagerTest.java @@ -36,6 +36,7 @@ import java.nio.charset.Charset; import java.util.Map; import org.junit.Test; +import static org.junit.Assert.*; /** * Test datastore handling (create, read and update): diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java index 13c3630..fe79109 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java @@ -35,6 +35,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java index 9b655aa..491cfff 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java @@ -41,6 +41,7 @@ import java.io.IOException; import org.apache.commons.httpclient.NameValuePair; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTNamespaceTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTNamespaceTest.java index a883ca2..cc30caf 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTNamespaceTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTNamespaceTest.java @@ -30,6 +30,7 @@ import it.geosolutions.geoserver.rest.decoder.RESTNamespace; import java.net.URI; import org.junit.Test; +import static org.junit.Assert.*; /** * Testcase for namespace management. 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 f6a1037..0da4c25 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublishShpCollectionTest.java @@ -30,6 +30,7 @@ import java.net.URI; import java.util.List; import org.junit.Test; +import static org.junit.Assert.*; import org.springframework.core.io.ClassPathResource; /** diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java index 5af61da..be868d4 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java @@ -32,6 +32,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 76ab08e..3611d73 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java @@ -39,6 +39,7 @@ 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java index b29abca..1e2afac 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * Copyright (C) 2007,2013 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -26,7 +26,10 @@ package it.geosolutions.geoserver.rest.publisher; import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import static it.geosolutions.geoserver.rest.GeoserverRESTTest.publisher; +import static it.geosolutions.geoserver.rest.GeoserverRESTTest.reader; import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.decoder.RESTStyle; import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; @@ -39,6 +42,7 @@ import org.apache.commons.io.IOUtils; import org.jdom.Element; import org.jdom.Namespace; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; @@ -64,18 +68,22 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest { assertEquals(0, reader.getStyles().size()); - final String styleName = "restteststyle"; + final String STYLENAME = "restteststyle"; File sldFile = new ClassPathResource("testdata/restteststyle.sld") .getFile(); // insert style assertTrue(publisher.publishStyle(sldFile)); - assertTrue(reader.existsStyle(styleName)); + assertTrue(reader.existsStyle(STYLENAME)); assertFalse(publisher.publishStyle(sldFile)); - assertTrue(reader.existsStyle(styleName)); + assertTrue(reader.existsStyle(STYLENAME)); - String sld = reader.getSLD(styleName); + RESTStyle style = reader.getStyle(STYLENAME); + assertEquals(STYLENAME, style.getName()); + assertNull(style.getWorkspace()); + + String sld = reader.getSLD(STYLENAME); assertNotNull(sld); Element styleEl = JDOMBuilder.buildElement(sld); @@ -86,7 +94,7 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest { try { - assertEquals(styleName, styleEl.getChild("NamedLayer", SLDNS) + assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS) .getChild("Name", SLDNS).getText()); assertEquals( "STYLE FOR TESTING PURPOSES", @@ -259,4 +267,88 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest { true); assertTrue("removeDatastore() failed", dsRemoved); } + + @Test + public void testStylesInWorkspace() throws IOException { + if (!enabled()) + return; + deleteAll(); + + final String WORKSPACE = "testWorkspace"; + final String STYLENAME = "restteststyle"; + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + publisher.createWorkspace(WORKSPACE); + + assertEquals(0, reader.getStyles().size()); + assertEquals(0, reader.getStyles(WORKSPACE).size()); + + + // insert style + assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile)); + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + assertFalse(reader.existsStyle(STYLENAME)); + + // insert style again + assertFalse(publisher.publishStyleInWorkspace(WORKSPACE, sldFile)); + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + assertFalse(reader.existsStyle(STYLENAME)); + + String sld = reader.getSLD(WORKSPACE, STYLENAME); + assertNotNull(sld); + + RESTStyle style = reader.getStyle(WORKSPACE, STYLENAME); + assertEquals(STYLENAME, style.getName()); + assertEquals(WORKSPACE, style.getWorkspace()); + + Element styleEl = JDOMBuilder.buildElement(sld); + assertNotNull(styleEl); + + Namespace SLDNS = Namespace.getNamespace("sld", + "http://www.opengis.net/sld"); + + try { + + assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS) + .getChild("Name", SLDNS).getText()); + assertEquals( + "STYLE FOR TESTING PURPOSES", + styleEl.getChild("NamedLayer", SLDNS) + .getChild("UserStyle", SLDNS) + .getChild("Title", SLDNS).getText()); + } catch (NullPointerException npe) { + fail("Error in SLD"); + } + + // assertEquals(1475, sld.length()); + + assertEquals(0, reader.getStyles().size()); + assertEquals(1, reader.getStyles(WORKSPACE).size()); + } + + @Test + public void testRemoveStylesInWorkspace() throws IOException { + if (!enabled()) + return; + deleteAll(); + + final String WORKSPACE = "testWorkspace"; + final String STYLENAME = "restteststyle"; + final File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + publisher.createWorkspace(WORKSPACE); + + assertEquals(0, reader.getStyles(WORKSPACE).size()); + + // insert style + assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile)); + assertEquals(1, reader.getStyles(WORKSPACE).size()); + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + + // remove style + assertTrue(publisher.removeStyleInWorkspace(WORKSPACE, STYLENAME, true)); + assertEquals(0, reader.getStyles(WORKSPACE).size()); + assertFalse(reader.existsStyle(WORKSPACE, STYLENAME)); + } + } diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java index 244c93d..79c104d 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java @@ -32,6 +32,7 @@ import java.io.File; import java.io.IOException; import org.junit.Test; +import static org.junit.Assert.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java index 9491362..ec176c8 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java @@ -33,6 +33,8 @@ import java.io.IOException; import org.apache.commons.httpclient.NameValuePair; import org.junit.Test; +import static org.junit.Assert.*; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource;