improvements to the HTTPUtils. fixes to close #23.
This commit is contained in:
parent
314771e33d
commit
d762161960
@ -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.
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,32 +24,43 @@
|
|||||||
*/
|
*/
|
||||||
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 {
|
||||||
|
|
||||||
protected final URL restURL;
|
protected final URL restURL;
|
||||||
protected final String gsuser;
|
protected final String gsuser;
|
||||||
protected final String gspass;
|
protected final String gspass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
*
|
*
|
||||||
* Indicates connection parameters to remote GeoServer instance.
|
* Indicates connection parameters to remote GeoServer instance.
|
||||||
*
|
*
|
||||||
* @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) {
|
*/
|
||||||
this.restURL = restURL;
|
public GeoServerRESTAbstractManager(URL restURL, String username, String password)
|
||||||
this.gsuser = username;
|
throws IllegalArgumentException, MalformedURLException {
|
||||||
this.gspass = password;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,57 +37,57 @@ 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 {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
*
|
*
|
||||||
* @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
|
||||||
public GeoServerRESTDatastoreManager(URL restURL, String username, String password) {
|
* @throws IllegalArgumentException
|
||||||
super(restURL, username, password);
|
*/
|
||||||
}
|
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
|
|
||||||
*/
|
|
||||||
|
|
||||||
public boolean create(String workspace, GSAbstractDatastoreEncoder datastore) {
|
/**
|
||||||
String sUrl = restURL + "/rest/workspaces/" + workspace
|
* Create a datastore.
|
||||||
+ "/datastores/";
|
*
|
||||||
String xml = datastore.toString();
|
* @param workspace Name of the workspace to contain the datastore. This
|
||||||
String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass);
|
* will also be the prefix of any layer names contained in the
|
||||||
return result != null;
|
* 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) {
|
||||||
* Update a datastore.
|
String sUrl = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/datastores/").toString();
|
||||||
*
|
String xml = datastore.toString();
|
||||||
* @param workspace
|
String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass);
|
||||||
* Name of the workspace that contains the datastore.
|
return result != null;
|
||||||
* @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
|
* Update a datastore.
|
||||||
* updated, <TT>false</TT> otherwise
|
*
|
||||||
*/
|
* @param workspace Name of the workspace that contains the datastore.
|
||||||
public boolean update(String workspace, GSAbstractDatastoreEncoder datastore) {
|
* @param datastore the set of parameters to be set to the datastore
|
||||||
String sUrl = restURL + "/rest/workspaces/" + workspace
|
* (including connection parameters).
|
||||||
+ "/datastores/" + datastore.getName();
|
* @return <TT>true</TT> if the datastore has been successfully updated,
|
||||||
String xml = datastore.toString();
|
* <TT>false</TT> otherwise
|
||||||
String result = HTTPUtils.putXml(sUrl, xml, gsuser, gspass);
|
*/
|
||||||
return result != null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user