From 2f0e6ccb053a214a98172d7eaf7a6eb72afc9912 Mon Sep 17 00:00:00 2001 From: ccancellieri Date: Thu, 2 Feb 2012 10:48:38 +0100 Subject: [PATCH] publisher improvements to fix issue #4 --- .../rest/GeoServerRESTPublisher.java | 144 ++++++-- .../publisher/GeoserverRESTStyleTest.java | 315 ++++++++++-------- 2 files changed, 287 insertions(+), 172 deletions(-) diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 3a9f47a..0cb8277 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -132,10 +132,10 @@ public class GeoServerRESTPublisher { */ public boolean publishStyle(String sldBody) { try { - return publishStyle(sldBody,null); - } catch (IllegalArgumentException e){ - if (LOGGER.isEnabledFor(Level.ERROR)){ - LOGGER.error(e.getLocalizedMessage(),e); + return publishStyle(sldBody, null); + } catch (IllegalArgumentException e) { + if (LOGGER.isEnabledFor(Level.ERROR)) { + LOGGER.error(e.getLocalizedMessage(), e); } } return false; @@ -162,13 +162,15 @@ public class GeoServerRESTPublisher { * @throws IllegalArgumentException * if the style body is null or empty */ - public boolean publishStyle(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"); + public boolean publishStyle(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"); } StringBuilder sUrl = new StringBuilder(restURL); sUrl.append("/rest/styles"); - if (name!=null && !name.isEmpty()){ + if (name != null && !name.isEmpty()) { sUrl.append("?name=").append(name); } final String result = HTTPUtils.post(sUrl.toString(), sldBody, @@ -209,6 +211,81 @@ public class GeoServerRESTPublisher { return result != null; } + /** + * Update SLD called as 'name'. + *

+ * 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}
+	 * 
+ * + * @param sldBody + * the SLD document as an XML String. + * @param name + * the Style name to modify. + * + * @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 { + 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; + } + + /** + * Update an SLD called 'name'. + * + * @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 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; + + } + /** * Remove a Style.
* @@ -232,7 +309,7 @@ public class GeoServerRESTPublisher { final StringBuffer sUrl = new StringBuffer(restURL); - // check style name + // check style name // TODO may we whant to throw an exception instead of // change style name? styleName = styleName.replaceAll(":", "_"); @@ -259,9 +336,9 @@ public class GeoServerRESTPublisher { public boolean removeStyle(String styleName) { try { return removeStyle(styleName, true); - } catch (IllegalArgumentException e){ - if (LOGGER.isEnabledFor(Level.ERROR)){ - LOGGER.error(e.getLocalizedMessage(),e); + } catch (IllegalArgumentException e) { + if (LOGGER.isEnabledFor(Level.ERROR)) { + LOGGER.error(e.getLocalizedMessage(), e); } } return false; @@ -466,7 +543,7 @@ public class GeoServerRESTPublisher { * @param layername * @param srs * @param defaultStyle - * @return + * @return */ public boolean publishDBLayer(String workspace, String storename, String layername, String srs, String defaultStyle) { @@ -649,7 +726,8 @@ public class GeoServerRESTPublisher { if (configure != null) { sbUrl.append("?configure=").append(configure); - if (params != (NameValuePair[])null && !configure.equals(ParameterConfigure.NONE)) { + if (params != (NameValuePair[]) null + && !configure.equals(ParameterConfigure.NONE)) { final String paramString = appendParameters(params); if (!paramString.isEmpty()) { sbUrl.append("&").append(paramString); @@ -1747,35 +1825,35 @@ public class GeoServerRESTPublisher { final int paramsSize = params.length; if (paramsSize > 0) { int i = 0; - NameValuePair param=params[i]; - while (param!=null && i++STYLE FOR TESTING PURPOSES", + "MODIFIED STYLE FOR TESTING"); + published = publisher.updateStyle(newSldContent, styleName); // update + assertTrue("publish() failed", published); - final String styleName = "restteststyle"; - { - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - cleanupTestStyle(styleName); - boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents - assertTrue("style publish() failed", sldpublished); - assertTrue(reader.existsStyle(styleName)); - } + // test delete + ok = publisher.removeStyle(styleName); + assertTrue("Unpublish() failed", ok); + assertFalse(reader.existsStyle(styleName)); - final String styleName2 = "restteststyle2"; - { - File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile(); - cleanupTestStyle(styleName2); - boolean sldpublished = publisher.publishStyle(sldFile,styleName2); - assertTrue("style publish() failed", sldpublished); - assertTrue(reader.existsStyle(styleName2)); - } + } - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + public void testUpdateDefaultStyle() throws FileNotFoundException, + IOException { + if (!enabled()) { + return; + } + deleteAll(); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile, "EPSG:4326", styleName); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); + String storeName = "resttestshp"; + String layerName = "cities"; - { - RESTLayer layer = reader.getLayer(layerName); - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals(styleName, layer.getDefaultStyle()); - } + final String styleName = "restteststyle"; + { + File sldFile = new ClassPathResource("testdata/restteststyle.sld") + .getFile(); + cleanupTestStyle(styleName); + boolean sldpublished = publisher.publishStyle(sldFile); // Will take + // the name + // from sld + // contents + assertTrue("style publish() failed", sldpublished); + assertTrue(reader.existsStyle(styleName)); + } - GSLayerEncoder le = new GSLayerEncoder(); - le.setDefaultStyle(styleName2); - publisher.configureLayer(DEFAULT_WS, layerName, le); + final String styleName2 = "restteststyle2"; + { + File sldFile = new ClassPathResource("testdata/restteststyle2.sld") + .getFile(); + cleanupTestStyle(styleName2); + boolean sldpublished = publisher.publishStyle(sldFile, styleName2); + assertTrue("style publish() failed", sldpublished); + assertTrue(reader.existsStyle(styleName2)); + } - { - RESTLayer layer = reader.getLayer(layerName); - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals(styleName2, layer.getDefaultStyle()); - } + File zipFile = new ClassPathResource("testdata/resttestshp.zip") + .getFile(); - // remove layer and datastore - boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, true); - assertTrue("removeDatastore() failed", dsRemoved); - } + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, + layerName, zipFile, "EPSG:4326", styleName); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + { + RESTLayer layer = reader.getLayer(layerName); + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + assertEquals(styleName, layer.getDefaultStyle()); + } + + GSLayerEncoder le = new GSLayerEncoder(); + le.setDefaultStyle(styleName2); + publisher.configureLayer(DEFAULT_WS, layerName, le); + + { + RESTLayer layer = reader.getLayer(layerName); + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + assertEquals(styleName2, layer.getDefaultStyle()); + } + + // remove layer and datastore + boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, + true); + assertTrue("removeDatastore() failed", dsRemoved); + } }