/** * Returns a JSON object containing only the specified fields from the provided resource. If the * list of fields is empty then the resource is returned unchanged. * * <p><b>NOTE:</b> this method only performs a shallow copy of extracted fields, so changes to the * filtered resource may impact the original resource, and vice-versa. * * @param resource The resource whose fields are to be filtered. * @param fields The list of fields to be extracted. * @return The filtered resource. */ public static Resource filterResource( final Resource resource, final Collection<JsonPointer> fields) { final JsonValue unfiltered = resource.getContent(); final JsonValue filtered = filterResource(unfiltered, fields); if (filtered == unfiltered) { return resource; // Unchanged. } else { return new Resource(resource.getId(), resource.getRevision(), filtered); } }
/** * TODO: Description. * * @throws SynchronizationException TODO. */ void create() throws SynchronizationException { _id = UUID.randomUUID().toString(); // client-assigned identifier JsonValue jv = toJsonValue(); try { CreateRequest r = Requests.newCreateRequest(linkId(null), _id, jv); Resource resource = mapping .getService() .getConnectionFactory() .getConnection() .create(mapping.getService().getServerContext(), r); this._id = resource.getId(); this._rev = resource.getRevision(); this.initialized = true; } catch (ResourceException ose) { LOGGER.debug("Failed to create link", ose); throw new SynchronizationException(ose); } }
/** * Update a link. * * @throws SynchronizationException if updating link fails */ void update() throws SynchronizationException { if (_id == null) { throw new SynchronizationException("Attempt to update non-existent link"); } JsonValue jv = toJsonValue(); try { UpdateRequest r = Requests.newUpdateRequest(linkId(_id), jv); r.setRevision(_rev); Resource resource = mapping .getService() .getConnectionFactory() .getConnection() .update(mapping.getService().getServerContext(), r); _rev = resource.getRevision(); } catch (ResourceException ose) { LOGGER.warn("Failed to update link", ose); throw new SynchronizationException(ose); } }