From d76216196089b4971abc3527416abc84565069d5 Mon Sep 17 00:00:00 2001 From: ccancellieri Date: Thu, 31 May 2012 17:23:09 +0200 Subject: [PATCH] improvements to the HTTPUtils. fixes to close #23. --- .../geoserver/rest/GeoServerRESTManager.java | 6 +- .../geoserver/rest/HTTPUtils.java | 42 +++++++- .../manager/GeoServerRESTAbstractManager.java | 53 ++++++---- .../GeoServerRESTDatastoreManager.java | 97 ++++++++++--------- 4 files changed, 124 insertions(+), 74 deletions(-) diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java index 1d4fc32..9e3d191 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTManager.java @@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.manager.GeoServerRESTAbstractManager; import it.geosolutions.geoserver.rest.manager.GeoServerRESTDatastoreManager; +import java.net.MalformedURLException; import java.net.URL; /** @@ -39,6 +40,7 @@ import java.net.URL; *
  • getFooManager, full-fledged management of catalog objects. * * @author Oscar Fonts + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { @@ -55,8 +57,10 @@ public class GeoServerRESTManager extends GeoServerRESTAbstractManager { * @param restURL GeoServer REST API endpoint * @param username GeoServer REST API authorized 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); // Internal publisher and reader, provide simple access methods. diff --git a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java index db91b89..a08da22 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java +++ b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java @@ -455,16 +455,50 @@ public class HTTPUtils { } /** - * recursively remove ending slashes - * * @param geoserverURL - * @return + * @return recursively remove ending slashes */ public static String decurtSlash(String geoserverURL) { - if (geoserverURL.endsWith("/")) { + if (geoserverURL!=null && geoserverURL.endsWith("/")) { geoserverURL = decurtSlash(geoserverURL.substring(0, geoserverURL.length() - 1)); } 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; + } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java index 6fc3464..dffa489 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTAbstractManager.java @@ -24,32 +24,43 @@ */ package it.geosolutions.geoserver.rest.manager; +import it.geosolutions.geoserver.rest.HTTPUtils; + +import java.net.MalformedURLException; import java.net.URL; /** - * Abstract manager, common functionality and interface - * for all GeoServerRESTFooManager classes. + * Abstract manager, common functionality and interface for all + * GeoServerRESTFooManager classes. * * @author Oscar Fonts + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ public abstract class GeoServerRESTAbstractManager { - - protected final URL restURL; - protected final String gsuser; - protected final String gspass; - - /** - * Default constructor. - * - * Indicates connection parameters to remote GeoServer instance. - * - * @param restURL GeoServer REST API endpoint - * @param username GeoServer REST API authorized username - * @param password GeoServer REST API password for the former username - */ - public GeoServerRESTAbstractManager(URL restURL, String username, String password) { - this.restURL = restURL; - this.gsuser = username; - this.gspass = password; - } + + protected final URL restURL; + protected final String gsuser; + protected final String gspass; + + /** + * Default constructor. + * + * Indicates connection parameters to remote GeoServer instance. + * + * @param restURL GeoServer REST API endpoint + * @param username GeoServer REST API authorized 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) + 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.gspass = password; + } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java index 36572be..06390f8 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java +++ b/src/main/java/it/geosolutions/geoserver/rest/manager/GeoServerRESTDatastoreManager.java @@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest.manager; import it.geosolutions.geoserver.rest.HTTPUtils; import it.geosolutions.geoserver.rest.encoder.datastore.GSAbstractDatastoreEncoder; +import java.net.MalformedURLException; import java.net.URL; /** @@ -36,57 +37,57 @@ import java.net.URL; * {@link GSAbstractDatastoreEncoder}. * * @author Oscar Fonts + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ public class GeoServerRESTDatastoreManager extends GeoServerRESTAbstractManager { - /** - * Default constructor. - * - * @param restURL GeoServer REST API endpoint - * @param username GeoServer REST API authorized username - * @param password GeoServer REST API password for the former username - */ - public GeoServerRESTDatastoreManager(URL restURL, String username, String password) { - super(restURL, username, password); - } - - /** - * Create a datastore. - * - * @param workspace - * Name of the workspace to contain the datastore. This will also - * be the prefix of any layer names contained in the datastore. - * @param datastore - * the set of parameters to be set to the datastore (including - * connection parameters). - * @return true if the datastore has been successfully - * created, false otherwise - */ + /** + * Default constructor. + * + * @param restURL GeoServer REST API endpoint + * @param username GeoServer REST API authorized username + * @param password GeoServer REST API password for the former username + * @throws MalformedURLException + * @throws IllegalArgumentException + */ + public GeoServerRESTDatastoreManager(URL restURL, String username, String password) + throws IllegalArgumentException, MalformedURLException { + super(restURL, username, password); + } - public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) { - String sUrl = restURL + "/rest/workspaces/" + workspace - + "/datastores/"; - String xml = datastore.toString(); - String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); - return result != null; - } + /** + * Create a datastore. + * + * @param workspace Name of the workspace to contain the datastore. This + * will also be the prefix of any layer names contained in the + * datastore. + * @param datastore the set of parameters to be set to the datastore + * (including connection parameters). + * @return true if the datastore has been successfully created, + * false otherwise + */ - /** - * Update a datastore. - * - * @param workspace - * Name of the workspace that contains the datastore. - * @param datastore - * the set of parameters to be set to the datastore (including - * connection parameters). - * @return true if the datastore has been successfully - * updated, false otherwise - */ - public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) { - String sUrl = restURL + "/rest/workspaces/" + workspace - + "/datastores/" + datastore.getName(); - String xml = datastore.toString(); - String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); - return result != null; - } + public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) { + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/").toString(); + String xml = datastore.toString(); + String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass); + return result != null; + } + + /** + * Update a datastore. + * + * @param workspace Name of the workspace that contains the datastore. + * @param datastore the set of parameters to be set to the datastore + * (including connection parameters). + * @return true if the datastore has been successfully updated, + * false otherwise + */ + public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) { + String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/", + datastore.getName()).toString(); + String xml = datastore.toString(); + String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass); + return result != null; + } }