org.enhydra.barracuda.core.util.http
Class HttpRequester

java.lang.Object
  |
  +--org.enhydra.barracuda.core.util.http.HttpRequester

public class HttpRequester
extends java.lang.Object

This class encapsulates access to/from a URL via both POST and GET methods. To use, simply set the URL, the method (POST/GET), and the params. If you're using get, the params are optional (they can be included as part of the URL). Also note that you can pass a username and password if you need to do basic authentication Refer to the source for this class (main method) to see an example of how you would use this class for both POST and GET methods:


Field Summary
protected  boolean authenticate
           
static java.lang.String GET
           
protected  java.io.BufferedReader in
           
protected  java.io.InputStream inStream
           
protected  java.lang.String method
           
protected  HttpOutputWriter outputWriter
           
protected  java.io.OutputStream outStream
           
protected  java.lang.String password
           
static java.lang.String POST
           
protected  java.util.Map props
           
protected  java.net.URL url
           
protected  java.lang.String user
           
 
Constructor Summary
HttpRequester()
           
 
Method Summary
 void connect()
          Connect to the URL
 void disconnect()
          Disconnect from the URL.
 java.io.InputStream getInputStream()
          Get the underlying input stream
 java.lang.String getMethod()
          Get the method we're using for this HttpRequest object
 java.io.OutputStream getOutputStream()
          Get the underlying output stream
 HttpOutputWriter getOutputWriter()
          Return the output writer.
 java.util.Map getParams()
          Return the HashMap object for this HttpRequest.
protected  java.lang.String getPassword()
          Get the password
 java.net.URL getUrl()
          Get the URL for the HttpRequest object
 java.lang.String getUser()
          Get the user name
static void main(java.lang.String[] args)
           
 java.lang.String readLine()
          Read responses from the URL
 void setMethod(java.lang.String imethod)
          Set the method we wish to use.
 void setOutputWriter(HttpOutputWriter ioutputWriter)
          Set the output writer to be used for posting data
 void setParams(java.util.Map iprops)
          Set the parmeters we wish to pass to the URL as name-value pairs.
 void setPassword(java.lang.String ipassword)
          Set the password (if we need to authenticate in order to make the connection)
 void setRequest(java.lang.String iurl, java.lang.String imethod, java.util.Map iprops)
          Set the Request.
 void setRequest(java.lang.String iurl, java.lang.String imethod, java.util.Map iprops, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(java.lang.String iurl, java.lang.String imethod, java.util.Map iprops, java.lang.String iuser, java.lang.String ipwd, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(java.net.URL iurl, java.lang.String imethod, java.util.Map iprops)
          Set the Request.
 void setRequest(java.net.URL iurl, java.lang.String imethod, java.util.Map iprops, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(java.net.URL iurl, java.lang.String imethod, java.util.Map iprops, java.lang.String iuser, java.lang.String ipwd, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setUrl(java.lang.String iurl)
          Set the URL we wish to access
 void setUrl(java.net.URL iurl)
          Set the URL we wish to access
 void setUser(java.lang.String iuser)
          Set the user (if we need to authenticate in order to make the connection)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POST

public static final java.lang.String POST
See Also:
Constant Field Values

GET

public static final java.lang.String GET
See Also:
Constant Field Values

url

protected java.net.URL url

method

protected java.lang.String method

props

protected java.util.Map props

outputWriter

protected HttpOutputWriter outputWriter

user

protected java.lang.String user

password

protected java.lang.String password

authenticate

protected boolean authenticate

outStream

protected java.io.OutputStream outStream

inStream

protected java.io.InputStream inStream

in

protected java.io.BufferedReader in
Constructor Detail

HttpRequester

public HttpRequester()
Method Detail

setRequest

public void setRequest(java.lang.String iurl,
                       java.lang.String imethod,
                       java.util.Map iprops)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setRequest

public void setRequest(java.net.URL iurl,
                       java.lang.String imethod,
                       java.util.Map iprops)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setRequest

public void setRequest(java.lang.String iurl,
                       java.lang.String imethod,
                       java.util.Map iprops,
                       HttpOutputWriter ioutputWriter)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setRequest

public void setRequest(java.net.URL iurl,
                       java.lang.String imethod,
                       java.util.Map iprops,
                       HttpOutputWriter ioutputWriter)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setRequest

public void setRequest(java.lang.String iurl,
                       java.lang.String imethod,
                       java.util.Map iprops,
                       java.lang.String iuser,
                       java.lang.String ipwd,
                       HttpOutputWriter ioutputWriter)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setRequest

public void setRequest(java.net.URL iurl,
                       java.lang.String imethod,
                       java.util.Map iprops,
                       java.lang.String iuser,
                       java.lang.String ipwd,
                       HttpOutputWriter ioutputWriter)
                throws java.net.MalformedURLException
Set the Request. This is a convenience method to encapsulate calls to setUrl, setMethod, and setParams all in one fell swoop.

Throws:
java.net.MalformedURLException

setUrl

public void setUrl(java.lang.String iurl)
            throws java.net.MalformedURLException
Set the URL we wish to access

Throws:
java.net.MalformedURLException

setUrl

public void setUrl(java.net.URL iurl)
Set the URL we wish to access


getUrl

public java.net.URL getUrl()
Get the URL for the HttpRequest object

Returns:
the URL behind this request

setMethod

public void setMethod(java.lang.String imethod)
Set the method we wish to use. Valid values are either GET or POST. Default is GET.


getMethod

public java.lang.String getMethod()
Get the method we're using for this HttpRequest object

Returns:
the method we're using (either GET or POST)

setParams

public void setParams(java.util.Map iprops)
Set the parmeters we wish to pass to the URL as name-value pairs. If you are using the POST method, it will look for properties in here. If you are using the get method, you can manually pass the properties as part of the URL string, and just ignore this method.


getParams

public java.util.Map getParams()
Return the HashMap object for this HttpRequest. If the map is null (ie. because you are using the GET method), we attempt to look for the properties in the actual URL string and build a HashMap from that.

Returns:
a Map containing all the parameters for this HttpRequest

setUser

public void setUser(java.lang.String iuser)
Set the user (if we need to authenticate in order to make the connection)


getUser

public java.lang.String getUser()
Get the user name

Returns:
the user name

setPassword

public void setPassword(java.lang.String ipassword)
Set the password (if we need to authenticate in order to make the connection)


getPassword

protected java.lang.String getPassword()
Get the password

Returns:
the password

setOutputWriter

public void setOutputWriter(HttpOutputWriter ioutputWriter)
Set the output writer to be used for posting data

Parameters:
ioutputWriter - the HttpOutputWriter

getOutputWriter

public HttpOutputWriter getOutputWriter()
Return the output writer. If none is set, the default will be used.

Returns:
the HttpOutputWriter

connect

public void connect()
             throws java.net.ConnectException,
                    java.io.IOException
Connect to the URL

Throws:
java.net.ConnectException
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.net.ConnectException,
                                 java.io.IOException
Read responses from the URL

Returns:
a String representation of what we got back
Throws:
java.net.ConnectException
java.io.IOException

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.net.ConnectException
Get the underlying output stream

Returns:
the output stream
Throws:
java.net.ConnectException

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.net.ConnectException
Get the underlying input stream

Returns:
the input stream
Throws:
java.net.ConnectException

disconnect

public void disconnect()
Disconnect from the URL. You really only need to call this if you terminate the readLine process on your end. if readLine() encounters a null value, it assumes input is complete and automatically calls this method.


main

public static void main(java.lang.String[] args)


Copyright © 2001 Enhydra.org