CADI.Client.Network
Class HTTPClient

java.lang.Object
  extended by CADI.Client.Network.HTTPClient

public class HTTPClient
extends java.lang.Object

This class implements a HTTP client.

Version:
1.0.3 2009/08/22
Author:
Group on Interactive Coding of Images (GICI)

Field Summary
private  java.lang.String accept
           
private  ClientSocket clientSocket
          It is the socket that will be used to send the client request to the server and to receive the response from the server.
static byte[] CRLF
          Is the carriage and return line.
private  java.lang.String host
          Is the local host name.
static java.lang.String HTTP_VERSION_11
          Indicates the HTTP version.
private  HTTPRequest httpRequest
          This object contains the HTTP request to be sent.
private static java.util.Set httpRequestHeaders
           
private static java.lang.String[] httpRequestHeadersList
          Allowed HTTP headers.
private  HTTPResponse httpResponse
          This object contains the last response that has been received.
private static java.util.Set<java.lang.String> httpResponseHeaders
           
private static java.lang.String[] httpResponseHeadersList
          Allowed HTTP response headers.
private  HTTPResponseReader httpResponseReader
          This attribute is used to read the http response in a easy way.
private  JPIPResponseFields jpipResponseFields
          This object contains the decoded JPIP response fields of the last response.
private  JPIPResponseFieldsParser jpipResponseFieldsParser
          This object contains the last received JPIP fields in the HTTP response.
private static java.util.Set<java.lang.String> jpipResponseHeaders
           
private static java.lang.String[] jpipResponseHeadersList
          Allowed JPIP headers in the HTTP response.
private  boolean keepAlive
          Indicates if http keep alive mode is set.
private  CADILog log
          Definition in Client.log
private static int MAX_ATTEMPTS
          Is the maximum number of attempts to send a request or receive a response
private static java.util.Set methods
           
private static java.lang.String[] methodsList
          Allowed HTTP methods
private  java.lang.String userAgent
          Is the client user agent.
 
Constructor Summary
HTTPClient(CADILog log)
          Constructor.
 
Method Summary
 void close()
           
private  void connect(java.lang.String server, int port, boolean force)
          This method is used to open a socket with the remote host.
private  void createRequest(java.lang.String server, int port, java.lang.String path, java.lang.String query, boolean absolute)
           
 HTTPResponse getHTTPResponse()
           
private  java.util.Hashtable<java.lang.String,java.lang.String> getHTTPResponseHeaders()
          Returns the HTTP response headers without the headers added by the JPIP protocol.
 HTTPResponseReader getHTTPResponseReader()
          Returns the http response reader object.
 JPIPResponseFields getJPIPResponseFields()
          Returns the jpipResponseFields object.
private  java.util.Hashtable<java.lang.String,java.lang.String> getJPIPResponseHeaders()
          Returns the HTTP response headers that belongs to the JPIP protocol.
 int getPort()
          Definition in Socket.getPort()
 java.lang.String getQuery()
          Returns the decoded query component of the last request.
 java.lang.String getRemoteHost()
          Definition in ClientSocket.getRemoteHost().
 java.lang.String getRequestURI()
          Returns the last requested URI.
 java.lang.String getResponseMessage()
          Definition in HTTPResponse.getResponseMessage().
 int getStatusCode()
          Definition in HTTPResponse.getResponseCode().
 boolean isKeepAlive()
          Returns whether the HTTP keep alive feature is or not set.
 boolean isResponseBody()
          Check if the HTTP response has a body or not.
 void list(java.io.PrintStream out)
          Prints this HTTP Client out to the specified output stream.
private  void parseHTTPResponseHeaders(java.util.Hashtable<java.lang.String,java.lang.String> httpHeaders)
           
private  void parseStatusLine(java.lang.String statusLine)
          Parses the status line of the HTTP response.
private  void readResponseHeader(HTTPResponseReader httpResponseReader)
          Decodes a http response which is recieved in the inputStream.
 void sendRequest(HTTPRequest httpRequest)
           
