it.geosolutions.geonetwork.util
Class HTTPUtils

java.lang.Object
  extended by it.geosolutions.geonetwork.util.HTTPUtils

public class HTTPUtils
extends Object

Low level HTTP utilities.


Constructor Summary
HTTPUtils()
           
HTTPUtils(String userName, String password)
           
 
Method Summary
 boolean delete(String url)
           
 boolean exists(String url)
          Used to query for REST resources.
 String get(String url)
          Performs an HTTP GET on the given URL.
protected static String getGeoNetworkErrorMessage(InputStream msg)
           
protected static String getGeoNetworkErrorMessage(String msg)
           
 int getLastHttpStatus()
           
 boolean httpPing(String url)
           
 boolean isIgnoreResponseContentOnSuccess()
           
 String post(String url, File file, String contentType)
          POSTs a File to the given URL.
 String post(String url, InputStream content, String contentType)
          POSTs a Stream content to the given URL.
 String post(String url, org.apache.commons.httpclient.methods.RequestEntity requestEntity)
          Performs a POST to the given URL.
 String post(String url, String content, String contentType)
          POSTs a String to the given URL.
 String postXml(String url, InputStream content)
          POSTs a Stream content representing an XML document to the given URL.
 String postXml(String url, String content)
          POSTs a String representing an XML document to the given URL.
 String put(String url, File file, String contentType)
          PUTs a File to the given URL.
 String put(String url, org.apache.commons.httpclient.methods.RequestEntity requestEntity)
          Performs a PUT to the given URL.
 String put(String url, String content, String contentType)
          PUTs a String to the given URL.
 String putXml(String url, String content)
          PUTs a String representing an XML document to the given URL.
protected  String send(org.apache.commons.httpclient.methods.EntityEnclosingMethod httpMethod, String url, org.apache.commons.httpclient.methods.RequestEntity requestEntity)
          Send an HTTP request (PUT or POST) to a server.
 void setIgnoreResponseContentOnSuccess(boolean ignoreResponseContentOnSuccess)
           
 void setXmlContentType(String xmlContentType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPUtils

public HTTPUtils()

HTTPUtils

public HTTPUtils(String userName,
                 String password)
Method Detail

setXmlContentType

public void setXmlContentType(String xmlContentType)

getLastHttpStatus

public int getLastHttpStatus()

isIgnoreResponseContentOnSuccess

public boolean isIgnoreResponseContentOnSuccess()

setIgnoreResponseContentOnSuccess

public void setIgnoreResponseContentOnSuccess(boolean ignoreResponseContentOnSuccess)

get

public 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

putXml

public String putXml(String url,
                     String content)
PUTs a String representing an XML document to the given URL.

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

put

public String put(String url,
                  File file,
                  String contentType)
PUTs a File to the given URL.

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

put

public String put(String url,
                  String content,
                  String contentType)
PUTs a String to the given URL.

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.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

put

public String put(String url,
                  org.apache.commons.httpclient.methods.RequestEntity requestEntity)
Performs a PUT to the given URL.

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

postXml

public String postXml(String url,
                      String content)
POSTs a String representing an XML document to the given URL.

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

postXml

public String postXml(String url,
                      InputStream content)
POSTs a Stream content representing an XML document to the given URL.

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

post

public String post(String url,
                   File file,
                   String contentType)
POSTs a File to the given URL.

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

post

public String post(String url,
                   String content,
                   String contentType)
POSTs a String to the given URL.

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.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

post

public String post(String url,
                   InputStream content,
                   String contentType)
POSTs a Stream content to the given URL.

Parameters:
url - The URL where to connect to.
content - The content to be sent as an InputStream.
contentType - The content-type to advert in the POST.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

post

public String post(String url,
                   org.apache.commons.httpclient.methods.RequestEntity requestEntity)
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.
Returns:
The HTTP response as a String if the HTTP response code was 200 (OK).
Throws:
MalformedURLException

send

protected String send(org.apache.commons.httpclient.methods.EntityEnclosingMethod httpMethod,
                      String url,
                      org.apache.commons.httpclient.methods.RequestEntity requestEntity)
Send an HTTP request (PUT or POST) to a server.
Basic auth is used if both username and pw are not null.

Only

are accepted as successful codes; in these cases the response string will be returned.

Returns:
the HTTP response or null on errors.

delete

public boolean delete(String url)

httpPing

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

exists

public boolean exists(String url)
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.

getGeoNetworkErrorMessage

protected static String getGeoNetworkErrorMessage(String msg)

getGeoNetworkErrorMessage

protected static String getGeoNetworkErrorMessage(InputStream msg)


Copyright © 2011-2012 GeoSolutions. All Rights Reserved.