|
|
|
@ -45,7 +45,7 @@ import org.springframework.util.MultiValueMap; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
|
|
|
|
|
|
|
|
private static Log logger = LogFactory.getLog(AbstractServerHttpResponse.class); |
|
|
|
private Log logger = LogFactory.getLog(getClass()); |
|
|
|
|
|
|
|
|
|
|
|
private static final int STATE_NEW = 1; |
|
|
|
private static final int STATE_NEW = 1; |
|
|
|
|
|
|
|
|
|
|
|
@ -53,17 +53,20 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
|
|
|
|
|
|
|
|
private static final int STATE_COMMITTED = 3; |
|
|
|
private static final int STATE_COMMITTED = 3; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final DataBufferFactory dataBufferFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpStatus statusCode; |
|
|
|
private HttpStatus statusCode; |
|
|
|
|
|
|
|
|
|
|
|
private final HttpHeaders headers; |
|
|
|
private final HttpHeaders headers; |
|
|
|
|
|
|
|
|
|
|
|
private final MultiValueMap<String, ResponseCookie> cookies; |
|
|
|
private final MultiValueMap<String, ResponseCookie> cookies; |
|
|
|
|
|
|
|
|
|
|
|
private final AtomicInteger state = new AtomicInteger(STATE_NEW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final List<Supplier<? extends Mono<Void>>> beforeCommitActions = new ArrayList<>(4); |
|
|
|
private final List<Supplier<? extends Mono<Void>>> beforeCommitActions = new ArrayList<>(4); |
|
|
|
|
|
|
|
|
|
|
|
private final DataBufferFactory dataBufferFactory; |
|
|
|
private final AtomicInteger state = new AtomicInteger(STATE_NEW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory) { |
|
|
|
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory) { |
|
|
|
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null"); |
|
|
|
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null"); |
|
|
|
@ -73,13 +76,29 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
this.cookies = new LinkedMultiValueMap<String, ResponseCookie>(); |
|
|
|
this.cookies = new LinkedMultiValueMap<String, ResponseCookie>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final DataBufferFactory bufferFactory() { |
|
|
|
public final DataBufferFactory bufferFactory() { |
|
|
|
return this.dataBufferFactory; |
|
|
|
return this.dataBufferFactory; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean setStatusCode(HttpStatus statusCode) { |
|
|
|
|
|
|
|
Assert.notNull(statusCode); |
|
|
|
|
|
|
|
if (STATE_NEW == this.state.get()) { |
|
|
|
|
|
|
|
this.statusCode = statusCode; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (logger.isDebugEnabled()) { |
|
|
|
|
|
|
|
logger.debug("Can't set the status " + statusCode.toString() + |
|
|
|
|
|
|
|
" because the HTTP response has already been committed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected HttpStatus getStatusCode() { |
|
|
|
protected HttpStatus getStatusCode() { |
|
|
|
return statusCode; |
|
|
|
return this.statusCode; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -100,6 +119,12 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
return this.cookies; |
|
|
|
return this.cookies; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void beforeCommit(Supplier<? extends Mono<Void>> action) { |
|
|
|
|
|
|
|
Assert.notNull(action); |
|
|
|
|
|
|
|
this.beforeCommitActions.add(action); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Mono<Void> writeWith(Publisher<DataBuffer> publisher) { |
|
|
|
public Mono<Void> writeWith(Publisher<DataBuffer> publisher) { |
|
|
|
return new ChannelSendOperator<>(publisher, writePublisher -> |
|
|
|
return new ChannelSendOperator<>(publisher, writePublisher -> |
|
|
|
@ -127,6 +152,12 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
return mono; |
|
|
|
return mono; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Implement this method to write to the underlying the response. |
|
|
|
|
|
|
|
* @param body the publisher to write with |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected abstract Mono<Void> writeWithInternal(Publisher<DataBuffer> body); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Implement this method to write the status code to the underlying response. |
|
|
|
* Implement this method to write the status code to the underlying response. |
|
|
|
* This method is called once only. |
|
|
|
* This method is called once only. |
|
|
|
@ -145,35 +176,10 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected abstract void writeCookies(); |
|
|
|
protected abstract void writeCookies(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Implement this method to write to the underlying the response. |
|
|
|
|
|
|
|
* @param body the publisher to write with |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected abstract Mono<Void> writeWithInternal(Publisher<DataBuffer> body); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void beforeCommit(Supplier<? extends Mono<Void>> action) { |
|
|
|
|
|
|
|
Assert.notNull(action); |
|
|
|
|
|
|
|
this.beforeCommitActions.add(action); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Mono<Void> setComplete() { |
|
|
|
public Mono<Void> setComplete() { |
|
|
|
return applyBeforeCommit(); |
|
|
|
return applyBeforeCommit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean setStatusCode(HttpStatus statusCode) { |
|
|
|
|
|
|
|
Assert.notNull(statusCode); |
|
|
|
|
|
|
|
if (STATE_NEW == this.state.get()) { |
|
|
|
|
|
|
|
this.statusCode = statusCode; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (logger.isDebugEnabled()) { |
|
|
|
|
|
|
|
logger.debug("Can't set the status " + statusCode.toString() + |
|
|
|
|
|
|
|
" because the HTTP response has already been committed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|