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); } }
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); } }
@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); } }
/** * {@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); } } }
byte[] getByteBodyContents() { if (bytePayload != null) return bytePayload; String body = (payload != null) ? payload : bodyParams.asFormUrlEncodedString(); try { return body.getBytes(getCharset()); } catch (UnsupportedEncodingException uee) { throw new AgoravaException("Unsupported Charset: " + getCharset(), uee); } }
@Override public <T> T mapToObject(Response resp, Class<T> clazz) throws ResponseException { String msg = ""; if (resp.getCode() != 200) { throw new ResponseException(resp); } try { msg = resp.getBody(); if (clazz.equals(String.class)) return (T) msg; return objectMapper.readValue(msg, clazz); } catch (IOException e) { throw new AgoravaException("Unable to map Json response for this response " + msg, e); } }
private void CommonsProcessOAuthTier(ProcessBean<? extends ProviderConfigOauth> pb) { Annotated annotated = pb.getAnnotated(); Set<Annotation> qualifiers = getAnnotationsWithMeta(annotated, ProviderRelated.class); if (qualifiers.size() != 1) throw new AgoravaException("A RemoteService bean should have one and only one service related Qualifier : " + pb .getAnnotated().toString()); Class<? extends ProviderConfigOauth> clazz = (Class<? extends ProviderConfigOauth>) pb.getBean().getBeanClass(); try { providerQualifiers2Version.put(getSingleProviderRelatedQualifier(qualifiers, true), clazz.newInstance().getOAuthVersion()); } catch (Exception e) { throw new AgoravaException("Error while retrieving version of OAuth in tier config", e); } }
@Override public UserSessionRepository createNew(String id) { if (get(id) != null) { throw new AgoravaException("Unable to create new user repository : repository with id " + id + " already exists"); } else { UserSessionRepository res = new UserSessionRepositoryImpl(id); add(res); setCurrent(res); return res; } }