|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.httpclient.HttpConnection
An abstraction of an HTTP InputStream
and OutputStream
pair, together with the relevant attributes.
The following options are set on the socket before getting the input/output
streams in the open()
method:
Socket Method | Sockets Option | Configuration |
---|---|---|
Socket.setTcpNoDelay(boolean)
| SO_NODELAY | None |
Socket.setSoTimeout(int)
| SO_TIMEOUT | setConnectionTimeout(int)
|
Inner Class Summary | |
interface |
HttpConnection.ConnectionTimeoutException
Signals that a timeout occured while opening the socket. |
Field Summary | |
protected boolean |
isOpen
Whether or not the connection is connected. |
Constructor Summary | |
HttpConnection(HostConfiguration hostConfiguration)
Creates a new HTTP connection for the given host configuration. |
|
HttpConnection(java.lang.String host,
int port)
Creates a new HTTP connection for the given host and port. |
|
HttpConnection(java.lang.String host,
int port,
boolean secure)
Deprecated. use HttpConnection(String, int, Protocol) |
|
HttpConnection(java.lang.String host,
int port,
Protocol protocol)
Creates a new HTTP connection for the given host and port using the given protocol. |
|
HttpConnection(java.lang.String proxyHost,
int proxyPort,
java.lang.String host,
int port)
Creates a new HTTP connection for the given host and port via the given proxy host and port using the default protocol. |
|
HttpConnection(java.lang.String proxyHost,
int proxyPort,
java.lang.String host,
int port,
boolean secure)
Deprecated. use HttpConnection(String, int, String, int, Protocol) |
|
HttpConnection(java.lang.String proxyHost,
int proxyPort,
java.lang.String host,
java.lang.String virtualHost,
int port,
Protocol protocol)
Creates a new HTTP connection for the given host with the virtual alias and port via the given proxy host and port using the given protocol. |
|
HttpConnection(java.lang.String host,
java.lang.String virtualHost,
int port,
Protocol protocol)
Creates a new HTTP connection for the given host with the virtual alias and port using given protocol. |
Method Summary | |
protected void |
assertNotOpen()
Throws an IllegalStateException if the connection is already open. |
protected void |
assertOpen()
Throws an IllegalStateException if the connection is not open. |
void |
close()
Closes the socket and streams. |
protected void |
closeSocketAndStreams()
Closes everything out. |
void |
flushRequestOutputStream()
Flushes the output request stream. |
java.lang.String |
getHost()
Returns the host. |
HttpConnectionManager |
getHttpConnectionManager()
Returns the httpConnectionManager. |
java.io.InputStream |
getLastResponseInputStream()
Returns the stream used to read the last response's body. |
java.net.InetAddress |
getLocalAddress()
Return the local address used when creating the connection. |
int |
getPort()
Returns the port of the host. |
Protocol |
getProtocol()
Returns the protocol used to establish the connection. |
java.lang.String |
getProxyHost()
Returns the proxy host. |
int |
getProxyPort()
Returns the port of the proxy host. |
java.io.OutputStream |
getRequestOutputStream()
Returns an OutputStream suitable for writing the request. |
java.io.OutputStream |
getRequestOutputStream(boolean useChunking)
Deprecated. Use new ChunkedOutputStream(httpConnecion.getRequestOutputStream()); |
java.io.InputStream |
getResponseInputStream()
Return a InputStream suitable for reading the response. |
java.io.InputStream |
getResponseInputStream(HttpMethod method)
Deprecated. Use getResponseInputStream() instead. |
int |
getSendBufferSize()
Gets the socket's sendBufferSize. |
int |
getSoTimeout()
Returns the Socket 's timeout, via Socket.getSoTimeout() , if the
connection is already open. |
java.lang.String |
getVirtualHost()
Returns the target virtual host. |
boolean |
isOpen()
Returns true if the connection is open, false otherwise. |
boolean |
isProxied()
Returns true if the connection is established via a proxy, false otherwise. |
boolean |
isResponseAvailable()
Tests if input data avaialble. |
boolean |
isResponseAvailable(int timeout)
Tests if input data becomes available within the given period time in milliseconds. |
boolean |
isSecure()
Returns true if the connection is established over a secure protocol. |
protected boolean |
isStale()
Determines whether this connection is "stale", which is to say that either it is no longer open, or an attempt to read the connection would fail. |
boolean |
isStaleCheckingEnabled()
Tests if stale checking is enabled. |
boolean |
isTransparent()
Indicates if the connection is completely transparent from end to end. |
void |
open()
Establishes a connection to the specified host and port (via a proxy if specified). |
void |
print(java.lang.String data)
Writes the specified String (as bytes) to the output stream. |
void |
printLine()
Writes "\r\n".getBytes() to the output stream. |
void |
printLine(java.lang.String data)
Writes the specified String (as bytes), followed by "\r\n".getBytes() to the output stream. |
java.lang.String |
readLine()
Reads up to "\n" from the (unchunked) input stream. |
void |
releaseConnection()
Release the connection. |
void |
setConnectionTimeout(int timeout)
Sets the connection timeout. |
void |
setHost(java.lang.String host)
Sets the host to connect to. |
void |
setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
Sets the httpConnectionManager. |
void |
setLastResponseInputStream(java.io.InputStream inStream)
Set the state to keep track of the last response for the last request. |
void |
setLocalAddress(java.net.InetAddress localAddress)
Set the local address used when creating the connection. |
void |
setPort(int port)
Sets the port to connect to. |
void |
setProtocol(Protocol protocol)
Sets the protocol used to establish the connection |
void |
setProxyHost(java.lang.String host)
Sets the host to proxy through. |
void |
setProxyPort(int port)
Sets the port of the host to proxy through. |
void |
setSecure(boolean secure)
Deprecated. use setProtocol(Protocol) |
void |
setSendBufferSize(int sendBufferSize)
Sets the socket's sendBufferSize. |
void |
setSoTimeout(int timeout)
Sets the Socket 's timeout, via Socket.setSoTimeout(int) . |
void |
setStaleCheckingEnabled(boolean staleCheckEnabled)
Sets whether or not isStale() will be called when testing if this connection is open. |
void |
setVirtualHost(java.lang.String host)
Sets the virtual host to target. |
void |
shutdownOutput()
Attempts to shutdown the Socket 's output, via Socket.shutdownOutput()
when running on JVM 1.3 or higher. |
void |
tunnelCreated()
Instructs the proxy to establish a secure tunnel to the host. |
void |
write(byte[] data)
Writes the specified bytes to the output stream. |
void |
write(byte[] data,
int offset,
int length)
Writes length bytes in data starting at offset to the output stream. |
void |
writeLine()
Writes "\r\n".getBytes() to the output stream. |
void |
writeLine(byte[] data)
Writes the specified bytes, followed by "\r\n".getBytes() to the output stream. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
protected boolean isOpen
Constructor Detail |
public HttpConnection(java.lang.String host, int port)
host
- the host to connect toport
- the port to connect topublic HttpConnection(java.lang.String host, int port, boolean secure)
host
- the host to connect toport
- the port to connect tosecure
- when true, connect via HTTPS (SSL)HttpConnection(String,int,Protocol)
public HttpConnection(java.lang.String host, int port, Protocol protocol)
host
- the host to connect toport
- the port to connect toprotocol
- the protocol to usepublic HttpConnection(java.lang.String host, java.lang.String virtualHost, int port, Protocol protocol)
host
- the host to connect tovirtualHost
- the virtual host requests will be sent toport
- the port to connect toprotocol
- the protocol to usepublic HttpConnection(java.lang.String proxyHost, int proxyPort, java.lang.String host, int port)
proxyHost
- the host to proxy viaproxyPort
- the port to proxy viahost
- the host to connect toport
- the port to connect topublic HttpConnection(java.lang.String proxyHost, int proxyPort, java.lang.String host, int port, boolean secure)
proxyHost
- the host I should proxy viaproxyPort
- the port I should proxy viahost
- the host to connect to. Parameter value must be non-null.port
- the port to connect tosecure
- when true, connect via HTTPS (SSL)HttpConnection(String, int, String, String, int, Protocol)
public HttpConnection(HostConfiguration hostConfiguration)
hostConfiguration
- the host/proxy/protocol to usepublic HttpConnection(java.lang.String proxyHost, int proxyPort, java.lang.String host, java.lang.String virtualHost, int port, Protocol protocol)
proxyHost
- the host to proxy viaproxyPort
- the port to proxy viahost
- the host to connect to. Parameter value must be non-null.virtualHost
- the virtual host requests will be sent toport
- the port to connect toprotocol
- The protocol to use. Parameter value must be non-null.Method Detail |
public java.lang.String getHost()
public void setHost(java.lang.String host) throws java.lang.IllegalStateException
host
- the host to connect to. Parameter value must be non-null.public java.lang.String getVirtualHost()
public void setVirtualHost(java.lang.String host) throws java.lang.IllegalStateException
host
- the virtual host name that should be used instead of
physical host name when sending HTTP requests. Virtual host
name can be set to null if virtual host name is not
to be usedpublic int getPort()
public void setPort(int port) throws java.lang.IllegalStateException
port
- the port to connect topublic java.lang.String getProxyHost()
public void setProxyHost(java.lang.String host) throws java.lang.IllegalStateException
host
- the host to proxy through.public int getProxyPort()
public void setProxyPort(int port) throws java.lang.IllegalStateException
port
- the port of the host to proxy through.public boolean isSecure()
public Protocol getProtocol()
public void setSecure(boolean secure) throws java.lang.IllegalStateException
secure
- whether or not to connect over a secure protocol.setProtocol(Protocol)
public void setProtocol(Protocol protocol)
protocol
- The protocol to use.public java.net.InetAddress getLocalAddress()
public void setLocalAddress(java.net.InetAddress localAddress)
localAddress
- the local address to usepublic boolean isOpen()
public boolean isStaleCheckingEnabled()
true
if enabledisStale()
public void setStaleCheckingEnabled(boolean staleCheckEnabled)
Setting this flag to false
will increase performance when reusing
connections, but it will also make them less reliable. Stale checking ensures that
connections are viable before they are used. When set to false
some
method executions will result in IOExceptions and they will have to be retried.
staleCheckEnabled
- true
to enable isStale()isStale()
,
isOpen()
protected boolean isStale()
Unfortunately, due to the limitations of the JREs prior to 1.4, it is not possible to test a connection to see if both the read and write channels are open - except by reading and writing. This leads to a difficulty when some connections leave the "write" channel open, but close the read channel and ignore the request. This function attempts to ameliorate that problem by doing a test read, assuming that the caller will be doing a write followed by a read, rather than the other way around.
To avoid side-effects, the underlying connection is wrapped by a
PushbackInputStream
, so although data might be read, what is visible
to clients of the connection will not change with this call.
public boolean isProxied()
public void setLastResponseInputStream(java.io.InputStream inStream)
The connection managers use this to ensure that previous requests are properly closed before a new request is attempted. That way, a GET request need not be read in its entirety before a new request is issued. Instead, this stream can be closed as appropriate.
inStream
- The stream associated with an HttpMethod.public java.io.InputStream getLastResponseInputStream()
Clients will generally not need to call this function unless
using HttpConnection directly, instead of calling HttpClient.executeMethod(org.apache.commons.httpclient.HttpMethod)
.
For those clients, call this function, and if it returns a non-null stream,
close the stream before attempting to execute a method. Note that
calling "close" on the stream returned by this function may close
the connection if the previous response contained a "Connection: close" header.
InputStream
corresponding to the body of the last
response.public void setSoTimeout(int timeout) throws java.net.SocketException, java.lang.IllegalStateException
Socket
's timeout, via Socket.setSoTimeout(int)
. If the
connection is already open, the SO_TIMEOUT is changed. If no connection
is open, then subsequent connections will use the timeout value.
Note: This is not a connection timeout but a timeout on network traffic!
timeout
- the timeout valuepublic int getSoTimeout() throws java.net.SocketException
Socket
's timeout, via Socket.getSoTimeout()
, if the
connection is already open. If no connection is open, return the value subsequent
connection will use.
Note: This is not a connection timeout but a timeout on network traffic!
public void setConnectionTimeout(int timeout)
timeout
- The timeout in milliseconds. 0 means timeout is not used.public void open() throws java.io.IOException
ProtocolSocketFactory
.public void tunnelCreated() throws java.lang.IllegalStateException, java.io.IOException
public boolean isTransparent()
public void flushRequestOutputStream() throws java.io.IOException
public java.io.OutputStream getRequestOutputStream() throws java.io.IOException, java.lang.IllegalStateException
OutputStream
suitable for writing the request.public java.io.OutputStream getRequestOutputStream(boolean useChunking) throws java.io.IOException, java.lang.IllegalStateException
OutputStream
suitable for writing the request.useChunking
- when true the chunked transfer-encoding will
be usedpublic java.io.InputStream getResponseInputStream(HttpMethod method) throws java.io.IOException, java.lang.IllegalStateException
InputStream
suitable for reading the response.
If the given HttpMethod
contains
a Transfer-Encoding: chunked header,
the returned stream will be configured
to read chunked bytes.
method
- This argument is ignored.public java.io.InputStream getResponseInputStream() throws java.io.IOException, java.lang.IllegalStateException
InputStream
suitable for reading the response.public boolean isResponseAvailable() throws java.io.IOException
public boolean isResponseAvailable(int timeout) throws java.io.IOException
timeout
- The number milliseconds to wait for input data to become availablepublic void write(byte[] data) throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
data
- the data to be writtenwrite(byte[],int,int)
public void write(byte[] data, int offset, int length) throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
data
- array containing the data to be written.offset
- the start offset in the data.length
- the number of bytes to write.public void writeLine(byte[] data) throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
data
- the bytes to be writtenpublic void writeLine() throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
public void print(java.lang.String data) throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
data
- the string to be writtenpublic void printLine(java.lang.String data) throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
data
- the data to be writtenpublic void printLine() throws java.io.IOException, java.lang.IllegalStateException, HttpRecoverableException
public java.lang.String readLine() throws java.io.IOException, java.lang.IllegalStateException
public void shutdownOutput()
Socket
's output, via Socket.shutdownOutput()
when running on JVM 1.3 or higher.public void close()
public HttpConnectionManager getHttpConnectionManager()
public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
httpConnectionManager
- The httpConnectionManager to setpublic void releaseConnection()
protected void closeSocketAndStreams()
protected void assertNotOpen() throws java.lang.IllegalStateException
IllegalStateException
if the connection is already open.protected void assertOpen() throws java.lang.IllegalStateException
IllegalStateException
if the connection is not open.public int getSendBufferSize() throws java.net.SocketException
Socket.getSendBufferSize()
public void setSendBufferSize(int sendBufferSize) throws java.net.SocketException
sendBufferSize
- the size to set for the socket OutputStreamSocket.setSendBufferSize(int)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |