com.buglabs.util.simplerestclient
Class HTTPRequest

java.lang.Object
  extended by com.buglabs.util.simplerestclient.HTTPRequest

public class HTTPRequest
extends java.lang.Object

class for dealing RESTfully with HTTP Requests Example Usage: HttpRequest req = new HttpRequest(myConnectionProvider) HttpResponse resp = req.get("http://some.url") System.out.println(resp.getString());


Constructor Summary
HTTPRequest()
          constructor that uses default connection provider
HTTPRequest(IConnectionProvider connectionProvider)
          constructor where client provides connectionProvider
 
Method Summary
 HTTPResponse delete(java.lang.String url)
          Do an HTTP DELETE to url
 HTTPResponse get(java.lang.String url)
          Do an authenticated HTTP GET from url
 HTTPResponse head(java.lang.String url)
          Do an HTTP HEAD to url
 HTTPResponse post(java.lang.String url, byte[] data)
          Post byte data to a url
 HTTPResponse post(java.lang.String url, java.io.InputStream stream)
          Do an HTTP POST to url
 HTTPResponse post(java.lang.String url, java.util.Map properties)
          Posts a Map of key, value pair properties, like a web form
 HTTPResponse post(java.lang.String url, java.lang.String data)
          Do an HTTP POST to url
 HTTPResponse post(java.lang.String url, java.lang.String data, java.util.Map headers)
          Do an HTTP POST to url w/ extra http headers
 HTTPResponse postMultipart(java.lang.String url, java.util.Map parameters)
          Does a multipart post which is different than a regular post mostly use this one if you're posting files
 HTTPResponse put(java.lang.String url, java.io.InputStream stream)
          Do an HTTP PUT to url
 HTTPResponse put(java.lang.String url, java.util.Map properties)
          Puts a Map of key, value pair properties, like a web form
 HTTPResponse put(java.lang.String url, java.lang.String data)
          Do an HTTP PUT to url
 HTTPResponse put(java.lang.String url, java.lang.String data, java.util.Map headers)
          Do an HTTP PUT to url with extra headers
static byte[] streamToByteArray(java.io.InputStream in)
          A simple helper function
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPRequest

public HTTPRequest(IConnectionProvider connectionProvider)
constructor where client provides connectionProvider


HTTPRequest

public HTTPRequest()
constructor that uses default connection provider

Method Detail

get

public HTTPResponse get(java.lang.String url)
                 throws java.io.IOException
Do an authenticated HTTP GET from url

Parameters:
url - String URL to connect to
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

post

public HTTPResponse post(java.lang.String url,
                         java.lang.String data)
                  throws java.io.IOException
Do an HTTP POST to url

Parameters:
url - String URL to connect to
data - String data to post
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

post

public HTTPResponse post(java.lang.String url,
                         java.lang.String data,
                         java.util.Map headers)
                  throws java.io.IOException
Do an HTTP POST to url w/ extra http headers

Parameters:
url -
data -
headers -
Returns:
Throws:
java.io.IOException

post

public HTTPResponse post(java.lang.String url,
                         java.io.InputStream stream)
                  throws java.io.IOException
Do an HTTP POST to url

Parameters:
url - String URL to connect to
stream - InputStream data to post
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

post

public HTTPResponse post(java.lang.String url,
                         java.util.Map properties)
                  throws java.io.IOException
Posts a Map of key, value pair properties, like a web form

Parameters:
url -
properties -
Returns:
Throws:
java.io.IOException

post

public HTTPResponse post(java.lang.String url,
                         byte[] data)
                  throws java.io.IOException
Post byte data to a url

Parameters:
url -
data -
Returns:
Throws:
java.io.IOException

postMultipart

public HTTPResponse postMultipart(java.lang.String url,
                                  java.util.Map parameters)
                           throws java.io.IOException
Does a multipart post which is different than a regular post mostly use this one if you're posting files

Parameters:
url -
parameters - Key-Value pairs in map. Keys are always string. Values can be string or IFormFile
properties -
Returns:
Throws:
java.io.IOException

put

public HTTPResponse put(java.lang.String url,
                        java.lang.String data)
                 throws java.io.IOException
Do an HTTP PUT to url

Parameters:
url - String URL to connect to
data - String data to post
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

put

public HTTPResponse put(java.lang.String url,
                        java.lang.String data,
                        java.util.Map headers)
                 throws java.io.IOException
Do an HTTP PUT to url with extra headers

Parameters:
url -
data -
headers -
Returns:
Throws:
java.io.IOException

put

public HTTPResponse put(java.lang.String url,
                        java.io.InputStream stream)
                 throws java.io.IOException
Do an HTTP PUT to url

Parameters:
url - String URL to connect to
stream - InputStream data to put
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

delete

public HTTPResponse delete(java.lang.String url)
                    throws java.io.IOException
Do an HTTP DELETE to url

Parameters:
url -
Returns:
Throws:
java.io.IOException

put

public HTTPResponse put(java.lang.String url,
                        java.util.Map properties)
                 throws java.io.IOException
Puts a Map of key, value pair properties, like a web form

Parameters:
url -
properties -
Returns:
Throws:
java.io.IOException

head

public HTTPResponse head(java.lang.String url)
                  throws java.io.IOException
Do an HTTP HEAD to url

Parameters:
url - String URL to connect to
Returns:
HttpURLConnection ready with response data
Throws:
java.io.IOException

streamToByteArray

public static byte[] streamToByteArray(java.io.InputStream in)
                                throws java.io.IOException
A simple helper function

Parameters:
in - InputStream to turn into a byte array
Returns:
byte array (byte[]) w/ contents of input stream
Throws:
java.io.IOException