diff --git a/src/main/java/org/takes/facets/cookies/TkJoinedCookies.java b/src/main/java/org/takes/facets/cookies/TkJoinedCookies.java new file mode 100644 index 000000000..01b106f26 --- /dev/null +++ b/src/main/java/org/takes/facets/cookies/TkJoinedCookies.java @@ -0,0 +1,102 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.facets.cookies; + +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.takes.Request; +import org.takes.Response; +import org.takes.Take; +import org.takes.rs.RsWithHeader; +import org.takes.rs.RsWithoutHeader; +import org.takes.tk.TkWrap; + +/** + * Set-Cookie headers will be joined. + * + *

The class is immutable and thread-safe. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.11 + */ +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public final class TkJoinedCookies extends TkWrap { + + /** + * Pattern to find them. + */ + private static final Pattern PTN = Pattern.compile( + "set-cookie: (.+)", Pattern.CASE_INSENSITIVE + ); + + /** + * Ctor. + * @param take The take to wrap + * @checkstyle AnonInnerLengthCheck (100 lines) + */ + public TkJoinedCookies(final Take take) { + super( + new Take() { + @Override + public Response act(final Request req) throws IOException { + return TkJoinedCookies.join(take.act(req)); + } + } + ); + } + + /** + * Join them. + * @param response The response + * @return New response + * @throws IOException If fails + */ + private static Response join(final Response response) throws IOException { + final StringBuilder cookies = new StringBuilder(); + for (final String header : response.head()) { + final Matcher matcher = + TkJoinedCookies.PTN.matcher(header); + if (!matcher.matches()) { + continue; + } + cookies.append(matcher.group(1)).append(", "); + } + final Response out; + if (cookies.length() > 0) { + out = new RsWithHeader( + new RsWithoutHeader(response, "Set-cookie"), + "Set-Cookie", cookies.toString() + ); + } else { + out = response; + } + return out; + } + +} diff --git a/src/test/java/org/takes/facets/cookies/TkJoinedCookiesTest.java b/src/test/java/org/takes/facets/cookies/TkJoinedCookiesTest.java new file mode 100644 index 000000000..94a56f603 --- /dev/null +++ b/src/test/java/org/takes/facets/cookies/TkJoinedCookiesTest.java @@ -0,0 +1,67 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.facets.cookies; + +import java.io.IOException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.takes.rq.RqFake; +import org.takes.rs.RsPrint; +import org.takes.rs.RsText; +import org.takes.rs.RsWithHeaders; +import org.takes.tk.TkFixed; + +/** + * Test case for {@link TkJoinedCookies}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.11 + */ +public final class TkJoinedCookiesTest { + + /** + * TkJoinedCookies can join cookies. + * @throws IOException If some problem inside + */ + @Test + public void joinsCookies() throws IOException { + MatcherAssert.assertThat( + new RsPrint( + new TkJoinedCookies( + new TkFixed( + new RsWithHeaders( + new RsText(), + "Set-Cookie: a=1", + "Set-cookie: b=1; Path=/" + ) + ) + ).act(new RqFake()) + ).print(), + Matchers.containsString("Set-Cookie: a=1, b=1; Path=/") + ); + } + +}