package org.oauth2.client4j.utils; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; import java.net.URLEncoder; import java.util.Collection; import java.util.Map; import org.oauth2.client4j.http.response.OAuthResponse; public class OAuthKit { private static final String ENCODING = "UTF-8"; private static final String PARAMETER_SEPARATOR = "&"; private static final String NAME_VALUE_SEPARATOR = "="; private static final String DEFAULT_CONTENT_CHARSET = ENCODING; @SuppressWarnings("unchecked") public static <T extends OAuthResponse> T createCustomResponse(String body, String contentType, String characterEncoding, int statusCode, Class<T> clazz) { OAuthResponse resp = (OAuthResponse) instantiateClassWithParameters( clazz, null, null); resp.init(body, contentType, characterEncoding, statusCode); return (T) resp; } public static boolean isEmpty(String value) { return value == null || "".equals(value); } public static String format( final Collection<? extends Map.Entry<String, String>> parameters, final String encoding) { final StringBuilder result = new StringBuilder(); for (final Map.Entry<String, String> parameter : parameters) { String value = parameter.getValue() == null ? null : String .valueOf(parameter.getValue()); if (!isEmpty(parameter.getKey()) && !isEmpty(value)) { final String encodedName = encode(parameter.getKey(), encoding); final String encodedValue = value != null ? encode(value, encoding) : ""; if (result.length() > 0) { result.append(PARAMETER_SEPARATOR); } result.append(encodedName); result.append(NAME_VALUE_SEPARATOR); result.append(encodedValue); } } return result.toString(); } private static String encode(final String content, final String encoding) { try { return URLEncoder.encode(content, encoding != null ? encoding : "UTF-8"); } catch (UnsupportedEncodingException problem) { throw new IllegalArgumentException(problem); } } public static <T> T instantiateClassWithParameters(Class<T> clazz, Class<?>[] paramsTypes, Object[] paramValues) { try { if (paramsTypes != null && paramValues != null) { if (!(paramsTypes.length == paramValues.length)) { throw new IllegalArgumentException( "Number of types and values must be equal"); } if (paramsTypes.length == 0 && paramValues.length == 0) { return clazz.newInstance(); } Constructor<T> clazzConstructor = clazz .getConstructor(paramsTypes); return clazzConstructor.newInstance(paramValues); } return clazz.newInstance(); } catch (Exception e) { throw new RuntimeException("error instance response class"); } } /** * Read data from Input Stream and save it as a String. * * @param is * InputStream to be read * @return String that was read from the stream */ public static String saveStreamAsString(InputStream is) throws IOException { return toString(is, ENCODING); } /** * Get the entity content as a String, using the provided default character * set if none is found in the entity. If defaultCharset is null, the * default "UTF-8" is used. * * @param is * input stream to be saved as string * @param defaultCharset * character set to be applied if none found in the entity * @return the entity content as a String * @throws IllegalArgumentException * if entity is null or if content length > Integer.MAX_VALUE * @throws IOException * if an error occurs reading the input stream */ public static String toString(final InputStream is, final String defaultCharset) throws IOException { if (is == null) { throw new IllegalArgumentException("InputStream may not be null"); } String charset = defaultCharset; if (charset == null) { charset = DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(is, charset); StringBuilder sb = new StringBuilder(); int l; try { char[] tmp = new char[4096]; while ((l = reader.read(tmp)) != -1) { sb.append(tmp, 0, l); } } finally { reader.close(); } return sb.toString(); } }