private  void sendRequest(java.io.OutputStream os)
          Sends the HTTP request through the output stream.
private  void sendRequest(java.io.OutputStream os, HTTPRequest httpRequest)
          Sends the HTTP request through the output stream.
 void sendRequest(java.lang.String requestURI)
           
 void sendRequest(java.lang.String server, int port, HTTPRequest httpRequest)
           
 void sendRequest(java.lang.String server, int port, HTTPRequest httpRequest, java.lang.String proxyServer, int proxyPort)
           
 void sendRequest(java.lang.String server, int port, java.lang.String requestURI)
          Sends a request URI to the server.
 void sendRequest(java.lang.String server, int port, java.lang.String requestURI, java.lang.String proxyServer, int proxyPort)
          Sends a request URI to the server.
 void sendRequest(java.lang.String requestURI, java.lang.String proxyServer, int proxyPort)
          Sends a request URI to the server.
 void setAccept(java.lang.String accept)
           
 void setKeepAlive(boolean keepAlive)
          Sets the HTTP keep alive feature.
 void setLocalHost(java.lang.String host)
          Sets the client host name.
private  void setRequestMethod(java.lang.String method)
          Sets the method that will be used to send the HTTP request.
 void setUserAgent(java.lang.String userAgent)
          Sets the user agent name.
 java.lang.String toString()
          For debugging purpose
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

httpResponseReader

private HTTPResponseReader httpResponseReader
This attribute is used to read the http response in a easy way.


jpipResponseFields

private JPIPResponseFields jpipResponseFields
This object contains the decoded JPIP response fields of the last response.


keepAlive

private boolean keepAlive
Indicates if http keep alive mode is set.


host

private java.lang.String host
Is the local host name.


userAgent

private java.lang.String userAgent
Is the client user agent.


accept

private java.lang.String accept

log

private CADILog log
Definition in Client.log


clientSocket

private ClientSocket clientSocket
It is the socket that will be used to send the client request to the server and to receive the response from the server.


MAX_ATTEMPTS

private static final int MAX_ATTEMPTS
Is the maximum number of attempts to send a request or receive a response

See Also:
Constant Field Values

HTTP_VERSION_11

public static final java.lang.String HTTP_VERSION_11
Indicates the HTTP version.

See Also:
Constant Field Values

CRLF

public static final byte[] CRLF
Is the carriage and return line.


httpRequest

private HTTPRequest httpRequest
This object contains the HTTP request to be sent.


methodsList

private static final java.lang.String[] methodsList
Allowed HTTP methods


methods

private static final java.util.Set methods

httpRequestHeadersList

private static final java.lang.String[] httpRequestHeadersList
Allowed HTTP headers.


httpRequestHeaders

private static final java.util.Set httpRequestHeaders
See Also:
httpRequestHeadersList

httpResponse

private HTTPResponse httpResponse
This object contains the last response that has been received.

See Also:
HTTPResponse

jpipResponseFieldsParser

private JPIPResponseFieldsParser jpipResponseFieldsParser
This object contains the last received JPIP fields in the HTTP response.


httpResponseHeadersList

private static final java.lang.String[] httpResponseHeadersList
Allowed HTTP response headers.

NOTICE: The extended HTTP response headers to support the JPIP protocol are not included.


httpResponseHeaders

private static final java.util.Set<java.lang.String> httpResponseHeaders
See Also:
httpResponseHeadersList

jpipResponseHeadersList

private static final java.lang.String[] jpipResponseHeadersList
Allowed JPIP headers in the HTTP response.

See Also:
httpResponseHeaders

jpipResponseHeaders

private static final java.util.Set<java.lang.String> jpipResponseHeaders
See Also:
jpipResponseHeadersList
Constructor Detail

HTTPClient

public HTTPClient(CADILog log)
Constructor.

Method Detail

