WebObjects 5.4.2

com.webobjects.appserver
Class WOHTTPConnection

java.lang.Object
  extended by com.webobjects.appserver.WOHTTPConnection
All Implemented Interfaces:
NSKeyValueCoding, NSKeyValueCoding.ErrorHandling, NSKeyValueCodingAdditions

public class WOHTTPConnection
extends Object
implements NSKeyValueCoding, NSKeyValueCoding.ErrorHandling, NSKeyValueCodingAdditions

The WOHTTPConnection class is intended to be used as a client for HTTP communications. It gives direct access to HTTP contents and headers. WOHTTPConnection's sendRequest method allows you to send a WORequest object directly to the server specified by the constructor's host and port parameters, and readResponse allows you to receive WOResponse objects from that same server.

Use HTTP/1.1 protocol in WORequest whenever possible.

See Also:
WORequest, WOResponse, WOHTTPConnection.WOHTTPConnection(java.lang.String, int), WOHTTPConnection.sendRequest(WORequest aRequest), WOHTTPConnection.readResponse()

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions
NSKeyValueCodingAdditions.DefaultImplementation, NSKeyValueCodingAdditions.Utility
 
Field Summary
 
Fields inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions
KeyPathSeparator
 
Constructor Summary
WOHTTPConnection(String aHost, int portNumber)
          Creates a WOHTTPConnection instance initialized with the specified host name and port number.
WOHTTPConnection(String aHost, int portNumber, int timeout)
          Deprecated. Use setReceiveTimeout, setSendTimeout to control the timeout after creating an instance.
 
Method Summary
static boolean canAccessFieldsDirectly()
          WOHTTPConnection's implementation of this static method returns true, indicating that key/value coding is allowed to access fields in this object if an appropriate method isn't present.
 int connectTimeout()
          Deprecated. There will be a connection only after a sendRequest has been executed. Thus, it would make more sense to control the send timeout using setSendTimeout.
static void expectContentLengthHeader(boolean expectContentLengthHeader, int contentTimeout)
          Deprecated. If you are using HTTP/1.1, there is no need to tell the receiver whether to expect content length since it follows RFC 2616 (HTTP/1.1).
 boolean followRedirects(boolean followRedirects)
          Gets boolean value indicating whether or not HTTP redirects (3xx) should be automatically followed.
 Object handleQueryWithUnboundKey(String key)
          Conformance to NSKeyValueCoding.ErrorHandling.
 void handleTakeValueForUnboundKey(Object value, String key)
          Conformance to NSKeyValueCoding.ErrorHandling.
 boolean isConnected()
          Deprecated. This is meaningless since there will not be a connection before sendRequest is executed.
 boolean keepAliveEnabled()
          Gets the previously set boolean value to indicate if a connection should be left open after a request has been sent.
 WOResponse readResponse()
          Receives a response after sendRequest is executed.
 long readTimeout()
          Gets the read timeout interval set previously for a finer-grained read operation.
 int receiveTimeout()
          Gets the receive timeout interval.
 boolean sendRequest(WORequest request)
          Sends a WORequest request to a destination connection point initialized in the constructor WOHTTPConnection.
 int sendTimeout()
          Gets the send timeout interval.
 void setConnectTimeout(int connectTimeout)
          Deprecated. There will be a connection only after a sendRequest has been executed. Thus, it would make more sense to control the send timeout using setSendTimeout.
 void setFollowRedirects(boolean followRedirects)
          Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.
 void setKeepAliveEnabled(boolean keepAlive)
          Specifies according to keepAlive whether the connection is to be left open after a request has been sent so that subsequent requests to the same destination connection point don't require the connection to be re-opened.
 void setReadTimeout(int readTimeout)
          Sets the timeout interval to control each finer-grained socket read operation when receiving data from a remote connection.
 void setReceiveTimeout(int receiveTimeout)
          Sets the receive timeout interval to receiveTimeout milliseconds.
 void setSendTimeout(int sendTimeout)
          Sets the send timeout interval to sendTimeout milliseconds.
