PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/facebook-java-api/src/main/java/com/google/code/facebookapi/BasicClientHelper.java

http://facebook-java-api.googlecode.com/
Java | 132 lines | 114 code | 15 blank | 3 comment | 22 complexity | 503e4261b55f7a276dcb11a1f0b3c698 MD5 | raw file
  1package com.google.code.facebookapi;
  2
  3import java.io.BufferedReader;
  4import java.io.Closeable;
  5import java.io.IOException;
  6import java.io.InputStream;
  7import java.io.InputStreamReader;
  8import java.io.Reader;
  9import java.io.UnsupportedEncodingException;
 10import java.net.HttpURLConnection;
 11import java.net.URLEncoder;
 12import java.util.Collection;
 13import java.util.Map;
 14
 15import org.apache.commons.logging.Log;
 16import org.apache.commons.logging.LogFactory;
 17
 18/**
 19 * Basic client taking care of rest call mechanics (signing, etc) to facebook. No api knowledge, nor response interpretation is planned.
 20 */
 21public class BasicClientHelper {
 22
 23	protected static Log log = LogFactory.getLog( BasicClientHelper.class );
 24
 25	public static String toString( InputStream data ) throws IOException {
 26		Reader in = new BufferedReader( new InputStreamReader( data, "UTF-8" ) );
 27		StringBuilder buffer = new StringBuilder();
 28		char[] buf = new char[1000];
 29		int l = 0;
 30		while ( l >= 0 ) {
 31			buffer.append( buf, 0, l );
 32			l = in.read( buf );
 33		}
 34		return buffer.toString();
 35	}
 36
 37
 38	public static CharSequence delimit( Iterable<?> iterable ) {
 39		if ( iterable == null ) {
 40			return null;
 41		}
 42		StringBuilder buffer = new StringBuilder();
 43		boolean empty = true;
 44		boolean notFirst = false;
 45		for ( Object item : iterable ) {
 46			if ( notFirst ) {
 47				buffer.append( "," );
 48			} else {
 49				empty = false;
 50				notFirst = true;
 51			}
 52			buffer.append( item.toString() );
 53		}
 54		if ( empty ) {
 55			return null;
 56		}
 57		return buffer;
 58	}
 59
 60	public static String encode( CharSequence target ) {
 61		if ( target == null ) {
 62			return "";
 63		}
 64		String result = target.toString();
 65		try {
 66			result = URLEncoder.encode( result, "UTF8" );
 67		}
 68		catch ( UnsupportedEncodingException ex ) {
 69			throw runtimeException( ex );
 70		}
 71		return result;
 72	}
 73
 74	public static String getResponse( InputStream data ) throws IOException {
 75		Reader in = new BufferedReader( new InputStreamReader( data, "UTF-8" ) );
 76		StringBuilder buffer = new StringBuilder();
 77		char[] buf = new char[1000];
 78		int l = 0;
 79		while ( l >= 0 ) {
 80			buffer.append( buf, 0, l );
 81			l = in.read( buf );
 82		}
 83		return buffer.toString();
 84	}
 85
 86	public static CharSequence delimit( Collection<Map.Entry<String,String>> entries, String delimiter, String equals, boolean doEncode ) {
 87		if ( entries == null || entries.isEmpty() ) {
 88			return null;
 89		}
 90		StringBuilder buffer = new StringBuilder();
 91		boolean notFirst = false;
 92		for ( Map.Entry<String,String> entry : entries ) {
 93			if ( notFirst ) {
 94				buffer.append( delimiter );
 95			} else {
 96				notFirst = true;
 97			}
 98			CharSequence value = entry.getValue();
 99			buffer.append( entry.getKey() ).append( equals ).append( doEncode ? encode( value ) : value );
100		}
101		return buffer;
102	}
103
104	public static void disconnect( HttpURLConnection conn ) {
105		if ( conn != null ) {
106			conn.disconnect();
107		}
108	}
109
110	public static void close( Closeable c ) {
111		if ( c != null ) {
112			try {
113				c.close();
114			}
115			catch ( IOException ex ) {
116				log.warn( "Trouble closing connection", ex );
117			}
118		}
119	}
120
121	public static RuntimeException runtimeException( Exception ex ) {
122		if ( ! ( ex instanceof RuntimeException ) ) {
123			return new RuntimeException( ex );
124		}
125		return (RuntimeException) ex;
126	}
127
128	protected static String toString( CharSequence cs ) {
129		return cs == null ? null : cs.toString();
130	}
131
132}