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) {
try {
return publishStyle(sldBody,null);
} catch (IllegalArgumentException e){
if (LOGGER.isEnabledFor(Level.ERROR)){
LOGGER.error(e.getLocalizedMessage(),e);
return publishStyle(sldBody, null);
} catch (IllegalArgumentException e) {
if (LOGGER.isEnabledFor(Level.ERROR)) {
LOGGER.error(e.getLocalizedMessage(), e);
}
}
return false;
@ -162,13 +162,15 @@ public class GeoServerRESTPublisher {
* @throws IllegalArgumentException
* if the style body is null or empty
*/
public boolean publishStyle(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");
public boolean publishStyle(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");
}
StringBuilder sUrl = new StringBuilder(restURL);
sUrl.append("/rest/styles");
if (name!=null && !name.isEmpty()){
if (name != null && !name.isEmpty()) {
sUrl.append("?name=").append(name);
}
final String result = HTTPUtils.post(sUrl.toString(), sldBody,
@ -209,6 +211,81 @@ public class GeoServerRESTPublisher {
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>
*
@ -259,9 +336,9 @@ public class GeoServerRESTPublisher {
public boolean removeStyle(String styleName) {
try {
return removeStyle(styleName, true);
} catch (IllegalArgumentException e){
if (LOGGER.isEnabledFor(Level.ERROR)){
LOGGER.error(e.getLocalizedMessage(),e);
} catch (IllegalArgumentException e) {
if (LOGGER.isEnabledFor(Level.ERROR)) {
LOGGER.error(e.getLocalizedMessage(), e);
}
}
return false;
@ -649,7 +726,8 @@ public class GeoServerRESTPublisher {
if (configure != null) {
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);
if (!paramString.isEmpty()) {
sbUrl.append("&").append(paramString);
@ -1747,29 +1825,29 @@ public class GeoServerRESTPublisher {
final int paramsSize = params.length;
if (paramsSize > 0) {
int i = 0;
NameValuePair param=params[i];
while (param!=null && i++<paramsSize){
final String name=param.getName();
final String value=param.getValue();
NameValuePair param = params[i];
while (param != null && i++ < paramsSize) {
final String name = param.getName();
final String value = param.getValue();
// success
if (name!=null && !name.isEmpty() && value!=null && !value.isEmpty()){
sbUrl.append(name).append("=")
.append(value);
if (name != null && !name.isEmpty() && value != null
&& !value.isEmpty()) {
sbUrl.append(name).append("=").append(value);
// end cycle
param=null;
param = null;
} else {
// next value
param=params[i];
param = params[i];
}
}
for (; i < paramsSize; i++) {
param=params[i];
if (param!=null){
final String name=param.getName();
final String value=param.getValue();
sbUrl.append(name).append("=")
.append(value);
if (name!=null && !name.isEmpty() && value!=null && !value.isEmpty()){
param = params[i];
if (param != null) {
final String name = param.getName();
final String value = param.getValue();
sbUrl.append(name).append("=").append(value);
if (name != null && !name.isEmpty() && value != null
&& !value.isEmpty()) {
sbUrl.append("&").append(name).append("=")
.append(value);
}

View File

@ -42,29 +42,32 @@ import org.jdom.Namespace;
import org.springframework.core.io.ClassPathResource;
/**
* Testcase for publishing layers on geoserver.
* We need a running GeoServer to properly run the tests.
* If such geoserver instance cannot be contacted, tests will be skipped.
* Testcase for publishing layers on geoserver. We need a running GeoServer to
* properly run the tests. If such geoserver instance cannot be contacted, tests
* will be skipped.
*
* @author etj
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
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) {
super(testName);
}
public void testStyles() throws IOException {
if (!enabled()) return;
if (!enabled())
return;
deleteAll();
assertEquals(0, reader.getStyles().size());
final String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// insert style
assertTrue(publisher.publishStyle(sldFile));
@ -79,17 +82,23 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
Element styleEl = JDOMBuilder.buildElement(sld);
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("STYLE FOR TESTING PURPOSES", styleEl.getChild("NamedLayer", SLDNS).getChild("UserStyle", SLDNS).getChild("Title", SLDNS).getText());
} catch(NullPointerException npe) {
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(1475, sld.length());
assertEquals(1, reader.getStyles().size());
}
@ -106,37 +115,47 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
assertFalse("Cleanup failed", reader.existsStyle(styleName));
}
public void testPublishDeleteStyleFile() throws FileNotFoundException, IOException {
public void testPublishDeleteStyleFile() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
// 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?
cleanupTestStyle(styleName);
// 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(reader.existsStyle(styleName));
//test delete
sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
published = publisher.updateStyle(sldFile, styleName); // update
assertTrue("update() failed", published);
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
public void testPublishDeleteStyleString() throws FileNotFoundException, IOException {
public void testPublishDeleteStyleString() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
// Assume.assumeTrue(enabled);
String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
@ -144,27 +163,37 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
// test insert
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(reader.existsStyle(styleName));
//test delete
// 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
published = publisher.publishStyle(sldContent, styleName); // Will set
// the name
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
//test delete
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
ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
public void testUpdateDefaultStyle() throws FileNotFoundException, IOException {
public void testUpdateDefaultStyle() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
@ -175,28 +204,35 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
final String styleName = "restteststyle";
{
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
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(reader.existsStyle(styleName));
}
final String styleName2 = "restteststyle2";
{
File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
File sldFile = new ClassPathResource("testdata/restteststyle2.sld")
.getFile();
cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile,styleName2);
boolean sldpublished = publisher.publishStyle(sldFile, styleName2);
assertTrue("style publish() failed", sldpublished);
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));
// 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(existsLayer(layerName));
@ -217,7 +253,8 @@ public class GeoserverRESTStyleTest extends GeoserverRESTTest {
}
// remove layer and datastore
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, true);
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,
true);
assertTrue("removeDatastore() failed", dsRemoved);
}
}