Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VerboseWire |
|
| 1.6666666666666667;1.667 |
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.aspects.Tv; | |
34 | import com.jcabi.http.Request; | |
35 | import com.jcabi.http.RequestBody; | |
36 | import com.jcabi.http.Response; | |
37 | import com.jcabi.http.Wire; | |
38 | import com.jcabi.log.Logger; | |
39 | import java.io.ByteArrayInputStream; | |
40 | import java.io.ByteArrayOutputStream; | |
41 | import java.io.IOException; | |
42 | import java.io.InputStream; | |
43 | import java.util.Collection; | |
44 | import java.util.Map; | |
45 | import lombok.EqualsAndHashCode; | |
46 | import lombok.ToString; | |
47 | ||
48 | /** | |
49 | * Verbose wire. | |
50 | * | |
51 | * <p>This wire makes HTTP request and response details visible in | |
52 | * log (we're using SLF4J logging facility), for example: | |
53 | * | |
54 | * <pre> String html = new JdkRequest("http://goggle.com") | |
55 | * .through(VerboseWire.class) | |
56 | * .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | |
57 | * .fetch() | |
58 | * .body();</pre> | |
59 | * | |
60 | * <p>The class is immutable and thread-safe. | |
61 | * | |
62 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
63 | * @version $Id: 93c7fc5336b46958e261ac0a124982e5e6c2f6ab $ | |
64 | * @since 0.10 | |
65 | */ | |
66 | @Immutable | |
67 | 0 | @ToString(of = "origin") |
68 | 0 | @EqualsAndHashCode(of = "origin") |
69 | public final class VerboseWire implements Wire { | |
70 | ||
71 | /** | |
72 | * Original wire. | |
73 | */ | |
74 | private final transient Wire origin; | |
75 | ||
76 | /** | |
77 | * Public ctor. | |
78 | * @param wire Original wire | |
79 | */ | |
80 | 14 | public VerboseWire(final Wire wire) { |
81 | 14 | this.origin = wire; |
82 | 14 | } |
83 | ||
84 | // @checkstyle ParameterNumber (7 lines) | |
85 | @Override | |
86 | public Response send(final Request req, final String home, | |
87 | final String method, | |
88 | final Collection<Map.Entry<String, String>> headers, | |
89 | final InputStream content, | |
90 | final int connect, | |
91 | final int read) throws IOException { | |
92 | 21 | final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
93 | 20 | final byte[] buffer = new byte[Tv.THOUSAND]; |
94 | 21 | for (int bytes = content.read(buffer); bytes != -1; |
95 | 6 | bytes = content.read(buffer)) { |
96 | 6 | output.write(buffer, 0, bytes); |
97 | } | |
98 | 20 | output.flush(); |
99 | 21 | final Response response = this.origin.send( |
100 | req, home, method, headers, | |
101 | new ByteArrayInputStream(output.toByteArray()), | |
102 | connect, read | |
103 | ); | |
104 | 21 | final StringBuilder text = new StringBuilder(0); |
105 | 21 | for (final Map.Entry<String, String> header : headers) { |
106 | 7 | text.append(header.getKey()) |
107 | .append(": ") | |
108 | .append(header.getValue()) | |
109 | .append('\n'); | |
110 | 7 | } |
111 | 21 | text.append('\n').append( |
112 | new RequestBody.Printable(output.toByteArray()).toString() | |
113 | ); | |
114 | 21 | Logger.info( |
115 | this, | |
116 | "#send(%s %s):\nHTTP Request (%s):\n%s\nHTTP Response (%s):\n%s", | |
117 | method, home, | |
118 | req.getClass().getName(), | |
119 | VerboseWire.indent(text.toString()), | |
120 | response.getClass().getName(), | |
121 | VerboseWire.indent(response.toString()) | |
122 | ); | |
123 | 21 | return response; |
124 | } | |
125 | ||
126 | /** | |
127 | * Indent provided text. | |
128 | * @param text Text to indent | |
129 | * @return Indented text | |
130 | */ | |
131 | private static String indent(final String text) { | |
132 | 42 | return new StringBuilder(" ") |
133 | .append(text.replaceAll("(\n|\n\r)", "$1 ")) | |
134 | .toString(); | |
135 | } | |
136 | ||
137 | } |