sendRequest

public void sendRequest(java.lang.String requestURI)
                 throws java.io.IOException,
                        WarningException
Parameters:
requestURI -
Throws:
java.io.IOException
WarningException

sendRequest

public void sendRequest(java.lang.String requestURI,
                        java.lang.String proxyServer,
                        int proxyPort)
                 throws java.io.IOException,
                        WarningException
Sends a request URI to the server. The request must be an absolute URI because the scheme and authority are needed to known where the request will be sent.

Parameters:
requestURI - the request to send.
Throws:
WarningException
java.io.IOException
See Also:
sendRequest(String, int, String)

sendRequest

public void sendRequest(java.lang.String server,
                        int port,
                        java.lang.String requestURI)
                 throws java.io.IOException,
                        WarningException
Sends a request URI to the server. This request can be an absolute or relative request. If it is an abosolute request, the authorithy and port (if it is) must be the same of the server and port parameters. Otherwise, a IllegalArgumentException will be thrown.

The server and port parameters are optionals when the requestURI is an absolute URI.

Parameters:
server - the name of the remote host.
port - the port number on the remote host.
requestURI - the request URI to send.
Throws:
WarningException
java.io.IOException
See Also:
sendRequest(String)

sendRequest

public void sendRequest(java.lang.String server,
                        int port,
                        java.lang.String requestURI,
                        java.lang.String proxyServer,
                        int proxyPort)
                 throws java.io.IOException,
                        WarningException
Sends a request URI to the server. This request can be an absolute or relative request. If it is an abosolute request, the authorithy and port (if it is) must be the same of the server and port parameters. Otherwise, a IllegalArgumentException will be thrown.

The server and port parameters are optionals when the requestURI is an absolute URI.

Parameters:
server - the name of the remote host.
port - the port number on the remote host.
requestURI - the request URI to send.
proxyServer - is the proxy server used to connect to the JPIP server
proxyPort - is the port in the proxy server
Throws:
WarningException
java.io.IOException
See Also:
sendRequest(String)

sendRequest

public void sendRequest(HTTPRequest httpRequest)
                 throws java.io.IOException,
                        WarningException
Parameters:
httpRequest -
Throws:
java.io.IOException
WarningException

sendRequest

public void sendRequest(java.lang.String server,
                        int port,
                        HTTPRequest httpRequest)
                 throws java.io.IOException,
                        WarningException
Parameters:
server -
port -
httpRequest -
Throws:
java.io.IOException
WarningException

sendRequest

public void sendRequest(java.lang.String server,
                        int port,
                        HTTPRequest httpRequest,
                        java.lang.String proxyServer,
                        int proxyPort)
                 throws java.io.IOException,
                        WarningException
Parameters:
server -
port -
httpRequest -
proxyServer -
proxyPort -
Throws:
java.io.IOException
WarningException

getHTTPResponseReader

public HTTPResponseReader getHTTPResponseReader()
Returns the http response reader object.

Returns:
the http response reader object.
See Also:
HTTPResponseReader

getStatusCode

public int getStatusCode()
Definition in HTTPResponse.getResponseCode().


getResponseMessage

public java.lang.String getResponseMessage()
Definition in HTTPResponse.getResponseMessage().


getHTTPResponse

public HTTPResponse getHTTPResponse()
Returns:

getJPIPResponseFields

public JPIPResponseFields getJPIPResponseFields()
Returns the jpipResponseFields object.

Returns:
the jpipResponseFields object.

setKeepAlive

public void setKeepAlive(boolean keepAlive)
Sets the HTTP keep alive feature.

Parameters:
keepAlive - if true the HTTP keep alive feature is set. Otherwise, it is not set.

isKeepAlive

public boolean isKeepAlive()
Returns whether the HTTP keep alive feature is or not set.

Returns:
whether the HTTP keep alive feature is or not set.

setUserAgent

public void setUserAgent(java.lang.String userAgent)
Sets the user agent name.