protected static Socket socketForHostAndPortAndTimeout(String host, int port, int timeout)
          Protected class method that returns a java.net.Socket for the provided hostname and port.
 void takeValueForKey(Object value, String key)
          Conformance to NSKeyValueCoding.
 void takeValueForKeyPath(Object value, String keyPath)
          Conformance to NSKeyValueCodingAdditions.
 String toString()
           
 void unableToSetNullForKey(String key)
          Conformance to NSKeyValueCoding.ErrorHandling.
 Object valueForKey(String key)
          Conformance to NSKeyValueCoding.
 Object valueForKeyPath(String keyPath)
          Conformance to NSKeyValueCodingAdditions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WOHTTPConnection

public WOHTTPConnection(String aHost,
                        int portNumber)
Creates a WOHTTPConnection instance initialized with the specified host name and port number.

Parameters:
aHost - the host name to connect to
portNumber - the port number to connect to

WOHTTPConnection

@Deprecated
public WOHTTPConnection(String aHost,
                                   int portNumber,
                                   int timeout)
Deprecated. Use setReceiveTimeout, setSendTimeout to control the timeout after creating an instance.

Creates a WOHTTPConnection instance initialized with the specified host name, port number and the timeout interval in milliseconds

Parameters:
aHost - the host name to connect to
portNumber - the port number to connect to
timeout - timeout interval in milliseconds
See Also:
WOHTTPConnection.setReceiveTimeout(int), WOHTTPConnection.setSendTimeout(int)
Method Detail

expectContentLengthHeader

@Deprecated
public static void expectContentLengthHeader(boolean expectContentLengthHeader,
                                                        int contentTimeout)
Deprecated. If you are using HTTP/1.1, there is no need to tell the receiver whether to expect content length since it follows RFC 2616 (HTTP/1.1).

Relevant only in HTTP/1.0, this static method allows you to specify whether a content-length header is expected to accompany HTTP content, and, if a content-length header is not found, how long (in milliseconds) WOHTTPConnection should wait to determine that all HTTP content has been received.

Set the first parameter to false and supply an appropriate timeout if a content-length header might not be expected to be present. If you set the first parameter to true, the timeout parameter is ignored and, if no content-length header is found among the HTTP content, no data will be returned when reading from the socket. If a content-length header is detected, the value the header specifies will be used to determine how much data to accumulate independent of the contentTimeout.

Parameters:
expectContentLengthHeader - specifies whether a content-length header is expected
contentTimeout - decides how long (in milliseconds) WOHTTPConnection should wait

setReceiveTimeout

public void setReceiveTimeout(int receiveTimeout)
Sets the receive timeout interval to receiveTimeout milliseconds. This timeout interval specifies the timeout applicable to the entire receive operation, including each individual sockets read operation. To control the timeout for each socket read operation, use setReadTimeout.

The default value is 30 seconds unless overridden by the WOHTTPReceiveTimeout property

Parameters:
receiveTimeout - the new receive timeout interval in milliseconds
See Also:
WOHTTPConnection.setReadTimeout(int timeout), WOHTTPConnection.receiveTimeout()

receiveTimeout

public int receiveTimeout()
Gets the receive timeout interval.

Returns:
the receive timeout interval in milliseconds
See Also:
WOHTTPConnection.setReceiveTimeout(int timeout)

isConnected

@Deprecated
public boolean isConnected()
Deprecated. This is meaningless since there will not be a connection before sendRequest is executed.

Returns:
true
See Also:
WOHTTPConnection.sendRequest(com.webobjects.appserver.WORequest)

setReadTimeout

public void setReadTimeout(int readTimeout)
Sets the timeout interval to control each finer-grained socket read operation when receiving data from a remote connection. To control the timeout for the entire receive operation, use setReceiveTimeout.

The default value is the system-dependent socket read timeout. Since there is a default receive timeout interval, you do not usually have to set the read timeout interval.

Parameters:
readTimeout - timeout in milliseconds to perform a finer-grained read operation
See Also:
WOHTTPConnection.readTimeout(), WOHTTPConnection.setReceiveTimeout(int)

readTimeout

public long readTimeout()
Gets the read timeout interval set previously for a finer-grained read operation.

Returns:
read timeout in milliseconds or 0 if not set previously
See Also:
WOHTTPConnection.setReadTimeout(int)

