Skip to content

Commit a7b851c

Browse files
committed
Make sure message annotation keys start with x-
1 parent 5164007 commit a7b851c

File tree

4 files changed

+31
-54
lines changed

4 files changed

+31
-54
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/AmqpConsumer.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public void discard(Map<String, Object> annotations) {
393393
if (settled.compareAndSet(false, true)) {
394394
try {
395395
annotations = annotations == null ? Collections.emptyMap() : annotations;
396-
checkAnnotations(annotations);
396+
Utils.checkMessageAnnotations(annotations);
397397
protonExecutor.execute(replenishCreditOperation);
398398
delivery.disposition(DeliveryState.modified(true, true, annotations), true);
399399
unsettledMessageCount.decrementAndGet();
@@ -427,7 +427,7 @@ public void requeue(Map<String, Object> annotations) {
427427
if (settled.compareAndSet(false, true)) {
428428
try {
429429
annotations = annotations == null ? Collections.emptyMap() : annotations;
430-
checkAnnotations(annotations);
430+
Utils.checkMessageAnnotations(annotations);
431431
protonExecutor.execute(replenishCreditOperation);
432432
delivery.disposition(DeliveryState.modified(false, false, annotations), true);
433433
unsettledMessageCount.decrementAndGet();
@@ -440,14 +440,4 @@ public void requeue(Map<String, Object> annotations) {
440440
}
441441
}
442442
}
443-
444-
static void checkAnnotations(Map<String, Object> annotations) {
445-
annotations.forEach(
446-
(k, v) -> {
447-
if (!k.startsWith("x-opt-")) {
448-
throw new IllegalArgumentException(
449-
"Message annotation keys must start with 'x-opt-': " + k);
450-
}
451-
});
452-
}
453443
}

src/main/java/com/rabbitmq/client/amqp/impl/Utils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.time.Duration;
2626
import java.util.Arrays;
2727
import java.util.Base64;
28+
import java.util.Map;
2829
import java.util.UUID;
2930
import java.util.concurrent.*;
3031
import java.util.concurrent.atomic.AtomicLong;
@@ -98,6 +99,16 @@ private static boolean isJava21OrMore() {
9899
return Runtime.version().compareTo(Runtime.Version.parse("21")) >= 0;
99100
}
100101

102+
static void checkMessageAnnotations(Map<String, Object> annotations) {
103+
annotations.forEach(
104+
(k, v) -> {
105+
if (!k.startsWith("x-")) {
106+
throw new IllegalArgumentException(
107+
"Message annotation keys must start with 'x-opt-': " + k);
108+
}
109+
});
110+
}
111+
101112
private static class NamedThreadFactory implements ThreadFactory {
102113

103114
private final ThreadFactory backingThreadFactory;

src/test/java/com/rabbitmq/client/amqp/impl/AmqpConsumerTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/test/java/com/rabbitmq/client/amqp/impl/UtilsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
1818
package com.rabbitmq.client.amqp.impl;
1919

20+
import static com.rabbitmq.client.amqp.impl.Utils.checkMessageAnnotations;
21+
import static java.util.Map.of;
2022
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2124

2225
import org.junit.jupiter.api.Test;
2326
import org.junit.jupiter.params.ParameterizedTest;
@@ -44,4 +47,19 @@ void validateMaxAgeOK(String input) {
4447
void validateMaxAgeKO(String input) {
4548
assertThat(Utils.validateMaxAge(input)).isFalse();
4649
}
50+
51+
@Test
52+
void checkAnnotationsOK() {
53+
checkMessageAnnotations(of());
54+
checkMessageAnnotations(of("x-foo", "bar"));
55+
checkMessageAnnotations(of("x-foo-1", "bar1", "x-foo-2", "bar2"));
56+
}
57+
58+
@Test
59+
void checkAnnotationsKO() {
60+
assertThatThrownBy(() -> checkMessageAnnotations(of("foo", "bar")))
61+
.isInstanceOf(IllegalArgumentException.class);
62+
assertThatThrownBy(() -> checkMessageAnnotations(of("x-foo", "bar1", "foo", "bar2")))
63+
.isInstanceOf(IllegalArgumentException.class);
64+
}
4765
}

0 commit comments

Comments
 (0)