diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 3804042..b3a3fcd 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -486,6 +486,14 @@ public class GeoServerRESTPublisher { public boolean publishStyleInWorkspace(String workspace, String sldBody, String name) throws IllegalArgumentException { return styleManager.publishStyleInWorkspace(workspace, sldBody, name); } + + /** + * @since GeoServer 2.2 + * @see GeoServerRESTStyleManager# + */ + public boolean publishStyleInWorkspace(String workspace, String sldBody, String name, boolean raw) throws IllegalArgumentException { + return styleManager.publishStyleInWorkspace(workspace, sldBody, name, raw); + } /** * @since GeoServer 2.2 @@ -502,6 +510,14 @@ public class GeoServerRESTPublisher { public boolean publishStyleInWorkspace(String workspace, File sldFile, String name) { return styleManager.publishStyleInWorkspace(workspace, sldFile, name); } + + /** + * @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, boolean raw) { + return styleManager.publishStyleInWorkspace(workspace, sldFile, name, raw); + } /** * @since GeoServer 2.2 @@ -510,6 +526,14 @@ public class GeoServerRESTPublisher { 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.lang.String, java.lang.String) + */ + public boolean updateStyleInWorkspace(String workspace, String sldBody, String name, boolean raw) throws IllegalArgumentException { + return styleManager.updateStyleInWorkspace(workspace, sldBody, name, raw); + } /** * @since GeoServer 2.2 @@ -518,6 +542,14 @@ public class GeoServerRESTPublisher { public boolean updateStyleInWorkspace(String workspace, File sldFile, String name) throws IllegalArgumentException { return styleManager.updateStyleInWorkspace(workspace, sldFile, 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, boolean raw) throws IllegalArgumentException { + return styleManager.updateStyleInWorkspace(workspace, sldFile, name, raw); + } /** * @since GeoServer 2.2 diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java index 5c72ff9..0a8d51c 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java @@ -37,6 +37,7 @@ import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; +import java.util.LinkedHashMap; /** * Layer encoder for Geoserver = 2.1 @@ -102,7 +103,7 @@ public class GSLayerEncoder21 extends GSLayerEncoder { */ public void addAuthorityURL(GSAuthorityURLInfoEncoder authorityURLInfo){ if(authorityURLList == null){ - authorityURLList = new HashMap(); + authorityURLList = new LinkedHashMap(); } authorityURLList.put(authorityURLInfo.getHref(), authorityURLInfo.getName()); String jsonStr = ""; @@ -152,7 +153,7 @@ public class GSLayerEncoder21 extends GSLayerEncoder { */ public void addIdentifier(GSIdentifierInfoEncoder identifierInfo){ if(identifierList == null){ - identifierList = new HashMap>(); + identifierList = new LinkedHashMap>(); } String authority = identifierInfo.getAuthority(); diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java index 00dc28d..3f98fac 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTStyleManager.java @@ -55,737 +55,833 @@ import org.xml.sax.SAXException; */ 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); - } + private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStyleManager.class); - /** - * 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 { - return existsStyle(name, Util.DEFAULT_QUIET_ON_NOT_FOUND); - } - - /** - * Check if a Style exists in the configured GeoServer instance. User can choose if log a possible exception or not - * @param name the name of the style to check for. - * @param quietOnNotFound if true, mute exception if false is returned - * @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, boolean quietOnNotFound) { - String url = buildXmlUrl(null, name); - String composed = Util.appendQuietOnNotFound(quietOnNotFound, url); - return HTTPUtils.exists(composed , gsuser, gspass); - } + /** + * 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); + } - /** - * 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); - } + /** + * 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 { + return existsStyle(name, Util.DEFAULT_QUIET_ON_NOT_FOUND); + } - String response = HTTPUtils.get(gsBaseUrl + url, gsuser, gspass); - return RESTStyleList.build(response); - } + /** + * Check if a Style exists in the configured GeoServer instance. User can choose if log a + * possible exception or not + * + * @param name the name of the style to check for. + * @param quietOnNotFound if true, mute exception if false is returned + * @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, boolean quietOnNotFound) { + String url = buildXmlUrl(null, name); + String composed = Util.appendQuietOnNotFound(quietOnNotFound, url); + return HTTPUtils.exists(composed, gsuser, gspass); + } - public RESTStyle getStyle(String name) { - String url = buildXmlUrl(null, name); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("### Retrieving Style " + name + " from " + url); - } + /** + * 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(url, gsuser, gspass); - return RESTStyle.build(response); - } + String response = HTTPUtils.get(gsBaseUrl + url, gsuser, gspass); + return RESTStyleList.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); - } + 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 - //========================================================================= + // Workspaces + //========================================================================= + /** + * + * @since GeoServer 2.2 + */ + public boolean existsStyle(String workspace, String name) { + return existsStyle(workspace, name, Util.DEFAULT_QUIET_ON_NOT_FOUND); + } - /** - * - * @since GeoServer 2.2 - */ - public boolean existsStyle(String workspace, String name) { - return existsStyle(workspace, name, Util.DEFAULT_QUIET_ON_NOT_FOUND); - } - - /** - * - * @since GeoServer 2.6 - */ - public boolean existsStyle(String workspace, String name, boolean quietOnNotFound) { - String url = buildXmlUrl(workspace, name); - String composed = Util.appendQuietOnNotFound(quietOnNotFound, url); - return HTTPUtils.exists(composed , gsuser, gspass); - } + /** + * + * @since GeoServer 2.6 + */ + public boolean existsStyle(String workspace, String name, boolean quietOnNotFound) { + String url = buildXmlUrl(workspace, name); + String composed = Util.appendQuietOnNotFound(quietOnNotFound, url); + return HTTPUtils.exists(composed, 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); - } + /** + * 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); - } + 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); - } + /** + * + * @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); - } + 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); - } + /** + * 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 - //========================================================================= + // 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. - * - * @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"); + } - /** - * 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); - String sUrl = buildPostUrl(null, name); + final String result = HTTPUtils.post(sUrl, sldBody, "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } - 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. - * - * @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; + } - /** - * 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; - } - - /** - * Store and publish a Style, assigning it a name and choosing the raw - * format. - * - * @param sldBody the full SLD document as a String. - * @param name the Style name. - * @param raw the raw format - * - * @return true if the operation completed successfully. - */ - public boolean publishStyle(final String sldBody, final String name, final boolean raw) { - /* - * 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&raw=$raw} - */ - if (sldBody == null || sldBody.isEmpty()) { - throw new IllegalArgumentException("The style body may not be null or empty"); - } - - StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name)); - Util.appendParameter(sUrl, "raw", ""+raw); - String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); - if(!this.checkSLD10Version(sldBody)){ - contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); - } - LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType); - String result = HTTPUtils.post(sUrl.toString(), sldBody, contentType, gsuser, gspass); - return result != null; - } - - /** - * Store and publish a Style, assigning it a name and choosing the raw - * format. - * - * @param sldFile the File containing the SLD document. - * @param name the Style name. - * @param raw the raw format - * - * @return true if the operation completed successfully. - */ - public boolean publishStyle(final File sldFile, final String name, final boolean raw) { - /* - * 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&raw=$raw} - */ - StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name)); - Util.appendParameter(sUrl, "raw", ""+raw); - String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); - if(!this.checkSLD10Version(sldFile)){ - contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); - } - LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType); - String result = HTTPUtils.post(sUrl.toString(), sldFile, contentType, gsuser, gspass); - return result != null; - } - - /** - * Update a Style. - * - * @param sldFile the File containing the SLD document. - * @param name the Style name. - * @param raw the raw format - * - * @return true if the operation completed successfully. - * @throws IllegalArgumentException if the style body or name are null or empty. - */ - public boolean updateStyle(final File sldFile, final String name, final boolean raw) - 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=$name&raw=$raw} - */ - 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"); - } - - StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null)); - Util.appendParameter(sUrl, "raw", ""+raw); - String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); - if(!this.checkSLD10Version(sldFile)){ - contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); - } - LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType); - String result = HTTPUtils.put(sUrl.toString(), sldFile, contentType, gsuser, gspass); - return result != null; - } - - /** - * Update a Style. - * - * @param sldBody the new SLD document as a String. - * @param name the Style name. - * @param raw the raw format - * - * @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, final boolean raw) - 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=$name&raw=$raw} - */ - 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"); - } - - StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null)); - Util.appendParameter(sUrl, "raw", ""+raw); - String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); - if(!this.checkSLD10Version(sldBody)){ - contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); - } - LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType); - String result = HTTPUtils.put(sUrl.toString(), sldBody, contentType, gsuser, gspass); - return result != null; - } + /** + * Store and publish a Style, assigning it a name and choosing the raw format. + * + * @param sldBody the full SLD document as a String. + * @param name the Style name. + * @param raw the raw format + * + * @return true if the operation completed successfully. + */ + public boolean publishStyle(final String sldBody, final String name, final boolean raw) { + /* + * 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&raw=$raw} + */ + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } - /** - * 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"); - } + StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name)); + Util.appendParameter(sUrl, "raw", "" + raw); + String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); + if (!this.checkSLD10Version(sldBody)) { + contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); + } + LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType); + String result = HTTPUtils.post(sUrl.toString(), sldBody, contentType, gsuser, gspass); + return result != null; + } - final String sUrl = buildUrl(null, name, null); + /** + * Store and publish a Style, assigning it a name and choosing the raw format. + * + * @param sldFile the File containing the SLD document. + * @param name the Style name. + * @param raw the raw format + * + * @return true if the operation completed successfully. + */ + public boolean publishStyle(final File sldFile, final String name, final boolean raw) { + /* + * 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&raw=$raw} + */ + StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name)); + Util.appendParameter(sUrl, "raw", "" + raw); + String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); + if (!this.checkSLD10Version(sldFile)) { + contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); + } + LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType); + String result = HTTPUtils.post(sUrl.toString(), sldFile, contentType, gsuser, gspass); + return result != 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. + * @param raw the raw format + * + * @return true if the operation completed successfully. + * @throws IllegalArgumentException if the style body or name are null or empty. + */ + public boolean updateStyle(final File sldFile, final String name, final boolean raw) + 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=$name&raw=$raw} + */ + 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"); + } - /** - * 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 { + StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null)); + Util.appendParameter(sUrl, "raw", "" + raw); + String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); + if (!this.checkSLD10Version(sldFile)) { + contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); + } + LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType); + String result = HTTPUtils.put(sUrl.toString(), sldFile, contentType, gsuser, gspass); + return result != null; + } - 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"); - } + /** + * Update a Style. + * + * @param sldBody the new SLD document as a String. + * @param name the Style name. + * @param raw the raw format + * + * @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, final boolean raw) + 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=$name&raw=$raw} + */ + 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); + StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null)); + Util.appendParameter(sUrl, "raw", "" + raw); + String contentType = GeoServerRESTPublisher.Format.SLD.getContentType(); + if (!this.checkSLD10Version(sldBody)) { + contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType(); + } + LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType); + String result = HTTPUtils.put(sUrl.toString(), sldBody, contentType, gsuser, gspass); + return result != null; + } - final String result = HTTPUtils.put(sUrl, sldFile, - "application/vnd.ogc.sld+xml", 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); - /** - * 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"); + 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(":", "_"); - + // 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(":", "_"); + // currently REST interface does't support URLencoded URL // styleName = URLEncoder.encode(styleName); + String sUrl = buildUrl(null, styleName, null); + if (purge) { + sUrl += "?purge=true"; + } - String sUrl = buildUrl(null, styleName, null); - if (purge) { - sUrl += "?purge=true"; - } + return HTTPUtils.delete(sUrl, gsuser, gspass); + } - 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; - } + /** + * 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 - //========================================================================= + // 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, null); + } catch (IllegalArgumentException e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getLocalizedMessage(), e); + } + } + return false; + } - /** - * 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 { - /** - * 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, 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, boolean raw) + 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; - } + if (sldBody == null || sldBody.isEmpty()) { + throw new IllegalArgumentException("The style body may not be null or empty"); + } + StringBuilder sUrl = new StringBuilder(buildPostUrl(workspace, name)); + Util.appendParameter(sUrl, "raw", "" + raw); + final String result = HTTPUtils.post(sUrl.toString(), 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. + * + * @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; - } + /** + * 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"); - } + /** + * 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, boolean raw) { + StringBuilder sUrl = new StringBuilder(buildPostUrl(workspace, name)); + Util.appendParameter(sUrl, "raw", "" + raw); + LOGGER.debug("POSTing new style " + name + " to " + sUrl); + String result = HTTPUtils.post(sUrl.toString(), sldFile, GeoServerRESTPublisher.Format.SLD.getContentType(), gsuser, gspass); + return result != null; + } - final String sUrl = buildUrl(workspace, name, 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 result = HTTPUtils.put(sUrl, sldBody, - "application/vnd.ogc.sld+xml", gsuser, gspass); - return result != null; - } + final String sUrl = buildUrl(workspace, name, 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 { + final String result = HTTPUtils.put(sUrl, sldBody, + "application/vnd.ogc.sld+xml", gsuser, gspass); + return result != null; + } - 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"); - } + /** + * 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, boolean raw) + 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 StringBuilder sUrl = new StringBuilder(buildUrl(workspace, name, null)); + Util.appendParameter(sUrl, "raw", "" + raw); - final String result = HTTPUtils.put(sUrl, sldFile, - "application/vnd.ogc.sld+xml", gsuser, gspass); - return result != null; - } + final String result = HTTPUtils.put(sUrl.toString(), sldBody, + "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"); + /** + * 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; + } + + /** + * 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, boolean raw) + 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(buildUrl(workspace, name, null)); + Util.appendParameter(sUrl, "raw", "" + raw); + + final String result = HTTPUtils.put(sUrl.toString(), 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); + // 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); + String sUrl = buildUrl(workspace, styleName, null); - if (purge) { - sUrl += "?purge=true"; - } + if (purge) { + sUrl += "?purge=true"; + } - return HTTPUtils.delete(sUrl, gsuser, gspass); - } + 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; - } + /** + * 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 - //========================================================================= + // 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"); - /** - * 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); + } - 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(); + } - 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"); - protected String buildXmlUrl(final String workspace, final String name) { - return buildUrl(workspace, name, ".xml"); - } + if (workspace != null) { + sUrl.append("/workspaces/").append(workspace); + } - /** - * 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"); + sUrl.append("/styles/").append(URLEncoder.encode(name)); - if(workspace != null) - sUrl.append("/workspaces/").append(workspace); + if (ext != null) { + sUrl.append(ext); + } - sUrl.append("/styles/").append(URLEncoder.encode(name)); - - if(ext != null) - sUrl.append(ext); + return sUrl.toString(); + } - return sUrl.toString(); - } + private boolean checkSLD10Version(String sldBody) { + boolean result = false; + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + InputStream stream = new ByteArrayInputStream(sldBody.getBytes(Charset.forName("UTF-8"))); + Document doc = builder.parse(stream); + result = this.checkSLD10Version(doc); + } catch (SAXException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } catch (IOException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } catch (ParserConfigurationException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } + return result; + } - private boolean checkSLD10Version(String sldBody) { - boolean result = false; - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - try { - DocumentBuilder builder = factory.newDocumentBuilder(); - InputStream stream = new ByteArrayInputStream(sldBody.getBytes(Charset.forName("UTF-8"))); - Document doc = builder.parse(stream); - result = this.checkSLD10Version(doc); - } catch (SAXException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } catch (IOException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } catch (ParserConfigurationException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } - return result; - } + private boolean checkSLD10Version(File fileSLD) { + boolean result = false; + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(fileSLD); + result = this.checkSLD10Version(doc); + } catch (SAXException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } catch (IOException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } catch (ParserConfigurationException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } + return result; + } + + private boolean checkSLD10Version(Document doc) { + boolean result = false; + try { + XPathFactory xPathfactory = XPathFactory.newInstance(); + XPath xpath = xPathfactory.newXPath(); + XPathExpression expr = xpath.compile("//@version='1.0.0'"); + result = (Boolean) expr.evaluate(doc, XPathConstants.BOOLEAN); + } catch (XPathExpressionException ex) { + LOGGER.error("Error parsing SLD file: " + ex); + } + return result; + } - private boolean checkSLD10Version(File fileSLD) { - boolean result = false; - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - try { - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(fileSLD); - result = this.checkSLD10Version(doc); - } catch (SAXException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } catch (IOException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } catch (ParserConfigurationException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } - return result; - } - - private boolean checkSLD10Version(Document doc) { - boolean result = false; - try { - XPathFactory xPathfactory = XPathFactory.newInstance(); - XPath xpath = xPathfactory.newXPath(); - XPathExpression expr = xpath.compile("//@version='1.0.0'"); - result = (Boolean)expr.evaluate(doc, XPathConstants.BOOLEAN); - } catch (XPathExpressionException ex) { - LOGGER.error("Error parsing SLD file: " + ex); - } - return result; - } - } diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java index c821369..f999ca1 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -106,9 +106,9 @@ public class GSLayerEncoder21Test { String[] kvp1_1 = props1[0].split("\":"); String[] kvp1_2 = props1[1].split("\":"); Assert.assertEquals(IdentifierInfo.authority.name(), kvp1_1[0].replace("\"", "")); - Assert.assertEquals("authority2", kvp1_1[1].replace("\"", "")); + Assert.assertEquals("authority1", kvp1_1[1].replace("\"", "")); Assert.assertEquals(IdentifierInfo.identifier.name(), kvp1_2[0].replace("\"", "")); - Assert.assertEquals("identifier2", kvp1_2[1].replace("\"", "")); + Assert.assertEquals("identifier1", kvp1_2[1].replace("\"", "")); String[] props2 = items[1].split(","); String[] kvp2_1 = props2[0].split("\":"); @@ -116,15 +116,15 @@ public class GSLayerEncoder21Test { Assert.assertEquals(IdentifierInfo.authority.name(), kvp2_1[0].replace("\"", "")); Assert.assertEquals("authority2", kvp2_1[1].replace("\"", "")); Assert.assertEquals(IdentifierInfo.identifier.name(), kvp2_2[0].replace("\"", "")); - Assert.assertEquals("additionalId", kvp2_2[1].replace("\"", "")); + Assert.assertEquals("identifier2", kvp2_2[1].replace("\"", "")); String[] props3 = items[2].split(","); String[] kvp3_1 = props3[0].split("\":"); String[] kvp3_2 = props3[1].split("\":"); Assert.assertEquals(IdentifierInfo.authority.name(), kvp3_1[0].replace("\"", "")); - Assert.assertEquals("authority1", kvp3_1[1].replace("\"", "")); + Assert.assertEquals("authority2", kvp3_1[1].replace("\"", "")); Assert.assertEquals(IdentifierInfo.identifier.name(), kvp3_2[0].replace("\"", "")); - Assert.assertEquals("identifier1", kvp3_2[1].replace("\"", "")); + Assert.assertEquals("additionalId", kvp3_2[1].replace("\"", "")); } } 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 dc03abf..3ff1c97 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java @@ -351,6 +351,64 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest { assertEquals(0, reader.getStyles().size()); assertEquals(1, reader.getStyles(WORKSPACE).size()); } + + @Test + public void testStylesInWorkspaceRaw() 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, null, true)); + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + assertFalse(reader.existsStyle(STYLENAME)); + + // insert style again + assertFalse(publisher.publishStyleInWorkspace(WORKSPACE, sldFile, null, true)); + 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 {