| Constructor: |
public HttpConnection(HostConfiguration hostConfiguration) {
this(hostConfiguration.getProxyHost(),
hostConfiguration.getProxyPort(),
hostConfiguration.getHost(),
hostConfiguration.getPort(),
hostConfiguration.getProtocol());
this.localAddress = hostConfiguration.getLocalAddress();
}
Creates a new HTTP connection for the given host configuration. Parameters:
hostConfiguration - the host/proxy/protocol to use
|
public HttpConnection(String host,
int port) {
this(null, -1, host, null, port, Protocol.getProtocol("http"));
}
Creates a new HTTP connection for the given host and port. Parameters:
host - the host to connect to
port - the port to connect to
|
public HttpConnection(String host,
int port,
Protocol protocol) {
this(null, -1, host, null, port, protocol);
}
Creates a new HTTP connection for the given host and port
using the given protocol. Parameters:
host - the host to connect to
port - the port to connect to
protocol - the protocol to use
|
public HttpConnection(String host,
String virtualHost,
int port,
Protocol protocol) {
this(null, -1, host, virtualHost, port, protocol);
}
Creates a new HTTP connection for the given host with the virtual
alias and port using given protocol. Parameters:
host - the host to connect to
virtualHost - the virtual host requests will be sent to
port - the port to connect to
protocol - the protocol to use
|
public HttpConnection(String proxyHost,
int proxyPort,
String host,
int port) {
this(proxyHost, proxyPort, host, null, port, Protocol.getProtocol("http"));
}
Creates a new HTTP connection for the given host and port via the
given proxy host and port using the default protocol. Parameters:
proxyHost - the host to proxy via
proxyPort - the port to proxy via
host - the host to connect to
port - the port to connect to
|
public HttpConnection(String proxyHost,
int proxyPort,
String host,
int port,
Protocol protocol) {
if (host == null) {
throw new IllegalArgumentException("host parameter is null");
}
if (protocol == null) {
throw new IllegalArgumentException("protocol is null");
}
proxyHostName = proxyHost;
proxyPortNumber = proxyPort;
hostName = host;
portNumber = protocol.resolvePort(port);
protocolInUse = 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. Parameters:
proxyHost - the host to proxy via
proxyPort - the port to proxy via
host - the host to connect to. Parameter value must be non-null.
port - the port to connect to
protocol - The protocol to use. Parameter value must be non-null.
|
public HttpConnection(String proxyHost,
int proxyPort,
String host,
String virtualHost,
int port,
Protocol protocol) {
this(proxyHost, proxyPort, host, port, 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. Parameters:
proxyHost - the host to proxy via
proxyPort - the port to proxy via
host - the host to connect to. Parameter value must be non-null.
virtualHost - No longer applicable.
port - the port to connect to
protocol - The protocol to use. Parameter value must be non-null.
|
| Method from org.apache.commons.httpclient.HttpConnection Detail: |
protected void assertNotOpen() throws IllegalStateException {
if (isOpen) {
throw new IllegalStateException("Connection is open");
}
}
|
protected void assertOpen() throws IllegalStateException {
if (!isOpen) {
throw new IllegalStateException("Connection is not open");
}
}
|
public void close() {
LOG.trace("enter HttpConnection.close()");
closeSocketAndStreams();
}
Closes the socket and streams. |
public boolean closeIfStale() throws IOException {
if (isOpen && isStale()) {
LOG.debug("Connection is stale, closing...");
close();
return true;
}
return false;
}
Closes the connection if stale. |
protected void closeSocketAndStreams() {
LOG.trace("enter HttpConnection.closeSockedAndStreams()");
isOpen = false;
// no longer care about previous responses...
lastResponseInputStream = null;
if (null != outputStream) {
OutputStream temp = outputStream;
outputStream = null;
try {
temp.close();
} catch (Exception ex) {
LOG.debug("Exception caught when closing output", ex);
// ignored
}
}
if (null != inputStream) {
InputStream temp = inputStream;
inputStream = null;
try {
temp.close();
} catch (Exception ex) {
LOG.debug("Exception caught when closing input", ex);
// ignored
}
}
if (null != socket) {
Socket temp = socket;
socket = null;
try {
temp.close();
} catch (Exception ex) {
LOG.debug("Exception caught when closing socket", ex);
// ignored
}
}
tunnelEstablished = false;
usingSecureSocket = false;
}
|
public void flushRequestOutputStream() throws IOException {
LOG.trace("enter HttpConnection.flushRequestOutputStream()");
assertOpen();
outputStream.flush();
}
Flushes the output request stream. This method should be called to
ensure that data written to the request OutputStream is sent to the server. |
public String getHost() {
return hostName;
}
|
public HttpConnectionManager getHttpConnectionManager() {
return httpConnectionManager;
}
Returns the httpConnectionManager. |
public InputStream getLastResponseInputStream() {
return lastResponseInputStream;
}
Returns the stream used to read the last response's body.
Clients will generally not need to call this function unless
using HttpConnection directly, instead of calling HttpClient#executeMethod .
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. |
public InetAddress getLocalAddress() {
return this.localAddress;
}
Return the local address used when creating the connection.
If null, the default address is used. |
public HttpConnectionParams getParams() {
return this.params;
}
|
public int getPort() {
if (portNumber < 0) {
return isSecure() ? 443 : 80;
} else {
return portNumber;
}
}
Returns the port of the host.
If the port is -1 (or less than 0) the default port for
the current protocol is returned. |
public Protocol getProtocol() {
return protocolInUse;
}
Returns the protocol used to establish the connection. |
public String getProxyHost() {
return proxyHostName;
}
|
public int getProxyPort() {
return proxyPortNumber;
}
Returns the port of the proxy host. |
public OutputStream getRequestOutputStream() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.getRequestOutputStream()");
assertOpen();
OutputStream out = this.outputStream;
if (Wire.CONTENT_WIRE.enabled()) {
out = new WireLogOutputStream(out, Wire.CONTENT_WIRE);
}
return out;
}
|
public InputStream getResponseInputStream() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.getResponseInputStream()");
assertOpen();
return inputStream;
}
Return a InputStream suitable for reading the response. |
public int getSendBufferSize() throws SocketException {
if (socket == null) {
return -1;
} else {
return socket.getSendBufferSize();
}
}
Gets the socket's sendBufferSize. |
public int getSoTimeout() throws SocketException {
return this.params.getSoTimeout();
} Deprecated! Use - HttpConnectionParams#getSoTimeout() ,
HttpConnection#getParams() .
|
protected Socket getSocket() {
return this.socket;
}
Returns the connection socket. |
public String getVirtualHost() {
return this.hostName;
} Deprecated! no - longer applicable
Returns the target virtual host. |
protected boolean isLocked() {
return locked;
}
Tests if the connection is locked. Locked connections cannot be released.
An attempt to release a locked connection will have no effect. |
public boolean isOpen() {
return isOpen;
}
Tests if the connection is open. |
public boolean isProxied() {
return (!(null == proxyHostName || 0 >= proxyPortNumber));
}
Returns true if the connection is established via a proxy,
false otherwise. |
public boolean isResponseAvailable() throws IOException {
LOG.trace("enter HttpConnection.isResponseAvailable()");
if (this.isOpen) {
return this.inputStream.available() > 0;
} else {
return false;
}
}
Tests if input data avaialble. This method returns immediately
and does not perform any read operations on the input socket |
public boolean isResponseAvailable(int timeout) throws IOException {
LOG.trace("enter HttpConnection.isResponseAvailable(int)");
assertOpen();
boolean result = false;
if (this.inputStream.available() > 0) {
result = true;
} else {
try {
this.socket.setSoTimeout(timeout);
inputStream.mark(1);
int byteRead = inputStream.read();
if (byteRead != -1) {
inputStream.reset();
LOG.debug("Input data available");
result = true;
} else {
LOG.debug("Input data not available");
}
} catch (InterruptedIOException e) {
if (!ExceptionUtil.isSocketTimeoutException(e)) {
throw e;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Input data not available after " + timeout + " ms");
}
} finally {
try {
socket.setSoTimeout(this.params.getSoTimeout());
} catch (IOException ioe) {
LOG.debug("An error ocurred while resetting soTimeout, we will assume that"
+ " no response is available.",
ioe);
result = false;
}
}
}
return result;
}
Tests if input data becomes available within the given period time in milliseconds. |
public boolean isSecure() {
return protocolInUse.isSecure();
}
Returns true if the connection is established over
a secure protocol. |
protected boolean isStale() throws IOException {
boolean isStale = true;
if (isOpen) {
// the connection is open, but now we have to see if we can read it
// assume the connection is not stale.
isStale = false;
try {
if (inputStream.available() < = 0) {
try {
socket.setSoTimeout(1);
inputStream.mark(1);
int byteRead = inputStream.read();
if (byteRead == -1) {
// again - if the socket is reporting all data read,
// probably stale
isStale = true;
} else {
inputStream.reset();
}
} finally {
socket.setSoTimeout(this.params.getSoTimeout());
}
}
} catch (InterruptedIOException e) {
if (!ExceptionUtil.isSocketTimeoutException(e)) {
throw e;
}
// aha - the connection is NOT stale - continue on!
} catch (IOException e) {
// oops - the connection is stale, the read or soTimeout failed.
LOG.debug(
"An error occurred while reading from the socket, is appears to be stale",
e
);
isStale = true;
}
}
return 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.
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
BufferedInputStream , so although data might be read, what is visible
to clients of the connection will not change with this call. |
public boolean isStaleCheckingEnabled() {
return this.params.isStaleCheckingEnabled();
} Deprecated! Use - HttpConnectionParams#isStaleCheckingEnabled() ,
HttpConnection#getParams() .
Tests if stale checking is enabled. |
public boolean isTransparent() {
return !isProxied() || tunnelEstablished;
}
Indicates if the connection is completely transparent from end to end. |
public void open() throws IOException {
LOG.trace("enter HttpConnection.open()");
final String host = (proxyHostName == null) ? hostName : proxyHostName;
final int port = (proxyHostName == null) ? portNumber : proxyPortNumber;
assertNotOpen();
if (LOG.isDebugEnabled()) {
LOG.debug("Open connection to " + host + ":" + port);
}
try {
if (this.socket == null) {
usingSecureSocket = isSecure() && !isProxied();
// use the protocol's socket factory unless this is a secure
// proxied connection
ProtocolSocketFactory socketFactory = null;
if (isSecure() && isProxied()) {
Protocol defaultprotocol = Protocol.getProtocol("http");
socketFactory = defaultprotocol.getSocketFactory();
} else {
socketFactory = this.protocolInUse.getSocketFactory();
}
this.socket = socketFactory.createSocket(
host, port,
localAddress, 0,
this.params);
}
/*
"Nagling has been broadly implemented across networks,
including the Internet, and is generally performed by default
- although it is sometimes considered to be undesirable in
highly interactive environments, such as some client/server
situations. In such cases, nagling may be turned off through
use of the TCP_NODELAY sockets option." */
socket.setTcpNoDelay(this.params.getTcpNoDelay());
socket.setSoTimeout(this.params.getSoTimeout());
int linger = this.params.getLinger();
if (linger >= 0) {
socket.setSoLinger(linger > 0, linger);
}
int sndBufSize = this.params.getSendBufferSize();
if (sndBufSize >= 0) {
socket.setSendBufferSize(sndBufSize);
}
int rcvBufSize = this.params.getReceiveBufferSize();
if (rcvBufSize >= 0) {
socket.setReceiveBufferSize(rcvBufSize);
}
int outbuffersize = socket.getSendBufferSize();
if ((outbuffersize > 2048) || (outbuffersize < = 0)) {
outbuffersize = 2048;
}
int inbuffersize = socket.getReceiveBufferSize();
if ((inbuffersize > 2048) || (inbuffersize < = 0)) {
inbuffersize = 2048;
}
inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
isOpen = true;
} catch (IOException e) {
// Connection wasn't opened properly
// so close everything out
closeSocketAndStreams();
throw e;
}
}
Establishes a connection to the specified host and port
(via a proxy if specified).
The underlying socket is created from the ProtocolSocketFactory . |
public void print(String data) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.print(String)");
write(EncodingUtil.getBytes(data, "ISO-8859-1"));
} Deprecated! Use - #print(String, String)
Writes the specified String (as bytes) to the output stream.
|
public void print(String data,
String charset) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.print(String)");
write(EncodingUtil.getBytes(data, charset));
}
Writes the specified String (as bytes) to the output stream. |
public void printLine() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.printLine()");
writeLine();
}
Writes "\r\n".getBytes() to the output stream. |
public void printLine(String data) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.printLine(String)");
writeLine(EncodingUtil.getBytes(data, "ISO-8859-1"));
} Deprecated! Use - #printLine(String, String)
Writes the specified String (as bytes), followed by
"\r\n".getBytes() to the output stream.
|
public void printLine(String data,
String charset) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.printLine(String)");
writeLine(EncodingUtil.getBytes(data, charset));
}
Writes the specified String (as bytes), followed by
"\r\n".getBytes() to the output stream. |
public String readLine() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.readLine()");
assertOpen();
return HttpParser.readLine(inputStream);
} Deprecated! use - #readLine(String)
Reads up to "\n" from the (unchunked) input stream.
If the stream ends before the line terminator is found,
the last part of the string will still be returned. |
public String readLine(String charset) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.readLine()");
assertOpen();
return HttpParser.readLine(inputStream, charset);
}
Reads up to "\n" from the (unchunked) input stream.
If the stream ends before the line terminator is found,
the last part of the string will still be returned. |
public void releaseConnection() {
LOG.trace("enter HttpConnection.releaseConnection()");
if (locked) {
LOG.debug("Connection is locked. Call to releaseConnection() ignored.");
} else if (httpConnectionManager != null) {
LOG.debug("Releasing connection back to connection manager.");
httpConnectionManager.releaseConnection(this);
} else {
LOG.warn("HttpConnectionManager is null. Connection cannot be released.");
}
}
Releases the connection. If the connection is locked or does not have a connection
manager associated with it, this method has no effect. Note that it is completely safe
to call this method multiple times. |
public void setConnectionTimeout(int timeout) {
this.params.setConnectionTimeout(timeout);
} Deprecated! Use - HttpConnectionParams#setConnectionTimeout(int) ,
HttpConnection#getParams() .
Sets the connection timeout. This is the maximum time that may be spent
until a connection is established. The connection will fail after this
amount of time. |
public void setHost(String host) throws IllegalStateException {
if (host == null) {
throw new IllegalArgumentException("host parameter is null");
}
assertNotOpen();
hostName = host;
}
Sets the host to connect to. |
public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
this.httpConnectionManager = httpConnectionManager;
}
Sets the httpConnectionManager. |
public void setLastResponseInputStream(InputStream inStream) {
lastResponseInputStream = inStream;
}
Set the state to keep track of the last response for the last request.
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. |
public void setLocalAddress(InetAddress localAddress) {
assertNotOpen();
this.localAddress = localAddress;
}
Set the local address used when creating the connection.
If unset or null, the default address is used. |
protected void setLocked(boolean locked) {
this.locked = locked;
}
Locks or unlocks the connection. Locked connections cannot be released.
An attempt to release a locked connection will have no effect. |
public void setParams(HttpConnectionParams params) {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
this.params = params;
}
|
public void setPort(int port) throws IllegalStateException {
assertNotOpen();
portNumber = port;
}
Sets the port to connect to. |
public void setProtocol(Protocol protocol) {
assertNotOpen();
if (protocol == null) {
throw new IllegalArgumentException("protocol is null");
}
protocolInUse = protocol;
}
Sets the protocol used to establish the connection |
public void setProxyHost(String host) throws IllegalStateException {
assertNotOpen();
proxyHostName = host;
}
Sets the host to proxy through. |
public void setProxyPort(int port) throws IllegalStateException {
assertNotOpen();
proxyPortNumber = port;
}
Sets the port of the host to proxy through. |
public void setSendBufferSize(int sendBufferSize) throws SocketException {
this.params.setSendBufferSize(sendBufferSize);
} Deprecated! Use - HttpConnectionParams#setSendBufferSize(int) ,
HttpConnection#getParams() .
Sets the socket's sendBufferSize. |
public void setSoTimeout(int timeout) throws SocketException, IllegalStateException {
this.params.setSoTimeout(timeout);
if (this.socket != null) {
this.socket.setSoTimeout(timeout);
}
} Deprecated! Use - HttpConnectionParams#setSoTimeout(int) ,
HttpConnection#getParams() .
|
public void setSocketTimeout(int timeout) throws SocketException, IllegalStateException {
assertOpen();
if (this.socket != null) {
this.socket.setSoTimeout(timeout);
}
}
Sets SO_TIMEOUT value directly on the underlying socket .
This method does not change the default read timeout value set via
HttpConnectionParams . |
public void setStaleCheckingEnabled(boolean staleCheckEnabled) {
this.params.setStaleCheckingEnabled(staleCheckEnabled);
} Deprecated! Use - HttpConnectionParams#setStaleCheckingEnabled(boolean) ,
HttpConnection#getParams() .
Sets whether or not isStale() will be called when testing if this connection is open.
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. |
public void setVirtualHost(String host) throws IllegalStateException {
assertNotOpen();
} Deprecated! no - longer applicable
Sets the virtual host to target. |
public void shutdownOutput() {
LOG.trace("enter HttpConnection.shutdownOutput()");
try {
// Socket.shutdownOutput is a JDK 1.3
// method. We'll use reflection in case
// we're running in an older VM
Class[] paramsClasses = new Class[0];
Method shutdownOutput =
socket.getClass().getMethod("shutdownOutput", paramsClasses);
Object[] params = new Object[0];
shutdownOutput.invoke(socket, params);
} catch (Exception ex) {
LOG.debug("Unexpected Exception caught", ex);
// Ignore, and hope everything goes right
}
// close output stream?
} Deprecated! unused -
Attempts to shutdown the Socket 's output, via Socket.shutdownOutput()
when running on JVM 1.3 or higher. |
public void tunnelCreated() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.tunnelCreated()");
if (!isSecure() || !isProxied()) {
throw new IllegalStateException(
"Connection must be secure "
+ "and proxied to use this feature");
}
if (usingSecureSocket) {
throw new IllegalStateException("Already using a secure socket");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Secure tunnel to " + this.hostName + ":" + this.portNumber);
}
SecureProtocolSocketFactory socketFactory =
(SecureProtocolSocketFactory) protocolInUse.getSocketFactory();
socket = socketFactory.createSocket(socket, hostName, portNumber, true);
int sndBufSize = this.params.getSendBufferSize();
if (sndBufSize >= 0) {
socket.setSendBufferSize(sndBufSize);
}
int rcvBufSize = this.params.getReceiveBufferSize();
if (rcvBufSize >= 0) {
socket.setReceiveBufferSize(rcvBufSize);
}
int outbuffersize = socket.getSendBufferSize();
if (outbuffersize > 2048) {
outbuffersize = 2048;
}
int inbuffersize = socket.getReceiveBufferSize();
if (inbuffersize > 2048) {
inbuffersize = 2048;
}
inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
usingSecureSocket = true;
tunnelEstablished = true;
}
Instructs the proxy to establish a secure tunnel to the host. The socket will
be switched to the secure socket. Subsequent communication is done via the secure
socket. The method can only be called once on a proxied secure connection. |
public void write(byte[] data) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.write(byte[])");
this.write(data, 0, data.length);
}
Writes the specified bytes to the output stream. |
public void write(byte[] data,
int offset,
int length) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.write(byte[], int, int)");
if (offset < 0) {
throw new IllegalArgumentException("Array offset may not be negative");
}
if (length < 0) {
throw new IllegalArgumentException("Array length may not be negative");
}
if (offset + length > data.length) {
throw new IllegalArgumentException("Given offset and length exceed the array length");
}
assertOpen();
this.outputStream.write(data, offset, length);
}
Writes length bytes in data starting at
offset to the output stream.
The general contract for
write(b, off, len) is that some of the bytes in the array b are written
to the output stream in order; element b[off] is the first byte written
and b[off+len-1] is the last byte written by this operation. |
public void writeLine() throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.writeLine()");
write(CRLF);
}
Writes "\r\n".getBytes() to the output stream. |
public void writeLine(byte[] data) throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.writeLine(byte[])");
write(data);
writeLine();
}
Writes the specified bytes, followed by "\r\n".getBytes() to the
output stream. |