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.http.message;
29
30 import java.util.Locale;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpVersion;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.ReasonPhraseCatalog;
37 import org.apache.http.StatusLine;
38 import org.apache.http.util.Args;
39 import org.apache.http.util.TextUtils;
40
41
42
43
44
45
46
47
48 public class BasicHttpResponse extends AbstractHttpMessage implements HttpResponse {
49
50 private StatusLine statusline;
51 private ProtocolVersion ver;
52 private int code;
53 private String reasonPhrase;
54 private HttpEntity entity;
55 private final ReasonPhraseCatalog reasonCatalog;
56 private Locale locale;
57
58
59
60
61
62
63
64
65
66
67
68
69 public BasicHttpResponse(final StatusLine statusline,
70 final ReasonPhraseCatalog catalog,
71 final Locale locale) {
72 super();
73 this.statusline = Args.notNull(statusline, "Status line");
74 this.ver = statusline.getProtocolVersion();
75 this.code = statusline.getStatusCode();
76 this.reasonPhrase = statusline.getReasonPhrase();
77 this.reasonCatalog = catalog;
78 this.locale = locale;
79 }
80
81
82
83
84
85
86
87
88 public BasicHttpResponse(final StatusLine statusline) {
89 super();
90 this.statusline = Args.notNull(statusline, "Status line");
91 this.ver = statusline.getProtocolVersion();
92 this.code = statusline.getStatusCode();
93 this.reasonPhrase = statusline.getReasonPhrase();
94 this.reasonCatalog = null;
95 this.locale = null;
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public BasicHttpResponse(final ProtocolVersion ver,
109 final int code,
110 final String reason) {
111 super();
112 Args.notNegative(code, "Status code");
113 this.statusline = null;
114 this.ver = ver;
115 this.code = code;
116 this.reasonPhrase = reason;
117 this.reasonCatalog = null;
118 this.locale = null;
119 }
120
121
122
123 @Override
124 public ProtocolVersion getProtocolVersion() {
125 return this.ver;
126 }
127
128
129 @Override
130 public StatusLine getStatusLine() {
131 if (this.statusline == null) {
132 this.statusline = new BasicStatusLine(
133 this.ver != null ? this.ver : HttpVersion.HTTP_1_1,
134 this.code,
135 this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code));
136 }
137 return this.statusline;
138 }
139
140
141 @Override
142 public HttpEntity getEntity() {
143 return this.entity;
144 }
145
146 @Override
147 public Locale getLocale() {
148 return this.locale;
149 }
150
151
152 @Override
153 public void setStatusLine(final StatusLine statusline) {
154 this.statusline = Args.notNull(statusline, "Status line");
155 this.ver = statusline.getProtocolVersion();
156 this.code = statusline.getStatusCode();
157 this.reasonPhrase = statusline.getReasonPhrase();
158 }
159
160
161 @Override
162 public void setStatusLine(final ProtocolVersion ver, final int code) {
163 Args.notNegative(code, "Status code");
164 this.statusline = null;
165 this.ver = ver;
166 this.code = code;
167 this.reasonPhrase = null;
168 }
169
170
171 @Override
172 public void setStatusLine(
173 final ProtocolVersion ver, final int code, final String reason) {
174 Args.notNegative(code, "Status code");
175 this.statusline = null;
176 this.ver = ver;
177 this.code = code;
178 this.reasonPhrase = reason;
179 }
180
181
182 @Override
183 public void setStatusCode(final int code) {
184 Args.notNegative(code, "Status code");
185 this.statusline = null;
186 this.code = code;
187 this.reasonPhrase = null;
188 }
189
190
191 @Override
192 public void setReasonPhrase(final String reason) {
193 this.statusline = null;
194 this.reasonPhrase = TextUtils.isBlank(reason) ? null : reason;
195 }
196
197
198 @Override
199 public void setEntity(final HttpEntity entity) {
200 this.entity = entity;
201 }
202
203 @Override
204 public void setLocale(final Locale locale) {
205 this.locale = Args.notNull(locale, "Locale");
206 this.statusline = null;
207 }
208
209
210
211
212
213
214
215
216
217
218 protected String getReason(final int code) {
219 return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
220 this.locale != null ? this.locale : Locale.getDefault()) : null;
221 }
222
223 @Override
224 public String toString() {
225 final StringBuilder sb = new StringBuilder();
226 sb.append(getStatusLine());
227 sb.append(' ');
228 sb.append(this.headergroup);
229 if (this.entity != null) {
230 sb.append(' ');
231 sb.append(this.entity);
232 }
233 return sb.toString();
234 }
235
236 }