/** * For use in test. */ PatreonAPI(String accessToken, RequestUtil requestUtil) { this.accessToken = accessToken; this.requestUtil = requestUtil; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.converter = new ResourceConverter( objectMapper, User.class, Campaign.class, Pledge.class ); this.converter.enableDeserializationOption(DeserializationFeature.ALLOW_UNKNOWN_INCLUSIONS); }
@Override public JSONAPIDocument<T> convert(ResponseBody responseBody) throws IOException { if (isCollection) { return (JSONAPIDocument<T>) parser.readDocumentCollection(responseBody.byteStream(), clazz); } else { return (JSONAPIDocument<T>) parser.readDocument(responseBody.byteStream(), clazz); } } }
Field relationshipLinksField = configuration.getRelationshipLinksField(object.getClass(), field); if (relationshipLinksField != null) { Links links = new Links(mapLinks(relationship.get(LINKS))); relationshipLinksField.set(object, links); RelationshipResolver resolver = getResolver(type); if (linkNode != null && ((link = getLink(linkNode)) != null)) { if (isCollection(relationship)) { relationshipField.set(object, readDocumentCollection(new ByteArrayInputStream(resolver.resolve(link)), type).get()); } else { relationshipField.set(object, readDocument(new ByteArrayInputStream(resolver.resolve(link)), type).get()); if (isCollection(relationship)) { @SuppressWarnings("rawtypes") Collection elements = createCollectionInstance(relationshipField.getType()); Object relationshipObject = parseRelationship(element, type); if (relationshipObject != null) { elements.add(relationshipObject); } else { try { Object relationshipObject = parseRelationship(relationship.get(DATA), type); if (relationshipObject != null) { relationshipField.set(object, relationshipObject);
String resourceId = getIdValue(object); removeField(attributesNode, configuration.getIdField(object.getClass())); JsonNode meta = null; if (metaField != null) { meta = removeField(attributesNode, metaField); JsonNode jsonLinks = getResourceLinks(object, attributesNode, resourceId, settings); if (jsonLinks != null) { if (jsonLinks.has(SELF)) { removeField(attributesNode, relationshipField); JsonNode relationshipMeta = getRelationshipMeta(object, relationshipName, settings); if (relationshipMeta != null) { relationshipDataNode.set(META, relationshipMeta); removeField(attributesNode, refField); JsonNode relationshipLinks = getRelationshipLinks(object, relationship, selfHref, settings); removeField(attributesNode, refField); String idValue = getIdValue(element); if (shouldSerializeRelationship(relationshipName, settings) && idValue != null) { String identifier = idValue.concat(relationshipType); if (!includedContainer.containsKey(identifier) && !resourceCache.contains(identifier)) {
boolean cached = false; if (dataNode != null && dataNode.isObject()) { String identifier = createIdentifier(dataNode); cached = identifier != null && resourceCache.contains(identifier); resourceObject = (T) resourceCache.get(identifier); } else { resourceObject = readObject(dataNode, clazz, false); resourceCache.cache(parseIncluded(rootNode)); handleRelationships(dataNode, resourceObject); result.setMeta(mapMeta(rootNode.get(META))); result.setLinks(new Links(mapLinks(rootNode.get(LINKS))));
/** * Serializes provided {@link JSONAPIDocument} into JSON API Spec compatible byte representation. * @param documentCollection {@link JSONAPIDocument} document collection to serialize * @param serializationSettings {@link SerializationSettings} settings that override global serialization settings * @return serialized content in bytes * @throws DocumentSerializationException thrown in case serialization fails */ public byte [] writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection, SerializationSettings serializationSettings) throws DocumentSerializationException { try { resourceCache.init(); ArrayNode results = objectMapper.createArrayNode(); Map<String, ObjectNode> includedDataMap = new LinkedHashMap<>(); for (Object object : documentCollection.get()) { results.add(getDataNode(object, includedDataMap, serializationSettings)); } ObjectNode result = objectMapper.createObjectNode(); result.set(DATA, results); // Handle global links and meta serializeMeta(documentCollection, result, serializationSettings); serializeLinks(documentCollection, result, serializationSettings); result = addIncludedSection(result, includedDataMap); return objectMapper.writeValueAsBytes(result); } catch (Exception e) { throw new DocumentSerializationException(e); } finally { resourceCache.clear(); } }
String identifier = createIdentifier(source); Class<?> type = getActualType(source, clazz); Field linkField = configuration.getLinksField(type); if (linkField != null) { linkField.set(result, new Links(mapLinks(source.get(LINKS)))); setIdValue(result, source.get(ID)); handleRelationships(source, result);
/** * Reads JSON API spec document and converts it into collection of target type objects. * @param data {@link byte} raw data (server response) * @param clazz {@link Class} target type * @param <T> type * @return {@link JSONAPIDocument} */ public <T> JSONAPIDocument<List<T>> readDocumentCollection(byte[] data, Class<T> clazz) { return readDocumentCollection(new ByteArrayInputStream(data), clazz); } /**
/** * Reads JSON API spec document and converts it into target type. * @param data {@link byte} raw data (server response) * @param clazz {@link Class} target type * @param <T> type * @return {@link JSONAPIDocument} */ public <T> JSONAPIDocument<T> readDocument(byte[] data, Class<T> clazz) { return readDocument(new ByteArrayInputStream(data), clazz); }
removeField(serializedResource, linksField); if (shouldSerializeLinks(settings)) { Map<String, Link> linkMap = new HashMap<>(); linkMap.put(SELF, new Link(createURL(baseURL, type.path().replace("{id}", resourceId))));
Map<String, Object> includedResources = getIncludedResources(parent); Object resourceObject = includedResources.get(createIdentifier(node)); if (resourceObject != null){ handleRelationships(node, resourceObject);
/** * Parses out included resources excluding relationships. * @param parent root node * @return map of identifier/resource pairs * @throws IOException * @throws IllegalAccessException * @throws InstantiationException */ private Map<String, Object> getIncludedResources(JsonNode parent) throws IOException, IllegalAccessException, InstantiationException { Map<String, Object> result = new HashMap<>(); if (parent.has(INCLUDED)) { for (JsonNode jsonNode : parent.get(INCLUDED)) { String type = jsonNode.get(TYPE).asText(); Class<?> clazz = configuration.getTypeClass(type); if (clazz != null) { Object object = readObject(jsonNode, clazz, false); if (object != null) { result.put(createIdentifier(jsonNode), object); } } else if (!deserializationFeatures.contains(DeserializationFeature.ALLOW_UNKNOWN_INCLUSIONS)) { throw new IllegalArgumentException("Included section contains unknown resource type: " + type); } } } return result; }
/** * Creates new JSONAPIConverterFactory. * @param mapper {@link ObjectMapper} raw data mapper * @param classes classes to be handled by this factory instance */ public JSONAPIConverterFactory(ObjectMapper mapper, Class<?>... classes) { this.deserializer = new ResourceConverter(mapper, classes); this.serializer = this.deserializer; }
private JsonNode getRelationshipLinks(Object source, Relationship relationship, String ownerLink, SerializationSettings settings) throws IllegalAccessException { if (shouldSerializeLinks(settings)) { Links links = null; Field relationshipLinksField = configuration .getRelationshipLinksField(source.getClass(), relationship.value()); if (relationshipLinksField != null) { links = (Links) relationshipLinksField.get(source); } Map<String, Link> linkMap = new HashMap<>(); if (links != null) { linkMap.putAll(links.getLinks()); } if (!relationship.path().trim().isEmpty() && !linkMap.containsKey(SELF)) { linkMap.put(SELF, new Link(createURL(ownerLink, relationship.path()))); } if (!relationship.relatedPath().trim().isEmpty() && !linkMap.containsKey(RELATED)) { linkMap.put(RELATED, new Link(createURL(ownerLink, relationship.relatedPath()))); } if (!linkMap.isEmpty()) { return objectMapper.valueToTree(new Links(linkMap)).get(LINKS); } } return null; }
Field relationshipLinksField = configuration.getRelationshipLinksField(object.getClass(), field); if (relationshipLinksField != null) { Links links = new Links(mapLinks(relationship.get(LINKS))); relationshipLinksField.set(object, links); RelationshipResolver resolver = getResolver(type); if (linkNode != null && ((link = getLink(linkNode)) != null)) { if (isCollection(relationship)) { relationshipField.set(object, readDocumentCollection(new ByteArrayInputStream(resolver.resolve(link)), type).get()); } else { relationshipField.set(object, readDocument(new ByteArrayInputStream(resolver.resolve(link)), type).get()); if (isCollection(relationship)) { @SuppressWarnings("rawtypes") Collection elements = createCollectionInstance(relationshipField.getType()); Object relationshipObject = parseRelationship(element, type); if (relationshipObject != null) { elements.add(relationshipObject); } else { try { Object relationshipObject = parseRelationship(relationship.get(DATA), type); if (relationshipObject != null) { relationshipField.set(object, relationshipObject);
String resourceId = getIdValue(object); removeField(attributesNode, configuration.getIdField(object.getClass())); JsonNode meta = null; if (metaField != null) { meta = removeField(attributesNode, metaField); JsonNode jsonLinks = getResourceLinks(object, attributesNode, resourceId, settings); if (jsonLinks != null) { if (jsonLinks.has(SELF)) { removeField(attributesNode, relationshipField); JsonNode relationshipMeta = getRelationshipMeta(object, relationshipName, settings); if (relationshipMeta != null) { relationshipDataNode.set(META, relationshipMeta); removeField(attributesNode, refField); JsonNode relationshipLinks = getRelationshipLinks(object, relationship, selfHref, settings); removeField(attributesNode, refField); String idValue = getIdValue(element); if (shouldSerializeRelationship(relationshipName, settings) && idValue != null) { String identifier = idValue.concat(relationshipType); if (!includedContainer.containsKey(identifier) && !resourceCache.contains(identifier)) {
boolean cached = false; if (dataNode != null && dataNode.isObject()) { String identifier = createIdentifier(dataNode); cached = identifier != null && resourceCache.contains(identifier); resourceObject = (T) resourceCache.get(identifier); } else { resourceObject = readObject(dataNode, clazz, false); resourceCache.cache(parseIncluded(rootNode)); handleRelationships(dataNode, resourceObject); result.setMeta(mapMeta(rootNode.get(META))); result.setLinks(new Links(mapLinks(rootNode.get(LINKS))));
/** * Serializes provided {@link JSONAPIDocument} into JSON API Spec compatible byte representation. * @param documentCollection {@link JSONAPIDocument} document collection to serialize * @param serializationSettings {@link SerializationSettings} settings that override global serialization settings * @return serialized content in bytes * @throws DocumentSerializationException thrown in case serialization fails */ public byte [] writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection, SerializationSettings serializationSettings) throws DocumentSerializationException { try { resourceCache.init(); ArrayNode results = objectMapper.createArrayNode(); Map<String, ObjectNode> includedDataMap = new LinkedHashMap<>(); for (Object object : documentCollection.get()) { results.add(getDataNode(object, includedDataMap, serializationSettings)); } ObjectNode result = objectMapper.createObjectNode(); result.set(DATA, results); // Handle global links and meta serializeMeta(documentCollection, result, serializationSettings); serializeLinks(documentCollection, result, serializationSettings); result = addIncludedSection(result, includedDataMap); return objectMapper.writeValueAsBytes(result); } catch (Exception e) { throw new DocumentSerializationException(e); } finally { resourceCache.clear(); } }
String identifier = createIdentifier(source); Class<?> type = getActualType(source, clazz); Field linkField = configuration.getLinksField(type); if (linkField != null) { linkField.set(result, new Links(mapLinks(source.get(LINKS)))); setIdValue(result, source.get(ID)); handleRelationships(source, result);
/** * Reads JSON API spec document and converts it into collection of target type objects. * @param data {@link byte} raw data (server response) * @param clazz {@link Class} target type * @param <T> type * @return {@link JSONAPIDocument} */ public <T> JSONAPIDocument<List<T>> readDocumentCollection(byte[] data, Class<T> clazz) { return readDocumentCollection(new ByteArrayInputStream(data), clazz); } /**