1 package com.zone.weixin4j.exception;
2
3
4
5
6
7 public class HttpResponseException extends Exception {
8
9 private static final long serialVersionUID = 1932809035537660697L;
10
11 private HttpResponseStatus httpResponseStatus;
12
13 public HttpResponseException(HttpResponseStatus httpResponseStatus) {
14 this.httpResponseStatus = httpResponseStatus;
15 }
16
17 public HttpResponseStatus getHttpResponseStatus() {
18 return httpResponseStatus;
19 }
20
21 public void setHttpResponseStatus(HttpResponseStatus httpResponseStatus) {
22 this.httpResponseStatus = httpResponseStatus;
23 }
24
25 public static class HttpResponseStatus {
26
27
28
29
30 public static final HttpResponseStatus CONTINUE = newStatus(100, "Continue");
31
32
33
34
35 public static final HttpResponseStatus SWITCHING_PROTOCOLS = newStatus(101, "Switching Protocols");
36
37
38
39
40 public static final HttpResponseStatus PROCESSING = newStatus(102, "Processing");
41
42
43
44
45 public static final HttpResponseStatus OK = newStatus(200, "OK");
46
47
48
49
50 public static final HttpResponseStatus CREATED = newStatus(201, "Created");
51
52
53
54
55 public static final HttpResponseStatus ACCEPTED = newStatus(202, "Accepted");
56
57
58
59
60 public static final HttpResponseStatus NON_AUTHORITATIVE_INFORMATION =
61 newStatus(203, "Non-Authoritative Information");
62
63
64
65
66 public static final HttpResponseStatus NO_CONTENT = newStatus(204, "No Content");
67
68
69
70
71 public static final HttpResponseStatus RESET_CONTENT = newStatus(205, "Reset Content");
72
73
74
75
76 public static final HttpResponseStatus PARTIAL_CONTENT = newStatus(206, "Partial Content");
77
78
79
80
81 public static final HttpResponseStatus MULTI_STATUS = newStatus(207, "Multi-Status");
82
83
84
85
86 public static final HttpResponseStatus MULTIPLE_CHOICES = newStatus(300, "Multiple Choices");
87
88
89
90
91 public static final HttpResponseStatus MOVED_PERMANENTLY = newStatus(301, "Moved Permanently");
92
93
94
95
96 public static final HttpResponseStatus FOUND = newStatus(302, "Found");
97
98
99
100
101 public static final HttpResponseStatus SEE_OTHER = newStatus(303, "See Other");
102
103
104
105
106 public static final HttpResponseStatus NOT_MODIFIED = newStatus(304, "Not Modified");
107
108
109
110
111 public static final HttpResponseStatus USE_PROXY = newStatus(305, "Use Proxy");
112
113
114
115
116 public static final HttpResponseStatus TEMPORARY_REDIRECT = newStatus(307, "Temporary Redirect");
117
118
119
120
121 public static final HttpResponseStatus BAD_REQUEST = newStatus(400, "Bad Request");
122
123
124
125
126 public static final HttpResponseStatus UNAUTHORIZED = newStatus(401, "Unauthorized");
127
128
129
130
131 public static final HttpResponseStatus PAYMENT_REQUIRED = newStatus(402, "Payment Required");
132
133
134
135
136 public static final HttpResponseStatus FORBIDDEN = newStatus(403, "Forbidden");
137
138
139
140
141 public static final HttpResponseStatus NOT_FOUND = newStatus(404, "Not Found");
142
143
144
145
146 public static final HttpResponseStatus METHOD_NOT_ALLOWED = newStatus(405, "Method Not Allowed");
147
148
149
150
151 public static final HttpResponseStatus NOT_ACCEPTABLE = newStatus(406, "Not Acceptable");
152
153
154
155
156 public static final HttpResponseStatus PROXY_AUTHENTICATION_REQUIRED =
157 newStatus(407, "Proxy Authentication Required");
158
159
160
161
162 public static final HttpResponseStatus REQUEST_TIMEOUT = newStatus(408, "Request Timeout");
163
164
165
166
167 public static final HttpResponseStatus CONFLICT = newStatus(409, "Conflict");
168
169
170
171
172 public static final HttpResponseStatus GONE = newStatus(410, "Gone");
173
174
175
176
177 public static final HttpResponseStatus LENGTH_REQUIRED = newStatus(411, "Length Required");
178
179
180
181
182 public static final HttpResponseStatus PRECONDITION_FAILED = newStatus(412, "Precondition Failed");
183
184
185
186
187 public static final HttpResponseStatus REQUEST_ENTITY_TOO_LARGE =
188 newStatus(413, "Request Entity Too Large");
189
190
191
192
193 public static final HttpResponseStatus REQUEST_URI_TOO_LONG = newStatus(414, "Request-URI Too Long");
194
195
196
197
198 public static final HttpResponseStatus UNSUPPORTED_MEDIA_TYPE = newStatus(415, "Unsupported Media Type");
199
200
201
202
203 public static final HttpResponseStatus REQUESTED_RANGE_NOT_SATISFIABLE =
204 newStatus(416, "Requested Range Not Satisfiable");
205
206
207
208
209 public static final HttpResponseStatus EXPECTATION_FAILED = newStatus(417, "Expectation Failed");
210
211
212
213
214
215
216 public static final HttpResponseStatus MISDIRECTED_REQUEST = newStatus(421, "Misdirected Request");
217
218
219
220
221 public static final HttpResponseStatus UNPROCESSABLE_ENTITY = newStatus(422, "Unprocessable Entity");
222
223
224
225
226 public static final HttpResponseStatus LOCKED = newStatus(423, "Locked");
227
228
229
230
231 public static final HttpResponseStatus FAILED_DEPENDENCY = newStatus(424, "Failed Dependency");
232
233
234
235
236 public static final HttpResponseStatus UNORDERED_COLLECTION = newStatus(425, "Unordered Collection");
237
238
239
240
241 public static final HttpResponseStatus UPGRADE_REQUIRED = newStatus(426, "Upgrade Required");
242
243
244
245
246 public static final HttpResponseStatus PRECONDITION_REQUIRED = newStatus(428, "Precondition Required");
247
248
249
250
251 public static final HttpResponseStatus TOO_MANY_REQUESTS = newStatus(429, "Too Many Requests");
252
253
254
255
256 public static final HttpResponseStatus REQUEST_HEADER_FIELDS_TOO_LARGE =
257 newStatus(431, "Request Header Fields Too Large");
258
259
260
261
262 public static final HttpResponseStatus INTERNAL_SERVER_ERROR = newStatus(500, "Internal Server Error");
263
264
265
266
267 public static final HttpResponseStatus NOT_IMPLEMENTED = newStatus(501, "Not Implemented");
268
269
270
271
272 public static final HttpResponseStatus BAD_GATEWAY = newStatus(502, "Bad Gateway");
273
274
275
276
277 public static final HttpResponseStatus SERVICE_UNAVAILABLE = newStatus(503, "Service Unavailable");
278
279
280
281
282 public static final HttpResponseStatus GATEWAY_TIMEOUT = newStatus(504, "Gateway Timeout");
283
284
285
286
287 public static final HttpResponseStatus HTTP_VERSION_NOT_SUPPORTED =
288 newStatus(505, "HTTP Version Not Supported");
289
290
291
292
293 public static final HttpResponseStatus VARIANT_ALSO_NEGOTIATES = newStatus(506, "Variant Also Negotiates");
294
295
296
297
298 public static final HttpResponseStatus INSUFFICIENT_STORAGE = newStatus(507, "Insufficient Storage");
299
300
301
302
303 public static final HttpResponseStatus NOT_EXTENDED = newStatus(510, "Not Extended");
304
305
306
307
308 public static final HttpResponseStatus NETWORK_AUTHENTICATION_REQUIRED =
309 newStatus(511, "Network Authentication Required");
310
311 private static HttpResponseStatus newStatus(int statusCode, String reasonPhrase) {
312 return new HttpResponseStatus(statusCode, reasonPhrase, true);
313 }
314
315 private final int code;
316
317 private final String reasonPhrase;
318
319 public int getCode() {
320 return code;
321 }
322
323 public String getReasonPhrase() {
324 return reasonPhrase;
325 }
326
327
328
329
330
331 private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) {
332 if (code < 0) {
333 throw new IllegalArgumentException(
334 "code: " + code + " (expected: 0+)");
335 }
336
337 if (reasonPhrase == null) {
338 throw new NullPointerException("reasonPhrase");
339 }
340
341 for (int i = 0; i < reasonPhrase.length(); i++) {
342 char c = reasonPhrase.charAt(i);
343
344 switch (c) {
345 case '\n':
346 case '\r':
347 throw new IllegalArgumentException(
348 "reasonPhrase contains one of the following prohibited characters: " +
349 "\\r\\n: " + reasonPhrase);
350 }
351 }
352
353 this.code = code;
354 this.reasonPhrase = reasonPhrase;
355 }
356 }
357
358
359 }