Parameters:
userAgent - user agent name.

setAccept

public void setAccept(java.lang.String accept)
Parameters:
accept -

getRequestURI

public java.lang.String getRequestURI()
Returns the last requested URI.

Returns:
the last requested URI.

getQuery

public java.lang.String getQuery()
Returns the decoded query component of the last request.

Returns:
the decoded query component.

setLocalHost

public void setLocalHost(java.lang.String host)
Sets the client host name.

Parameters:
host - the client host name.

getRemoteHost

public java.lang.String getRemoteHost()
Definition in ClientSocket.getRemoteHost().


getPort

public int getPort()
Definition in Socket.getPort()


isResponseBody

public boolean isResponseBody()
Check if the HTTP response has a body or not.

Returns:
true whether HTTP response has a body. Otherwise, returns false.

close

public void close()

toString

public java.lang.String toString()
For debugging purpose

Overrides:
toString in class java.lang.Object

list

public void list(java.io.PrintStream out)
Prints this HTTP Client out to the specified output stream. This method is useful for debugging.

Parameters:
out - an output stream.

connect

private void connect(java.lang.String server,
                     int port,
                     boolean force)
              throws java.io.IOException
This method is used to open a socket with the remote host. This method takes into account if the keep alive feature (persistents connections) is or not set, and

Parameters:
server -
port -
force -
Throws:
java.io.IOException

setRequestMethod

private void setRequestMethod(java.lang.String method)
                       throws java.net.ProtocolException
Sets the method that will be used to send the HTTP request.

Parameters:
method - definition in HTTPRequest.method.
Throws:
java.net.ProtocolException - if an error in the underlying protocol has occurred.

createRequest

private void createRequest(java.lang.String server,
                           int port,
                           java.lang.String path,
                           java.lang.String query,
                           boolean absolute)
Parameters:
server -
port -
path -
query -
absolute -

sendRequest

private void sendRequest(java.io.OutputStream os)
                  throws java.io.IOException
Sends the HTTP request through the output stream.

Parameters:
os - the output stream
Throws:
java.io.IOException

sendRequest

private void sendRequest(java.io.OutputStream os,
                         HTTPRequest httpRequest)
                  throws java.io.IOException
Sends the HTTP request through the output stream.

Parameters:
os - the output stream
httpRequest -
Throws:
java.io.IOException

readResponseHeader

private void readResponseHeader(HTTPResponseReader httpResponseReader)
                         throws WarningException,
                                java.io.IOException
Decodes a http response which is recieved in the inputStream.

Parameters:
httpResponseReader - definition in httpResponseReader.
Throws:
WarningException - if an error is found while http response is being decoded.
java.io.IOException - if there is none data to read from the input stream. It might be because input stream is closed by remote host.

parseStatusLine

private void parseStatusLine(java.lang.String statusLine)
                      throws java.net.ProtocolException,
                             WarningException
Parses the status line of the HTTP response.

Parameters:
statusLine - the status line of the HTTP response.
Throws:
java.net.ProtocolException
WarningException

parseHTTPResponseHeaders

private void parseHTTPResponseHeaders(java.util.Hashtable<java.lang.String,java.lang.String> httpHeaders)
                               throws java.net.ProtocolException,
                                      WarningException
Parameters:
httpHeaders -
Throws:
java.net.ProtocolException
WarningException

getHTTPResponseHeaders

private java.util.Hashtable<java.lang.String,java.lang.String> getHTTPResponseHeaders()
Returns the HTTP response headers without the headers added by the JPIP protocol.

Returns:
the HTTP response headers.

getJPIPResponseHeaders

private java.util.Hashtable<java.lang.String,java.lang.String> getJPIPResponseHeaders()
Returns the HTTP response headers that belongs to the JPIP protocol.

Returns:
the JPIP response headers.


CADI - Copyright (C) 2007
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Group on Interactive Coding of Images (GICI) (contact)