Merge pull request from branch 'master' of https://github.com/andypower/geoserver-manager.

Fix tests and style REST URLs.
Fix v110 SLD.
Closes #145.
This commit is contained in:
etj 2015-04-01 01:23:59 +02:00
commit c9f51cf388
4 changed files with 668 additions and 357 deletions

View File

@ -1,7 +1,7 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* 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
@ -356,6 +356,62 @@ public class GeoServerRESTPublisher {
return styleManager.publishStyle(sldFile, name);
}
/**
* 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 <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(String sldBody, String name, boolean raw) {
return styleManager.publishStyle(sldBody, name, raw);
}
/**
* 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 <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(File sldFile, String name, boolean raw) {
return styleManager.publishStyle(sldFile, name, raw);
}
/**
* Update a Style.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> 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, boolean raw)
throws IllegalArgumentException {
return styleManager.updateStyle(sldFile, name, raw);
}
/**
* Update a Style.
*
* @param sldBody the new SLD document as a String.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> 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, boolean raw)
throws IllegalArgumentException {
return styleManager.updateStyle(sldBody, name, raw);
}
/**
* Update a Style.
*
@ -1340,7 +1396,7 @@ public class GeoServerRESTPublisher {
* </ul>
*/
public enum Format {
XML, JSON, HTML, SLD;
XML, JSON, HTML, SLD, SLD_1_1_0;
/**
* Gets the mime type from a format.
@ -1358,6 +1414,8 @@ public class GeoServerRESTPublisher {
return "application/json";
case SLD:
return "application/vnd.ogc.sld+xml";
case SLD_1_1_0:
return "application/vnd.ogc.se+xml";
default:
return null;
}

View File

@ -1,7 +1,7 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* 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
@ -29,11 +29,25 @@ import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.Util;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.RESTStyleList;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
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;
/**
*
@ -260,6 +274,136 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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 <TT>true</TT> 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");
}
String sUrl = buildPostUrl(null, name);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.post(sUrl, 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 <TT>true</TT> 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}
*/
String sUrl = buildPostUrl(null, name);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.post(sUrl, 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 <TT>true</TT> 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");
}
String sUrl = buildUrl(null, name, null);
sUrl += "?raw=" + raw;
LOGGER.debug("POSTing style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.put(sUrl, 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 <TT>true</TT> 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");
}
String sUrl = buildUrl(null, name, null);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.put(sUrl, sldBody, contentType, gsuser, gspass);
return result != null;
}
/**
* Update a Style.
*
@ -596,4 +740,52 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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(StandardCharsets.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;
}
}

View File

@ -1,7 +1,7 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* 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
@ -79,6 +79,15 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
assertFalse(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(STYLENAME));
// insert style v110
final String STYLENAMEV110 = "restteststyleV110";
File sldFileV110 = new ClassPathResource("testdata/" + STYLENAMEV110 + ".sld").getFile();
assertTrue(publisher.publishStyle(sldFileV110, STYLENAMEV110, true));
assertTrue(reader.existsStyle(STYLENAMEV110));
assertFalse(publisher.publishStyle(sldFileV110, STYLENAMEV110, true));
RESTStyle style = reader.getStyle(STYLENAME);
assertEquals(STYLENAME, style.getName());
assertNull(style.getWorkspace());
@ -96,8 +105,7 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS)
.getChild("Name", SLDNS).getText());
assertEquals(
"STYLE FOR TESTING PURPOSES",
assertEquals("STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
@ -107,7 +115,7 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
// assertEquals(1475, sld.length());
assertEquals(1, reader.getStyles().size());
assertEquals(2, reader.getStyles().size());
}
protected void cleanupTestStyle(final String styleName) {
@ -134,8 +142,13 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
final String STYLENAMEV110 = "restteststyleV110";
File sldFileV110 = new ClassPathResource("testdata/" + STYLENAMEV110 + ".sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
cleanupTestStyle(STYLENAMEV110);
// test insert
boolean published = publisher.publishStyle(sldFile); // Will take the
@ -152,6 +165,19 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
published = publisher.publishStyle(sldFileV110, STYLENAMEV110, true);
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(STYLENAMEV110));
boolean updated = publisher.updateStyle(sldFileV110, STYLENAMEV110, true);
assertTrue("update() failed", updated);
// test delete
ok = publisher.removeStyle(STYLENAMEV110);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(STYLENAMEV110));
}
@Test

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.1.0"
xmlns="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:se="http://www.opengis.net/se"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd">
<NamedLayer>
<se:Name>OCEANSEA_1M:Foundation</se:Name>
<UserStyle>
<se:Name>GEOSYM</se:Name>
<IsDefault>1</IsDefault>
<se:FeatureTypeStyle>
<se:FeatureTypeName>Foundation</se:FeatureTypeName>
<se:Rule>
<se:Name>main</se:Name>
<se:PolygonSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<se:Name>MySymbol</se:Name>
<se:Description>
<se:Title>Example Symbol</se:Title>
<se:Abstract>This is just a simple example.</se:Abstract>
</se:Description>
<se:Geometry>
<ogc:PropertyName>GEOMETRY</ogc:PropertyName>
</se:Geometry>
<se:Fill>
<se:SvgParameter name="fill">#96C3F5</se:SvgParameter>
</se:Fill>
</se:PolygonSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>