Add ServerHttpResponse.addCookie method

This commit adds a `addCookie(ResponseCookie)` method to the reactive
`ServerHttpResponse` interface.

Issue: SPR-15523
This commit is contained in:
Arjen Poutsma
2017-05-09 10:18:39 +02:00
parent add1305252
commit b649041976
4 changed files with 28 additions and 3 deletions

View File

@@ -120,6 +120,19 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
CollectionUtils.unmodifiableMultiValueMap(this.cookies) : this.cookies);
}
@Override
public void addCookie(ResponseCookie cookie) {
Assert.notNull(cookie, "'cookie' must not be null");
if (this.state.get() == State.COMMITTED) {
throw new IllegalStateException("Can't add the cookie " + cookie +
"because the HTTP response has already been committed");
}
else {
getCookies().add(cookie.getName(), cookie);
}
}
@Override
public String encodeUrl(String url) {
return (this.urlEncoder != null ? this.urlEncoder.apply(url) : url);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -50,6 +50,13 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
*/
MultiValueMap<String, ResponseCookie> getCookies();
/**
* Add the given {@code ResponseCookie}.
* @param cookie the cookie to add
* @throws IllegalStateException if the response has already been committed
*/
void addCookie(ResponseCookie cookie);
/**
* A mechanism for URL rewriting that applications and libraries such as
* HTML template libraries to use consistently for all URLs emitted by

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -74,6 +74,11 @@ public class ServerHttpResponseDecorator implements ServerHttpResponse {
return getDelegate().getCookies();
}
@Override
public void addCookie(ResponseCookie cookie) {
getDelegate().addCookie(cookie);
}
@Override
public String encodeUrl(String url) {
return getDelegate().encodeUrl(url);