diff --git a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java
index dd7d527..db91b89 100644
--- a/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java
+++ b/src/main/java/it/geosolutions/geoserver/rest/HTTPUtils.java
@@ -36,6 +36,7 @@ import java.net.URL;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
@@ -59,68 +60,74 @@ public class HTTPUtils {
/**
* Performs an HTTP GET on the given URL.
- *
- * @param url The URL where to connect to.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ *
+ * @param url The URL where to connect to.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
*/
- public static String get(String url) throws MalformedURLException {
+ public static String get(String url) throws MalformedURLException {
return get(url, null, null);
}
/**
- * Performs an HTTP GET on the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * Performs an HTTP GET on the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
*/
- public static String get(String url, String username, String pw) throws MalformedURLException {
+ public static String get(String url, String username, String pw) throws MalformedURLException {
GetMethod httpMethod = null;
- try {
- HttpClient client = new HttpClient();
+ HttpClient client = new HttpClient();
+ HttpConnectionManager connectionManager = client.getHttpConnectionManager();
+ try {
setAuth(client, url, username, pw);
- httpMethod = new GetMethod(url);
- client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
- int status = client.executeMethod(httpMethod);
- if(status == HttpStatus.SC_OK) {
+ httpMethod = new GetMethod(url);
+ connectionManager.getParams().setConnectionTimeout(5000);
+ int status = client.executeMethod(httpMethod);
+ if (status == HttpStatus.SC_OK) {
InputStream is = httpMethod.getResponseBodyAsStream();
- String response = IOUtils.toString(is);
- if(response.trim().length()==0) { // sometime gs rest fails
- LOGGER.warn("ResponseBody is empty");
- return null;
- } else {
+ String response = IOUtils.toString(is);
+ IOUtils.closeQuietly(is);
+ if (response.trim().length() == 0) { // sometime gs rest fails
+ LOGGER.warn("ResponseBody is empty");
+ return null;
+ } else {
return response;
}
- } else {
- LOGGER.info("("+status+") " + HttpStatus.getStatusText(status) + " -- " + url );
- }
- } catch (ConnectException e) {
- LOGGER.info("Couldn't connect to ["+url+"]");
- } catch (IOException e) {
- LOGGER.info("Error talking to ["+url+"]", e);
- } finally {
- if(httpMethod != null)
+ } else {
+ LOGGER.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
+ }
+ } catch (ConnectException e) {
+ LOGGER.info("Couldn't connect to [" + url + "]");
+ } catch (IOException e) {
+ LOGGER.info("Error talking to [" + url + "]", e);
+ } finally {
+ if (httpMethod != null)
httpMethod.releaseConnection();
+ connectionManager.closeIdleConnections(0);
}
- return null;
- }
+ return null;
+ }
/**
- * PUTs a File to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param file The File to be sent.
+ * PUTs a File to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param file The File to be sent.
* @param contentType The content-type to advert in the PUT.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -129,15 +136,16 @@ public class HTTPUtils {
}
/**
- * PUTs a String to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param content The content to be sent as a String.
+ * PUTs a String to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param content The content to be sent as a String.
* @param contentType The content-type to advert in the PUT.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -151,14 +159,15 @@ public class HTTPUtils {
}
/**
- * PUTs a String representing an XML document to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param content The XML content to be sent as a String.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * PUTs a String representing an XML document to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param content The XML content to be sent as a String.
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -167,14 +176,15 @@ public class HTTPUtils {
}
/**
- * Performs a PUT to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
+ * Performs a PUT to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
* @param requestEntity The request to be sent.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -183,15 +193,16 @@ public class HTTPUtils {
}
/**
- * POSTs a File to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param file The File to be sent.
+ * POSTs a File to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param file The File to be sent.
* @param contentType The content-type to advert in the POST.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -200,15 +211,16 @@ public class HTTPUtils {
}
/**
- * POSTs a String to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param content The content to be sent as a String.
+ * POSTs a String to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param content The content to be sent as a String.
* @param contentType The content-type to advert in the POST.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -222,14 +234,15 @@ public class HTTPUtils {
}
/**
- * POSTs a String representing an XML document to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
- * @param content The XML content to be sent as a String.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * POSTs a String representing an XML document to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
+ * @param content The XML content to be sent as a String.
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -238,14 +251,15 @@ public class HTTPUtils {
}
/**
- * Performs a POST to the given URL.
- *
Basic auth is used if both username and pw are not null.
- *
- * @param url The URL where to connect to.
+ * Performs a POST to the given URL.
+ * Basic auth is used if both username and pw are not null.
+ *
+ * @param url The URL where to connect to.
* @param requestEntity The request to be sent.
- * @param username Basic auth credential. No basic auth if null.
- * @param pw Basic auth credential. No basic auth if null.
- * @return The HTTP response as a String if the HTTP response code was 200 (OK).
+ * @param username Basic auth credential. No basic auth if null.
+ * @param pw Basic auth credential. No basic auth if null.
+ * @return The HTTP response as a String if the HTTP response code was 200
+ * (OK).
* @throws MalformedURLException
* @return the HTTP response or null on errors.
*/
@@ -254,181 +268,192 @@ public class HTTPUtils {
}
/**
- * Send an HTTP request (PUT or POST) to a server.
- *
Basic auth is used if both username and pw are not null.
+ * Send an HTTP request (PUT or POST) to a server.
+ * Basic auth is used if both username and pw are not null.
*
- * Only