diff --git a/changelog/@unreleased/pr-2033.v2.yml b/changelog/@unreleased/pr-2033.v2.yml new file mode 100644 index 000000000..76cbda17e --- /dev/null +++ b/changelog/@unreleased/pr-2033.v2.yml @@ -0,0 +1,5 @@ +type: feature +feature: + description: Add `RequestContext` method to get cookie values. + links: + - https://github.com/palantir/conjure-java/pull/2033 diff --git a/conjure-java-undertow-runtime/src/main/java/com/palantir/conjure/java/undertow/runtime/ConjureContexts.java b/conjure-java-undertow-runtime/src/main/java/com/palantir/conjure/java/undertow/runtime/ConjureContexts.java index 20f35fb00..ed946ae46 100644 --- a/conjure-java-undertow-runtime/src/main/java/com/palantir/conjure/java/undertow/runtime/ConjureContexts.java +++ b/conjure-java-undertow-runtime/src/main/java/com/palantir/conjure/java/undertow/runtime/ConjureContexts.java @@ -25,6 +25,7 @@ import com.palantir.logsafe.logger.SafeLogger; import com.palantir.logsafe.logger.SafeLoggerFactory; import io.undertow.server.HttpServerExchange; +import io.undertow.server.handlers.Cookie; import io.undertow.util.HeaderValues; import java.security.cert.Certificate; import java.util.Collections; @@ -71,6 +72,11 @@ public Optional firstHeader(String headerName) { return Optional.ofNullable(exchange.getRequestHeaders().getFirst(headerName)); } + @Override + public Optional cookie(String cookieName) { + return Optional.ofNullable(exchange.getRequestCookie(cookieName)).map(Cookie::getValue); + } + @Override public ImmutableListMultimap queryParameters() { ImmutableListMultimap cachedQueryParamsSnapshot = cachedQueryParams; diff --git a/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/RequestContext.java b/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/RequestContext.java index 1678d32e6..b6f359b11 100644 --- a/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/RequestContext.java +++ b/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/RequestContext.java @@ -41,6 +41,12 @@ public interface RequestContext { */ Optional firstHeader(String headerName); + /** + * Returns the value of the cookie named {@code cookieName}. + * An {@link Optional#empty()} is returned if no such cookie exists. + */ + Optional cookie(String cookieName); + /** * Returns all query parameters associated with the current request. */