Skip to content

Commit afdcaa9

Browse files
committed
Polishing Javadoc
See gh-25821
1 parent 543333f commit afdcaa9

File tree

3 files changed

+94
-83
lines changed

3 files changed

+94
-83
lines changed

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,76 +38,62 @@
3838
import org.springframework.util.StringUtils;
3939

4040
/**
41-
* A base for classes providing strongly typed getters and setters as well as
42-
* behavior around specific categories of headers (e.g. STOMP headers).
43-
* Supports creating new headers, modifying existing headers (when still mutable),
44-
* or copying and modifying existing headers.
45-
*
46-
* <p>The method {@link #getMessageHeaders()} provides access to the underlying,
47-
* fully-prepared {@link MessageHeaders} that can then be used as-is (i.e.
48-
* without copying) to create a single message as follows:
41+
* Wrapper around {@link MessageHeaders} that provides extra features such as
42+
* strongly typed accessors for specific headers, the ability to leave headers
43+
* in a {@link Message} mutable, and the option to suppress automatic generation
44+
* of {@link MessageHeaders#ID id} and {@link MessageHeaders#TIMESTAMP
45+
* timesteamp} headers. Sub-classes such as {@link NativeMessageHeaderAccessor}
46+
* and others provide support for managing processing vs external source headers
47+
* as well as protocol specific headers.
4948
*
49+
* <p>Below is a workflow to initialize headers via {@code MessageHeaderAccessor},
50+
* or one of its sub-classes, then create a {@link Message}, and then re-obtain
51+
* the accessor possibly from a different component:
5052
* <pre class="code">
53+
* // Create a message with headers
5154
* MessageHeaderAccessor accessor = new MessageHeaderAccessor();
5255
* accessor.setHeader("foo", "bar");
53-
* Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders());
54-
* </pre>
56+
* MessageHeaders headers = accessor.getMessageHeaders();
57+
* Message message = MessageBuilder.createMessage("payload", headers);
5558
*
56-
* <p>After the above, by default the {@code MessageHeaderAccessor} becomes
57-
* immutable. However it is possible to leave it mutable for further initialization
58-
* in the same thread, for example:
59+
* // Later on
60+
* MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message);
61+
* Assert.notNull(accessor, "No MessageHeaderAccessor");
62+
* </pre>
5963
*
64+
* <p>In order for the above to work, all participating components must use
65+
* {@code MessageHeaders} to create, access, or modify headers, or otherwise
66+
* {@link MessageHeaderAccessor#getAccessor(Message, Class)} will return null.
67+
* Below is a workflow that shows how headers are created and left mutable,
68+
* then modified possibly by a different component, and finally made immutable
69+
* perhaps before the possibility of being accessed on a different thread:
6070
* <pre class="code">
71+
* // Create a message with mutable headers
6172
* MessageHeaderAccessor accessor = new MessageHeaderAccessor();
6273
* accessor.setHeader("foo", "bar");
6374
* accessor.setLeaveMutable(true);
64-
* Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders());
75+
* MessageHeaders headers = accessor.getMessageHeaders();
76+
* Message message = MessageBuilder.createMessage("payload", headers);
6577
*
66-
* // later on in the same thread...
78+
* // Later on
79+
* MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message);
80+
* if (accessor.isMutable()) {
81+
* // It's mutable, just change the headers
82+
* accessor.setHeader("bar", "baz");
83+
* }
84+
* else {
85+
* // It's not, so get a mutable copy, change and re-create
86+
* accessor = MessageHeaderAccessor.getMutableAccessor(message);
87+
* accessor.setHeader("bar", "baz");
88+
* accessor.setLeaveMutable(true); // leave mutable again or not?
89+
* message = MessageBuilder.createMessage(message.getPayload(), accessor);
90+
* }
6791
*
92+
* // Make the accessor immutable
6893
* MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message);
69-
* accessor.setHeader("bar", "baz");
7094
* accessor.setImmutable();
7195
* </pre>
7296
*
73-
* <p>The method {@link #toMap()} returns a copy of the underlying headers. It can
74-
* be used to prepare multiple messages from the same {@code MessageHeaderAccessor}
75-
* instance:
76-
* <pre class="code">
77-
* MessageHeaderAccessor accessor = new MessageHeaderAccessor();
78-
* MessageBuilder builder = MessageBuilder.withPayload("payload").setHeaders(accessor);
79-
*
80-
* accessor.setHeader("foo", "bar1");
81-
* Message message1 = builder.build();
82-
*
83-
* accessor.setHeader("foo", "bar2");
84-
* Message message2 = builder.build();
85-
*
86-
* accessor.setHeader("foo", "bar3");
87-
* Message message3 = builder.build();
88-
* </pre>
89-
*
90-
* <p>However note that with the above style, the header accessor is shared and
91-
* cannot be re-obtained later on. Alternatively it is also possible to create
92-
* one {@code MessageHeaderAccessor} per message:
93-
*
94-
* <pre class="code">
95-
* MessageHeaderAccessor accessor1 = new MessageHeaderAccessor();
96-
* accessor.set("foo", "bar1");
97-
* Message message1 = MessageBuilder.createMessage("payload", accessor1.getMessageHeaders());
98-
*
99-
* MessageHeaderAccessor accessor2 = new MessageHeaderAccessor();
100-
* accessor.set("foo", "bar2");
101-
* Message message2 = MessageBuilder.createMessage("payload", accessor2.getMessageHeaders());
102-
*
103-
* MessageHeaderAccessor accessor3 = new MessageHeaderAccessor();
104-
* accessor.set("foo", "bar3");
105-
* Message message3 = MessageBuilder.createMessage("payload", accessor3.getMessageHeaders());
106-
* </pre>
107-
*
108-
* <p>Note that the above examples aim to demonstrate the general idea of using
109-
* header accessors. The most likely usage however is through subclasses.
110-
*
11197
* @author Rossen Stoyanchev
11298
* @author Juergen Hoeller
11399
* @since 4.0
@@ -566,6 +552,21 @@ public String toString() {
566552

567553
// Static factory methods
568554

555+
/**
556+
* Return the original {@code MessageHeaderAccessor} used to create the headers
557+
* of the given {@code Message}, or {@code null} if that's not available or if
558+
* its type does not match the required type.
559+
* <p>This is for cases where the existence of an accessor is strongly expected
560+
* (followed up with an assertion) or where an accessor will be created otherwise.
561+
* @param message the message to get an accessor for
562+
* @return an accessor instance of the specified type, or {@code null} if none
563+
* @since 5.1.19
564+
*/
565+
@Nullable
566+
public static MessageHeaderAccessor getAccessor(Message<?> message) {
567+
return getAccessor(message.getHeaders(), null);
568+
}
569+
569570
/**
570571
* Return the original {@code MessageHeaderAccessor} used to create the headers
571572
* of the given {@code Message}, or {@code null} if that's not available or if
@@ -625,6 +626,11 @@ public static MessageHeaderAccessor getMutableAccessor(Message<?> message) {
625626
}
626627

627628

629+
/**
630+
* Extension of {@link MessageHeaders} that helps to preserve the link to
631+
* the outer {@link MessageHeaderAccessor} instance that created it as well
632+
* as keeps track of whether headers are still mutable.
633+
*/
628634
@SuppressWarnings("serial")
629635
private class MutableMessageHeaders extends MessageHeaders {
630636

spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,40 +30,34 @@
3030
import org.springframework.util.ObjectUtils;
3131

3232
/**
33-
* An extension of {@link MessageHeaderAccessor} that also stores and provides read/write
34-
* access to message headers from an external source -- e.g. a Spring {@link Message}
35-
* created to represent a STOMP message received from a STOMP client or message broker.
36-
* Native message headers are kept in a {@code Map<String, List<String>>} under the key
37-
* {@link #NATIVE_HEADERS}.
33+
* {@link MessageHeaderAccessor} sub-class that supports storage and access of
34+
* headers from an external source such as a message broker. Headers from the
35+
* external source are kept separate from other headers, in a sub-map under the
36+
* key {@link #NATIVE_HEADERS}. This allows separating processing headers from
37+
* headers that need to be sent to or received from the external source.
3838
*
39-
* <p>This class is not intended for direct use but is rather expected to be used
40-
* indirectly through protocol-specific sub-classes such as
41-
* {@link org.springframework.messaging.simp.stomp.StompHeaderAccessor StompHeaderAccessor}.
42-
* Such sub-classes may provide factory methods to translate message headers from
43-
* an external messaging source (e.g. STOMP) to Spring {@link Message} headers and
44-
* reversely to translate Spring {@link Message} headers to a message to send to an
45-
* external source.
39+
* <p>This class is likely to be used through indirectly through a protocol
40+
* specific sub-class that also provide factory methods to translate
41+
* message headers to an from an external messaging source.
4642
*
4743
* @author Rossen Stoyanchev
4844
* @since 4.0
4945
*/
5046
public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
5147

52-
/**
53-
* The header name used to store native headers.
54-
*/
48+
/** The header name used to store native headers. */
5549
public static final String NATIVE_HEADERS = "nativeHeaders";
5650

5751

5852
/**
59-
* A protected constructor to create new headers.
53+
* Protected constructor to create a new instance.
6054
*/
6155
protected NativeMessageHeaderAccessor() {
6256
this((Map<String, List<String>>) null);
6357
}
6458

6559
/**
66-
* A protected constructor to create new headers.
60+
* Protected constructor to create an instance with the given native headers.
6761
* @param nativeHeaders native headers to create the message with (may be {@code null})
6862
*/
6963
protected NativeMessageHeaderAccessor(@Nullable Map<String, List<String>> nativeHeaders) {
@@ -73,7 +67,7 @@ protected NativeMessageHeaderAccessor(@Nullable Map<String, List<String>> native
7367
}
7468

7569
/**
76-
* A protected constructor accepting the headers of an existing message to copy.
70+
* Protected constructor that copies headers from another message.
7771
*/
7872
protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
7973
super(message);
@@ -88,14 +82,18 @@ protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
8882
}
8983
}
9084

