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.core5.http.message;
29
30 import java.util.Locale;
31
32 import org.apache.hc.core5.http.HttpResponse;
33 import org.apache.hc.core5.http.ProtocolVersion;
34 import org.apache.hc.core5.http.ReasonPhraseCatalog;
35 import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
36 import org.apache.hc.core5.util.Args;
37 import org.apache.hc.core5.util.TextUtils;
38
39
40
41
42
43
44 public class BasicHttpResponse extends HeaderGroup implements HttpResponse {
45
46 private static final long serialVersionUID = 1L;
47
48 private final ReasonPhraseCatalog reasonCatalog;
49
50 private ProtocolVersion version;
51 private Locale locale;
52 private int code;
53 private String reasonPhrase;
54
55
56
57
58
59
60
61
62
63
64
65 public BasicHttpResponse(
66 final int code,
67 final ReasonPhraseCatalog catalog,
68 final Locale locale) {
69 super();
70 this.code = Args.positive(code, "Status code");
71 this.reasonCatalog = catalog != null ? catalog : EnglishReasonPhraseCatalog.INSTANCE;
72 this.locale = locale;
73 }
74
75
76
77
78
79
80
81 public BasicHttpResponse(final int code, final String reasonPhrase) {
82 this.code = Args.positive(code, "Status code");
83 this.reasonPhrase = reasonPhrase;
84 this.reasonCatalog = EnglishReasonPhraseCatalog.INSTANCE;
85 }
86
87
88
89
90
91
92 public BasicHttpResponse(final int code) {
93 this.code = Args.positive(code, "Status code");
94 this.reasonPhrase = null;
95 this.reasonCatalog = EnglishReasonPhraseCatalog.INSTANCE;
96 }
97
98 @Override
99 public void addHeader(final String name, final Object value) {
100 Args.notNull(name, "Header name");
101 addHeader(new BasicHeader(name, value));
102 }
103
104 @Override
105 public void setHeader(final String name, final Object value) {
106 Args.notNull(name, "Header name");
107 setHeader(new BasicHeader(name, value));
108 }
109
110 @Override
111 public void setVersion(final ProtocolVersion version) {
112 this.version = version;
113 }
114
115 @Override
116 public ProtocolVersion getVersion() {
117 return this.version;
118 }
119
120 @Override
121 public int getCode() {
122 return this.code;
123 }
124
125 @Override
126 public Locale getLocale() {
127 return this.locale;
128 }
129
130 @Override
131 public void setCode(final int code) {
132 Args.positive(code, "Status code");
133 this.code = code;
134 this.reasonPhrase = null;
135 }
136
137 @Override
138 public String getReasonPhrase() {
139 return this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code);
140 }
141
142 @Override
143 public void setReasonPhrase(final String reason) {
144 this.reasonPhrase = TextUtils.isBlank(reason) ? null : reason;
145 }
146
147 @Override
148 public void setLocale(final Locale locale) {
149 this.locale = Args.notNull(locale, "Locale");
150 }
151
152
153
154
155
156
157
158
159
160
161 protected String getReason(final int code) {
162 return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
163 this.locale != null ? this.locale : Locale.getDefault()) : null;
164 }
165
166 @Override
167 public String toString() {
168 final StringBuilder sb = new StringBuilder();
169 sb.append(this.code).append(' ').append(this.reasonPhrase).append(' ').append(this.version);
170 return sb.toString();
171 }
172
173 }