Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UserAgentWire |
|
| 2.5;2.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 com.jcabi.manifests.Manifests; | |
38 | import java.io.IOException; | |
39 | import java.io.InputStream; | |
40 | import java.util.Collection; | |
41 | import java.util.LinkedList; | |
42 | import java.util.Map; | |
43 | import javax.ws.rs.core.HttpHeaders; | |
44 | import lombok.EqualsAndHashCode; | |
45 | import lombok.ToString; | |
46 | ||
47 | /** | |
48 | * Wire with default user agent. | |
49 | * | |
50 | * <p>This wire adds an extra HTTP header {@code User-Agent} to the request, | |
51 | * if it's not yet provided, for example: | |
52 | * | |
53 | * <pre> String html = new JdkRequest("http://goggle.com") | |
54 | * .through(UserAgentWire.class) | |
55 | * .fetch() | |
56 | * .body();</pre> | |
57 | * | |
58 | * <p>An actual HTTP request will be sent with {@code User-Agent} | |
59 | * header with a value {@code ReXSL-0.1/abcdef0 Java/1.6} (for example). It | |
60 | * is recommended to use this wire decorator when you're working with | |
61 | * third party RESTful services, to properly identify yourself and avoid | |
62 | * troubles. | |
63 | * | |
64 | * <p>The class is immutable and thread-safe. | |
65 | * | |
66 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
67 | * @version $Id: d2638432483e984d4e638f1154343ea7baaea4e2 $ | |
68 | * @since 0.10 | |
69 | * @see <a href="http://tools.ietf.org/html/rfc2616#section-14.43">RFC 2616 section 14.43 "User-Agent"</a> | |
70 | */ | |
71 | @Immutable | |
72 | 0 | @ToString(of = "origin") |
73 | 0 | @EqualsAndHashCode(of = "origin") |
74 | public final class UserAgentWire implements Wire { | |
75 | ||
76 | /** | |
77 | * Default user agent. | |
78 | */ | |
79 | 1 | private static final String AGENT = String.format( |
80 | "jcabi-%s/%s Java/%s", | |
81 | Manifests.read("JCabi-Version"), | |
82 | Manifests.read("JCabi-Build"), | |
83 | System.getProperty("java.version") | |
84 | ); | |
85 | ||
86 | /** | |
87 | * Original wire. | |
88 | */ | |
89 | private final transient Wire origin; | |
90 | ||
91 | /** | |
92 | * Public ctor. | |
93 | * @param wire Original wire | |
94 | */ | |
95 | 3 | public UserAgentWire(final Wire wire) { |
96 | 3 | this.origin = wire; |
97 | 3 | } |
98 | ||
99 | // @checkstyle ParameterNumber (7 lines) | |
100 | @Override | |
101 | public Response send(final Request req, final String home, | |
102 | final String method, | |
103 | final Collection<Map.Entry<String, String>> headers, | |
104 | final InputStream content, | |
105 | final int connect, | |
106 | final int read) throws IOException { | |
107 | 3 | final Collection<Map.Entry<String, String>> hdrs = |
108 | new LinkedList<Map.Entry<String, String>>(); | |
109 | 3 | boolean absent = true; |
110 | 3 | for (final Map.Entry<String, String> header : headers) { |
111 | 2 | hdrs.add(header); |
112 | 2 | if (header.getKey().equals(HttpHeaders.USER_AGENT)) { |
113 | 0 | absent = false; |
114 | } | |
115 | 2 | } |
116 | 3 | if (absent) { |
117 | 3 | hdrs.add( |
118 | new ImmutableHeader( | |
119 | HttpHeaders.USER_AGENT, | |
120 | UserAgentWire.AGENT | |
121 | ) | |
122 | ); | |
123 | } | |
124 | 3 | return this.origin.send( |
125 | req, home, method, hdrs, content, connect, read | |
126 | ); | |
127 | } | |
128 | } |