publisher improvements to fix issue #4

This commit is contained in:
ccancellieri 2012-02-02 10:48:38 +01:00
parent f9b640cdab
commit 9493343f1b
2 changed files with 287 additions and 172 deletions

View File

@ -162,9 +162,11 @@ public class GeoServerRESTPublisher {
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the style body is null or empty * if the style body is null or empty
*/ */
public boolean publishStyle(final String sldBody, final String name) throws IllegalArgumentException { public boolean publishStyle(final String sldBody, final String name)
throws IllegalArgumentException {
if (sldBody == null || sldBody.isEmpty()) { if (sldBody == null || sldBody.isEmpty()) {
throw new IllegalArgumentException("The style body may not be null or empty"); throw new IllegalArgumentException(
"The style body may not be null or empty");
} }
StringBuilder sUrl = new StringBuilder(restURL); StringBuilder sUrl = new StringBuilder(restURL);
sUrl.append("/rest/styles"); sUrl.append("/rest/styles");
@ -209,6 +211,81 @@ public class GeoServerRESTPublisher {
return result != null; return result != null;
} }
/**
* Update SLD called as 'name'.
* <P>
* This is the equivalent call with cUrl:
*
* <PRE>
* {@code curl -u admin:geoserver -XPUT \
* -H 'Content-type: application/vnd.ogc.sld+xml' \
* -d @$FULLSLD \
* http://$GSIP:$GSPORT/$SERVLET/rest/styles/$NAME}
* </PRE>
*
* @param sldBody
* the SLD document as an XML String.
* @param name
* the Style name to modify.
*
* @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)
throws IllegalArgumentException {
if (sldBody == null || sldBody.isEmpty()) {
throw new IllegalArgumentException(
"The style body may not be null or empty");
} else if (name == null || name.isEmpty()) {
throw new IllegalArgumentException(
"The style name may not be null or empty");
}
final StringBuilder sUrl = new StringBuilder(restURL);
sUrl.append("/rest/styles/").append(encode(name));
final String result = HTTPUtils.put(sUrl.toString(), sldBody,
"application/vnd.ogc.sld+xml", gsuser, gspass);
return result != null;
}
/**
* Update an SLD called 'name'.
*
* @param sldFile
* the File containing the SLD document.
* @param name
* the Style name.
*
* @return <TT>true</TT> if the operation completed successfully.
*
* @throws IllegalArgumentException
* if the sldFile file or name are null or name is empty
*
*/
public boolean updateStyle(final File sldFile, final String name)
throws IllegalArgumentException {
if (sldFile == null) {
throw new IllegalArgumentException(
"Unable to updateStyle using a null parameter file");
} else if (name == null || name.isEmpty()) {
throw new IllegalArgumentException(
"The style name may not be null or empty");
}
final StringBuilder sUrl = new StringBuilder(restURL);
sUrl.append("/rest/styles/").append(encode(name));
final String result = HTTPUtils.put(sUrl.toString(), sldFile,
"application/vnd.ogc.sld+xml", gsuser, gspass);
return result != null;
}
/** /**
* Remove a Style.<br> * Remove a Style.<br>
* *
@ -649,7 +726,8 @@ public class GeoServerRESTPublisher {
if (configure != null) { if (configure != null) {
sbUrl.append("?configure=").append(configure); sbUrl.append("?configure=").append(configure);
if (params != (NameValuePair[])null && !configure.equals(ParameterConfigure.NONE)) { if (params != (NameValuePair[]) null
&& !configure.equals(ParameterConfigure.NONE)) {
final String paramString = appendParameters(params); final String paramString = appendParameters(params);
if (!paramString.isEmpty()) { if (!paramString.isEmpty()) {
sbUrl.append("&").append(paramString); sbUrl.append("&").append(paramString);
@ -1752,9 +1830,9 @@ public class GeoServerRESTPublisher {
final String name = param.getName(); final String name = param.getName();
final String value = param.getValue(); final String value = param.getValue();
// success // success
if (name!=null && !name.isEmpty() && value!=null && !value.isEmpty()){ if (name != null && !name.isEmpty() && value != null
sbUrl.append(name).append("=") && !value.isEmpty()) {
.append(value); sbUrl.append(name).append("=").append(value);
// end cycle // end cycle
param = null; param = null;
} else { } else {
@ -1767,9 +1845,9 @@ public class GeoServerRESTPublisher {
if (param != null) { if (param != null) {
final String name = param.getName(); final String name = param.getName();
final String value = param.getValue(); final String value = param.getValue();
sbUrl.append(name).append("=") sbUrl.append(name).append("=").append(value);
.append(value); if (name != null && !name.isEmpty() && value != null
if (name!=null && !name.isEmpty() && value!=null && !value.isEmpty()){ && !value.isEmpty()) {
sbUrl.append("&").append(name).append("=") sbUrl.append("&").append(name).append("=")
.append(value); .append(value);
} }

View File

@ -42,29 +42,32 @@ import org.jdom.Namespace;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
/** /**
* Testcase for publishing layers on geoserver. * Testcase for publishing layers on geoserver. We need a running GeoServer to
* We need a running GeoServer to properly run the tests. * properly run the tests. If such geoserver instance cannot be contacted, tests
* If such geoserver instance cannot be contacted, tests will be skipped. * will be skipped.
* *
* @author etj * @author etj
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/ */
public class GeoserverRESTStyleTest extends GeoserverRESTTest { public class GeoserverRESTStyleTest extends GeoserverRESTTest {
private final static Logger LOGGER = Logger.getLogger(GeoserverRESTStyleTest.class); private final static Logger LOGGER = Logger
.getLogger(GeoserverRESTStyleTest.class);
public GeoserverRESTStyleTest(String testName) { public GeoserverRESTStyleTest(String testName) {
super(testName); super(testName);
} }
public void testStyles() throws IOException { public void testStyles() throws IOException {
if (!enabled()) return; if (!enabled())
return;
deleteAll(); deleteAll();
assertEquals(0, reader.getStyles().size()); assertEquals(0, reader.getStyles().size());
final String styleName = "restteststyle"; final String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// insert style // insert style
assertTrue(publisher.publishStyle(sldFile)); assertTrue(publisher.publishStyle(sldFile));
@ -79,12 +82,18 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
Element styleEl = JDOMBuilder.buildElement(sld); Element styleEl = JDOMBuilder.buildElement(sld);
assertNotNull(styleEl); assertNotNull(styleEl);
Namespace SLDNS = Namespace.getNamespace("sld", "http://www.opengis.net/sld"); Namespace SLDNS = Namespace.getNamespace("sld",
"http://www.opengis.net/sld");
try { try {
assertEquals(styleName, styleEl.getChild("NamedLayer", SLDNS).getChild("Name",SLDNS).getText()); assertEquals(styleName, styleEl.getChild("NamedLayer", SLDNS)
assertEquals("STYLE FOR TESTING PURPOSES", styleEl.getChild("NamedLayer", SLDNS).getChild("UserStyle", SLDNS).getChild("Title", SLDNS).getText()); .getChild("Name", SLDNS).getText());
assertEquals(
"STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
fail("Error in SLD"); fail("Error in SLD");
} }
@ -106,37 +115,47 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
assertFalse("Cleanup failed", reader.existsStyle(styleName)); assertFalse("Cleanup failed", reader.existsStyle(styleName));
} }
public void testPublishDeleteStyleFile() throws FileNotFoundException, IOException { public void testPublishDeleteStyleFile() throws FileNotFoundException,
IOException {
if (!enabled()) { if (!enabled()) {
return; return;
} }
// Assume.assumeTrue(enabled); // Assume.assumeTrue(enabled);
final String styleName = "restteststyle"; final String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state? // known state?
cleanupTestStyle(styleName); cleanupTestStyle(styleName);
// test insert // test insert
boolean published = publisher.publishStyle(sldFile); // Will take the name from sld contents boolean published = publisher.publishStyle(sldFile); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published); assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
published = publisher.updateStyle(sldFile, styleName); // update
assertTrue("update() failed", published);
// test delete // test delete
boolean ok = publisher.removeStyle(styleName); boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok); assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName)); assertFalse(reader.existsStyle(styleName));
} }
public void testPublishDeleteStyleString() throws FileNotFoundException, IOException { public void testPublishDeleteStyleString() throws FileNotFoundException,
IOException {
if (!enabled()) { if (!enabled()) {
return; return;
} }
// Assume.assumeTrue(enabled); // Assume.assumeTrue(enabled);
String styleName = "restteststyle"; String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state? // known state?
cleanupTestStyle(styleName); cleanupTestStyle(styleName);
@ -144,7 +163,9 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
// test insert // test insert
String sldContent = IOUtils.toString(new FileInputStream(sldFile)); String sldContent = IOUtils.toString(new FileInputStream(sldFile));
boolean published = publisher.publishStyle(sldContent); // Will take the name from sld contents boolean published = publisher.publishStyle(sldContent); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published); assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
// test delete // test delete
@ -154,9 +175,16 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
styleName = "restteststyle_with_name"; styleName = "restteststyle_with_name";
// test insert with name // test insert with name
published = publisher.publishStyle(sldContent,styleName); // Will set the name published = publisher.publishStyle(sldContent, styleName); // Will set
// the name
assertTrue("publish() failed", published); assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
String newSldContent = sldContent.replace(
"<sld:Title>STYLE FOR TESTING PURPOSES</sld:Title>",
"<sld:Title>MODIFIED STYLE FOR TESTING</sld:Title>");
published = publisher.updateStyle(newSldContent, styleName); // update
assertTrue("publish() failed", published);
// test delete // test delete
ok = publisher.removeStyle(styleName); ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok); assertTrue("Unpublish() failed", ok);
@ -164,7 +192,8 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
} }
public void testUpdateDefaultStyle() throws FileNotFoundException, IOException { public void testUpdateDefaultStyle() throws FileNotFoundException,
IOException {
if (!enabled()) { if (!enabled()) {
return; return;
} }
@ -175,28 +204,35 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
final String styleName = "restteststyle"; final String styleName = "restteststyle";
{ {
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
cleanupTestStyle(styleName); cleanupTestStyle(styleName);
boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents boolean sldpublished = publisher.publishStyle(sldFile); // Will take
// the name
// from sld
// contents
assertTrue("style publish() failed", sldpublished); assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
} }
final String styleName2 = "restteststyle2"; final String styleName2 = "restteststyle2";
{ {
File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile(); File sldFile = new ClassPathResource("testdata/restteststyle2.sld")
.getFile();
cleanupTestStyle(styleName2); cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile, styleName2); boolean sldpublished = publisher.publishStyle(sldFile, styleName2);
assertTrue("style publish() failed", sldpublished); assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName2)); assertTrue(reader.existsStyle(styleName2));
} }
File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); File zipFile = new ClassPathResource("testdata/resttestshp.zip")
.getFile();
assertTrue(publisher.createWorkspace(DEFAULT_WS)); assertTrue(publisher.createWorkspace(DEFAULT_WS));
// test insert // test insert
boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile, "EPSG:4326", styleName); boolean published = publisher.publishShp(DEFAULT_WS, storeName,
layerName, zipFile, "EPSG:4326", styleName);
assertTrue("publish() failed", published); assertTrue("publish() failed", published);
assertTrue(existsLayer(layerName)); assertTrue(existsLayer(layerName));
@ -217,7 +253,8 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
} }
// remove layer and datastore // remove layer and datastore
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, true); boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,
true);
assertTrue("removeDatastore() failed", dsRemoved); assertTrue("removeDatastore() failed", dsRemoved);
} }
} }