winterwell.jtwitter
Class URLConnectionHttpClient

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

public class URLConnectionHttpClient
extends java.lang.Object
implements Twitter.IHttpClient, java.io.Serializable

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).

See Also:
Serialized Form

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.net.HttpURLConnection connect(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Lower-level GET method.
 Twitter.IHttpClient copy()
           
protected  void disconnect(java.net.HttpURLConnection connection)
           
 java.lang.String getHeader(java.lang.String headerName)
          Fetch a header from the last http request.
 java.lang.String getPage(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Send an HTTP GET request and return the response body.
 RateLimit getRateLimit(Twitter.KRequestType reqType)
           
 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.
 java.net.HttpURLConnection post2_connect(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars)
          Lower-level POST method.
protected  java.lang.String post2_getPayload(java.util.Map<java.lang.String,java.lang.String> vars)
           
 java.lang.String postMultipartForm(java.lang.String url, java.util.Map<java.lang.String,?> vars)
           
protected  void processHeaders(java.net.HttpURLConnection connection)
          Cache headers for getHeader(String)
protected  void setAuthentication(java.net.URLConnection connection, java.lang.String name, java.lang.String password)
          Set a header for basic authentication login.
 void setHtmlImpliesError(boolean htmlImpliesError)
           
 void setMinRateLimit(int minRateLimit)
          Use this to protect your Twitter API rate-limit.
 void setRetryOnError(boolean retryOnError)
          False by default.
 void setTimeout(int millisecs)
          Set the timeout for a single get/post request.
 java.lang.String toString()
           
 
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()

URLConnectionHttpClient

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

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.

Parameters:
input - Can be null

setHtmlImpliesError

public void setHtmlImpliesError(boolean htmlImpliesError)
Parameters:
htmlImpliesError - default is true. If true, an html response will be treated as a server error & generate a TwitterException.E50X

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 Twitter_Account.verifyCredentials() if you need to check a login.

Specified by:
canAuthenticate in interface Twitter.IHttpClient

connect

public java.net.HttpURLConnection connect(java.lang.String url,
                                          java.util.Map<java.lang.String,java.lang.String> vars,
                                          boolean authenticate)
                                   throws java.io.IOException
Description copied from interface: Twitter.IHttpClient
Lower-level GET method.

Specified by:
connect in interface Twitter.IHttpClient
Returns:
Throws:
java.io.IOException

copy

public Twitter.IHttpClient copy()
Specified by:
copy in interface Twitter.IHttpClient
Returns:
a copy of this client. The copy can share structure, but it MUST be safe for passing to a new thread to be used in parallel with the original.

disconnect

protected final void disconnect(java.net.HttpURLConnection connection)

getHeader

public java.lang.String getHeader(java.lang.String headerName)
Description copied from interface: Twitter.IHttpClient
Fetch a header from the last http request. This is inherently NOT thread safe. Headers from error messages should (probably) be cached.

Specified by:
getHeader in interface Twitter.IHttpClient
Returns:
header value, or null if unset

getPage

public final java.lang.String getPage(java.lang.String url,
                                      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:
url - 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

getRateLimit

public RateLimit getRateLimit(Twitter.KRequestType reqType)
Specified by:
getRateLimit in interface Twitter.IHttpClient
See Also:
This is where the Twitter method is implemented.

postMultipartForm

public final java.lang.String postMultipartForm(java.lang.String url,
                                                java.util.Map<java.lang.String,?> vars)
                                         throws TwitterException
Parameters:
uri -
vars - Can include File values
Returns:
Throws:
TwitterException

post

public final 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_connect

public java.net.HttpURLConnection post2_connect(java.lang.String uri,
                                                java.util.Map<java.lang.String,java.lang.String> vars)
                                         throws java.lang.Exception
Description copied from interface: Twitter.IHttpClient
Lower-level POST method.

Specified by:
post2_connect in interface Twitter.IHttpClient
Returns:
a freshly opened authorised connection
Throws:
TwitterException
java.lang.Exception

post2_getPayload

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

processHeaders

protected final void processHeaders(java.net.HttpURLConnection connection)
Cache headers for getHeader(String)

Parameters:
connection -

setAuthentication

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


setMinRateLimit

public void setMinRateLimit(int minRateLimit)
Use this to protect your Twitter API rate-limit. E.g. if you want to keep some credit in reserve for core activity. 0 by default. If set above zero, this JTwitter object will start pre-emptively throwing rate-limit exceptions when it gets down to the specified level.


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/2 a second and make a second attempt.


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 - implementations can ignore it!

Specified by:
setTimeout in interface Twitter.IHttpClient

toString

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