winterwell.jtwitter
Class URLConnectionHttpClient

java.lang.Object
  extended by winterwell.jtwitter.URLConnectionHttpClient
All Implemented Interfaces:
Twitter.IHttpClient
Direct Known Subclasses:
OAuthSignpostClient

public class URLConnectionHttpClient
extends java.lang.Object
implements Twitter.IHttpClient

A simple http client that uses the built in URLConnection class.

Provides Twitter-focused error-handling, generating the right TwitterException. Also has a retry-on-error mode which can help smooth out Twitter's sometimes intermittent service. See setRetryOnError(boolean).


Field Summary
protected  java.lang.String name
           
protected  int timeout
           
 
Constructor Summary
URLConnectionHttpClient()
           
URLConnectionHttpClient(java.lang.String name, java.lang.String password)
           
 
Method Summary
 boolean canAuthenticate()
          Whether this client is setup to do authentication when contacting the Twitter server.
protected static void close(java.io.Closeable input)
          Close a reader/writer/stream, ignoring any exceptions that result.
 java.lang.String getPage(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Send an HTTP GET request and return the response body.
 java.lang.String post(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Send an HTTP POST request and return the response body.
protected  java.lang.String post2_getPayload(java.util.Map<java.lang.String,java.lang.String> vars)
           
protected  void setAuthentication(java.net.URLConnection connection, java.lang.String name, java.lang.String password)
          Set a header for basic authentication login.
 void setRetryOnError(boolean retryOnError)
          False by default.
 void setTimeout(int millisecs)
          Set the timeout for a single get/post request.
 java.lang.String toString()
           
protected static java.lang.String toString(java.io.InputStream inputStream)
          Use a bufferred reader (preferably UTF-8) to extract the contents of the given stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name

timeout

protected int timeout
Constructor Detail

URLConnectionHttpClient

public URLConnectionHttpClient(java.lang.String name,
                               java.lang.String password)

URLConnectionHttpClient

public URLConnectionHttpClient()
Method Detail

canAuthenticate

public boolean canAuthenticate()
Description copied from interface: Twitter.IHttpClient
Whether this client is setup to do authentication when contacting the Twitter server. Note: This is a fast method that does not call the server, so it does not check whether the access token or password is valid. See {Twitter#isValidLogin()} or TwitterAccount.verifyCredentials() if you need to check a login.

Specified by:
canAuthenticate in interface Twitter.IHttpClient

setTimeout

public void setTimeout(int millisecs)
Description copied from interface: Twitter.IHttpClient
Set the timeout for a single get/post request. This is an optional method - implementation can ignore it!

Specified by:
setTimeout in interface Twitter.IHttpClient

getPage

public java.lang.String getPage(java.lang.String uri,
                                java.util.Map<java.lang.String,java.lang.String> vars,
                                boolean authenticate)
                         throws TwitterException
Description copied from interface: Twitter.IHttpClient
Send an HTTP GET request and return the response body. Note that this will change all line breaks into system line breaks!

Specified by:
getPage in interface Twitter.IHttpClient
Parameters:
uri - The uri to fetch
vars - get arguments to add to the uri
authenticate - If true, use authentication. The authentication method used depends on the implementation (basic-auth, OAuth). It is an error to use true if no authentication details have been set.
Throws:
TwitterException - for a variety of reasons
TwitterException.E404 - for resource-does-not-exist errors

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setAuthentication

protected void setAuthentication(java.net.URLConnection connection,
                                 java.lang.String name,
                                 java.lang.String password)
Set a header for basic authentication login.


post

public java.lang.String post(java.lang.String uri,
                             java.util.Map<java.lang.String,java.lang.String> vars,
                             boolean authenticate)
                      throws TwitterException
Description copied from interface: Twitter.IHttpClient
Send an HTTP POST request and return the response body.

Specified by:
post in interface Twitter.IHttpClient
Parameters:
uri - The uri to post to.
vars - The form variables to send. These are URL encoded before sending.
authenticate - If true, send user authentication
Returns:
The response from the server.
Throws:
TwitterException - for a variety of reasons
TwitterException.E404 - for resource-does-not-exist errors

post2_getPayload

protected java.lang.String post2_getPayload(java.util.Map<java.lang.String,java.lang.String> vars)

setRetryOnError

public void setRetryOnError(boolean retryOnError)
False by default. Setting this to true switches on a robustness workaround: when presented with a 50X server error, the system will wait 1 second and make a second attempt.


toString

protected static java.lang.String toString(java.io.InputStream inputStream)
Use a bufferred reader (preferably UTF-8) to extract the contents of the given stream. A convenience method for toString(Reader).


close

protected static void close(java.io.Closeable input)
Close a reader/writer/stream, ignoring any exceptions that result. Also flushes if there is a flush() method.