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

com.sap.cds.services.mt.impl.MtTenantProviderSaasHandler Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/**************************************************************************
 * (C) 2019-2021 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.services.mt.impl;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

import com.sap.cds.Struct;
import com.sap.cds.feature.mt.SaasClient;
import com.sap.cds.services.environment.ServiceBinding;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.HandlerOrder;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.mt.ReadProviderTenantEventContext;
import com.sap.cds.services.mt.ReadTenantsEventContext;
import com.sap.cds.services.mt.TenantInfo;
import com.sap.cds.services.mt.TenantProviderService;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cds.services.utils.OrderConstants;

@ServiceName(value = TenantProviderService.DEFAULT_NAME, type = TenantProviderService.class)
public class MtTenantProviderSaasHandler implements EventHandler {

	private static final String KEY_TENANT_ID = "consumerTenantId";
	private static final String KEY_PROVIDER_TENANT_ID = "tenantid";

	private final SaasClient saasClient;
	private final String providerTenant;

	public MtTenantProviderSaasHandler(ServiceBinding binding) {
		this.saasClient = new SaasClient(binding);
		this.providerTenant = (String) binding.getCredentials().get(KEY_PROVIDER_TENANT_ID);
	}

	@On
	@HandlerOrder(OrderConstants.On.FEATURE)
	public void readTenants(ReadTenantsEventContext context) {
		try {
			context.setResult(saasClient.getSubscribedTenants().stream().map(this::toTenantInfo).collect(Collectors.toList()));
		} catch (IOException e) {
			throw new ErrorStatusException(CdsErrorStatuses.TENANT_READ_FAILED, e);
		}
	}

	@On
	@HandlerOrder(OrderConstants.On.FEATURE)
	public void readProviderTenant(ReadProviderTenantEventContext context) {
		if (providerTenant != null) {
			context.setResult(providerTenant);
		}
	}

	private TenantInfo toTenantInfo(Map tenant) {
		TenantInfo info = Struct.access(tenant).as(TenantInfo.class);
		info.setTenant(info.get(KEY_TENANT_ID).toString());
		return info;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy