@JsonMapper.JsonMappingCompleted void convertData(JsonMapper mapper) { if (data != null) { metaData = mapper.toJavaObject(data, MetaData.class); } }
private List<MessageTag> createTags(String rawTags, JsonMapper jsonMapper) { try { return jsonMapper.toJavaList(rawTags, MessageTag.class); } catch (FacebookJsonMappingException je) { } // message tags not in Graph API 2.5 format, ignore this exception and try another way try { List<MessageTag> resultList = new ArrayList<>(); JsonObject rawMessageTagsObject = jsonMapper.toJavaObject(rawTags, JsonObject.class); for (String key : rawMessageTagsObject.names()) { String tagArrayString = rawMessageTagsObject.get(key).toString(); resultList.addAll(jsonMapper.toJavaList(tagArrayString, MessageTag.class)); } return resultList; } catch (FacebookJsonMappingException je) { return new ArrayList<>(); } }
/** * @see com.restfb.FacebookClient#executeBatch(java.util.List, java.util.List) */ @Override public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments) { verifyParameterPresence("binaryAttachments", binaryAttachments); if (batchRequests == null || batchRequests.isEmpty()) { throw new IllegalArgumentException("You must specify at least one batch request."); } return jsonMapper.toJavaList( makeRequest("", true, false, binaryAttachments, Parameter.with("batch", jsonMapper.toJson(batchRequests, true))), BatchResponse.class); }
/** * @see com.restfb.LegacyFacebookClient#executeForList(String, * String, Class, com.restfb.Parameter[]) */ @Override public <T> List<T> executeForList(String method, String sessionKey, Class<T> resultType, Parameter... parameters) { return jsonMapper.toJavaList(makeRequest(method, sessionKey, parameters), resultType); }
/** * Creates a new parameter with the given {@code name} and {@code value}. * * @param name The parameter name. * @param value The parameter value. * @param jsonMapper Mapper for converting the parameter value to JSON. * @throws IllegalArgumentException If {@code name} is {@code null} or a blank string or either * {@code value} or {@code jsonMapper} is {@code null}. */ private Parameter(String name, Object value, JsonMapper jsonMapper) { if (isBlank(name) || value == null) throw new IllegalArgumentException(Parameter.class + " instances must have a non-blank name and non-null value."); if (jsonMapper == null) throw new IllegalArgumentException("Provided " + JsonMapper.class + " must not be null."); this.name = trimToEmpty(name).toLowerCase(); // Special handling for Date types - turn them into Facebook date strings. // Otherwise, use the JSON value of the type. this.value = value instanceof Date ? new SimpleDateFormat(FACEBOOK_LONG_DATE_FORMAT).format(value) : jsonMapper .toJson(value); }
for (String key : names) { if ("datetime".equals(key)) { List<NlpDatetime> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpDatetime.class); convertedEntities.addAll(list); } else if ("bye".equals(key)) { List<NlpBye> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpBye.class); convertedEntities.addAll(list); } else if ("reminder".equals(key)) { List<NlpReminder> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpReminder.class); convertedEntities.addAll(list); } else if ("greetings".equals(key)) { List<NlpGreetings> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpGreetings.class); convertedEntities.addAll(list); } else if ("amount_of_money".equals(key)) { List<NlpAmountOfMoney> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpAmountOfMoney.class); convertedEntities.addAll(list); } else if ("phone_number".equals(key)) { List<NlpPhoneNumber> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpPhoneNumber.class); convertedEntities.addAll(list); } else if ("email".equals(key)) { List<NlpEmail> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpEmail.class); convertedEntities.addAll(list); } else if ("distance".equals(key)) { List<NlpDistance> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpDistance.class); convertedEntities.addAll(list); } else if ("volume".equals(key)) { List<NlpVolume> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpVolume.class); convertedEntities.addAll(list); } else if ("temperature".equals(key)) {
/** * Creates a new parameter with the given {@code name} and {@code value}. * * @param name * The parameter name. * @param value * The parameter value. * @param jsonMapper * Mapper for converting the parameter value to JSON. * @throws IllegalArgumentException * If {@code name} is {@code null} or a blank string or either {@code value} or {@code jsonMapper} is * {@code null}. */ private Parameter(String name, Object value, JsonMapper jsonMapper) { if (isBlank(name) || value == null) { throw new IllegalArgumentException(Parameter.class + " instances must have a non-blank name and non-null value." + " Got instead name:" + name + ", value:" + value); } if (jsonMapper == null) { throw new IllegalArgumentException("Provided " + JsonMapper.class + " must not be null."); } this.name = trimToEmpty(name); // Use the JSON value of the type. this.value = jsonMapper.toJson(value, true); }
/** * @see com.restfb.LegacyFacebookClient#execute(String, * String, Class, com.restfb.Parameter[]) */ @Override public <T> T execute(String method, String sessionKey, Class<T> resultType, Parameter... parameters) { return jsonMapper.toJavaObject(makeRequest(method, sessionKey, parameters), resultType); }
/** * Post-JSON-mapping operation that populates the {@code messageTags} field "by hand". * * @param jsonMapper * The {@code JsonMapper} that was used to map to this type. */ @JsonMappingCompleted protected void jsonMappingCompleted(JsonMapper jsonMapper) { if (rawNameTags == null) { return; } try { nameTags = jsonMapper.toJavaList(rawNameTags, EntityAtTextRange.class); return; } catch (FacebookJsonMappingException je) { } // message tags not in Graph API 2.5 format, ignore this exception and try another way try { JsonObject rawMessageTagsObject = jsonMapper.toJavaObject(rawNameTags, JsonObject.class); for (String key : rawMessageTagsObject.names()) { String tagArrayString = rawMessageTagsObject.get(key).toString(); nameTags.addAll(jsonMapper.toJavaList(tagArrayString, EntityAtTextRange.class)); } } catch (FacebookJsonMappingException je) { } // cannot parse message tags, but don't break the flow here }
/** * @see com.restfb.FacebookClient#executeQuery(String, * Class, com.restfb.Parameter[]) */ @Override public <T> List<T> executeQuery(String query, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("query", query); verifyParameterPresence("objectType", objectType); for (Parameter parameter : parameters) if (QUERY_PARAM_NAME.equals(parameter.name)) throw new IllegalArgumentException("You cannot specify the '" + QUERY_PARAM_NAME + "' URL parameter yourself - " + "RestFB will populate this for you with " + "the query you passed to this method."); return jsonMapper.toJavaList( makeRequest("fql.query", true, false, false, null, parametersWithAdditionalParameter(Parameter.with(QUERY_PARAM_NAME, query), parameters)), objectType); }
/** * @see com.restfb.FacebookClient#publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment, * com.restfb.Parameter[]) */ @Override public <T> T publish(String connection, Class<T> objectType, List<BinaryAttachment> binaryAttachments, Parameter... parameters) { verifyParameterPresence("connection", connection); return jsonMapper.toJavaObject(makeRequest(connection, true, false, binaryAttachments, parameters), objectType); }
/** * Post-JSON-mapping operation that populates the {@code messageTags} field "by hand". * * @param jsonMapper * The {@code JsonMapper} that was used to map to this type. */ @JsonMappingCompleted protected void jsonMappingCompleted(JsonMapper jsonMapper) { if (rawMessageTags == null) { return; } try { messageTags = jsonMapper.toJavaList(rawMessageTags, MessageTag.class); return; } catch (FacebookJsonMappingException je) { } // message tags not in Graph API 2.5 format, ignore this exception and try another way try { JsonObject rawMessageTagsObject = jsonMapper.toJavaObject(rawMessageTags, JsonObject.class); for (String key : rawMessageTagsObject.names()) { String tagArrayString = rawMessageTagsObject.get(key).toString(); messageTags.addAll(jsonMapper.toJavaList(tagArrayString, MessageTag.class)); } } catch (FacebookJsonMappingException je) { } // cannot parse message tags, but don't break the flow here }
/** * @see com.restfb.FacebookClient#convertSessionKeysToAccessTokens(java.lang.String, java.lang.String, * java.lang.String[]) */ @Override public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) { verifyParameterPresence("appId", appId); verifyParameterPresence("secretKey", secretKey); if (sessionKeys == null || sessionKeys.length == 0) { return emptyList(); } String json = makeRequest("/oauth/exchange_sessions", true, false, null, Parameter.with("client_id", appId), Parameter.with("client_secret", secretKey), Parameter.with("sessions", join(sessionKeys))); return jsonMapper.toJavaList(json, AccessToken.class); }
/** * @see com.restfb.FacebookClient#publish(String, Class, * java.io.InputStream, com.restfb.Parameter[]) */ @Override public <T> T publish(String connection, Class<T> objectType, InputStream binaryAttachment, Parameter... parameters) { verifyParameterPresence("connection", connection); return jsonMapper.toJavaObject(makeRequest(connection, false, true, false, binaryAttachment, parameters), objectType); }
/** * @see com.restfb.FacebookClient#convertSessionKeysToAccessTokens(String, * String, String[]) */ @Override public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) { verifyParameterPresence("appId", appId); verifyParameterPresence("secretKey", secretKey); if (sessionKeys == null || sessionKeys.length == 0) return emptyList(); String json = makeRequest("/oauth/exchange_sessions", false, true, false, null, Parameter.with("client_id", appId), Parameter.with("client_secret", secretKey), Parameter.with("sessions", join(sessionKeys))); return jsonMapper.toJavaList(json, AccessToken.class); }
@Override public void handleCallback(String payload, String signature) { if (callbackHandler == null) { if (logger.isLoggable(FINE)) { logger.fine("Webhook received but no webhook handler configured"); } } else { if (logger.isLoggable(FINE)) { logger.fine(format("Handling webhook for payload: %s, signature: %s", payload, signature)); } if (appSecret != null) { if (!verifySignature(payload, signature)) { if (logger.isLoggable(FINE)) { logger.fine("Invalid signature received; webhook handler not invoked"); } return; } } WebhookObject webhookObject = facebookClient.getJsonMapper().toJavaObject(payload, WebhookObject.class); if (!OBJECT_PAGE_VALUE.equals(webhookObject.getObject())) { if (logger.isLoggable(FINE)) { logger.fine(format("Ignoring webhook object: %s; webhook handler not invoked", webhookObject.getObject())); } return; } callbackHandler.onCallback(this, webhookObject); } }
@JsonMappingCompleted protected void convertLabels(JsonMapper jsonMapper) { JsonObject rawLabels = null; if (this.rawLabels != null) { JsonValue jsonValue = Json.parse(this.rawLabels); if (jsonValue.isObject()) { rawLabels = jsonValue.asObject(); } } if (rawLabels != null && rawLabels.isObject()) { String innerLabelsString = rawLabels.get("data").toString(); labels = jsonMapper.toJavaList(innerLabelsString, PageLabel.class); } }
private AccessToken getAccessTokenFromResponse(String response) { try { return getJsonMapper().toJavaObject(response, AccessToken.class); } catch (FacebookJsonMappingException fjme) { CLIENT_LOGGER.trace("could not map response to access token class try to fetch directly from String", fjme); return AccessToken.fromQueryString(response); } }
/** * @see com.restfb.FacebookClient#fetchObject(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("object", object); verifyParameterPresence("objectType", objectType); return jsonMapper.toJavaObject(makeRequest(object, parameters), objectType); }
/** * @see com.restfb.FacebookClient#fetchObject(String, * Class, com.restfb.Parameter[]) */ @Override public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) { verifyParameterPresence("object", object); verifyParameterPresence("objectType", objectType); return jsonMapper.toJavaObject(makeRequest(object, parameters), objectType); }