com.sap.cds.repackaged.audit.client.impl.TransactionalLogImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-feature-auditlog-v2 Show documentation
Show all versions of cds-feature-auditlog-v2 Show documentation
Handler to send auditlog messages to AuditLog Service V2
package com.sap.cds.repackaged.audit.client.impl;
import static com.sap.cds.repackaged.audit.client.impl.Utils.USER_VALUE;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.UUID;
import com.sap.cds.repackaged.audit.api.TransactionalAuditLogMessage;
import com.sap.cds.repackaged.audit.api.exception.AuditLogNotAvailableException;
import com.sap.cds.repackaged.audit.api.exception.AuditLogWriteException;
import com.sap.xs.audit.message.ConfigurationChange;
import com.sap.xs.audit.message.DataModification;
import com.sap.xs.audit.message.TransactionalStatusMessage;
import com.sap.xs.audit.message.ValidationError;
import com.sap.xs.audit.message.utils.JsonParserUtils;
public class TransactionalLogImpl extends AuditLogMessageImpl implements TransactionalAuditLogMessage {
public TransactionalLogImpl(AuditLogMessageFactoryImpl factory) {
super(factory);
}
public TransactionalLogImpl(Communicator communicator) {
super(communicator);
}
@Override
public void logPrepare() throws AuditLogNotAvailableException, AuditLogWriteException {
if (alreadyLogged) {
LOGGER.warn(ALREADY_LOGGED_WARNING);
return;
}
if (message.getUser() == null || USER_VALUE.equals(message.getUser())) {
String user = Utils.getUser();
message.setUser(user != null ? user : communicator.getClientId());
}
if(eventTime == null) {
eventTime = Instant.now();
}
message.setTime(eventTime);
message.setStatus("BEGIN");
try {
message.validate();
} catch (ValidationError e) {
LOGGER.error("Audit log message cannot be validated.");
throw new AuditLogWriteException("Audit log message cannot be validated.", message.getErrors());
}
try {
String resp = communicator.send(serializeMessage(), endpoint, message.getSubscriberTokenIssuer());
AuditLogIdentifier auditLogIdentifier = JsonParserUtils.desrializeMessage(resp, AuditLogIdentifier.class);
message.setId(auditLogIdentifier.getId());
alreadyPrepared = true;
} catch (IOException e) {
throw new AuditLogWriteException("Audit server returned invalid response.", e);
}
}
@Override
public void logSuccess() throws AuditLogNotAvailableException, AuditLogWriteException {
logStatus(true);
}
@Override
public void logFailure() throws AuditLogNotAvailableException, AuditLogWriteException {
logStatus(false);
}
private void logStatus(boolean success) throws AuditLogNotAvailableException, AuditLogWriteException {
if (alreadyLogged) {
LOGGER.warn(ALREADY_LOGGED_WARNING);
return;
}
// generate a unique GUID for the success/non-success message that to be different than the prepare message guid
message.setUuid(UUID.randomUUID().toString());
if (message.getUser() == null || USER_VALUE.equals(message.getUser())) {
String user = Utils.getUser();
message.setUser(user != null ? user : communicator.getClientId());
}
if(eventTime == null) {
eventTime = Instant.now();
}
message.setTime(eventTime);
try {
message.validate();
} catch (ValidationError e) {
throw new AuditLogWriteException("Audit log message cannot be validated.", message.getErrors());
}
message.setSuccess(success);
message.setStatus("END");
if (communicator.getServiceUrl().endsWith("/audit-log/v1/") || communicator.getServiceUrl().endsWith("/audit-log/oauth2/v1/")) {
if (message.getId() == null) {
communicator.send(serializeMessage(), endpoint, message.getSubscriberTokenIssuer());
} else {
TransactionalStatusMessage transStatusMsg = new TransactionalStatusMessage();
try {
String id = URLEncoder.encode(message.getId(), StandardCharsets.ISO_8859_1.toString());
transStatusMsg.setSuccess(success);
transStatusMsg.setTenant(message.getTenant());
transStatusMsg.setUuid(message.getUuid());
transStatusMsg.validate();
String endpoint = "";
if (message instanceof ConfigurationChange) {
endpoint = "configuration-changes/";
} else if (message instanceof DataModification) {
endpoint = "data-modifications/";
}
communicator.send(serializeMessage(transStatusMsg), communicator.getServiceUrl() + endpoint + id, message.getSubscriberTokenIssuer());
} catch (UnsupportedEncodingException e) {
LOGGER.error("The Character Encoding is not supported.");
} catch (ValidationError e) {
throw new AuditLogWriteException("Audit log status message cannot be validated.", transStatusMsg.getErrors());
}
}
} else { // "/audit-log/v2/" or "/audit-log/oauth2/v2/"
communicator.send(serializeMessage(), endpoint, message.getSubscriberTokenIssuer());
}
alreadyLogged = true;
}
}