/** * Generates a collection meta token representing this collection. * * @return String. */ public String buildCollectionMetaToken() { Map<String, Object> claims = data.asMap(); boolean isNetworkIssued = isNetworkIssued(); claims.put("iss", isNetworkIssued ? site.getNetwork().getUrn() : site.getUrn()); return LivefyreUtil.serializeAndSign( claims, isNetworkIssued ? site.getNetwork().getData().getKey() : site.getData().getKey()); }
private String getPayload() { Map<String, Object> payload = ImmutableMap.<String, Object>of( "articleId", data.getArticleId(), "checksum", buildChecksum(), "collectionMeta", buildCollectionMetaToken()); return LivefyreUtil.mapToJsonString(payload); }
/** * Generates a MD5-encrypted checksum based on this collection's attributes. * * @return String. */ public String buildChecksum() { try { Map<String, Object> attr = data.asMap(); byte[] digest = MessageDigest.getInstance("MD5").digest(LivefyreUtil.mapToJsonString(attr).getBytes()); return printHexBinary(digest); } catch (NoSuchAlgorithmException e) { throw new LivefyreException("MD5 message digest missing. This shouldn't ever happen." + e); } }
/** * Informs Livefyre to either create or update a collection based on the attributes of this * Collection. Makes an external API call. Returns this. * * @return Collection */ public Collection createOrUpdate() { ClientResponse response = invokeCollectionApi("create"); if (response.getStatus() == 200) { data.setId( LivefyreUtil.stringToJson(response.getEntity(String.class)) .getAsJsonObject("data") .get("collectionId") .getAsString()); return this; } else if (response.getStatus() == 409) { response = invokeCollectionApi("update"); if (response.getStatus() == 200) { data.setId( LivefyreUtil.stringToJson(response.getEntity(String.class)) .getAsJsonObject("data") .get("collectionId") .getAsString()); return this; } } throw new ApiException(response.getStatus()); }