Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CookieOptimizingWire |
|
| 4.5;4.5 |
1 | /** | |
2 | * Copyright (c) 2011-2017, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.http.wire; | |
31 | ||
32 | import com.jcabi.aspects.Immutable; | |
33 | import com.jcabi.http.ImmutableHeader; | |
34 | import com.jcabi.http.Request; | |
35 | import com.jcabi.http.Response; | |
36 | import com.jcabi.http.Wire; | |
37 | import java.io.IOException; | |
38 | import java.io.InputStream; | |
39 | import java.util.Collection; | |
40 | import java.util.LinkedList; | |
41 | import java.util.Map; | |
42 | import java.util.concurrent.ConcurrentHashMap; | |
43 | import java.util.concurrent.ConcurrentMap; | |
44 | import javax.ws.rs.core.HttpHeaders; | |
45 | import lombok.EqualsAndHashCode; | |
46 | import lombok.ToString; | |
47 | ||
48 | /** | |
49 | * Wire that compresses cookies before sending. | |
50 | * | |
51 | * <p>This wire compresses all provided {@code Cookie} headers into one | |
52 | * and removes empty cookies, for example: | |
53 | * | |
54 | * <pre> String html = new JdkRequest("http://goggle.com") | |
55 | * .through(CookieOptimizingWire.class) | |
56 | * .header(HttpHeaders.Cookie, "alpha=test") | |
57 | * .header(HttpHeaders.Cookie, "beta=") | |
58 | * .header(HttpHeaders.Cookie, "gamma=foo") | |
59 | * .fetch() | |
60 | * .body();</pre> | |
61 | * | |
62 | * <p>An actual HTTP request will be sent with just one {@code Cookie} | |
63 | * header with a value {@code alpha=test; gamma=foo}. | |
64 | * | |
65 | * <p>It is highly recommended to use this wire decorator when you're | |
66 | * working with cookies. | |
67 | * | |
68 | * <p>The class is immutable and thread-safe. | |
69 | * | |
70 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
71 | * @version $Id: 7e8b52de61cbad41840939a3eec129912c0d169e $ | |
72 | * @since 0.10 | |
73 | * @see <a href="http://tools.ietf.org/html/rfc2965">RFC 2965 "HTTP State Management Mechanism"</a> | |
74 | */ | |
75 | @Immutable | |
76 | 0 | @ToString(of = "origin") |
77 | 0 | @EqualsAndHashCode(of = "origin") |
78 | public final class CookieOptimizingWire implements Wire { | |
79 | ||
80 | /** | |
81 | * Original wire. | |
82 | */ | |
83 | private final transient Wire origin; | |
84 | ||
85 | /** | |
86 | * Public ctor. | |
87 | * @param wire Original wire | |
88 | */ | |
89 | 2 | public CookieOptimizingWire(final Wire wire) { |
90 | 2 | this.origin = wire; |
91 | 2 | } |
92 | ||
93 | // @checkstyle ParameterNumber (7 lines) | |
94 | @Override | |
95 | public Response send(final Request req, final String home, | |
96 | final String method, | |
97 | final Collection<Map.Entry<String, String>> headers, | |
98 | final InputStream content, | |
99 | final int connect, | |
100 | final int read) throws IOException { | |
101 | 4 | final Collection<Map.Entry<String, String>> hdrs = |
102 | new LinkedList<Map.Entry<String, String>>(); | |
103 | 4 | final ConcurrentMap<String, String> cookies = |
104 | new ConcurrentHashMap<String, String>(0); | |
105 | 4 | for (final Map.Entry<String, String> header : headers) { |
106 | 10 | if (header.getKey().equals(HttpHeaders.COOKIE)) { |
107 | 10 | final String cookie = header.getValue(); |
108 | 10 | final int split = cookie.indexOf('='); |
109 | 10 | final String name = cookie.substring(0, split); |
110 | 10 | final String value = cookie.substring(split + 1); |
111 | 10 | if (value.isEmpty()) { |
112 | 1 | cookies.remove(name); |
113 | } else { | |
114 | 9 | cookies.put(name, value); |
115 | } | |
116 | 10 | } else { |
117 | 0 | hdrs.add(header); |
118 | } | |
119 | 10 | } |
120 | 4 | if (!cookies.isEmpty()) { |
121 | 4 | final StringBuilder text = new StringBuilder(0); |
122 | 4 | for (final Map.Entry<String, String> cookie : cookies.entrySet()) { |
123 | 7 | if (cookie.getValue().isEmpty()) { |
124 | 0 | continue; |
125 | } | |
126 | 7 | if (text.length() > 0) { |
127 | 3 | text.append("; "); |
128 | } | |
129 | 7 | text.append(cookie.getKey()) |
130 | .append('=') | |
131 | .append(cookie.getValue()); | |
132 | 7 | } |
133 | 4 | hdrs.add( |
134 | new ImmutableHeader( | |
135 | HttpHeaders.COOKIE, | |
136 | text.toString() | |
137 | ) | |
138 | ); | |
139 | } | |
140 | 4 | return this.origin.send( |
141 | req, home, method, hdrs, content, connect, read | |
142 | ); | |
143 | } | |
144 | } |