
com.sap.cds.feature.messaging.em.client.EnterpriseMessagingManagementClient Maven / Gradle / Ivy
/**************************************************************************
* (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.feature.messaging.em.client;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.http.HttpStatus;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sap.cds.feature.messaging.em.client.EnterpriseMessagingOAuth2PropertySupplier.EnterpriseMessagingOptions;
import com.sap.cds.integration.cloudsdk.rest.client.JsonRestClient;
import com.sap.cds.integration.cloudsdk.rest.client.JsonRestClientResponseException;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.sdk.cloudplatform.connectivity.OnBehalfOf;
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
/**
* Implementation of the enterprise messaging management API.
*/
public class EnterpriseMessagingManagementClient extends JsonRestClient {
private static final String API_BASE = "/hub/rest/api/v1/management/messaging";
private static final String API_QUEUES = API_BASE + "/queues";
private static final String API_CREATE_QUEUE = API_QUEUES + "/%s";
private static final String API_SUBSCRIPTIONS = API_QUEUES + "/%s/subscriptions";
private static final String API_SUBSCRIBE = API_SUBSCRIPTIONS + "/%s";
private static final String API_READINESS = API_BASE + "/readinessCheck";
static {
EnterpriseMessagingOAuth2PropertySupplier.initialize();
}
public EnterpriseMessagingManagementClient(ServiceBinding binding) {
super(ServiceBindingDestinationOptions.forService(binding)
.onBehalfOf(OnBehalfOf.TECHNICAL_USER_CURRENT_TENANT)
.withOption(EnterpriseMessagingOptions.MANAGEMENT_API)
.build());
}
public void removeQueue(String name) throws IOException {
String encName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
deleteRequest(API_CREATE_QUEUE.formatted(encName));
}
public void createQueue(String name, Map properties) throws IOException {
ObjectNode data = mapper.convertValue(properties, ObjectNode.class);
String encName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
putRequest(API_CREATE_QUEUE.formatted(encName), data);
}
/**
* Returns the queues created on the broker client.
*
* @return created queues
* @throws IOException in case an HTTP error occurs
*/
public ArrayNode getQueues() throws IOException {
return (ArrayNode) getRequest(API_QUEUES);
}
/**
* Returns the queue corresponding the specified name.
*
* @param queue full qualified name
* @return queue data
* @throws IOException in case an HTTP error occurs
*/
public JsonNode getQueue(String queue) throws IOException {
try {
String encName = URLEncoder.encode(queue, StandardCharsets.UTF_8.toString());
return getRequest(API_CREATE_QUEUE.formatted(encName));
} catch (JsonRestClientResponseException e) {
if (e.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
throw e;
}
}
public void createQueueSubscription(String queue, String topic) throws IOException {
JsonNode data = mapper.createObjectNode();
((ObjectNode)data).put("maxQueueSizeInBytes", "0");
String encQueue = URLEncoder.encode(queue, StandardCharsets.UTF_8.toString());
String encTopic = URLEncoder.encode(topic, StandardCharsets.UTF_8.toString());
putRequest(API_SUBSCRIBE.formatted(encQueue, encTopic), null);
}
/**
* Returns the queue subscriptions.
*
* @param queue full qualified queue name
* @return queue subscriptions
*
* @throws IOException in case an HTTP error occurs
*/
public ArrayNode getQueueSubscriptions(String queue) throws IOException {
try {
String encName = URLEncoder.encode(queue, StandardCharsets.UTF_8.toString());
return (ArrayNode) getRequest(API_SUBSCRIPTIONS.formatted(encName));
} catch (JsonRestClientResponseException e) {
if (e.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
return mapper.createArrayNode();
}
throw e;
}
}
/**
* Returns true
if the tenant service instance is ready to use and false
otherwise.
* @return true
if the tenant service instance is ready
*
* @throws IOException throws if any problems occurs with the connection or invalid token authorization
*/
public boolean checkTenantInstanceReadiness() throws IOException {
int resp = getRequestWithOnlyResponseCode(API_READINESS);
switch (resp) {
case 200: return true;
case 503: return false;
default : throw new IOException("The tenant readiness check failed with status code " + resp + "");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy