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.GeoServerRESTDatastoreManager;
import java.net.MalformedURLException;
import java.net.URL;
/**
@ -39,6 +40,7 @@ import java.net.URL;
* <li>get<i>Foo</i>Manager, full-fledged management of catalog objects.
* </ul>
* @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.

View File

@ -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;
}
}

View File

@ -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 GeoServerREST<i>Foo</i>Manager classes.
* Abstract manager, common functionality and interface for all
* GeoServerREST<i>Foo</i>Manager 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;
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;
}
/**
* 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;
}
}

View File

@ -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);
}
/**
* 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);
}
/**
* 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 <TT>true</TT> if the datastore has been successfully
* created, <TT>false</TT> otherwise
*/
/**
* 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 <TT>true</TT> if the datastore has been successfully created,
* <TT>false</TT> otherwise
*/
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;
}
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 <TT>true</TT> if the datastore has been successfully
* updated, <TT>false</TT> 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;
}
/**
* 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 <TT>true</TT> if the datastore has been successfully updated,
* <TT>false</TT> 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;
}
}