com.github.fge.jsonpatch.diff.JsonDiff Java Examples

The following examples show how to use com.github.fge.jsonpatch.diff.JsonDiff. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SNSNotificationServiceImpl.java    From metacat with Apache License 2.0 4 votes vote down vote up
private UpdateOrRenameTableMessageBase createUpdateorRenameTableMessage(
    final String id,
    final long timestamp,
    final String requestId,
    final QualifiedName name,
    final TableDto oldTable,
    final TableDto currentTable,
    final String exceptionMessage,
    final String metricName,
    final SNSMessageType messageType
) {
    try {
        final JsonPatch patch = JsonDiff.asJsonPatch(
            this.mapper.valueToTree(oldTable),
            this.mapper.valueToTree(currentTable)
        );
        if (messageType == SNSMessageType.TABLE_UPDATE) {
            return new UpdateTableMessage(
                id,
                timestamp,
                requestId,
                name.toString(),
                new UpdatePayload<>(oldTable, patch)
            );
        } else {
            return new RenameTableMessage(
                id,
                timestamp,
                requestId,
                name.toString(),
                new UpdatePayload<>(oldTable, patch)
            );
        }
    } catch (final Exception e) {
        this.notificationMetric.handleException(
            name,
            exceptionMessage,
            metricName,
            null,
            e
        );
    }
    return null;
}
 
Example #2
Source File: AuditBuilder.java    From graviteeio-access-management with Apache License 2.0 4 votes vote down vote up
public Audit build(ObjectMapper mapper) {
    Audit audit = new Audit();
    audit.setId(id);
    audit.setTransactionId(transactionalId);
    audit.setReferenceType(referenceType);
    audit.setReferenceId(referenceId);
    audit.setType(type);
    audit.setTimestamp(timestamp);

    // The actor and/or target of an event is dependent on the action performed.
    // All events have actors but not all have targets.
    AuditEntity actor = new AuditEntity();
    actor.setId(actorId);
    actor.setType(actorType);
    actor.setAlternativeId(actorAlternativeId);
    actor.setDisplayName(actorDisplayName);
    actor.setReferenceType(actorReferenceType);
    actor.setReferenceId(actorReferenceId);
    audit.setActor(actor);

    // Network access point
    AuditAccessPoint accessPoint = new AuditAccessPoint();
    accessPoint.setId(accessPointId);
    accessPoint.setAlternativeId(accessPointAlternativeId);
    accessPoint.setDisplayName(accessPointName);
    accessPoint.setIpAddress(ipAddress);
    accessPoint.setUserAgent(userAgent);
    audit.setAccessPoint(accessPoint);

    // target
    if (targetId != null) {
        AuditEntity target = new AuditEntity();
        target.setId(targetId);
        target.setType(targetType);
        target.setAlternativeId(targetAlternativeId);
        target.setDisplayName(targetDisplayName);
        target.setReferenceType(targetReferenceType);
        target.setReferenceId(targetReferenceId);
        audit.setTarget(target);
    }

    // result
    AuditOutcome result = new AuditOutcome();
    if (throwable == null) {
        result.setStatus(Status.SUCCESS);

        // set details
        if (newValue != null || oldValue != null) {
            ContainerNode oldNode;
            ContainerNode newNode;
            if (EventType.USER_CONSENT_CONSENTED.equals(type) || EventType.USER_CONSENT_REVOKED.equals(type)) {
                oldNode = mapper.createArrayNode();
                newNode = mapper.createArrayNode();
                mapper.convertValue(newValue, ArrayNode.class).forEach(jsonNode -> {
                    ((ArrayNode) newNode).add(((ObjectNode) jsonNode).remove(Arrays.asList("updatedAt", "createdAt", "expiresAt", "userId", "domain")));
                });
            } else {
                oldNode = oldValue == null
                        ? mapper.createObjectNode()
                        : mapper.convertValue(oldValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt", "lastEvent"));
                newNode = newValue == null
                        ? mapper.createObjectNode()
                        : mapper.convertValue(newValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt", "lastEvent"));
            }
            clean(oldNode, null, null);
            clean(newNode, null, null);
            result.setMessage(JsonDiff.asJson(oldNode, newNode).toString());
        }
    } else {
        result.setStatus(Status.FAILURE);
        result.setMessage(throwable.getMessage());
    }
    audit.setOutcome(result);

    return audit;
}
 
Example #3
Source File: AuditServiceImpl.java    From gravitee-management-rest-api with Apache License 2.0 4 votes vote down vote up
@Async
protected void create(Audit.AuditReferenceType referenceType, String referenceId, Map<Audit.AuditProperties,String> properties,
                      Audit.AuditEvent event, Date createdAt,
                      Object oldValue, Object newValue) {

    Audit audit = new Audit();
    audit.setId(UUID.toString(UUID.random()));
    audit.setCreatedAt(createdAt);

    final UserDetails authenticatedUser = getAuthenticatedUser();
    final String user;
    if (authenticatedUser != null && "token".equals(authenticatedUser.getSource())) {
        user = userService.findById(authenticatedUser.getUsername()).getDisplayName() +
                " - (using token \"" + authenticatedUser.getSourceId() + "\")";
    } else {
        user = getAuthenticatedUsernameOrSystem();
    }
    audit.setUser(user);

    if (properties != null) {
        Map<String, String> stringStringMap = new HashMap<>(properties.size());
        properties.forEach((auditProperties, s) -> stringStringMap.put(auditProperties.name(), s));
        audit.setProperties(stringStringMap);
    }

    audit.setReferenceType(referenceType);
    audit.setReferenceId(referenceId);
    audit.setEvent(event.name());

    ObjectNode oldNode = oldValue == null
            ? mapper.createObjectNode()
            : mapper.convertValue(oldValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));
    ObjectNode newNode = newValue == null
            ? mapper.createObjectNode()
            : mapper.convertValue(newValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));

    audit.setPatch(JsonDiff.asJson(oldNode, newNode).toString());

    try {
        auditRepository.create(audit);
    } catch (TechnicalException e) {
        LOGGER.error("Error occurs during the creation of an Audit Log {}.", e);
    }
}