/** * Default constructor. * * @param value verifier value */ public Verifier(String value) { Preconditions.checkNotNull(value, "Must provide a valid string as verifier"); this.value = value; }
/** * Checks that an object is not null. * * @param object any object * @param errorMsg error message * @throws IllegalArgumentException if the object is null */ public static void checkNotNull(Object object, String errorMsg) { check(object != null, errorMsg); }
/** * Checks that a URL is valid * * @param url any string * @param errorMsg error message */ public static void checkValidUrl(String url, String errorMsg) { checkEmptyString(url, errorMsg); check(isUrl(url), errorMsg); }
/** * Join an array of Object with a given character as a separator * * @param array array to join * @param separator char to separate values * @return resulting string of the concatained values with separator */ public static String join(Object[] array, char separator) { if (array != null) return join(Arrays.asList(array), ','); else return ""; } }
/** * {@inheritDoc} */ public String getSignature(String baseString, String apiSecret, String tokenSecret) { try { Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string"); return OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret); } catch (Exception e) { throw new OAuthSignatureException(baseString, e); } }
/** * {@inheritDoc} */ public Token extract(String response) { Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string"); Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response); if (matcher.find()) { String token = OAuthEncoder.decode(matcher.group(1)); return new Token(token, EMPTY_SECRET); } else { throw new AgoravaException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null); } } }
@SignatureType(PLAINTEXT) public class PlaintextSignatureService implements SignatureService { private static final String METHOD = "PLAINTEXT";
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String verifier = req.getParameter(lifeCycleService.getVerifierParamName()); lifeCycleService.endDance(verifier); renderResponse(req, resp); } }
/** * {@inheritDoc} */ public Token extract(String response) { Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string"); String token = extract(response, TOKEN_REGEX); String secret = extract(response, SECRET_REGEX); return new Token(token, secret); }
@Override public OAuthSession getSession() { return OAuthLifeCycleService.resolveSessionForQualifier(config.getQualifier()); }
@Override public <T> T post(String uri, Map<String, ? extends Object> params, Class<T> clazz) { OAuthRequest request = requestFactory(POST, uri); request.addBodyParameters(params); return getJsonMapper().mapToObject(sendSignedRequest(request), clazz); }
/** * Checks that a URL is a valid OAuth callback * * @param url any string * @param errorMsg error message */ public static void checkValidOAuthCallback(String url, String errorMsg) { checkEmptyString(url, errorMsg); if (url.toLowerCase(ENGLISH).compareToIgnoreCase(AgoravaConstants.OUT_OF_BAND) != 0) { check(isUrl(url), errorMsg); } }
@Override public List<TwitterProfile> getUsers(String... userIds) { String joinedIds = join(userIds, ','); return getService().get(buildUri(LOOKUP, "user_id", joinedIds), TwitterProfileList.class); }
/** * {@inheritDoc} */ public String getSignature(String baseString, String apiSecret, String tokenSecret) { try { Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string"); Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string"); return doSign(baseString, OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret)); } catch (Exception e) { throw new OAuthSignatureException(baseString, e); } }
/** * Default constructor * * @param token token value. Can't be null. * @param secret token secret. Can't be null. */ public Token(String token, String secret) { Preconditions.checkNotNull(token, "Token can't be null"); Preconditions.checkNotNull(secret, "Secret can't be null"); this.token = token; this.secret = secret; }
/** * Checks that a string is not null or empty * * @param string any string * @param errorMsg error message * @throws IllegalArgumentException if the string is null or empty */ public static void checkEmptyString(String string, String errorMsg) { check(string != null && !string.trim().equals(""), errorMsg); }
@Override public String asOauthBaseString() { Collections.sort(params); return OAuthEncoder.encode(asFormUrlEncodedString()); }
@Override public List<TwitterProfile> getUsersByName(String... screenNames) { String joinedScreenNames = join(screenNames, ','); return getService().get(buildUri(LOOKUP, "screen_name", joinedScreenNames), TwitterProfileList.class); }
@Override public String asUrl(String url) { Preconditions.checkNotNull(url, "Cannot append to null URL"); String queryString = asFormUrlEncodedString(); if (queryString.equals(EMPTY_STRING)) { return url; } else { url += url.indexOf(QUERY_STRING_SEPARATOR) != -1 ? PARAM_SEPARATOR : QUERY_STRING_SEPARATOR; url += queryString; return url; } }