Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

raining.client
Class HttpClient  view HttpClient download HttpClient.java

java.lang.Object
  extended byraining.core.NioSocket
      extended byraining.client.HttpClient
Direct Known Subclasses:
SBHttpClient

public class HttpClient
extends raining.core.NioSocket

A sample HTTPClient that extends NioSocket. Command line for: - file to take URLs from - use proxy, server, port, authstring FOr Testing or loading (this should go into another extends) - how many concurrent connections (A) - how long to keep hitting URL - any pause between A's - max hits RK modified on 20031004 13:08:05 Made proxy setting once and for all. User should just set and forget. I am removing the stupid idea of allowing setting encoded string in the command line. just makes things more complic for developers. TODO As much as i dislike wasting my time on the proxy thing, i wd like to make it static, so we dont have to keep messing with it in each URL. May make life simpler for those instantiating this. Rather than wait for -1, we have to do as follows for HTTP Content-Length: XXXX gives you the length after the header Header end is 2 "\r\n\r\n". We can close after this length s crossed. also we shd not give the header.


Field Summary
(package private)  java.lang.String absoluteUrl
           
(package private)  int contentLength
          as read from header.
(package private)  java.lang.String filename
           
(package private)  boolean getrequired
           
(package private)  java.lang.String GETString
           
(package private)  int header_end
          location where the header ends, updated only after "\r\n\r\n" detected.
(package private)  java.lang.String host
           
static java.lang.String P
           
(package private)  java.lang.String path
           
(package private)  int port
           
(package private)  sun.net.www.MessageHeader properties
           
(package private) static java.lang.String proxyAuth
           
(package private) static java.lang.String proxyBase64
           
(package private) static int proxyPort
           
(package private) static java.lang.String proxyServer
           
protected  int responseCode
          An int representing the three digit HTTP Status-Code.
protected  java.lang.String responseMessage
          The HTTP response message.
(package private)  boolean seen_header
           
protected  java.net.URL u
           
(package private) static boolean useProxy
           
 
Fields inherited from class raining.core.NioSocket
acceptcount, acceptctr, activeSocketCount, ADD_EVENT, conncount, connctr, crcount, ctcount, debug, DEL_EVENT, id, READ_BUFFER_SIZE, readcount, REPORT_AFTER, rsb, selector, stopWhenIdle, totalBytesRead, WRITE_BUFFER_SIZE, writecount
 
Constructor Summary
HttpClient()
          Constructor to be used if you have created a GETString already.
HttpClient(java.lang.String Url)
          Constructor that takes absolute URL and splits it.
 
Method Summary
static java.lang.String encode(java.lang.String authString)
          encode a clear text auth string, into Base 64, needed for proxy authentication cases.
 int getContentLength()
          Call this routine to get the content-length associated with this object.
static java.lang.String[] getFileContentsAsArray(java.lang.String filename)
          return content of file as array of Strings.
 java.lang.String getHeaderField(int n)
          Return the value for the nth header field.
 int getResponseCode()
           
 java.lang.String getResponseMessage()
           
 void handle_read_complete(java.lang.String mdata)
          we implement our own processing here.
 int handle_read()
          Implementation of how a HTTPClient handles a read.
static void main(java.lang.String[] args)
           
 void makeGETString()
           
static URLDetails makeGETString(java.lang.String Url)
          Return the GET String given an absolute url as a string, for the purpose of caching a reusing.
static URLDetails makeGETString(java.lang.String Url, java.lang.String proxyBase64)
          return the GET String, this one caters to proxy cases.
 void push()
          push the GET Url.
 HttpClient setGETString(java.lang.String s)
          set the string to be used for pushing.
 HttpClient setHostPort(java.lang.String hostname, int port)
          required by caching clients (non-proxy) on a connection level.
static void setProxy(java.lang.String hostname, int port)
          tell HTTPClients to use a proxy server
static void setProxyAuthString(java.lang.String authString)
           
 
Methods inherited from class raining.core.NioSocket
accept_accounting, accept, add_to_map, channel, clear_read_buffer, conn_reset_handler, conn_time_out_handler, connect_accounting, create_client_socket, create_client_socket, create_server_socket, del_from_map, getReadData, getReadDataAsBuffer, getReadDataAsString, handle_accept, handle_close, handle_connect, handle_read_complete, handle_read_event, handle_terminator, handle_write_complete, handle_write_event, handle_write, is_read_complete, nio_close, on_exit_handler, readable, recv, resetSocketCounter, send, serverflag, setDebug, setReadBufferSize, setSendData, setSendData, setSendData, setWriteBufferSize, socketCount, socketCountChangeEvent, start, start, stop_now, stopWhenIdle, stopWhenIdle, writeable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P

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