setSendTimeout

public void setSendTimeout(int sendTimeout)
Sets the send timeout interval to sendTimeout milliseconds.

The default value is 10 seconds.

Parameters:
sendTimeout - the new send timeout interval in milliseconds
See Also:
WOHTTPConnection.sendTimeout()

sendTimeout

public int sendTimeout()
Gets the send timeout interval.

Returns:
the send timeout interval in milliseconds
See Also:
WOHTTPConnection.setSendTimeout(int timeout)

setConnectTimeout

@Deprecated
public void setConnectTimeout(int connectTimeout)
Deprecated. There will be a connection only after a sendRequest has been executed. Thus, it would make more sense to control the send timeout using setSendTimeout.

Sets the connection timeout interval to connectTimeout milliseconds. The default value for this timeout is 5 seconds unless overridden by the WOHTTPConnectTimeout property.

Parameters:
connectTimeout - the connection timeout interval
See Also:
WOHTTPConnection.setSendTimeout(int timeout), WOHTTPConnection.sendRequest(com.webobjects.appserver.WORequest)

connectTimeout

@Deprecated
public int connectTimeout()
Deprecated. There will be a connection only after a sendRequest has been executed. Thus, it would make more sense to control the send timeout using setSendTimeout.

Gets the connection timeout interval.

Returns:
the connection timeout interval in milliseconds
See Also:
WOHTTPConnection.sendTimeout(), WOHTTPConnection.sendRequest(com.webobjects.appserver.WORequest)

setKeepAliveEnabled

public void setKeepAliveEnabled(boolean keepAlive)
Specifies according to keepAlive whether the connection is to be left open after a request has been sent so that subsequent requests to the same destination connection point don't require the connection to be re-opened.

The default for HTTP/1.1 is to keep the connection open while for HTTP/1.0 is to close the connection.

In cases where you make a lot of one-time connections to different servers using HTTP/1.1, you want to consider using this method with keepAlive=false to prevent too many opened connections (sockets). Take note that this value overwrites the "connection" header in the WORequest and the default value is true. Thus, if your intention is to have "connection=close" in the header (which results in the connection closing), make sure that you set the parameter to false.

Parameters:
keepAlive - the flag that decides whether the connection is to be left open after each request has been sent
See Also:
WOHTTPConnection.keepAliveEnabled(), WORequest

keepAliveEnabled

public boolean keepAliveEnabled()
Gets the previously set boolean value to indicate if a connection should be left open after a request has been sent.

The default value is true.

Returns:
true if the connection will be left open after a request has been sent; false otherwise
See Also:
WOHTTPConnection.setKeepAliveEnabled(boolean)

setFollowRedirects

public void setFollowRedirects(boolean followRedirects)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class. True by default. Applets cannot change this variable.

If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
followRedirects - a boolean indicating whether or not to follow HTTP redirects
See Also:
SecurityManager.checkSetFactory(), WOHTTPConnection.followRedirects(boolean)

followRedirects

public boolean followRedirects(boolean followRedirects)
Gets boolean value indicating whether or not HTTP redirects (3xx) should be automatically followed. The parameter is ignored.

Returns:
true if HTTP redirects should be automatically followed, false if not
See Also:
WOHTTPConnection.setFollowRedirects(boolean)

socketForHostAndPortAndTimeout

protected static Socket socketForHostAndPortAndTimeout(String host,
                                                       int port,
                                                       int timeout)
Protected class method that returns a java.net.Socket for the provided hostname and port. This method may throw a java.io.IOException or a java.net.UnknownHostException if it is unable to create a new socket based upon the supplied hostname and port.

Parameters:
host - the receiver's host name
port - the receiver's portNumber
timeout - timeout interval in milliseconds
Returns:
returns a java.net.Socket for the provided hostname and port

sendRequest

public boolean sendRequest(WORequest request)
Sends a WORequest request to a destination connection point initialized in the constructor WOHTTPConnection. After a request has been sent, use readReponse to receive the response back.

Use setSendTimeout to control the send timeout interval.

Parameters:
request - request sent to the server
Returns:
true if the WORequest was successfully sent, or false otherwise
See Also:
WOHTTPConnection, WOHTTPConnection.readResponse(), WOHTTPConnection.setSendTimeout(int), WOHTTPConnection.setKeepAliveEnabled(boolean flag)

readResponse

public WOResponse readResponse()
Receives a response after sendRequest is executed. The response is then encapsulated as a WOResponse object and returned.

Use setReceiveTimeout to control the receive timeout interval.

Returns:
a WOResponse object or null if an error is detected while reading or interpreting the response.
See Also:
WOHTTPConnection.sendRequest(WORequest aRequest), WOHTTPConnection.setReceiveTimeout(int)

toString

public String toString()
Overrides:
toString in class Object
Returns:
A string representation of the receiver.

canAccessFieldsDirectly

public static boolean canAccessFieldsDirectly()
WOHTTPConnection's implementation of this static method returns true, indicating that key/value coding is allowed to access fields in this object if an appropriate method isn't present.

Returns:

valueForKey

public Object valueForKey(String key)
Conformance to NSKeyValueCoding.

Specified by:
valueForKey in interface NSKeyValueCoding
Parameters:
key - identifies the property of an object
Returns:
the receiver's value for the property identified by key
See Also:
NSKeyValueCoding.NullValue, NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String), NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.ErrorHandling.handleQueryWithUnboundKey(java.lang.String)

takeValueForKey

public void takeValueForKey(Object value,
                            String key)
Conformance to NSKeyValueCoding.

Specified by:
takeValueForKey in interface NSKeyValueCoding
Parameters:
key - identifies the property to be set
value - the value to which the property specified by key should be set
See Also:
NSKeyValueCoding.NullValue, NSKeyValueCoding.valueForKey, NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.ErrorHandling.handleTakeValueForUnboundKey(java.lang.Object, java.lang.String)

handleQueryWithUnboundKey

public Object handleQueryWithUnboundKey(String key)
Conformance to NSKeyValueCoding.ErrorHandling.

Specified by:
handleQueryWithUnboundKey in interface NSKeyValueCoding.ErrorHandling
Parameters:
key -
Returns:
See Also:
NSKeyValueCoding.valueForKey, NSKeyValueCoding.UnknownKeyException, NSKeyValueCoding.DefaultImplementation

handleTakeValueForUnboundKey

public void handleTakeValueForUnboundKey(Object value,
                                         String key)
Conformance to NSKeyValueCoding.ErrorHandling.

Specified by:
handleTakeValueForUnboundKey in interface NSKeyValueCoding.ErrorHandling
Parameters:
value -
key -
See Also:
NSKeyValueCoding.takeValueForKey, NSKeyValueCoding.UnknownKeyException, NSKeyValueCoding.DefaultImplementation

unableToSetNullForKey

public void unableToSetNullForKey(String key)
Conformance to NSKeyValueCoding.ErrorHandling.

Specified by:
unableToSetNullForKey in interface NSKeyValueCoding.ErrorHandling
Parameters:
key -
See Also:
NSKeyValueCoding.takeValueForKey, NSKeyValueCoding.DefaultImplementation

valueForKeyPath

public Object valueForKeyPath(String keyPath)
Conformance to NSKeyValueCodingAdditions.

Specified by:
valueForKeyPath in interface NSKeyValueCodingAdditions
Parameters:
keyPath - identifies the derived property of an object
Returns:
receiver's value for the derived property identified by keyPath
See Also:
NSKeyValueCoding.valueForKey(java.lang.String), NSKeyValueCodingAdditions.takeValueForKeyPath(java.lang.Object, java.lang.String), NSKeyValueCodingAdditions.DefaultImplementation

takeValueForKeyPath

public void takeValueForKeyPath(Object value,
                                String keyPath)
Conformance to NSKeyValueCodingAdditions.

Specified by:
takeValueForKeyPath in interface NSKeyValueCodingAdditions
Parameters:
keyPath - identifies a derived property of the receiver
value - value to which the derived property identified by keyPath will be set
See Also:
NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String), NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String), NSKeyValueCodingAdditions.DefaultImplementation

Last updated June 2008

Copyright © 2000-2008 Apple Inc.