it.geosolutions.geoserver.rest.manager.GeoServerRESTStyleManager Maven / Gradle / Ivy
Show all versions of geoserver-manager Show documentation
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2015 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.Util;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.RESTStyleList;
import it.geosolutions.geoserver.rest.encoder.GSStyleEncoder;
import it.geosolutions.geoserver.rest.http.GeoServerRestAuthenticator;
import it.geosolutions.geoserver.rest.http.HTTPUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* GeoServerRESTStyleManager class.
*
* @author ETj (etj at geo-solutions.it)
* @version $Id: $
*/
public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
private final static Logger LOGGER = LoggerFactory.getLogger(GeoServerRESTStyleManager.class);
/**
* Default constructor.
*
* @param restURL GeoServer REST API endpoint
* @param auth auth credentials
* @throws java.lang.IllegalArgumentException if any.
*/
public GeoServerRESTStyleManager(URL restURL, GeoServerRestAuthenticator auth)
throws IllegalArgumentException {
super(restURL, auth);
}
/**
* 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 java.lang.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 java.lang.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 , auth);
}
/**
* 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, auth);
return RESTStyleList.build(response);
}
/**
* getStyle
*
* @param name a {@link java.lang.String} object.
* @return a {@link it.geosolutions.geoserver.rest.decoder.RESTStyle} object.
*/
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, auth);
return RESTStyle.build(response);
}
/**
* Get the SLD body of a Style.
*
* @param styleName a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
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, auth);
}
//=========================================================================
// Workspaces
//=========================================================================
/**
* existsStyle
*
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
* @param name a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean existsStyle(String workspace, String name) {
return existsStyle(workspace, name, Util.DEFAULT_QUIET_ON_NOT_FOUND);
}
/**
* existsStyle
*
* @since GeoServer 2.6
* @param workspace a {@link java.lang.String} object.
* @param name a {@link java.lang.String} object.
* @param quietOnNotFound a boolean.
* @return a boolean.
*/
public boolean existsStyle(String workspace, String name, boolean quietOnNotFound) {
String url = buildXmlUrl(workspace, name);
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed , auth);
}
/**
* Get summary info about Styles in a workspace.
*
* @return summary info about Styles as a {@link RESTStyleList}
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
public RESTStyleList getStyles(String workspace) {
String url = "/rest/workspaces/"+ HTTPUtils.enc(workspace) +"/styles.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Styles list from " + url);
}
String response = HTTPUtils.get(gsBaseUrl + url, auth);
return RESTStyleList.build(response);
}
/**
* getStyle
*
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
* @param name a {@link java.lang.String} object.
* @return a {@link it.geosolutions.geoserver.rest.decoder.RESTStyle} object.
*/
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, auth);
return RESTStyle.build(response);
}
/**
* Get the SLD body of a Style.
*
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
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, auth);
}
//=========================================================================
// 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 java.lang.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", auth);
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(), auth);
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, auth);
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, auth);
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 java.lang.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, auth);
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 java.lang.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, auth);
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 java.lang.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", auth);
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 java.lang.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", auth);
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 java.lang.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(":", "_");
// currently REST interface does't support URLencoded URL
// styleName = URLEncoder.encode(styleName);
String sUrl = buildUrl(null, styleName, null);
if (purge) {
sUrl += "?purge=true";
}
return HTTPUtils.delete(sUrl, auth);
}
/**
* 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
* @param workspace a {@link java.lang.String} object.
*/
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, 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 java.lang.IllegalArgumentException if the style body is null or empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
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", auth);
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
* @param workspace a {@link java.lang.String} object.
*/
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
* @param workspace a {@link java.lang.String} object.
*/
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(), auth);
return result != null;
}
/**
* Store and publish a Style, assigning it a name.
*
* @param zipFile the ZIP file.
* @param name the Style name.
* @return true if the operation completed successfully.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
public boolean publishStyleZippedInWorkspace(final String workspace, File zipFile, String name) {
String sUrl = buildPostUrl(workspace, name);
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
String result = HTTPUtils.post(sUrl, zipFile, GeoServerRESTPublisher.Format.ZIP.getContentType(), auth);
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 java.lang.IllegalArgumentException if the style body or name are null or empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
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", auth);
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 java.lang.IllegalArgumentException if the style body or name are null or empty.
*/
public boolean updateStyleInWorkspace(final String sldBody, final String workspace,
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(workspace, 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, auth);
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 java.lang.IllegalArgumentException if the sldFile file or name are null or name is empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
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", auth);
return result != null;
}
/**
* Update a Style.
*
* @param encoder the style metadata.
* @param name the Style name.
* @return true if the operation completed successfully.
* @throws java.lang.IllegalArgumentException if the sldFile file or name are null or name is empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
public boolean updateStyleInWorkspace(final String workspace, final GSStyleEncoder encoder, final String name)
throws IllegalArgumentException {
if (encoder == null) {
throw new IllegalArgumentException("Unable to updateStyle using a null parameter encoder");
} 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.putXml(sUrl, encoder.toString(), auth);
return result != null;
}
/**
* Update a Style.
*
* @param zipFile the zip file.
* @param name the Style name.
* @return true if the operation completed successfully.
* @throws java.lang.IllegalArgumentException if the sldFile file or name are null or name is empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
public boolean updateStyleZippedInWorkspace(final String workspace, final File zipFile, final String name)
throws IllegalArgumentException {
if (zipFile == 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, zipFile,
GeoServerRESTPublisher.Format.ZIP.getContentType(), auth);
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 java.lang.IllegalArgumentException if styleName is null or empty.
* @since GeoServer 2.2
* @param workspace a {@link java.lang.String} object.
*/
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(":", "_");
String sUrl = buildUrl(workspace, styleName, null);
if (purge) {
sUrl += "?purge=true";
}
return HTTPUtils.delete(sUrl, auth);
}
/**
* 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
* @param workspace a {@link java.lang.String} object.
*/
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 a {@link java.lang.String} object.
*/
protected String buildPostUrl(final String workspace, String name) {
StringBuilder sUrl = new StringBuilder(gsBaseUrl.toString()).append("/rest");
if(workspace != null)
sUrl.append("/workspaces/").append(HTTPUtils.enc(workspace));
sUrl.append("/styles");
if ( name != null && !name.isEmpty()) {
sUrl.append("?name=").append(HTTPUtils.enc(name));
}
return sUrl.toString();
}
/**
*
buildXmlUrl
*
* @param workspace a {@link java.lang.String} object.
* @param name a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
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")
* @return a {@link java.lang.String} object.
*/
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(HTTPUtils.enc(workspace));
sUrl.append("/styles/").append(HTTPUtils.enc(name));
if(ext != null)
sUrl.append(ext);
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(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;
}
}