org.enhydra.barracuda.plankton.http
Class HttpRequester

java.lang.Object
  extended byorg.enhydra.barracuda.plankton.http.HttpRequester

public class HttpRequester
extends 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. This class also now supports cookies, thanks to Shawn Wilson [shawnw@atmreports.com] - look at the sample code down in the main method for an example of how to use it (basically, you just use the requestor to access a URL, thereby getting the cookie, and then you re-use the requestor to access any other URLs which depend on that cookie). 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 acceptCookies
           
protected  boolean authenticate
           
protected  URLConnection conn
           
protected  List cookies
           
static String GET
           
protected  BufferedReader in
           
protected  InputStream inStream
           
protected  String method
           
protected  HttpOutputWriter outputWriter
           
protected  OutputStream outStream
           
protected  String password
           
static String POST
           
protected  Map props
           
protected  URL url
           
protected  String user
           
 
Constructor Summary
HttpRequester()
           
 
Method Summary
 void clearCookies()
          Clear any cookies this client knows about.
 void connect()
          Connect to the URL
 void disconnect()
          Disconnect from the URL.
 boolean getAcceptCookies()
          Determine whether or not we are accepting cookies.
 List getCookies()
          Return a list of cookies this client is sending to the server
 InputStream getInputStream()
          Get the underlying input stream
 String getMethod()
          Get the method we're using for this HttpRequest object
 OutputStream getOutputStream()
          Get the underlying output stream
 HttpOutputWriter getOutputWriter()
          Return the output writer.
 Map getParams()
          Return the HashMap object for this HttpRequest.
protected  String getPassword()
          Get the password
 URL getUrl()
          Get the URL for the HttpRequest object
 URLConnection getURLConnection()
          Return the connection.
 String getUser()
          Get the user name
static void main(String[] args)
           
 String readLine()
          Read responses from the URL
 void setAcceptCookies(boolean accept)
          Set whether or not to accept cookies from the server.
 void setMethod(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(Map iprops)
          Set the parmeters we wish to pass to the URL as name-value pairs.
 void setPassword(String ipwd)
          Set the password (if we need to authenticate in order to make the connection)
 void setRequest(String iurl, String imethod, Map iprops)
          Set the Request.
 void setRequest(String iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(String iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(URL iurl, String imethod, Map iprops)
          Set the Request.
 void setRequest(URL iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setRequest(URL iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter)
          Set the Request.
 void setUrl(String iurl)
          Set the URL we wish to access
 void setUrl(URL iurl)
          Set the URL we wish to access
 void setUser(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 String POST
See Also:
Constant Field Values

GET

public static final String GET
See Also:
Constant Field Values

url

protected URL url

method

protected String method

props

protected Map props

outputWriter

protected HttpOutputWriter outputWriter

user

protected String user

password

protected String password

authenticate

protected boolean authenticate

acceptCookies

protected boolean acceptCookies

cookies

protected List cookies

outStream

protected OutputStream outStream

inStream

protected InputStream inStream

in

protected BufferedReader in

conn

protected URLConnection conn
Constructor Detail

HttpRequester

public HttpRequester()
Method Detail

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
Throws:
MalformedURLException

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
Throws:
MalformedURLException

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
ioutputWriter - the HttpOutputWriter we wish to write to
Throws:
MalformedURLException

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
ioutputWriter - the HttpOutputWriter we wish to write to
Throws:
MalformedURLException

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
iuser - the user named required to connect
ipwd - the password named required to connect
ioutputWriter - the HttpOutputWriter we wish to write to
Throws:
MalformedURLException

setRequest

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

Parameters:
iurl - the URL we wish to access
imethod - the method we wish to use (either GET or POST)
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.
iuser - the user named required to connect
ipwd - the password named required to connect
ioutputWriter - the HttpOutputWriter we wish to write to
Throws:
MalformedURLException

setUrl

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

Parameters:
iurl - the URL we wish to access
Throws:
MalformedURLException

setUrl

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

Parameters:
iurl - the URL we wish to access

getUrl

public URL getUrl()
Get the URL for the HttpRequest object

Returns:
the URL behind this request

setMethod

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

Parameters:
imethod - the method we wish to use (either GET or POST)

getMethod

public 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(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.

Parameters:
iprops - the Map contains our key-value URL parameter pairs. If the value is a Set, the resulting URL will contain a key-value mapping for each entry in the Set.

getParams

public 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(String iuser)
Set the user (if we need to authenticate in order to make the connection)

Parameters:
iuser - the user name

getUser

public String getUser()
Get the user name

Returns:
the user name

setPassword

public void setPassword(String ipwd)
Set the password (if we need to authenticate in order to make the connection)

Parameters:
ipwd - the password

getPassword

protected String getPassword()
Get the password

Returns:
the password

setAcceptCookies

public void setAcceptCookies(boolean accept)
Set whether or not to accept cookies from the server. The default is true. Setting this value to false after cookies have already been obtained does not clear the current cookies, it simply will not accept any new cookies.

See Also:
clearCookies()

getAcceptCookies

public boolean getAcceptCookies()
Determine whether or not we are accepting cookies.


getCookies

public List getCookies()
Return a list of cookies this client is sending to the server

Returns:
a list of cookies, or null if the server has not set any cookies in the client
See Also:
Cookie

clearCookies

public void clearCookies()
Clear any cookies this client knows about.


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

getURLConnection

public URLConnection getURLConnection()
Return the connection. Only set after connect has been called

Returns:
the URLConnection

connect

public void connect()
             throws ConnectException,
                    IOException
Connect to the URL

Throws:
ConnectException
IOException

readLine

public String readLine()
                throws ConnectException,
                       IOException
Read responses from the URL

Returns:
a String representation of what we got back
Throws:
ConnectException
IOException

getOutputStream

public OutputStream getOutputStream()
                             throws ConnectException
Get the underlying output stream

Returns:
the output stream
Throws:
ConnectException

getInputStream

public InputStream getInputStream()
                           throws ConnectException
Get the underlying input stream

Returns:
the input stream
Throws:
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(String[] args)


Copyright © 2003 BarracudaMVC.org All Rights Reserved.