publish style to workspace raw, preserve attribute order, removed recursion problem

This commit is contained in:
Tobias Warneke 2017-04-06 07:44:07 +02:00
parent a4268dda60
commit 3decadc243
5 changed files with 855 additions and 668 deletions

View File

@ -489,6 +489,14 @@ public class GeoServerRESTPublisher {
/**
* @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
* @see GeoServerRESTStyleManager#publishStyleInWorkspace(java.lang.String, java.io.File)
*/
public boolean publishStyleInWorkspace(String workspace, File sldFile) {
@ -505,6 +513,14 @@ public class GeoServerRESTPublisher {
/**
* @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
* @see GeoServerRESTStyleManager#updateStyleInWorkspace(java.lang.String, java.lang.String, java.lang.String)
*/
public boolean updateStyleInWorkspace(String workspace, String sldBody, String name) throws IllegalArgumentException {
@ -513,6 +529,14 @@ public class GeoServerRESTPublisher {
/**
* @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
* @see GeoServerRESTStyleManager#updateStyleInWorkspace(java.lang.String, java.io.File, java.lang.String)
*/
public boolean updateStyleInWorkspace(String workspace, File sldFile, String name) throws IllegalArgumentException {
@ -521,6 +545,14 @@ public class GeoServerRESTPublisher {
/**
* @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
* @see GeoServerRESTStyleManager#removeStyleInWorkspace(java.lang.String, java.lang.String, boolean)
*/
public boolean removeStyleInWorkspace(String workspace, String styleName, boolean purge) throws IllegalArgumentException {

View File

@ -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<String,String>();
authorityURLList = new LinkedHashMap<String,String>();
}
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<String,List<String>>();
identifierList = new LinkedHashMap<String,List<String>>();
}
String authority = identifierInfo.getAuthority();

View File

@ -71,6 +71,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
/**
* Check if a Style exists in the configured GeoServer instance.
*
* @param name the name of the style to check for.
* @return <TT>true</TT> on HTTP 200, <TT>false</TT> on HTTP 404
* @throws RuntimeException if any other HTTP code than 200 or 404 was retrieved.
@ -80,7 +81,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
/**
* Check if a Style exists in the configured GeoServer instance. User can choose if log a possible exception or not
* 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 <TT>true</TT> on HTTP 200, <TT>false</TT> on HTTP 404
@ -89,7 +92,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
public boolean existsStyle(String name, boolean quietOnNotFound) {
String url = buildXmlUrl(null, name);
String composed = Util.appendQuietOnNotFound(quietOnNotFound, url);
return HTTPUtils.exists(composed , gsuser, gspass);
return HTTPUtils.exists(composed, gsuser, gspass);
}
/**
@ -125,14 +128,12 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving SLD body from " + url);
}
return HTTPUtils.get( url, gsuser, gspass);
return HTTPUtils.get(url, gsuser, gspass);
}
//=========================================================================
// Workspaces
//=========================================================================
/**
*
* @since GeoServer 2.2
@ -148,7 +149,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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);
return HTTPUtils.exists(composed, gsuser, gspass);
}
/**
@ -158,7 +159,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
* @since GeoServer 2.2
*/
public RESTStyleList getStyles(String workspace) {
String url = "/rest/workspaces/"+workspace+"/styles.xml";
String url = "/rest/workspaces/" + workspace + "/styles.xml";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("### Retrieving Styles list from " + url);
}
@ -183,6 +184,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
/**
* Get the SLD body of a Style.
*
* @since GeoServer 2.2
*/
public String getSLD(String workspace, String name) {
@ -196,7 +198,6 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
//=========================================================================
// Publishing
//=========================================================================
/**
* Store and publish a Style.
*
@ -275,8 +276,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
/**
* Store and publish a Style, assigning it a name and choosing the raw
* format.
* 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.
@ -296,9 +296,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name));
Util.appendParameter(sUrl, "raw", ""+raw);
Util.appendParameter(sUrl, "raw", "" + raw);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
if (!this.checkSLD10Version(sldBody)) {
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType);
@ -307,8 +307,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
/**
* Store and publish a Style, assigning it a name and choosing the raw
* format.
* 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.
@ -324,9 +323,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
* http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=$name&raw=$raw}
*/
StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name));
Util.appendParameter(sUrl, "raw", ""+raw);
Util.appendParameter(sUrl, "raw", "" + raw);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
if (!this.checkSLD10Version(sldFile)) {
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType);
@ -359,9 +358,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null));
Util.appendParameter(sUrl, "raw", ""+raw);
Util.appendParameter(sUrl, "raw", "" + raw);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
if (!this.checkSLD10Version(sldFile)) {
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType);
@ -394,9 +393,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
}
StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null));
Util.appendParameter(sUrl, "raw", ""+raw);
Util.appendParameter(sUrl, "raw", "" + raw);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
if (!this.checkSLD10Version(sldBody)) {
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType);
@ -472,20 +471,21 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
*/
public boolean removeStyle(String styleName, final boolean purge)
throws IllegalArgumentException {
if (styleName == null || styleName.isEmpty())
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+"]");
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";
@ -517,7 +517,6 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
//=========================================================================
// Publishing in workspace
//=========================================================================
/**
* Store and publish a Style.
*
@ -528,7 +527,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
*/
public boolean publishStyleInWorkspace(final String workspace, String sldBody) {
try {
return publishStyleInWorkspace(workspace, sldBody);
return publishStyleInWorkspace(workspace, sldBody, null);
} catch (IllegalArgumentException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error(e.getLocalizedMessage(), e);
@ -558,6 +557,28 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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 <TT>true</TT> 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");
}
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.
*
@ -586,6 +607,23 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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 <TT>true</TT> 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;
}
/**
* Update a Style.
*
@ -611,6 +649,32 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
return result != null;
}
/**
* Update a Style.
*
* @param sldBody the new SLD document as a String.
* @param name the Style name to update.
*
* @return <TT>true</TT> 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 StringBuilder sUrl = new StringBuilder(buildUrl(workspace, name, null));
Util.appendParameter(sUrl, "raw", "" + raw);
final String result = HTTPUtils.put(sUrl.toString(), sldBody,
"application/vnd.ogc.sld+xml", gsuser, gspass);
return result != null;
}
/**
* Update a Style.
*
@ -637,6 +701,33 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
return result != null;
}
/**
* Update a Style.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
*
* @return <TT>true</TT> 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.
* <P>
@ -651,14 +742,16 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
*/
public boolean removeStyleInWorkspace(final String workspace, String styleName, final boolean purge)
throws IllegalArgumentException {
if (styleName == null || styleName.isEmpty())
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+"]");
if (styleName.contains(":")) {
LOGGER.warn("Style name is going to be changed [" + styleName + "]");
}
styleName = styleName.replaceAll(":", "_");
styleName = URLEncoder.encode(styleName);
@ -695,9 +788,9 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
//=========================================================================
// Util methods
//=========================================================================
/**
* Creates a URL for the given stylename with the name in querystring
*
* @param workspace nullable workspace name
* @param name style name
* @return
@ -705,23 +798,24 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
protected String buildPostUrl(final String workspace, String name) {
StringBuilder sUrl = new StringBuilder(gsBaseUrl.toString()).append("/rest");
if(workspace != null)
if (workspace != null) {
sUrl.append("/workspaces/").append(workspace);
}
sUrl.append("/styles");
if ( name != null && !name.isEmpty()) {
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")
@ -729,13 +823,15 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
protected String buildUrl(final String workspace, final String name, final String ext) {
StringBuilder sUrl = new StringBuilder(gsBaseUrl.toString()).append("/rest");
if(workspace != null)
if (workspace != null) {
sUrl.append("/workspaces/").append(workspace);
}
sUrl.append("/styles/").append(URLEncoder.encode(name));
if(ext != null)
if (ext != null) {
sUrl.append(ext);
}
return sUrl.toString();
}
@ -781,7 +877,7 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//@version='1.0.0'");
result = (Boolean)expr.evaluate(doc, XPathConstants.BOOLEAN);
result = (Boolean) expr.evaluate(doc, XPathConstants.BOOLEAN);
} catch (XPathExpressionException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
}

View File

@ -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("\"", ""));
}
}

View File

@ -352,6 +352,64 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
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 {
if (!enabled())