Skip to content

Commit 16dd68a

Browse files
committed
Review nullability of spring-ws-support
See gh-1562
1 parent e3e9cdb commit 16dd68a

24 files changed

+221
-130
lines changed

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/JmsMessageReceiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jakarta.jms.Message;
2121
import jakarta.jms.Session;
2222
import jakarta.jms.TextMessage;
23+
import org.jspecify.annotations.Nullable;
2324

2425
import org.springframework.jms.core.MessagePostProcessor;
2526
import org.springframework.ws.transport.WebServiceMessageReceiver;
@@ -44,7 +45,7 @@ public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSup
4445

4546
private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
4647

47-
private MessagePostProcessor postProcessor;
48+
private @Nullable MessagePostProcessor postProcessor;
4849

4950
/**
5051
* Sets the encoding used to read from and write to {@link TextMessage} messages.

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/JmsMessageSender.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import jakarta.jms.Queue;
2828
import jakarta.jms.Session;
2929
import jakarta.jms.Topic;
30+
import org.jspecify.annotations.Nullable;
3031

3132
import org.springframework.jms.connection.ConnectionFactoryUtils;
3233
import org.springframework.jms.core.MessagePostProcessor;
3334
import org.springframework.jms.support.JmsUtils;
3435
import org.springframework.jms.support.destination.JmsDestinationAccessor;
36+
import org.springframework.util.Assert;
3537
import org.springframework.util.StringUtils;
3638
import org.springframework.ws.transport.WebServiceConnection;
3739
import org.springframework.ws.transport.WebServiceMessageSender;
@@ -117,7 +119,7 @@ public class JmsMessageSender extends JmsDestinationAccessor implements WebServi
117119

118120
private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
119121

120-
private MessagePostProcessor postProcessor;
122+
private @Nullable MessagePostProcessor postProcessor;
121123

122124
/**
123125
* Create a new {@code JmsMessageSender}
@@ -165,15 +167,17 @@ public void setPostProcessor(MessagePostProcessor postProcessor) {
165167

166168
@Override
167169
public WebServiceConnection createConnection(URI uri) throws IOException {
170+
ConnectionFactory connectionFactory = getConnectionFactory();
171+
Assert.notNull(connectionFactory, "ConnectionFactory is required");
168172
Connection jmsConnection = null;
169173
Session jmsSession = null;
170174
try {
171175
jmsConnection = createConnection();
172176
jmsSession = createSession(jmsConnection);
173177
Destination requestDestination = resolveRequestDestination(jmsSession, uri);
174178
Message requestMessage = createRequestMessage(jmsSession, uri);
175-
JmsSenderConnection wsConnection = new JmsSenderConnection(getConnectionFactory(), jmsConnection,
176-
jmsSession, requestDestination, requestMessage);
179+
JmsSenderConnection wsConnection = new JmsSenderConnection(connectionFactory, jmsConnection, jmsSession,
180+
requestDestination, requestMessage);
177181
wsConnection.setDeliveryMode(JmsTransportUtils.getDeliveryMode(uri));
178182
wsConnection.setPriority(JmsTransportUtils.getPriority(uri));
179183
wsConnection.setReceiveTimeout(this.receiveTimeout);
@@ -197,10 +201,12 @@ public boolean supports(URI uri) {
197201
}
198202

199203
private Destination resolveRequestDestination(Session session, URI uri) throws JMSException {
200-
return resolveDestinationName(session, JmsTransportUtils.getDestinationName(uri));
204+
String destinationName = JmsTransportUtils.getDestinationName(uri);
205+
Assert.notNull(destinationName, "No destination name found for URI [" + uri + "]");
206+
return resolveDestinationName(session, destinationName);
201207
}
202208

203-
private Destination resolveResponseDestination(Session session, URI uri) throws JMSException {
209+
private @Nullable Destination resolveResponseDestination(Session session, URI uri) throws JMSException {
204210
String destinationName = JmsTransportUtils.getReplyToName(uri);
205211
return StringUtils.hasLength(destinationName) ? resolveDestinationName(session, destinationName) : null;
206212
}

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import jakarta.jms.MessageProducer;
3030
import jakarta.jms.Session;
3131
import jakarta.jms.TextMessage;
32+
import org.jspecify.annotations.Nullable;
3233

3334
import org.springframework.jms.core.MessagePostProcessor;
3435
import org.springframework.jms.support.JmsUtils;
@@ -57,11 +58,11 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
5758

5859
private final Session session;
5960

60-
private Message responseMessage;
61+
private @Nullable Message responseMessage;
6162

62-
private String textMessageEncoding;
63+
private @Nullable String textMessageEncoding;
6364

64-
private MessagePostProcessor postProcessor;
65+
private @Nullable MessagePostProcessor postProcessor;
6566

6667
private JmsReceiverConnection(Message requestMessage, Session session) {
6768
Assert.notNull(requestMessage, "requestMessage must not be null");
@@ -89,7 +90,7 @@ protected JmsReceiverConnection(TextMessage requestMessage, String encoding, Ses
8990
this.textMessageEncoding = encoding;
9091
}
9192

92-
void setPostProcessor(MessagePostProcessor postProcessor) {
93+
void setPostProcessor(@Nullable MessagePostProcessor postProcessor) {
9394
this.postProcessor = postProcessor;
9495
}
9596

@@ -105,7 +106,7 @@ public Message getRequestMessage() {
105106
* Returns the response message, if any, for this connection. Returns either a
106107
* {@link BytesMessage} or a {@link TextMessage}.
107108
*/
108-
public Message getResponseMessage() {
109+
public @Nullable Message getResponseMessage() {
109110
return this.responseMessage;
110111
}
111112

@@ -114,7 +115,7 @@ public Message getResponseMessage() {
114115
*/
115116

116117
@Override
117-
public URI getUri() throws URISyntaxException {
118+
public @Nullable URI getUri() throws URISyntaxException {
118119
try {
119120
return JmsTransportUtils.toUri(this.requestMessage.getJMSDestination());
120121
}
@@ -128,7 +129,7 @@ public URI getUri() throws URISyntaxException {
128129
*/
129130

130131
@Override
131-
public String getErrorMessage() throws IOException {
132+
public @Nullable String getErrorMessage() throws IOException {
132133
return null;
133134
}
134135

@@ -167,6 +168,7 @@ protected InputStream getRequestInputStream() throws IOException {
167168
return new BytesMessageInputStream((BytesMessage) this.requestMessage);
168169
}
169170
else if (this.requestMessage instanceof TextMessage) {
171+
Assert.notNull(this.textMessageEncoding, "MessageEncoding for TextMessage is required");
170172
return new TextMessageInputStream((TextMessage) this.requestMessage, this.textMessageEncoding);
171173
}
172174
else {
@@ -181,15 +183,7 @@ else if (this.requestMessage instanceof TextMessage) {
181183
@Override
182184
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
183185
try {
184-
if (this.requestMessage instanceof BytesMessage) {
185-
this.responseMessage = this.session.createBytesMessage();
186-
}
187-
else if (this.requestMessage instanceof TextMessage) {
188-
this.responseMessage = this.session.createTextMessage();
189-
}
190-
else {
191-
throw new IllegalStateException("Unknown request message type [" + this.requestMessage + "]");
192-
}
186+
this.responseMessage = createResponseMessage();
193187
String correlation = this.requestMessage.getJMSCorrelationID();
194188
if (correlation == null) {
195189
correlation = this.requestMessage.getJMSMessageID();
@@ -201,8 +195,21 @@ else if (this.requestMessage instanceof TextMessage) {
201195
}
202196
}
203197

198+
private Message createResponseMessage() throws JMSException {
199+
if (this.requestMessage instanceof BytesMessage) {
200+
return this.session.createBytesMessage();
201+
}
202+
else if (this.requestMessage instanceof TextMessage) {
203+
return this.session.createTextMessage();
204+
}
205+
else {
206+
throw new IllegalStateException("Unknown request message type [" + this.requestMessage + "]");
207+
}
208+
}
209+
204210
@Override
205211
public void addResponseHeader(String name, String value) throws IOException {
212+
Assert.state(this.responseMessage != null, "Response message is not available");
206213
try {
207214
JmsTransportUtils.addHeader(this.responseMessage, name, value);
208215
}
@@ -217,6 +224,7 @@ protected OutputStream getResponseOutputStream() throws IOException {
217224
return new BytesMessageOutputStream((BytesMessage) this.responseMessage);
218225
}
219226
else if (this.responseMessage instanceof TextMessage) {
227+
Assert.notNull(this.textMessageEncoding, "MessageEncoding for TextMessage is required");
220228
return new TextMessageOutputStream((TextMessage) this.responseMessage, this.textMessageEncoding);
221229
}
222230
else {
@@ -226,6 +234,7 @@ else if (this.responseMessage instanceof TextMessage) {
226234

227235
@Override
228236
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
237+
Assert.state(this.responseMessage != null, "Response message is not available");
229238
MessageProducer messageProducer = null;
230239
try {
231240
if (this.requestMessage.getJMSReplyTo() != null) {

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import jakarta.jms.Session;
3535
import jakarta.jms.TemporaryQueue;
3636
import jakarta.jms.TextMessage;
37+
import org.jspecify.annotations.Nullable;
3738

3839
import org.springframework.jms.connection.ConnectionFactoryUtils;
3940
import org.springframework.jms.core.MessagePostProcessor;
@@ -64,9 +65,9 @@ public class JmsSenderConnection extends AbstractSenderConnection {
6465

6566
private Message requestMessage;
6667

67-
private Destination responseDestination;
68+
private @Nullable Destination responseDestination;
6869

69-
private Message responseMessage;
70+
private @Nullable Message responseMessage;
7071

7172
private long receiveTimeout;
7273

@@ -76,9 +77,9 @@ public class JmsSenderConnection extends AbstractSenderConnection {
7677

7778
private int priority;
7879

79-
private String textMessageEncoding;
80+
private @Nullable String textMessageEncoding;
8081

81-
private MessagePostProcessor postProcessor;
82+
private @Nullable MessagePostProcessor postProcessor;
8283

8384
private boolean sessionTransacted = false;
8485

@@ -111,15 +112,15 @@ public Message getRequestMessage() {
111112
* Returns the response message, if any, for this connection. Returns either a
112113
* {@link BytesMessage} or a {@link TextMessage}.
113114
*/
114-
public Message getResponseMessage() {
115+
public @Nullable Message getResponseMessage() {
115116
return this.responseMessage;
116117
}
117118

118119
/*
119120
* Package-friendly setters
120121
*/
121122

122-
void setResponseDestination(Destination responseDestination) {
123+
void setResponseDestination(@Nullable Destination responseDestination) {
123124
this.responseDestination = responseDestination;
124125
}
125126

@@ -143,7 +144,7 @@ void setTextMessageEncoding(String textMessageEncoding) {
143144
this.textMessageEncoding = textMessageEncoding;
144145
}
145146

146-
void setPostProcessor(MessagePostProcessor postProcessor) {
147+
void setPostProcessor(@Nullable MessagePostProcessor postProcessor) {
147148
this.postProcessor = postProcessor;
148149
}
149150

@@ -156,7 +157,7 @@ void setSessionTransacted(boolean sessionTransacted) {
156157
*/
157158

158159
@Override
159-
public URI getUri() throws URISyntaxException {
160+
public @Nullable URI getUri() throws URISyntaxException {
160161
try {
161162
return JmsTransportUtils.toUri(this.requestDestination);
162163
}
@@ -175,7 +176,7 @@ public boolean hasError() throws IOException {
175176
}
176177

177178
@Override
178-
public String getErrorMessage() throws IOException {
179+
public @Nullable String getErrorMessage() throws IOException {
179180
return null;
180181
}
181182

@@ -199,6 +200,7 @@ protected OutputStream getRequestOutputStream() throws IOException {
199200
return new BytesMessageOutputStream((BytesMessage) this.requestMessage);
200201
}
201202
else if (this.requestMessage instanceof TextMessage) {
203+
Assert.notNull(this.textMessageEncoding, "MessageEncoding for TextMessage is required");
202204
return new TextMessageOutputStream((TextMessage) this.requestMessage, this.textMessageEncoding);
203205
}
204206
else {
@@ -277,9 +279,10 @@ else if (message != null) {
277279
}
278280
finally {
279281
JmsUtils.closeMessageConsumer(messageConsumer);
280-
if (this.temporaryResponseQueueCreated) {
282+
if (this.temporaryResponseQueueCreated
283+
&& this.responseDestination instanceof TemporaryQueue temporaryQueue) {
281284
try {
282-
((TemporaryQueue) this.responseDestination).delete();
285+
temporaryQueue.delete();
283286
}
284287
catch (JMSException ex) {
285288
// ignore
@@ -296,6 +299,7 @@ protected boolean hasResponse() throws IOException {
296299
@Override
297300
public Iterator<String> getResponseHeaderNames() throws IOException {
298301
try {
302+
Assert.state(this.responseMessage != null, "ResponseMessage is required");
299303
return JmsTransportUtils.getHeaderNames(this.responseMessage);
300304
}
301305
catch (JMSException ex) {
@@ -306,6 +310,7 @@ public Iterator<String> getResponseHeaderNames() throws IOException {
306310
@Override
307311
public Iterator<String> getResponseHeaders(String name) throws IOException {
308312
try {
313+
Assert.state(this.responseMessage != null, "ResponseMessage is required");
309314
return JmsTransportUtils.getHeaders(this.responseMessage, name);
310315
}
311316
catch (JMSException ex) {
@@ -319,6 +324,7 @@ protected InputStream getResponseInputStream() throws IOException {
319324
return new BytesMessageInputStream((BytesMessage) this.responseMessage);
320325
}
321326
else if (this.responseMessage instanceof TextMessage) {
327+
Assert.notNull(this.textMessageEncoding, "MessageEncoding for TextMessage is required");
322328
return new TextMessageInputStream((TextMessage) this.responseMessage, this.textMessageEncoding);
323329
}
324330
else {

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Package providing support for handling messages via JMS.
1919
*/
20+
@NullMarked
2021
package org.springframework.ws.transport.jms;
22+
23+
import org.jspecify.annotations.NullMarked;

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/support/JmsTransportUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import jakarta.jms.Message;
3333
import jakarta.jms.Queue;
3434
import jakarta.jms.Topic;
35+
import org.jspecify.annotations.Nullable;
3536

3637
import org.springframework.ws.transport.jms.JmsTransportConstants;
3738

@@ -100,7 +101,7 @@ public static String jmsPropertyToHeader(String propertyName) {
100101
* @param destination the destination
101102
* @return a jms URI
102103
*/
103-
public static URI toUri(Destination destination) throws URISyntaxException, JMSException {
104+
public static @Nullable URI toUri(@Nullable Destination destination) throws URISyntaxException, JMSException {
104105
if (destination == null) {
105106
return null;
106107
}
@@ -118,7 +119,7 @@ else if (destination instanceof Topic topic) {
118119
}
119120

120121
/** Returns the destination name of the given URI. */
121-
public static String getDestinationName(URI uri) {
122+
public static @Nullable String getDestinationName(URI uri) {
122123
return getStringParameter(DESTINATION_NAME_PATTERN, uri);
123124
}
124125

@@ -215,11 +216,11 @@ public static int getPriority(URI uri) {
215216
* Returns the reply-to name of the given URI.
216217
* @see Message#setJMSReplyTo(Destination)
217218
*/
218-
public static String getReplyToName(URI uri) {
219+
public static @Nullable String getReplyToName(URI uri) {
219220
return getStringParameter(REPLY_TO_NAME_PATTERN, uri);
220221
}
221222

222-
private static String getStringParameter(Pattern pattern, URI uri) {
223+
private static @Nullable String getStringParameter(Pattern pattern, URI uri) {
223224
Matcher matcher = pattern.matcher(uri.getSchemeSpecificPart());
224225
if (matcher.find() && matcher.groupCount() == 1) {
225226
return matcher.group(1);

spring-ws-support/src/main/java/org/springframework/ws/transport/jms/support/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Classes supporting the org.springframework.ws.transport.jms package.
1919
*/
20+
@NullMarked
2021
package org.springframework.ws.transport.jms.support;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)