Merge pull request #147 from andypower/master

Improve on appending the raw parameters on existing query string to publish and update raw SLDs
This commit is contained in:
Emanuele Tajariol 2015-05-27 12:06:16 +02:00
commit 7d0a8319ea
2 changed files with 63 additions and 20 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
@ -27,15 +27,18 @@ package it.geosolutions.geoserver.rest;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
*
* @author ETj (etj at geo-solutions.it)
*/
public class Util {
public static final String QUIET_ON_NOT_FOUND_PARAM = "quietOnNotFound=";
public static final String QUIET_ON_NOT_FOUND_PARAM = "quietOnNotFound=";
public static final boolean DEFAULT_QUIET_ON_NOT_FOUND = true;
@ -70,4 +73,44 @@ public class Util {
String composed = url + (contains ? "&":"?") + QUIET_ON_NOT_FOUND_PARAM + quietOnNotFound;
return composed;
}
public static <T> List<T> safeList(List<T> list) {
return list == null ? Collections.EMPTY_LIST : list;
}
public static <T> Collection<T> safeCollection(Collection<T> collection) {
return collection == null ? Collections.EMPTY_SET : collection;
}
public static <TK, TV> Map<TK, TV> safeMap(Map<TK, TV> map) {
return map == null ? Collections.EMPTY_MAP : map;
}
public static char getParameterSeparator(String url) {
char parameterSeparator = '?';
if (url.contains("?")) {
parameterSeparator = '&';
}
return parameterSeparator;
}
public static char getParameterSeparator(StringBuilder url) {
char parameterSeparator = '?';
if (url.indexOf("?") != -1) {
parameterSeparator = '&';
}
return parameterSeparator;
}
public static boolean appendParameter(StringBuilder url, String parameterName,
String parameterValue) {
boolean result = false;
if (parameterName != null && !parameterName.isEmpty()
&& parameterValue != null && !parameterValue.isEmpty()) {
char parameterSeparator = getParameterSeparator(url);
url.append(parameterSeparator).append(parameterName.trim())
.append('=').append(parameterValue.trim());
}
return result;
}
}

View File

@ -294,15 +294,15 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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);
StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name));
Util.appendParameter(sUrl, "raw", ""+raw);
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);
LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType);
String result = HTTPUtils.post(sUrl.toString(), sldBody, contentType, gsuser, gspass);
return result != null;
}
@ -323,14 +323,14 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
* {@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);
StringBuilder sUrl = new StringBuilder(buildPostUrl(null, name));
Util.appendParameter(sUrl, "raw", ""+raw);
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);
LOGGER.debug("POSTing new style " + name + " to " + sUrl + " using version: " + contentType);
String result = HTTPUtils.post(sUrl.toString(), sldFile, contentType, gsuser, gspass);
return result != null;
}
@ -358,14 +358,14 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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);
StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null));
Util.appendParameter(sUrl, "raw", ""+raw);
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);
LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType);
String result = HTTPUtils.put(sUrl.toString(), sldFile, contentType, gsuser, gspass);
return result != null;
}
@ -393,14 +393,14 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
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);
StringBuilder sUrl = new StringBuilder(buildUrl(null, name, null));
Util.appendParameter(sUrl, "raw", ""+raw);
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);
LOGGER.debug("PUTting style " + name + " to " + sUrl + " using version: " + contentType);
String result = HTTPUtils.put(sUrl.toString(), sldBody, contentType, gsuser, gspass);
return result != null;
}