All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.repackaged.audit.client.impl.TransactionalLogImpl Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy