improvements to the HTTPUtils. fixes to close #23.

This commit is contained in:
ccancellieri 2012-05-31 17:23:09 +02:00
parent 314771e33d
commit d762161960
4 changed files with 124 additions and 74 deletions

View File

@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTDatastoreManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTDatastoreManager;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
/** /**
@ -39,6 +40,7 @@ import java.net.URL;
* <li>get<i>Foo</i>Manager, full-fledged management of catalog objects. * <li>get<i>Foo</i>Manager, full-fledged management of catalog objects.
* </ul> * </ul>
* @author Oscar Fonts * @author Oscar Fonts
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/ */
public class GeoServerRESTManager extends GeoServerRESTAbstractManager { public class GeoServerRESTManager extends GeoServerRESTAbstractManager {
@ -55,8 +57,10 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager {
* @param restURL GeoServer REST API endpoint * @param restURL GeoServer REST API endpoint
* @param username GeoServer REST API authorized username * @param username GeoServer REST API authorized username
* @param password GeoServer REST API password for the former username * @param password GeoServer REST API password for the former username
* @throws MalformedURLException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)}
* @throws IllegalArgumentException {@link GeoServerRESTAbstractManager#GeoServerRESTAbstractManager(URL, String, String)}
*/ */
public GeoServerRESTManager(URL restURL, String username, String password) { public GeoServerRESTManager(URL restURL, String username, String password) throws IllegalArgumentException, MalformedURLException {
super(restURL, username, password); super(restURL, username, password);
// Internal publisher and reader, provide simple access methods. // Internal publisher and reader, provide simple access methods.

View File

@ -455,16 +455,50 @@ public class HTTPUtils {
} }
/** /**
* recursively remove ending slashes
*
* @param geoserverURL * @param geoserverURL
* @return * @return recursively remove ending slashes
*/ */
public static String decurtSlash(String geoserverURL) { public static String decurtSlash(String geoserverURL) {
if (geoserverURL.endsWith("/")) { if (geoserverURL!=null && geoserverURL.endsWith("/")) {
geoserverURL = decurtSlash(geoserverURL.substring(0, geoserverURL.length() - 1)); geoserverURL = decurtSlash(geoserverURL.substring(0, geoserverURL.length() - 1));
} }
return geoserverURL; return geoserverURL;
} }
/**
* @param str a string array
* @return create a StringBuffer appending all the passed arguments
*/
public static StringBuffer append(String ... str){
if (str==null){
return null;
}
StringBuffer buf=new StringBuffer();
for (String s: str){
if (s!=null)
buf.append(s);
}
return buf;
}
/**
* Wrapper for {@link #append(String...)}
* @param base base URL
* @param str strings to append
* @return the base URL with parameters attached
*/
public static StringBuffer append(URL base, String ... str){
if (str==null){
return append(base.toString());
}
StringBuffer buf=new StringBuffer(base.toString());
for (String s: str){
if (s!=null)
buf.append(s);
}
return buf;
}
} }

View File

@ -24,13 +24,17 @@
*/ */
package it.geosolutions.geoserver.rest.manager; package it.geosolutions.geoserver.rest.manager;
import it.geosolutions.geoserver.rest.HTTPUtils;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
/** /**
* Abstract manager, common functionality and interface * Abstract manager, common functionality and interface for all
* for all GeoServerREST<i>Foo</i>Manager classes. * GeoServerREST<i>Foo</i>Manager classes.
* *
* @author Oscar Fonts * @author Oscar Fonts
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/ */
public abstract class GeoServerRESTAbstractManager { public abstract class GeoServerRESTAbstractManager {
@ -46,9 +50,16 @@ public abstract class GeoServerRESTAbstractManager {
* @param restURL GeoServer REST API endpoint * @param restURL GeoServer REST API endpoint
* @param username GeoServer REST API authorized username * @param username GeoServer REST API authorized username
* @param password GeoServer REST API password for the former username * @param password GeoServer REST API password for the former username
* @throws MalformedURLException if restURL is malformed
*/ */
public GeoServerRESTAbstractManager(URL restURL, String username, String password) { public GeoServerRESTAbstractManager(URL restURL, String username, String password)
this.restURL = restURL; throws IllegalArgumentException, MalformedURLException {
if (restURL == null || username == null || password == null)
throw new IllegalArgumentException("Unable to create the manager using a null argument");
this.restURL = new URL(restURL.getProtocol(), restURL.getHost(), restURL.getPort(),
HTTPUtils.decurtSlash(restURL.getPath()), null);
this.gsuser = username; this.gsuser = username;
this.gspass = password; this.gspass = password;
} }

View File

@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest.manager;
import it.geosolutions.geoserver.rest.HTTPUtils; import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
/** /**
@ -36,6 +37,7 @@ import java.net.URL;
* {@link GSAbstractDatastoreEncoder}. * {@link GSAbstractDatastoreEncoder}.
* *
* @author Oscar Fonts * @author Oscar Fonts
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/ */
public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager { public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager {
@ -45,27 +47,28 @@ public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager
* @param restURL GeoServer REST API endpoint * @param restURL GeoServer REST API endpoint
* @param username GeoServer REST API authorized username * @param username GeoServer REST API authorized username
* @param password GeoServer REST API password for the former username * @param password GeoServer REST API password for the former username
* @throws MalformedURLException
* @throws IllegalArgumentException
*/ */
public GeoServerRESTDatastoreManager(URL restURL, String username, String password) { public GeoServerRESTDatastoreManager(URL restURL, String username, String password)
throws IllegalArgumentException, MalformedURLException {
super(restURL, username, password); super(restURL, username, password);
} }
/** /**
* Create a datastore. * Create a datastore.
* *
* @param workspace * @param workspace Name of the workspace to contain the datastore. This
* Name of the workspace to contain the datastore. This will also * will also be the prefix of any layer names contained in the
* be the prefix of any layer names contained in the datastore. * datastore.
* @param datastore * @param datastore the set of parameters to be set to the datastore
* the set of parameters to be set to the datastore (including * (including connection parameters).
* connection parameters). * @return <TT>true</TT> if the datastore has been successfully created,
* @return <TT>true</TT> if the datastore has been successfully * <TT>false</TT> otherwise
* created, <TT>false</TT> otherwise
*/ */
public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) { public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) {
String sUrl = restURL + "/rest/workspaces/" + workspace String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/").toString();
+ "/datastores/";
String xml = datastore.toString(); String xml = datastore.toString();
String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass);
return result != null; return result != null;
@ -74,17 +77,15 @@ public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager
/** /**
* Update a datastore. * Update a datastore.
* *
* @param workspace * @param workspace Name of the workspace that contains the datastore.
* Name of the workspace that contains the datastore. * @param datastore the set of parameters to be set to the datastore
* @param datastore * (including connection parameters).
* the set of parameters to be set to the datastore (including * @return <TT>true</TT> if the datastore has been successfully updated,
* connection parameters). * <TT>false</TT> otherwise
* @return <TT>true</TT> if the datastore has been successfully
* updated, <TT>false</TT> otherwise
*/ */
public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) { public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) {
String sUrl = restURL + "/rest/workspaces/" + workspace String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/",
+ "/datastores/" + datastore.getName(); datastore.getName()).toString();
String xml = datastore.toString(); String xml = datastore.toString();
String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass);
return result != null; return result != null;