com.sap.cds.feature.messaging.mq.client.MessageQueuingManagementClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-feature-message-queuing Show documentation
Show all versions of cds-feature-message-queuing Show documentation
Message Queuing feature for CDS Services Java
The newest version!
/**************************************************************************
* (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.feature.messaging.mq.client;
import java.io.IOException;
import java.net.URI;
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.ObjectNode;
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.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.OnBehalfOf;
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
/**
* Implementation of the enterprise messaging management API.
*/
public class MessageQueuingManagementClient extends JsonRestClient {
private static final String API_BASE = "/v1/management";
private static final String API_QUEUE = API_BASE + "/queues/%s";
private static final String API_SUBSCRIBE = API_BASE + "/queues/%s/subscriptions/topics/%s";
static {
OAuth2ServiceBindingDestinationLoader.registerPropertySupplier(
ServiceIdentifier.of("message-queuing"),
OAuth2Supplier::new);
}
private static class OAuth2Supplier extends DefaultOAuth2PropertySupplier {
public OAuth2Supplier(ServiceBindingDestinationOptions options) {
super(options);
}
@Override
public URI getServiceUri() {
return getCredentialOrThrow(URI.class, "management", "url");
}
}
public MessageQueuingManagementClient(ServiceBinding binding) {
super(ServiceBindingDestinationOptions.forService(binding).onBehalfOf(OnBehalfOf.TECHNICAL_USER_PROVIDER).build());
}
public void removeQueue(String name) throws IOException {
String encName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
deleteRequest(API_QUEUE.formatted(encName));
}
public JsonNode getQueue(String name) throws IOException {
try {
String encName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
return getRequest(API_QUEUE.formatted(encName));
} catch (JsonRestClientResponseException e) {
if (e.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
throw e;
}
}
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_QUEUE.formatted(encName), data);
}
public void createQueueSubscription(String queue, String topic) throws IOException {
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);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy