Skip to content

Commit 16939b7

Browse files
violetaggrstoyanchev
authored andcommitted
AbstractListenerServerHttpResponse improvements
This commit changes writeWithInternal(Publisher<DataBuffer> body). It is implemented as writeAndFlushWith(Mono.just(body)).
1 parent fffea06 commit 16939b7

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerServerHttpResponse.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,7 @@ public AbstractListenerServerHttpResponse(DataBufferFactory dataBufferFactory) {
4040

4141
@Override
4242
protected final Mono<Void> writeWithInternal(Publisher<DataBuffer> body) {
43-
if (this.writeCalled.compareAndSet(false, true)) {
44-
Processor<DataBuffer, Void> bodyProcessor = createBodyProcessor();
45-
return Mono.from(subscriber -> {
46-
body.subscribe(bodyProcessor);
47-
bodyProcessor.subscribe(subscriber);
48-
});
49-
50-
} else {
51-
return Mono.error(new IllegalStateException(
52-
"writeWith() or writeAndFlushWith() has already been called"));
53-
}
43+
return writeAndFlushWithInternal(Mono.just(body));
5444
}
5545

5646
@Override
@@ -68,13 +58,6 @@ protected final Mono<Void> writeAndFlushWithInternal(Publisher<Publisher<DataBuf
6858
}
6959
}
7060

71-
/**
72-
* Abstract template method to create a {@code Processor<DataBuffer, Void>} that
73-
* will write the response body to the underlying output. Called from
74-
* {@link #writeWithInternal(Publisher)}.
75-
*/
76-
protected abstract Processor<DataBuffer, Void> createBodyProcessor();
77-
7861
/**
7962
* Abstract template method to create a {@code Processor<Publisher<DataBuffer>, Void>}
8063
* that will write the response body with flushes to the underlying output. Called from

spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import java.nio.charset.Charset;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.concurrent.atomic.AtomicBoolean;
2625
import javax.servlet.ServletOutputStream;
2726
import javax.servlet.WriteListener;
2827
import javax.servlet.http.Cookie;
2928
import javax.servlet.http.HttpServletResponse;
3029

3130
import org.reactivestreams.Processor;
31+
import org.reactivestreams.Publisher;
3232

3333
import org.springframework.core.io.buffer.DataBuffer;
3434
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -44,7 +44,7 @@
4444
*/
4545
public class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
4646

47-
private final AtomicBoolean listenerRegistered = new AtomicBoolean();
47+
private final ResponseBodyWriteListener writeListener = new ResponseBodyWriteListener();
4848

4949
private volatile ResponseBodyProcessor bodyProcessor;
5050

@@ -112,15 +112,17 @@ protected void writeCookies() {
112112
}
113113
}
114114

115-
private void registerListener() throws IOException {
116-
if (this.listenerRegistered.compareAndSet(false, true)) {
117-
ResponseBodyWriteListener writeListener = new ResponseBodyWriteListener();
118-
this.response.getOutputStream().setWriteListener(writeListener);
115+
private void registerListener() {
116+
try {
117+
outputStream().setWriteListener(writeListener);
118+
}
119+
catch (IOException e) {
120+
throw new UncheckedIOException(e);
119121
}
120122
}
121123

122124
private void flush() throws IOException {
123-
ServletOutputStream outputStream = this.response.getOutputStream();
125+
ServletOutputStream outputStream = outputStream();
124126
if (outputStream.isReady()) {
125127
try {
126128
outputStream.flush();
@@ -136,22 +138,15 @@ private void flush() throws IOException {
136138
}
137139
}
138140

139-
@Override
140-
protected ResponseBodyProcessor createBodyProcessor() {
141-
try {
142-
registerListener();
143-
this.bodyProcessor = new ResponseBodyProcessor(this.response.getOutputStream(),
144-
this.bufferSize);
145-
return this.bodyProcessor;
146-
}
147-
catch (IOException ex) {
148-
throw new UncheckedIOException(ex);
149-
}
141+
private ServletOutputStream outputStream() throws IOException {
142+
return this.response.getOutputStream();
150143
}
151144

152145
@Override
153-
protected AbstractResponseBodyFlushProcessor createBodyFlushProcessor() {
154-
return new ResponseBodyFlushProcessor();
146+
protected Processor<Publisher<DataBuffer>, Void> createBodyFlushProcessor() {
147+
Processor<Publisher<DataBuffer>, Void> processor = new ResponseBodyFlushProcessor();
148+
registerListener();
149+
return processor;
155150
}
156151

157152
private class ResponseBodyProcessor extends AbstractResponseBodyProcessor {
@@ -238,7 +233,13 @@ private class ResponseBodyFlushProcessor extends AbstractResponseBodyFlushProces
238233

239234
@Override
240235
protected Processor<DataBuffer, Void> createBodyProcessor() {
241-
return ServletServerHttpResponse.this.createBodyProcessor();
236+
try {
237+
bodyProcessor = new ResponseBodyProcessor(outputStream(), bufferSize);
238+
return bodyProcessor;
239+
}
240+
catch (IOException ex) {
241+
throw new UncheckedIOException(ex);
242+
}
242243
}
243244

244245
@Override

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ protected void writeCookies() {
123123
}
124124
}
125125

126-
@Override
127-
protected ResponseBodyProcessor createBodyProcessor() {
126+
private ResponseBodyProcessor createBodyProcessor() {
128127
if (this.responseChannel == null) {
129128
this.responseChannel = this.exchange.getResponseChannel();
130129
}

0 commit comments

Comments
 (0)