1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.async.methods;
29
30 import java.util.Iterator;
31
32 import org.apache.hc.core5.http.ContentType;
33 import org.apache.hc.core5.http.Header;
34 import org.apache.hc.core5.http.HttpResponse;
35 import org.apache.hc.core5.http.message.BasicHttpResponse;
36 import org.apache.hc.core5.util.Args;
37
38
39
40
41
42
43
44
45 public final class SimpleHttpResponse extends BasicHttpResponse {
46
47 private static final long serialVersionUID = 1L;
48 private SimpleBody body;
49
50 public SimpleHttpResponse(final int code) {
51 super(code);
52 }
53
54 public SimpleHttpResponse(final int code, final String reasonPhrase) {
55 super(code, reasonPhrase);
56 }
57
58 public static SimpleHttpResponse copy(final HttpResponse original) {
59 Args.notNull(original, "HTTP response");
60 final SimpleHttpResponseync/methods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse copy = new SimpleHttpResponse(original.getCode());
61 copy.setVersion(original.getVersion());
62 for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
63 copy.addHeader(it.next());
64 }
65 return copy;
66 }
67
68 public static SimpleHttpResponse create(final int code) {
69 return new SimpleHttpResponse(code);
70 }
71
72 public static SimpleHttpResponse create(final int code, final String content, final ContentType contentType) {
73 final SimpleHttpResponsemethods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse response = new SimpleHttpResponse(code);
74 if (content != null) {
75 response.setBody(content, contentType);
76 }
77 return response;
78 }
79
80 public static SimpleHttpResponse create(final int code, final String content) {
81 return create(code, content, ContentType.TEXT_PLAIN);
82 }
83
84 public static SimpleHttpResponse create(final int code, final byte[] content, final ContentType contentType) {
85 final SimpleHttpResponsemethods/SimpleHttpResponse.html#SimpleHttpResponse">SimpleHttpResponse response = new SimpleHttpResponse(code);
86 if (content != null) {
87 response.setBody(content, contentType);
88 }
89 return response;
90 }
91
92 public static SimpleHttpResponse create(final int code, final byte[] content) {
93 return create(code, content, ContentType.TEXT_PLAIN);
94 }
95
96 public void setBody(final SimpleBody body) {
97 this.body = body;
98 }
99
100 public void setBody(final byte[] bodyBytes, final ContentType contentType) {
101 this.body = SimpleBody.create(bodyBytes, contentType);
102 }
103
104 public void setBody(final String bodyText, final ContentType contentType) {
105 this.body = SimpleBody.create(bodyText, contentType);
106 }
107
108 public SimpleBody getBody() {
109 return body;
110 }
111
112 public ContentType getContentType() {
113 return body != null ? body.getContentType() : null;
114 }
115
116 public String getBodyText() {
117 return body != null ? body.getBodyText() : null;
118 }
119
120 public byte[] getBodyBytes() {
121 return body != null ? body.getBodyBytes() : null;
122 }
123
124 }
125