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

com.sap.cds.repackaged.audit.client.impl.AuditLogMessageFactoryImpl 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.AUDIT_SERVICE_URL_PREFIX;
import static com.sap.cds.repackaged.audit.client.impl.Utils.LOGGER_NAME;
import static com.sap.cds.repackaged.audit.client.impl.Utils.OAUTH2_PLAN;
import static com.sap.cds.repackaged.audit.client.impl.Utils.STANDARD_PLAN;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.cds.repackaged.audit.api.AuditLogMessageFactory;
import com.sap.cds.repackaged.audit.api.ConfigurationChangeAuditMessage;
import com.sap.cds.repackaged.audit.api.DataAccessAuditMessage;
import com.sap.cds.repackaged.audit.api.DataModificationAuditMessage;
import com.sap.cds.repackaged.audit.api.SecurityEventAuditMessage;
import com.sap.cds.repackaged.audit.api.exception.AuditLogException;
import com.sap.cds.repackaged.audit.client.impl.v2.AuditLogMessageFactoryImplBase;
import com.sap.xs.env.Credentials;
import com.sap.xs.env.VcapServices;


public class AuditLogMessageFactoryImpl extends AuditLogMessageFactoryImplBase implements AuditLogMessageFactory {

	private static final Logger LOGGER = LoggerFactory.getLogger(LOGGER_NAME);

	static final String AUDIT_SERVICE_URL_PATH_STANDARD_PLAN = AUDIT_SERVICE_URL_PREFIX + "v1/";
	static final String AUDIT_SERVICE_URL_PATH_OAUTH2_PLAN = AUDIT_SERVICE_URL_PREFIX + "oauth2/v1/";

	private Communicator communicator = null;

	public AuditLogMessageFactoryImpl() throws AuditLogException {
		this(null);
	}
	
	public AuditLogMessageFactoryImpl(VcapServices vcapServices) throws AuditLogException {
		Credentials credentials = getCredentials(vcapServices, null);
		if (credentials == null) {
			communicator = new ConsoleCommunicator();
		} else {
			String plan = getServicePlan(vcapServices, null);
			String urlFromCredentials = credentials.getUrl();
			String auditlogServiceUrl = plan.equals(OAUTH2_PLAN) //
					? urlFromCredentials + AUDIT_SERVICE_URL_PATH_OAUTH2_PLAN //
					: urlFromCredentials + AUDIT_SERVICE_URL_PATH_STANDARD_PLAN; //
			this.communicator = new HttpCommunicator(credentials, plan, auditlogServiceUrl);
		}
	}

	// constructor only for standard plan
	public AuditLogMessageFactoryImpl(String serviceUrl, String serviceUser, String servicePassword) {
		if (serviceUrl == null || serviceUser == null || servicePassword == null) {
			LOGGER.error("Missing audit log service credentials parameters. Cannot send audit log message to the backend.");
			communicator = new ConsoleCommunicator();
			return;
		}
		Credentials credentials = new Credentials();
		credentials.setUser(serviceUser);
		credentials.setPassword(servicePassword);
		credentials.setUrl(serviceUrl);
		
		this.communicator = new HttpCommunicator(credentials, STANDARD_PLAN, serviceUrl);
	}

	// constructor only for oauth2 plan
	public AuditLogMessageFactoryImpl(String serviceUrl, String xsuaaClientId, String xsuaaSecret, String xsuaaUrl) {
		if (serviceUrl == null || xsuaaClientId == null || xsuaaSecret == null || xsuaaUrl == null) {
			LOGGER.error("Missing audit log service credentials parameters. Cannot send audit log message to the backend.");
			communicator = new ConsoleCommunicator();
			return;
		}
		Credentials credentials = new Credentials();
		credentials.set("uaa", assembleUaaObject(xsuaaClientId, xsuaaSecret, xsuaaUrl));
		credentials.setUrl(serviceUrl);
		this.communicator = new HttpCommunicator(credentials, OAUTH2_PLAN, serviceUrl); 
	}

    public AuditLogMessageFactoryImpl(String serviceUrl, String xsuaaClientId, String xsuaaCertUrl, byte[] certificate, byte[] key) {
        if (serviceUrl == null || xsuaaClientId == null || certificate == null || key == null || xsuaaCertUrl == null) {
            LOGGER.error("Missing audit log service credentials parameters. Cannot send audit log message to the backend.");
            communicator = new ConsoleCommunicator();
            return;
        }
        Credentials credentials = new Credentials();
        credentials.set("uaa", assembleUaaObject(xsuaaClientId, xsuaaCertUrl, certificate, key));
        credentials.setUrl(serviceUrl);
        this.communicator = new HttpCommunicator(credentials, OAUTH2_PLAN, serviceUrl);
    }

	String getServiceUrl() {
		return communicator.getServiceUrl();
	}

	Communicator getCommunicator() {
		return communicator;
	}

	void setCommunicator(Communicator httpCommunicator) {
		this.communicator = httpCommunicator;
	}

	@Override
	public DataAccessAuditMessage createDataAccessAuditMessage() {
		return new DataAccessMessageImpl(this);
	}

	@Override
	public DataModificationAuditMessage createDataModificationAuditMessage() {
		return new DataModificationMessageImpl(this);
	}
	
	@Override
	public ConfigurationChangeAuditMessage createConfigurationChangeAuditMessage() {
		return new ConfigurationChangeMessageImpl(this);
	}

	@Override
	public SecurityEventAuditMessage createSecurityEventAuditMessage() {
		return new SecurityEventMessageImpl(this);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy