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

com.sap.cds.feature.mt.SmsClient Maven / Gradle / Ivy

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

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.sap.cloud.environment.servicebinding.api.ServiceIdentifier;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultOAuth2PropertySupplier;
import com.sap.cloud.sdk.cloudplatform.connectivity.OAuth2ServiceBindingDestinationLoader;
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.base.Objects;
import com.sap.cds.integration.cloudsdk.rest.client.JsonRestClient;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cds.services.utils.environment.ServiceBindingUtils;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.sdk.cloudplatform.connectivity.OnBehalfOf;

/**
 * Implementation of the SMS REST client.
 */
public class SmsClient extends JsonRestClient {

	private static final Logger logger = LoggerFactory.getLogger(SmsClient.class);

	private static final int PAGE_SIZE = 500;

	private static final String SMS = "subscription-manager";

	public static final String BINDING_URL_KEY = "subscription_manager_url";

	public static final String SUBSCRIPTIONS_PATH = "/subscription-manager/v1/subscriptions";

	public static final String SUBSCRIPTION_STATE = "subscriptionState";

	public static Optional findBinding(CdsRuntime runtime) {
		return runtime.getEnvironment().getServiceBindings().filter(b -> ServiceBindingUtils.matches(b, null, SMS)).findFirst();
	}

	static {
		OAuth2ServiceBindingDestinationLoader.registerPropertySupplier(
				ServiceIdentifier.of(SMS),
				SmsClient.OAuth2Supplier::new);
	}

	private static class OAuth2Supplier extends DefaultOAuth2PropertySupplier {

		public OAuth2Supplier(ServiceBindingDestinationOptions options) {
			super(options, Collections.emptyList());
		}

		@Override
		public URI getServiceUri() {
			return getCredentialOrThrow(URI.class, BINDING_URL_KEY);
		}

	}

	public SmsClient(ServiceBinding binding) {
		super(ServiceBindingDestinationOptions.forService(binding).onBehalfOf(OnBehalfOf.TECHNICAL_USER_PROVIDER).build());
	}

	/**
	 * Retrieves the tenant info of all subscribed tenants
	 *
	 * @return tenants info
	 *
	 * @throws IOException throws when any connection problems occur
	 */
	public List> getSubscribedTenants() throws IOException {
		List> result = new ArrayList<>();
		boolean morePages = true;
		int skip = 0;

		while (morePages) {
			logger.debug("Retrieving page {} of all tenants metadata from the Subscription Manager Service", (skip / PAGE_SIZE) + 1);

			String paginatedUrlPath = paginatedUrl(SUBSCRIPTIONS_PATH, skip);
			JsonNode jsonResponse = getRequest(paginatedUrlPath);
			ArrayNode resp = getNode(jsonResponse, "subscriptions");
			BooleanNode morePagesResp = getNode(jsonResponse, "morePages");

			resp.forEach(tenant -> {
				String state = tenant.get(SUBSCRIPTION_STATE) != null ? tenant.get(SUBSCRIPTION_STATE).asText() : null;
				if (Objects.equal(state, "SUBSCRIBED") || Objects.equal(state, "UPDATE_FAILED")) {
					result.add(mapper.convertValue(tenant, new TypeReference<>() {
					}));
				}
			});

			morePages = morePagesResp != null && morePagesResp.booleanValue();
			skip = skip + PAGE_SIZE;

			logger.debug("More tenants metadata from the Subscription Manager Service available: {}", morePages ? "yes" : "no");
		}

		return result;
	}

	private static String paginatedUrl(String urlPath, int skip) {
		String url = urlPath + (urlPath.contains("?") ? "&" : "?");
		url = url + "skip=" + skip + "&top=" + PAGE_SIZE;
		return url;
	}

	@SuppressWarnings("unchecked")
	private static  N getNode(JsonNode jsonResponse, String keyName) {
		return (N) jsonResponse.get(keyName);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy