it.geosolutions.geoserver.rest
Class HTTPUtils

java.lang.Object
  extended by it.geosolutions.geoserver.rest.HTTPUtils

public class HTTPUtils
extends Object

Low level HTTP utilities.


Constructor Summary
HTTPUtils()
           
 
Method Summary
static StringBuffer append(String... str)
           
static StringBuffer append(URL base, String... str)
          Wrapper for append(String...)
static String decurtSlash(String geoserverURL)
           
static boolean delete(String url, String user, String pw)
           
static boolean exists(String url, String username, String pw)
          Used to query for REST resources.
static String get(String url)
          Performs an HTTP GET on the given URL.
static String get(String url, String username, String pw)
          Performs an HTTP GET on the given URL.
static boolean httpPing(String url)
           
static boolean httpPing(String url, String username, String pw)
           
static String post(String url, File file, String contentType, String username, String pw)
          POSTs a File to the given URL.
static String post(String url, org.apache.commons.httpclient.methods.RequestEntity requestEntity, String username, String pw)
          Performs a POST to the given URL.
static String post(String url, String content, String contentType, String username, String pw)
          POSTs a String to the given URL.
static String postXml(String url, String content, String username, String pw)
          POSTs a String representing an XML document to the given URL.
static String put(String url, File file, String contentType, String username, String pw)
          PUTs a File to the given URL.
static String put(String url, org.apache.commons.httpclient.methods.RequestEntity requestEntity, String username, String pw)
          Performs a PUT to the given URL.
static String put(String url, String content, String contentType, String username, String pw)
          PUTs a String to the given URL.
static String putXml(String url, String content, String username, String pw)
          PUTs a String representing an XML document to the given URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPUtils

public HTTPUtils()
Method Detail

get

public static String get(String url)
                  throws MalformedURLException
Performs an HTTP GET on the given URL.

Parameters:
url - The URL where to connect to.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

get

public static String get(String url,
                         String username,
                         String pw)
                  throws MalformedURLException
Performs an HTTP GET on the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

put

public static String put(String url,
                         File file,
                         String contentType,
                         String username,
                         String pw)
PUTs a File to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
file - The File to be sent.
contentType - The content-type to advert in the PUT.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

put

public static String put(String url,
                         String content,
                         String contentType,
                         String username,
                         String pw)
PUTs a String to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
content - The content to be sent as a String.
contentType - The content-type to advert in the PUT.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

putXml

public static String putXml(String url,
                            String content,
                            String username,
                            String pw)
PUTs a String representing an XML document to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
content - The XML content to be sent as a String.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

put

public static String put(String url,
                         org.apache.commons.httpclient.methods.RequestEntity requestEntity,
                         String username,
                         String pw)
Performs a PUT to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
requestEntity - The request to be sent.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

post

public static String post(String url,
                          File file,
                          String contentType,
                          String username,
                          String pw)
POSTs a File to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
file - The File to be sent.
contentType - The content-type to advert in the POST.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

post

public static String post(String url,
                          String content,
                          String contentType,
                          String username,
                          String pw)
POSTs a String to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
content - The content to be sent as a String.
contentType - The content-type to advert in the POST.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

postXml

public static String postXml(String url,
                             String content,
                             String username,
                             String pw)
POSTs a String representing an XML document to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
content - The XML content to be sent as a String.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

post

public static String post(String url,
                          org.apache.commons.httpclient.methods.RequestEntity requestEntity,
                          String username,
                          String pw)
Performs a POST to the given URL.
Basic auth is used if both username and pw are not null.

Parameters:
url - The URL where to connect to.
requestEntity - The request to be sent.
username - Basic auth credential. No basic auth if null.
pw - Basic auth credential. No basic auth if null.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

delete

public static boolean delete(String url,
                             String user,
                             String pw)

httpPing

public static boolean httpPing(String url)
Returns:
true if the server response was an HTTP_OK

httpPing

public static boolean httpPing(String url,
                               String username,
                               String pw)

exists

public static boolean exists(String url,
                             String username,
                             String pw)
Used to query for REST resources.

Parameters:
url - The URL of the REST resource to query about.
username -
pw -
Returns:
true on 200, false on 404.
Throws:
RuntimeException - on unhandled status or exceptions.

decurtSlash

public static String decurtSlash(String geoserverURL)
Parameters:
geoserverURL -
Returns:
recursively remove ending slashes

append

public static StringBuffer append(String... str)
Parameters:
str - a string array
Returns:
create a StringBuffer appending all the passed arguments

append

public static StringBuffer append(URL base,
                                  String... str)
Wrapper for append(String...)

Parameters:
base - base URL
str - strings to append
Returns:
the base URL with parameters attached


Copyright © 2007-2013 GeoSolutions. All Rights Reserved.