Skip to content

Commit ce4b28c

Browse files
committed
Reformatted code in alignment with the new CAP Java code formatting rules
1 parent 9db3804 commit ce4b28c

File tree

14 files changed

+976
-904
lines changed

14 files changed

+976
-904
lines changed
Lines changed: 128 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,141 @@
11
package com.sap.cds.feature.messaging.aem.client;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.sap.cds.feature.messaging.aem.client.binding.AemEndpointView;
5+
import com.sap.cds.services.ServiceException;
6+
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
7+
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
38
import java.io.IOException;
49
import java.net.URLEncoder;
510
import java.nio.charset.StandardCharsets;
611
import java.util.Arrays;
712
import java.util.HashMap;
813
import java.util.Map;
9-
1014
import org.slf4j.Logger;
1115
import org.slf4j.LoggerFactory;
1216

13-
import com.fasterxml.jackson.databind.JsonNode;
14-
import com.sap.cds.feature.messaging.aem.client.binding.AemEndpointView;
15-
import com.sap.cds.services.ServiceException;
16-
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
17-
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
18-
1917
public class AemManagementClient extends RestClient {
20-
private static final Logger logger = LoggerFactory.getLogger(AemManagementClient.class);
21-
22-
private static final String API_BASE = "/SEMP/v2/config/msgVpns/%s";
23-
private static final String API_QUEUE = API_BASE + "/queues";
24-
private static final String API_QUEUE_NAME = API_BASE + "/queues/%s";
25-
private static final String API_QUEUE_NAME_SUBSCRIPTION = API_BASE + "/queues/%s/subscriptions";
26-
27-
private static final String ATTR_EGRESS_ENABLED = "egressEnabled";
28-
private static final String ATTR_INGRESS_ENABLED = "ingressEnabled";
29-
public static final String ATTR_DEAD_MSG_QUEUE = "deadMsgQueue";
30-
private static final String ATTR_PERMISSION = "permission";
31-
private static final String ATTR_QUEUE_NAME = "queueName";
32-
private static final String ATTR_SUBSCRIPTION_TOPIC = "subscriptionTopic";
33-
private static final String VAL_CONSUME = "consume";
34-
35-
private final AemEndpointView endpointView;
36-
private final String vpn;
37-
38-
public AemManagementClient(ServiceBinding binding) {
39-
super(ServiceBindingDestinationOptions.forService(binding).build());
40-
this.endpointView = new AemEndpointView(binding);
41-
this.vpn = getVpn();
42-
}
43-
44-
public String getEndpoint() {
45-
return this.endpointView.getUri().orElseThrow(() -> new ServiceException("Management endpoint not available in binding"));
46-
}
47-
48-
public void removeQueue(String queue) throws IOException {
49-
logger.debug("Removing queue {}", queue);
50-
51-
deleteRequest(uri(API_QUEUE_NAME, this.vpn, queue));
52-
53-
logger.debug("Successfully removed queue {}", queue);
54-
}
55-
56-
public JsonNode getQueue(String name) throws IOException {
57-
try {
58-
logger.debug("Retrieving information for queue {}", name);
59-
60-
JsonNode result = getRequest(uri(API_QUEUE_NAME, this.vpn, name));
61-
62-
logger.debug("Successfully retrieved information for queue {}: {}", name, result.asText());
63-
64-
return result;
65-
} catch (Exception e) {
66-
logger.error("Failed to retrieve information for queue {}", name, e);
67-
return null;
68-
}
69-
}
70-
71-
public void createQueue(String name, Map<String, Object> properties) throws IOException {
72-
// We have to read the queue first to check if it exists; only create it if it doesn't
73-
logger.debug("Checking if queue {} exists", name);
74-
JsonNode queue = getQueue(name);
75-
76-
if (queue == null) {
77-
logger.debug("Queue {} does not exist, creating it", name);
78-
79-
Map<String, Object> attributes = new HashMap<>(properties);
80-
attributes.put(ATTR_QUEUE_NAME, name);
81-
attributes.put(ATTR_PERMISSION, VAL_CONSUME);
82-
attributes.put(ATTR_INGRESS_ENABLED, true);
83-
attributes.put(ATTR_EGRESS_ENABLED, true);
84-
85-
postRequest(uri(API_QUEUE, this.vpn), attributes);
86-
}
87-
}
88-
89-
public JsonNode getQueueSubscription(String queue) throws IOException {
90-
logger.debug("Retrieving information for queue subscription {}", queue);
91-
JsonNode result = getRequest(uri(API_QUEUE_NAME_SUBSCRIPTION, this.vpn, queue));
92-
93-
return result;
94-
}
95-
96-
public void createQueueSubscription(String queue, String topic) throws IOException {
97-
logger.debug("Checking if queue {} is subscribed to topic {}", queue, topic);
98-
99-
if (!isTopicSubscribed(getQueueSubscription(queue), queue, topic)) {
100-
logger.debug("Queue {} is not subscribed to topic {}, subscribing it", queue, topic);
101-
102-
Map<String, Object> attributes = Map.of(ATTR_SUBSCRIPTION_TOPIC, topic);
103-
postRequest(uri(API_QUEUE_NAME_SUBSCRIPTION, this.vpn, queue), attributes);
104-
}
105-
}
106-
107-
private String uri(String path, Object... args) {
108-
return String.format(
109-
path,
110-
(Object[]) Arrays.stream(args).map(Object::toString).map(this::urlEncode).toArray(String[]::new));
111-
}
112-
113-
private String urlEncode(String value) {
114-
return URLEncoder.encode(value, StandardCharsets.UTF_8);
115-
}
116-
117-
public boolean isTopicSubscribed(JsonNode jsonNode, String queueName, String topic) {
118-
String rawTopic = topic.replace("topic://", "");
119-
if (jsonNode.has("data")) {
120-
for (JsonNode dataNode : jsonNode.get("data")) {
121-
if (dataNode.has("msgVpnName") && dataNode.has("queueName") && dataNode.has("subscriptionTopic")) {
122-
String nodeMsgVpnName = dataNode.get("msgVpnName").asText();
123-
String nodeQueueName = dataNode.get("queueName").asText();
124-
String nodeSubscriptionTopic = dataNode.get("subscriptionTopic").asText();
125-
126-
if (this.getVpn().equals(nodeMsgVpnName) && queueName.equals(nodeQueueName) && rawTopic.equals(nodeSubscriptionTopic)) {
127-
return true;
128-
}
129-
}
130-
}
131-
}
132-
return false;
133-
}
134-
135-
private String getVpn() {
136-
return this.endpointView.getVpn().get();
137-
}
138-
18+
public static final String ATTR_DEAD_MSG_QUEUE = "deadMsgQueue";
19+
private static final Logger logger = LoggerFactory.getLogger(AemManagementClient.class);
20+
private static final String API_BASE = "/SEMP/v2/config/msgVpns/%s";
21+
private static final String API_QUEUE = API_BASE + "/queues";
22+
private static final String API_QUEUE_NAME = API_BASE + "/queues/%s";
23+
private static final String API_QUEUE_NAME_SUBSCRIPTION = API_BASE + "/queues/%s/subscriptions";
24+
private static final String ATTR_EGRESS_ENABLED = "egressEnabled";
25+
private static final String ATTR_INGRESS_ENABLED = "ingressEnabled";
26+
private static final String ATTR_PERMISSION = "permission";
27+
private static final String ATTR_QUEUE_NAME = "queueName";
28+
private static final String ATTR_SUBSCRIPTION_TOPIC = "subscriptionTopic";
29+
private static final String VAL_CONSUME = "consume";
30+
31+
private final AemEndpointView endpointView;
32+
private final String vpn;
33+
34+
public AemManagementClient(ServiceBinding binding) {
35+
super(ServiceBindingDestinationOptions.forService(binding).build());
36+
this.endpointView = new AemEndpointView(binding);
37+
this.vpn = getVpn();
38+
}
39+
40+
public String getEndpoint() {
41+
return this.endpointView
42+
.getUri()
43+
.orElseThrow(() -> new ServiceException("Management endpoint not available in binding"));
44+
}
45+
46+
public void removeQueue(String queue) throws IOException {
47+
logger.debug("Removing queue {}", queue);
48+
49+
deleteRequest(uri(API_QUEUE_NAME, this.vpn, queue));
50+
51+
logger.debug("Successfully removed queue {}", queue);
52+
}
53+
54+
public JsonNode getQueue(String name) throws IOException {
55+
try {
56+
logger.debug("Retrieving information for queue {}", name);
57+
58+
JsonNode result = getRequest(uri(API_QUEUE_NAME, this.vpn, name));
59+
60+
logger.debug("Successfully retrieved information for queue {}: {}", name, result.asText());
61+
62+
return result;
63+
} catch (Exception e) {
64+
logger.error("Failed to retrieve information for queue {}", name, e);
65+
return null;
66+
}
67+
}
68+
69+
public void createQueue(String name, Map<String, Object> properties) throws IOException {
70+
// We have to read the queue first to check if it exists; only create it if it doesn't
71+
logger.debug("Checking if queue {} exists", name);
72+
JsonNode queue = getQueue(name);
73+
74+
if (queue == null) {
75+
logger.debug("Queue {} does not exist, creating it", name);
76+
77+
Map<String, Object> attributes = new HashMap<>(properties);
78+
attributes.put(ATTR_QUEUE_NAME, name);
79+
attributes.put(ATTR_PERMISSION, VAL_CONSUME);
80+
attributes.put(ATTR_INGRESS_ENABLED, true);
81+
attributes.put(ATTR_EGRESS_ENABLED, true);
82+
83+
postRequest(uri(API_QUEUE, this.vpn), attributes);
84+
}
85+
}
86+
87+
public JsonNode getQueueSubscription(String queue) throws IOException {
88+
logger.debug("Retrieving information for queue subscription {}", queue);
89+
JsonNode result = getRequest(uri(API_QUEUE_NAME_SUBSCRIPTION, this.vpn, queue));
90+
91+
return result;
92+
}
93+
94+
public void createQueueSubscription(String queue, String topic) throws IOException {
95+
logger.debug("Checking if queue {} is subscribed to topic {}", queue, topic);
96+
97+
if (!isTopicSubscribed(getQueueSubscription(queue), queue, topic)) {
98+
logger.debug("Queue {} is not subscribed to topic {}, subscribing it", queue, topic);
99+
100+
Map<String, Object> attributes = Map.of(ATTR_SUBSCRIPTION_TOPIC, topic);
101+
postRequest(uri(API_QUEUE_NAME_SUBSCRIPTION, this.vpn, queue), attributes);
102+
}
103+
}
104+
105+
private String uri(String path, Object... args) {
106+
return String.format(
107+
path,
108+
(Object[])
109+
Arrays.stream(args).map(Object::toString).map(this::urlEncode).toArray(String[]::new));
110+
}
111+
112+
private String urlEncode(String value) {
113+
return URLEncoder.encode(value, StandardCharsets.UTF_8);
114+
}
115+
116+
public boolean isTopicSubscribed(JsonNode jsonNode, String queueName, String topic) {
117+
String rawTopic = topic.replace("topic://", "");
118+
if (jsonNode.has("data")) {
119+
for (JsonNode dataNode : jsonNode.get("data")) {
120+
if (dataNode.has("msgVpnName")
121+
&& dataNode.has("queueName")
122+
&& dataNode.has("subscriptionTopic")) {
123+
String nodeMsgVpnName = dataNode.get("msgVpnName").asText();
124+
String nodeQueueName = dataNode.get("queueName").asText();
125+
String nodeSubscriptionTopic = dataNode.get("subscriptionTopic").asText();
126+
127+
if (this.getVpn().equals(nodeMsgVpnName)
128+
&& queueName.equals(nodeQueueName)
129+
&& rawTopic.equals(nodeSubscriptionTopic)) {
130+
return true;
131+
}
132+
}
133+
}
134+
}
135+
return false;
136+
}
137+
138+
private String getVpn() {
139+
return this.endpointView.getVpn().get();
140+
}
139141
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.sap.cds.feature.messaging.aem.client;
22

3+
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
4+
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
35
import java.io.IOException;
46
import java.net.URI;
57
import java.net.URISyntaxException;
68
import java.util.Map;
79

8-
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
9-
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
10-
1110
public class AemValidationClient extends RestClient {
1211

13-
public AemValidationClient(ServiceBinding binding) {
14-
super(ServiceBindingDestinationOptions.forService(binding).build());
15-
}
12+
public AemValidationClient(ServiceBinding binding) {
13+
super(ServiceBindingDestinationOptions.forService(binding).build());
14+
}
1615

17-
public void validate(String managementUri) throws IOException, URISyntaxException {
18-
URI uri = new URI(managementUri);
19-
String payload = this.mapper.writeValueAsString(Map.of("hostName", uri.getHost()));
16+
public void validate(String managementUri) throws IOException, URISyntaxException {
17+
URI uri = new URI(managementUri);
18+
String payload = this.mapper.writeValueAsString(Map.of("hostName", uri.getHost()));
2019

21-
// The response is not used, only the status code is relevant. If there is a status code not equal to 200,
22-
// an exception is thrown which means that the validation failed.
23-
postRequest("", payload, Map.of());
24-
}
20+
// The response is not used, only the status code is relevant. If there is a status code not
21+
// equal to 200,
22+
// an exception is thrown which means that the validation failed.
23+
postRequest("", payload, Map.of());
24+
}
2525
}

0 commit comments

Comments
 (0)