public static ResourceBundle retrievePropertyBundle(String bundleName) { ClassLoader cl=Thread.currentThread().getContextClassLoader(); InputStream is =cl.getResourceAsStream(bundleName+".properties"); if(is==null) { throw new AgoravaException("Property file : " +bundleName+".properties, not found"); } try { return new PropertyResourceBundle(is); } catch (IOException e) { throw new AgoravaException("Didn't found Agorava properties file",e); } }
public static ResourceBundle retrievePropertyBundle(String bundleName) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl.getResourceAsStream(bundleName + ".properties"); try { return new PropertyResourceBundle(is); } catch (IOException e) { throw new AgoravaException("Didn't found Agorava properties file", e); } }
@Override public String objectToJsonString(Object obj) { try { if (obj.getClass().equals(String.class)) return (String) obj; return objectMapper.writeValueAsString(obj); } catch (Exception e) { throw new AgoravaException("Unable to map a " + obj.getClass().getName() + " to json", e); } }
public static Annotation getSingleProviderRelatedQualifier(Set<Annotation> qualifiers, boolean emptyAccepted) { Set<Annotation> annotations = getAnnotationsWithMeta(qualifiers, ProviderRelated.class); if (annotations.size() == 0) if (emptyAccepted) return null; else throw new AgoravaException("No ProviderRelated qualifier found"); else if (annotations.size() > 1) throw new AgoravaException("Type should not have more than one ProviderRelated Qualifier"); return annotations.iterator().next(); }
@SuppressWarnings("unchecked") private <T> List<T> deserializeDataList(JsonNode jsonNode, final Class<T> elementType) { try { CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, elementType); return (List<T>) objectMapper.readValue(jsonNode.textValue(), listType); } catch (IOException e) { throw new AgoravaException("Error deserializing data from Facebook: " + e.getMessage(), e); } }
/** * This reflection method tries to call a field setter in the builder from its name * It's useful when settings are reading from a text source. * * @param k name of the param * @param value value to set in the param * @throws AgoravaException if the setter can't be found or invoked */ void invokeSetter(String k, String value) { Method setter; Class<? extends OAuthAppSettingsBuilder> clazz = getClass(); try { setter = clazz.getMethod(k, String.class); setter.invoke(this, value); } catch (Exception e) { throw new AgoravaException("Unable to invoke setter for " + k + " in class " + clazz, e); } } }
private String extract(String response, Pattern p) { Matcher matcher = p.matcher(response); if (matcher.find() && matcher.groupCount() >= 1) { return OAuthEncoder.decode(matcher.group(1)); } else { throw new AgoravaException("Response body is incorrect. Can't extract token and secret from this: '" + response + "'", null); } } }
/** * Decode a String from OAuth format * * @param encoded the string to decode * @return decoded string */ public static String decode(String encoded) { Preconditions.checkNotNull(encoded, "Cannot decode null object"); try { return URLDecoder.decode(encoded, CHARSET); } catch (UnsupportedEncodingException uee) { throw new AgoravaException("Charset not found while decoding string: " + CHARSET, uee); } } }
private <T> T deserializePost(String postType, Class<T> type, ObjectNode node) { try { if (postType == null) { postType = determinePostType(node); } // Must have separate postType field for polymorphic deserialization. If we key off of the "type" field, // then it will // be null when trying to deserialize the type property. node.put("postType", postType); // used for polymorphic deserialization node.put("type", postType); // used to set Post's type property return objectMapper.reader(type).readValue(node.toString()); // TODO: EXTREMELY HACKY--TEMPORARY UNTIL I FIGURE // OUT HOW JACKSON 2 DOES THIS } catch (IOException shouldntHappen) { throw new AgoravaException("Error deserializing " + postType + " post", shouldntHappen); } }
public Token extract(String response) { Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String"); Matcher matcher = accessTokenPattern.matcher(response); if (matcher.find()) { return new Token(matcher.group(1), ""); } else { throw new AgoravaException("Cannot extract an acces token. Response was: " + response); } }
/** * Default constructor used to find Provider's name in {@link org.agorava.api.atinject.ProviderRelated} meta * annotation that must be present on mandatory annotation on all concrete ProviderConfig descendants */ protected ProviderConfig() { Annotation res = null; Class<? extends ProviderConfig> clazz = getClass(); for (Annotation a : clazz.getAnnotations()) { if (a.annotationType().isAnnotationPresent(ProviderRelated.class)) { if (res == null) res = a; else throw new AgoravaException("Class " + getClass() + " have more than one ProviderRelated " + "annotation"); } } if (res == null) throw new AgoravaException("Class " + getClass() + " doesn't have a ProviderRelated annotation"); providerAnnotation = res; providerName = res.annotationType().getAnnotation(ProviderRelated.class).value(); }
ResponseImpl(HttpURLConnection connection) { try { connection.connect(); url = connection.getURL(); code = connection.getResponseCode(); headers = parseHeaders(connection); InputStream res = isSuccessful() ? connection.getInputStream() : connection.getErrorStream(); if (GZIP_CONTENT_ENCODING.equals(headers.get(CONTENT_ENCODING))) try { stream = new GZIPInputStream(res); } catch (IOException e) { throw new AgoravaException("Unable to create GZIPInputStream", e); } else stream = res; } catch (IOException e) { throw new AgoravaException("The IP address of a host could not be determined.", e); } }
@Override public OAuthSession buildSessionFor(String providerName) { OAuthSession res; Annotation qualifier = getServicesToQualifier().get(providerName); if (qualifier == null) { throw new AgoravaException("Cannot find configured service provider with name : " + providerName); } return buildSessionFor(qualifier); }
@Override public String startDanceFor(Class<? extends Annotation> providerClass) { Annotation qualifier = getClassToQualifierQualifier().get(providerClass); if (qualifier == null) { throw new AgoravaException("Cannot find configured service provider with class : " + providerClass); } return startDanceFor(qualifier); }
@Override public String getBodyContents() { try { return new String(getByteBodyContents(), getCharset()); } catch (UnsupportedEncodingException uee) { throw new AgoravaException("Unsupported Charset: " + charset, uee); } }
protected void renderResponse(HttpServletRequest req, HttpServletResponse resp) { String internalCallBack = (String) lifeCycleService.getCurrentSession().getExtraData().get(AgoravaConstants .INTERN_CALLBACK_PARAM); if (internalCallBack == null) internalCallBack = AgoravaContext.getInternalCallBack(); try { if (internalCallBack.startsWith("/")) internalCallBack = req.getContextPath() + internalCallBack; internalCallBack = resp.encodeRedirectURL(internalCallBack + "?" + req.getQueryString()); resp.sendRedirect(internalCallBack); } catch (IOException e) { throw new AgoravaException(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); } } }
@Override public HttpParametersImpl getQueryStringParams() { try { HttpParametersImpl result = new HttpParametersImpl(); String queryString = new URL(url).getQuery(); result.addQuerystring(queryString); result.addAll(querystringParams); return result; } catch (MalformedURLException mue) { throw new AgoravaException("Malformed URL : " + url, mue); } }
/** * Encode a String in OAuth format * * @param plain string to encode * @return encoded string */ public static String encode(String plain) { Preconditions.checkNotNull(plain, "Cannot encode null object"); String encoded = ""; try { encoded = URLEncoder.encode(plain, CHARSET); } catch (UnsupportedEncodingException uee) { throw new AgoravaException("Charset not found while encoding string: " + CHARSET, uee); } for (Map.Entry<String, String> rule : ENCODING_RULES.entrySet()) { encoded = applyRule(encoded, rule.getKey(), rule.getValue()); } return encoded; }
/** * {@inheritDoc} */ @Override 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); } } }