85+
86+
/**
87+
* Sub-classes can use this method to access the "native" headers sub-map.
88+
*/
9189
@SuppressWarnings("unchecked")
9290
@Nullable
9391
protected Map<String, List<String>> getNativeHeaders() {
9492
return (Map<String, List<String>>) getHeader(NATIVE_HEADERS);
9593
}
9694

9795
/**
98-
* Return a copy of the native header values or an empty map.
96+
* Return a copy of the native headers sub-map, or an empty map.
9997
*/
10098
public Map<String, List<String>> toNativeHeaderMap() {
10199
Map<String, List<String>> map = getNativeHeaders();
@@ -124,8 +122,7 @@ public boolean containsNativeHeader(String headerName) {
124122
}
125123

126124
/**
127-
* Return all values for the specified native header.
128-
* or {@code null} if none.
125+
* Return the values for the specified native header, if present.
129126
*/
130127
@Nullable
131128
public List<String> getNativeHeader(String headerName) {
@@ -134,8 +131,7 @@ public List<String> getNativeHeader(String headerName) {
134131
}
135132

136133
/**
137-
* Return the first value for the specified native header,
138-
* or {@code null} if none.
134+
* Return the first value for the specified native header, if present.
139135
*/
140136
@Nullable
141137
public String getFirstNativeHeader(String headerName) {
@@ -151,6 +147,8 @@ public String getFirstNativeHeader(String headerName) {
151147

152148
/**
153149
* Set the specified native header value replacing existing values.
150+
* <p>In order for this to work, the accessor must be {@link #isMutable()
151+
* mutable}. See {@link MessageHeaderAccessor} for details.
154152
*/
155153
public void setNativeHeader(String name, @Nullable String value) {
156154
Assert.state(isMutable(), "Already immutable");
@@ -176,6 +174,8 @@ public void setNativeHeader(String name, @Nullable String value) {
176174

177175
/**
178176
* Add the specified native header value to existing values.
177+
* <p>In order for this to work, the accessor must be {@link #isMutable()
178+
* mutable}. See {@link MessageHeaderAccessor} for details.
179179
*/
180180
public void addNativeHeader(String name, @Nullable String value) {
181181
Assert.state(isMutable(), "Already immutable");
@@ -199,6 +199,11 @@ public void addNativeHeaders(@Nullable MultiValueMap<String, String> headers) {
199199
headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value)));
200200
}
201201

202+
/**
203+
* Remove the specified native header value replacing existing values.
204+
* <p>In order for this to work, the accessor must be {@link #isMutable()
205+
* mutable}. See {@link MessageHeaderAccessor} for details.
206+
*/
202207
@Nullable
203208
public List<String> removeNativeHeader(String name) {
204209
Assert.state(isMutable(), "Already immutable");

spring-messaging/src/test/java/org/springframework/messaging/support/NativeMessageHeaderAccessorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,7 +148,7 @@ public void setNativeHeaderLazyInit() {
148148
NativeMessageHeaderAccessor headerAccessor = new NativeMessageHeaderAccessor();
149149
headerAccessor.setNativeHeader("foo", "baz");
150150

151-
assertEquals(Arrays.asList("baz"), headerAccessor.getNativeHeader("foo"));
151+
assertEquals(Collections.singletonList("baz"), headerAccessor.getNativeHeader("foo"));
152152
}
153153

154154
@Test
@@ -190,15 +190,15 @@ public void addNativeHeaderNullValue() {
190190
NativeMessageHeaderAccessor headers = new NativeMessageHeaderAccessor(nativeHeaders);
191191
headers.addNativeHeader("foo", null);
192192

193-
assertEquals(Arrays.asList("bar"), headers.getNativeHeader("foo"));
193+
assertEquals(Collections.singletonList("bar"), headers.getNativeHeader("foo"));
194194
}
195195

196196
@Test
197197
public void addNativeHeaderLazyInit() {
198198
NativeMessageHeaderAccessor headerAccessor = new NativeMessageHeaderAccessor();
199199
headerAccessor.addNativeHeader("foo", "bar");
200200

201-
assertEquals(Arrays.asList("bar"), headerAccessor.getNativeHeader("foo"));
201+
assertEquals(Collections.singletonList("bar"), headerAccessor.getNativeHeader("foo"));
202202
}
203203

204204
@Test

0 commit comments

Comments
 (0)