forked from Stromner/sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmqpConstants.java
More file actions
127 lines (110 loc) · 4.37 KB
/
Copy pathAmqpConstants.java
File metadata and controls
127 lines (110 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2018-Present The CloudEvents Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.cloudevents.amqp.impl;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.messaging.Data;
import org.apache.qpid.proton.amqp.messaging.Section;
import io.cloudevents.core.message.impl.MessageUtils;
/**
* Constants and methods used throughout the Proton-based implementation of the AMQP 1.0 protocol
* binding for cloud events.
*/
public final class AmqpConstants {
private AmqpConstants() {
// prevent instantiation
}
/**
* The prefix name for CloudEvent attributes for use in the <em>application-properties</em> section
* of an AMQP 1.0 message.
*/
public static final String CE_PREFIX = "cloudEvents:";
/**
* The AMQP 1.0 <em>content-type</em> message property
*/
public static final String PROPERTY_CONTENT_TYPE = "content-type";
/**
* Map a cloud event attribute name to a value. All values except the <em>datacontenttype</em> attribute are prefixed
* with "cloudEvents:" as mandated by the spec.
*/
public static final Map<String, String> ATTRIBUTES_TO_PROPERTYNAMES = MessageUtils.generateAttributesToHeadersMapping(CEA -> {
if (CEA.equals("datacontenttype")) {
return PROPERTY_CONTENT_TYPE;
}
// prefix the attribute
return CE_PREFIX + CEA;
});
public static final String APP_PROPERTY_SPEC_VERSION = ATTRIBUTES_TO_PROPERTYNAMES.get("specversion");
/**
* Gets the value of a specific <em>application property</em>.
*
* @param <T> The expected type of the property to retrieve.
* @param props The application properties to retrieve the value from.
* @param name The name of the application property.
* @param type The expected value type.
* @return The value or {@code null} if the properties do not contain a value of the expected type for the given
* name.
*/
@SuppressWarnings("unchecked")
public static <T> T getApplicationProperty(final ApplicationProperties props, final String name,
final Class<T> type) {
if (props == null) {
return null;
} else {
final Object value = props.getValue().get(name);
if (type.isInstance(value)) {
return (T) value;
} else {
return null;
}
}
}
/**
* Parses a message payload into a byte array.
* <p>
* The bytes in the array are determined as follows:
* <ul>
* <li>If the body is a Data section, the bytes contained in the
* Data section are returned.</li>
* <li>If the body is an AmqpValue section and contains a byte array,
* the bytes in the array are returned.</li>
* <li>If the body is an AmqpValue section and contains a non-empty String,
* the UTF-8 encoding of the String is returned.</li>
* <li>In all other cases, {@code null} is returned.</li>
* </ul>
* @param payload The message payload to extract the bytes from.
* @return The payload bytes or {@code null} if the above stated conditions are not met.
*/
public static byte[] getPayloadAsByteArray(final Section payload) {
if (payload == null) {
return null;
}
if (payload instanceof Data body) {
return body.getValue().getArray();
} else if (payload instanceof AmqpValue body) {
if (body.getValue() instanceof byte[]) {
return (byte[]) body.getValue();
} else if (body.getValue() instanceof String &&
((String) body.getValue()).length() > 0 ) {
return ((String) body.getValue()).getBytes(StandardCharsets.UTF_8);
}
}
return null;
}
}