1 /*
2 * Copyright (c) 2011-2022, 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;
31
32 import com.jcabi.aspects.Immutable;
33 import java.util.List;
34 import java.util.Map;
35
36 /**
37 * RESTful response returned by {@link Request#fetch()}.
38 *
39 * <p>You can get this response from one of implementations of {@link Request}:
40 *
41 * <pre> Response response = new JdkRequest("https://www.google.com")
42 * .header("Accept", "text/html")
43 * .fetch();</pre>
44 *
45 * <p>Instances of this interface are immutable and thread-safe.
46 *
47 * @since 0.8
48 * @see com.jcabi.http.request.JdkRequest
49 */
50 @Immutable
51 public interface Response {
52
53 /**
54 * Get back to the request it's related to.
55 * @return The request we're in
56 */
57 Request back();
58
59 /**
60 * Get status of the response as a positive integer number.
61 * @return The status code
62 */
63 int status();
64
65 /**
66 * Get status line reason phrase.
67 * @return The status line reason phrase
68 */
69 String reason();
70
71 /**
72 * Get a collection of all headers.
73 * @return The headers
74 */
75 Map<String, List<String>> headers();
76
77 /**
78 * Get body as a string, assuming it's {@code UTF-8} (if there is something
79 * else that can't be translated into a UTF-8 string a runtime exception
80 * will be thrown).
81 *
82 * <p><strong>DISCLAIMER</strong>:
83 * The only encoding supported here is UTF-8. If the body of response
84 * contains any chars that can't be used and should be replaced with
85 * a "replacement character", a {@link RuntimeException} will be thrown. If
86 * you need to use some other encodings, use
87 * {@link #binary()} instead.
88 *
89 * @return The body, as a UTF-8 string
90 */
91 String body();
92
93 /**
94 * Raw body as a an array of bytes.
95 * @return The body, as a UTF-8 string
96 */
97 byte[] binary();
98
99 /**
100 * Convert it to another type, by encapsulation.
101 * @param type Type to use
102 * @param <T> Type to use
103 * @return New response
104 */
105 @SuppressWarnings("PMD.ShortMethodName")
106 //@checkstyle MethodName (1 lines)
107 <T extends Response> T as(Class<T> type);
108
109 }