/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

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