Skip to content

Commit 547a0dc

Browse files
author
Vincent Potucek
committed
remove false comment about try-with-resources statement in StompSubProtocolHandler
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 9741afe commit 547a0dc

File tree

2 files changed

+21
-135
lines changed

2 files changed

+21
-135
lines changed

spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
import org.springframework.util.MimeTypeUtils;
3939
import org.springframework.util.StreamUtils;
4040

41-
import static java.lang.Math.min;
42-
import static org.springframework.util.StreamUtils.copyRange;
43-
4441
/**
4542
* Implementation of {@link HttpMessageConverter} that can write a single
4643
* {@link ResourceRegion} or Collections of {@link ResourceRegion ResourceRegions}.
@@ -166,18 +163,29 @@ private boolean supportsRepeatableWrites(ResourceRegion region) {
166163

167164
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
168165
Assert.notNull(region, "ResourceRegion must not be null");
166+
HttpHeaders responseHeaders = outputMessage.getHeaders();
169167

170-
var start = region.getPosition();
171-
var resourceLength = region.getResource().contentLength();
172-
var end = min(start + region.getCount() - 1, resourceLength - 1);
173-
var responseHeaders = outputMessage.getHeaders();
174-
responseHeaders.setContentLength(end - start + 1);
168+
long start = region.getPosition();
169+
long end = start + region.getCount() - 1;
170+
long resourceLength = region.getResource().contentLength();
171+
end = Math.min(end, resourceLength - 1);
172+
long rangeLength = end - start + 1;
175173
responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
174+
responseHeaders.setContentLength(rangeLength);
176175

177-
try (var in = region.getResource().getInputStream()) {
178-
copyRange(in, outputMessage.getBody(), start, end);
176+
InputStream in = region.getResource().getInputStream();
177+
// We cannot use try-with-resources here for the InputStream, since we have
178+
// custom handling of the close() method in a finally-block.
179+
try {
180+
StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
179181
}
180-
catch (IOException ignored) {
182+
finally {
183+
try {
184+
in.close();
185+
}
186+
catch (IOException ex) {
187+
// ignore
188+
}
181189
}
182190
}
183191

@@ -219,14 +227,14 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
219227
println(out);
220228
}
221229
long resourceLength = region.getResource().contentLength();
222-
end = min(end, resourceLength - inputStreamPosition - 1);
230+
end = Math.min(end, resourceLength - inputStreamPosition - 1);
223231
print(out, "Content-Range: bytes " +
224232
region.getPosition() + '-' + (region.getPosition() + region.getCount() - 1) +
225233
'/' + resourceLength);
226234
println(out);
227235
println(out);
228236
// Printing content
229-
copyRange(in, out, start, end);
237+
StreamUtils.copyRange(in, out, start, end);
230238
inputStreamPosition += (end + 1);
231239
}
232240
}

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -531,129 +531,7 @@ public boolean send(Message<?> message, long timeout) {
531531
verify(runnable, times(1)).run();
532532
}
533533

534-
@Test
535-
void handleMessageFromClientWithBinaryStompFrame() {
536-
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
537-
byte[] payload = new StompEncoder().encode(headers.getMessageHeaders(), EMPTY_PAYLOAD);
538-
BinaryMessage binaryMessage = new BinaryMessage(payload);
539-
540-
this.protocolHandler.afterSessionStarted(this.session, this.channel);
541-
this.protocolHandler.handleMessageFromClient(this.session, binaryMessage, this.channel);
542-
543-
verify(this.channel).send(this.messageCaptor.capture());
544-
Message<?> actual = this.messageCaptor.getValue();
545-
assertThat(actual).isNotNull();
546-
assertThat(StompHeaderAccessor.wrap(actual).getCommand()).isEqualTo(StompCommand.CONNECT);
547-
}
548-
549-
@Test
550-
void handleMessageFromClientWithPartialStompFrame() {
551-
TextMessage partialMessage = new TextMessage("CONNECT\naccept-version:1.2\n\n");
552-
553-
this.protocolHandler.afterSessionStarted(this.session, this.channel);
554-
this.protocolHandler.handleMessageFromClient(this.session, partialMessage, this.channel);
555-
556-
verifyNoInteractions(this.channel);
557-
assertThat(this.session.getSentMessages()).isEmpty();
558-
}
559-
560-
@Test
561-
void handleMessageFromClientWithInvalidStompFrame() {
562-
TextMessage invalidMessage = new TextMessage("INVALID_COMMAND\n\n\0");
563-
564-
this.protocolHandler.afterSessionStarted(this.session, this.channel);
565-
this.protocolHandler.handleMessageFromClient(this.session, invalidMessage, this.channel);
566-
567-
verifyNoInteractions(this.channel);
568-
assertThat(this.session.getSentMessages()).hasSize(1);
569-
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
570-
assertThat(actual.getPayload()).startsWith("ERROR");
571-
}
572-
573-
@Test
574-
void handleMessageToClientWithEmptyPayload() {
575-
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
576-
headers.setDestination("/topic/foo");
577-
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
578-
579-
this.protocolHandler.handleMessageToClient(this.session, message);
580-
581-
assertThat(this.session.getSentMessages()).hasSize(1);
582-
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
583-
assertThat(textMessage.getPayload()).isEqualTo("MESSAGE\ndestination:/topic/foo\ncontent-length:0\n\n\u0000");
584-
}
585-
586-
@Test
587-
void handleMessageToClientWithLargePayload() {
588-
byte[] largePayload = new byte[1024 * 64]; // 64KB
589-
Arrays.fill(largePayload, (byte) 'A');
590-
591-
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
592-
headers.setDestination("/topic/foo");
593-
headers.setContentType(MimeTypeUtils.APPLICATION_OCTET_STREAM);
594-
Message<byte[]> message = MessageBuilder.createMessage(largePayload, headers.getMessageHeaders());
595-
596-
this.protocolHandler.handleMessageToClient(this.session, message);
597-
598-
assertThat(this.session.getSentMessages()).hasSize(1);
599-
WebSocketMessage<?> webSocketMessage = this.session.getSentMessages().get(0);
600-
assertThat(webSocketMessage).isInstanceOf(BinaryMessage.class);
601-
}
602-
603-
@Test
604-
void handleMessageToClientWithErrorFrame() {
605-
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.ERROR);
606-
headers.setMessage("Test error");
607-
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
608-
609-
this.protocolHandler.handleMessageToClient(this.session, message);
610-
611-
assertThat(this.session.getSentMessages()).hasSize(1);
612-
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
613-
assertThat(actual.getPayload()).startsWith("ERROR\nmessage:Test error");
614-
615-
// Verify session was closed
616-
assertThat(this.session.isOpen()).isFalse();
617-
assertThat(this.session.getCloseStatus()).isEqualTo(CloseStatus.PROTOCOL_ERROR);
618-
}
619-
620-
@Test
621-
void handleMessageToClientWithHeartbeat() {
622-
StompHeaderAccessor headers = StompHeaderAccessor.createForHeartbeat();
623-
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
624534

625-
this.protocolHandler.handleMessageToClient(this.session, message);
626-
627-
assertThat(this.session.getSentMessages()).hasSize(1);
628-
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
629-
assertThat(actual.getPayload()).isEqualTo("\n");
630-
}@Test
631-
void sessionAttributesArePreserved() {
632-
this.session.getAttributes().put("key", "value");
633-
634-
TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
635-
this.protocolHandler.afterSessionStarted(this.session, this.channel);
636-
this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
637-
638-
verify(this.channel).send(this.messageCaptor.capture());
639-
Message<?> actual = this.messageCaptor.getValue();
640-
assertThat(SimpMessageHeaderAccessor.getSessionAttributes(actual.getHeaders()))
641-
.containsEntry("key", "value");
642-
}
643-
644-
@Test
645-
void immutableMessageHandling() {
646-
// Create immutable message
647-
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
648-
headers.setImmutable();
649-
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
650-
651-
this.protocolHandler.handleMessageToClient(this.session, message);
652-
653-
assertThat(this.session.getSentMessages()).hasSize(1);
654-
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
655-
assertThat(actual.getPayload()).contains("SEND");
656-
}
657535
private static class UniqueUser extends TestPrincipal implements DestinationUserNameProvider {
658536

659537
private UniqueUser(String name) {

0 commit comments

Comments
 (0)