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
46
47
48
49
50
51
52 public final class SimpleHttpResponse extends BasicHttpResponse {
53
54 private static final long serialVersionUID = 1L;
55 private SimpleBody body;
56
57 public SimpleHttpResponse(final int code) {
58 super(code);
59 }
60
61 public SimpleHttpResponse(final int code, final String reasonPhrase) {
62 super(code, reasonPhrase);
63 }
64
65 public static SimpleHttpResponse copy(final HttpResponse original) {
66 Args.notNull(original, "HTTP response");
67 final SimpleHttpResponse copy = new SimpleHttpResponse(original.getCode());
68 copy.setVersion(original.getVersion());
69 for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
70 copy.addHeader(it.next());
71 }
72 return copy;
73 }
74
75 public static SimpleHttpResponse create(final int code) {
76 return new SimpleHttpResponse(code);
77 }
78
79 public static SimpleHttpResponse create(final int code, final String content, final ContentType contentType) {
80 final SimpleHttpResponse response = new SimpleHttpResponse(code);
81 if (content != null) {
82 response.setBody(content, contentType);
83 }
84 return response;
85 }
86
87 public static SimpleHttpResponse create(final int code, final String content) {
88 return create(code, content, ContentType.TEXT_PLAIN);
89 }
90
91 public static SimpleHttpResponse create(final int code, final byte[] content, final ContentType contentType) {
92 final SimpleHttpResponse response = new SimpleHttpResponse(code);
93 if (content != null) {
94 response.setBody(content, contentType);
95 }
96 return response;
97 }
98
99 public static SimpleHttpResponse create(final int code, final byte[] content) {
100 return create(code, content, ContentType.TEXT_PLAIN);
101 }
102
103 public void setBody(final SimpleBody body) {
104 this.body = body;
105 }
106
107 public void setBody(final byte[] bodyBytes, final ContentType contentType) {
108 this.body = SimpleBody.create(bodyBytes, contentType);
109 }
110
111 public void setBody(final String bodyText, final ContentType contentType) {
112 this.body = SimpleBody.create(bodyText, contentType);
113 }
114
115 public SimpleBody getBody() {
116 return body;
117 }
118
119 public ContentType getContentType() {
120 return body != null ? body.getContentType() : null;
121 }
122
123 public String getBodyText() {
124 return body != null ? body.getBodyText() : null;
125 }
126
127 public byte[] getBodyBytes() {
128 return body != null ? body.getBodyBytes() : null;
129 }
130
131 }
132