u

protected java.net.URL u

host

java.lang.String host

path

java.lang.String path

absoluteUrl

java.lang.String absoluteUrl

port

int port

proxyServer

static java.lang.String proxyServer

proxyPort

static int proxyPort

proxyAuth

static java.lang.String proxyAuth

proxyBase64

static java.lang.String proxyBase64

useProxy

static boolean useProxy

filename

java.lang.String filename

GETString

java.lang.String GETString

getrequired

boolean getrequired

responseCode

protected int responseCode
An int representing the three digit HTTP Status-Code.
  • 1xx: Informational
  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client Error
  • 5xx: Server Error


responseMessage

protected java.lang.String responseMessage
The HTTP response message.


contentLength

int contentLength
as read from header. If not found in header, it remains 0, so that the read may terminate fast, since it may be erroneous.


header_end

int header_end
location where the header ends, updated only after "\r\n\r\n" detected.


seen_header

boolean seen_header

properties

sun.net.www.MessageHeader properties
Constructor Detail

HttpClient

public HttpClient(java.lang.String Url)
           throws java.net.MalformedURLException,
                  java.io.IOException,
                  java.lang.Exception
Constructor that takes absolute URL and splits it. e.g. http://www.benegal.net/


HttpClient

public HttpClient()
           throws java.io.IOException,
                  java.lang.Exception
Constructor to be used if you have created a GETString already. this is required if you wish to blast one URL with many connections. The sequence would be: 1. use the blank cons 2. create a GET String using makeGETString with Url and save In a loop: 3. set the URL with setGETString. 4. set the port and host (setHostPort or setProxy) 5. push

Method Detail

main

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

setGETString

public HttpClient setGETString(java.lang.String s)
set the string to be used for pushing. Must be used if yuo used a blank constructor.


setProxy

public static void setProxy(java.lang.String hostname,
                            int port)
tell HTTPClients to use a proxy server


setHostPort

public HttpClient setHostPort(java.lang.String hostname,
                              int port)
required by caching clients (non-proxy) on a connection level.


setProxyAuthString

public static void setProxyAuthString(java.lang.String authString)

encode

public static java.lang.String encode(java.lang.String authString)
encode a clear text auth string, into Base 64, needed for proxy authentication cases. put as a method so that extenders can call.


push

public void push()
          throws java.io.IOException,
                 java.lang.Exception
push the GET Url. I think even the string creations should go into a method, for those overriding. Also, it needs to be StringBuffered.


makeGETString

public static URLDetails makeGETString(java.lang.String Url)
                                throws java.net.MalformedURLException
Return the GET String given an absolute url as a string, for the purpose of caching a reusing. This one caters to non proxy cases. This helps a caller cache a string and use it multiple times. Is the URL parse light, or should we do a parse here itself.


makeGETString

public static URLDetails makeGETString(java.lang.String Url,
                                       java.lang.String proxyBase64)
                                throws java.net.MalformedURLException
return the GET String, this one caters to proxy cases. Pass the encoded string as the second parameter.


makeGETString

public void makeGETString()

handle_read_complete

public void handle_read_complete(java.lang.String mdata)
we implement our own processing here. e.g. we could write to disk or db. we would do any blocking writes in a thread if we want scalability.


handle_read

public int handle_read()
Implementation of how a HTTPClient handles a read. Takes into account parsing the header and looking for content length, and closing channel if content has been obtained, withuot waiting for channel to get a -1. should we minus header length from bytes read or handle it internally. XXX how is the person here to know abt rsb. if he uses


getFileContentsAsArray

public static java.lang.String[] getFileContentsAsArray(java.lang.String filename)
return content of file as array of Strings. Used by client parser for reading up url list. Should close file pointer in finally.


getContentLength

public int getContentLength()
Call this routine to get the content-length associated with this object.


getResponseCode

public int getResponseCode()
                    throws java.io.IOException

getResponseMessage

public java.lang.String getResponseMessage()
                                    throws java.io.IOException

getHeaderField

public java.lang.String getHeaderField(int n)
Return the value for the nth header field. Returns null if there are fewer than n fields. This can be used in conjunction with getHeaderFieldKey to iterate through all the headers in the message.