publisher improvements to fix issue #4

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

View File

@ -132,10 +132,10 @@ public class GeoServerRESTPublisher {
*/ */
public boolean publishStyle(String sldBody) { public boolean publishStyle(String sldBody) {
try { try {
return publishStyle(sldBody,null); return publishStyle(sldBody, null);
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e) {
if (LOGGER.isEnabledFor(Level.ERROR)){ if (LOGGER.isEnabledFor(Level.ERROR)) {
LOGGER.error(e.getLocalizedMessage(),e); LOGGER.error(e.getLocalizedMessage(), e);
} }
} }
return false; return false;
@ -162,13 +162,15 @@ 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)
if (sldBody==null || sldBody.isEmpty()){ throws IllegalArgumentException {
throw new IllegalArgumentException("The style body may not be null or empty"); if (sldBody == null || sldBody.isEmpty()) {
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");
if (name!=null && !name.isEmpty()){ if (name != null && !name.isEmpty()) {
sUrl.append("?name=").append(name); sUrl.append("?name=").append(name);
} }
final String result = HTTPUtils.post(sUrl.toString(), sldBody, final String result = HTTPUtils.post(sUrl.toString(), sldBody,
@ -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>
* *
@ -232,7 +309,7 @@ public class GeoServerRESTPublisher {
final StringBuffer sUrl = new StringBuffer(restURL); final StringBuffer sUrl = new StringBuffer(restURL);
// check style name // check style name
// TODO may we whant to throw an exception instead of // TODO may we whant to throw an exception instead of
// change style name? // change style name?
styleName = styleName.replaceAll(":", "_"); styleName = styleName.replaceAll(":", "_");
@ -259,9 +336,9 @@ public class GeoServerRESTPublisher {
public boolean removeStyle(String styleName) { public boolean removeStyle(String styleName) {
try { try {
return removeStyle(styleName, true); return removeStyle(styleName, true);
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e) {
if (LOGGER.isEnabledFor(Level.ERROR)){ if (LOGGER.isEnabledFor(Level.ERROR)) {
LOGGER.error(e.getLocalizedMessage(),e); LOGGER.error(e.getLocalizedMessage(), e);
} }
} }
return false; return false;
@ -466,7 +543,7 @@ public class GeoServerRESTPublisher {
* @param layername * @param layername
* @param srs * @param srs
* @param defaultStyle * @param defaultStyle
* @return * @return
*/ */
public boolean publishDBLayer(String workspace, String storename, public boolean publishDBLayer(String workspace, String storename,
String layername, String srs, String defaultStyle) { String layername, String srs, String defaultStyle) {
@ -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);
@ -1747,35 +1825,35 @@ public class GeoServerRESTPublisher {
final int paramsSize = params.length; final int paramsSize = params.length;
if (paramsSize > 0) { if (paramsSize > 0) {
int i = 0; int i = 0;
NameValuePair param=params[i]; NameValuePair param = params[i];
while (param!=null && i++<paramsSize){ while (param != null && i++ < paramsSize) {
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 {
// next value // next value
param=params[i]; param = params[i];
} }
} }
for (; i < paramsSize; i++) { for (; i < paramsSize; i++) {
param=params[i]; param = params[i];
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,182 +42,219 @@ 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())
deleteAll(); return;
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));
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
assertFalse(publisher.publishStyle(sldFile)); assertFalse(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(styleName)); assertTrue(reader.existsStyle(styleName));
String sld = reader.getSLD(styleName); String sld = reader.getSLD(styleName);
assertNotNull(sld); assertNotNull(sld);
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());
} catch(NullPointerException npe) { assertEquals(
fail("Error in SLD"); "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(1475, sld.length());
assertEquals(1, reader.getStyles().size()); assertEquals(1, reader.getStyles().size());
} }
protected void cleanupTestStyle(final String styleName) { protected void cleanupTestStyle(final String styleName) {
// dry run delete to work in a known state // dry run delete to work in a known state
if (reader.existsStyle(styleName)) { if (reader.existsStyle(styleName)) {
LOGGER.info("Clearing stale test style " + styleName); LOGGER.info("Clearing stale test style " + styleName);
boolean ok = publisher.removeStyle(styleName); boolean ok = publisher.removeStyle(styleName);
if (!ok) { if (!ok) {
fail("Could not unpublish style " + styleName); fail("Could not unpublish style " + styleName);
} }
} }
assertFalse("Cleanup failed", reader.existsStyle(styleName)); assertFalse("Cleanup failed", reader.existsStyle(styleName));
} }
public void testPublishDeleteStyleFile() throws FileNotFoundException, IOException { public void testPublishDeleteStyleFile() throws FileNotFoundException,
if (!enabled()) { IOException {
return; if (!enabled()) {
} return;
// Assume.assumeTrue(enabled); }
final String styleName = "restteststyle"; // Assume.assumeTrue(enabled);
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
assertTrue("publish() failed", published); // name from sld
assertTrue(reader.existsStyle(styleName)); // contents
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
//test delete sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
boolean ok = publisher.removeStyle(styleName); published = publisher.updateStyle(sldFile, styleName); // update
assertTrue("Unpublish() failed", ok); assertTrue("update() failed", published);
assertFalse(reader.existsStyle(styleName));
}
public void testPublishDeleteStyleString() throws FileNotFoundException, IOException { // test delete
if (!enabled()) { boolean ok = publisher.removeStyle(styleName);
return; assertTrue("Unpublish() failed", ok);
} assertFalse(reader.existsStyle(styleName));
// Assume.assumeTrue(enabled); }
String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); public void testPublishDeleteStyleString() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
String styleName = "restteststyle";
// known state? File sldFile = new ClassPathResource("testdata/restteststyle.sld")
cleanupTestStyle(styleName); .getFile();
// test insert // known state?
String sldContent = IOUtils.toString(new FileInputStream(sldFile)); cleanupTestStyle(styleName);
boolean published = publisher.publishStyle(sldContent); // Will take the name from sld contents // test insert
assertTrue("publish() failed", published); String sldContent = IOUtils.toString(new FileInputStream(sldFile));
assertTrue(reader.existsStyle(styleName));
//test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
styleName = "restteststyle_with_name";
// test insert with name
published = publisher.publishStyle(sldContent,styleName); // Will set the name
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
//test delete
ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
public void testUpdateDefaultStyle() throws FileNotFoundException, IOException { boolean published = publisher.publishStyle(sldContent); // Will take the
if (!enabled()) { // name from sld
return; // contents
} assertTrue("publish() failed", published);
deleteAll(); assertTrue(reader.existsStyle(styleName));
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
String storeName = "resttestshp"; styleName = "restteststyle_with_name";
String layerName = "cities"; // test insert with name
published = publisher.publishStyle(sldContent, styleName); // Will set
// the name
assertTrue("publish() failed", published);
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);
final String styleName = "restteststyle"; // test delete
{ ok = publisher.removeStyle(styleName);
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); assertTrue("Unpublish() failed", ok);
cleanupTestStyle(styleName); assertFalse(reader.existsStyle(styleName));
boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName));
}
final String styleName2 = "restteststyle2"; }
{
File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile,styleName2);
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName2));
}
File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); public void testUpdateDefaultStyle() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
deleteAll();
assertTrue(publisher.createWorkspace(DEFAULT_WS)); String storeName = "resttestshp";
String layerName = "cities";
// test insert
boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile, "EPSG:4326", styleName);
assertTrue("publish() failed", published);
assertTrue(existsLayer(layerName));
{ final String styleName = "restteststyle";
RESTLayer layer = reader.getLayer(layerName); {
LOGGER.info("Layer style is " + layer.getDefaultStyle()); File sldFile = new ClassPathResource("testdata/restteststyle.sld")
assertEquals(styleName, layer.getDefaultStyle()); .getFile();
} cleanupTestStyle(styleName);
boolean sldpublished = publisher.publishStyle(sldFile); // Will take
// the name
// from sld
// contents
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName));
}
GSLayerEncoder le = new GSLayerEncoder(); final String styleName2 = "restteststyle2";
le.setDefaultStyle(styleName2); {
publisher.configureLayer(DEFAULT_WS, layerName, le); File sldFile = new ClassPathResource("testdata/restteststyle2.sld")
.getFile();
cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile, styleName2);
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName2));
}
{ File zipFile = new ClassPathResource("testdata/resttestshp.zip")
RESTLayer layer = reader.getLayer(layerName); .getFile();
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName2, layer.getDefaultStyle());
}
// remove layer and datastore assertTrue(publisher.createWorkspace(DEFAULT_WS));
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, true);
assertTrue("removeDatastore() failed", dsRemoved); // test insert
} boolean published = publisher.publishShp(DEFAULT_WS, storeName,
layerName, zipFile, "EPSG:4326", styleName);
assertTrue("publish() failed", published);
assertTrue(existsLayer(layerName));
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName, layer.getDefaultStyle());
}
GSLayerEncoder le = new GSLayerEncoder();
le.setDefaultStyle(styleName2);
publisher.configureLayer(DEFAULT_WS, layerName, le);
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName2, layer.getDefaultStyle());
}
// remove layer and datastore
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,
true);
assertTrue("removeDatastore() failed", dsRemoved);
}
} }