View Javadoc
1   package com.zone.weixin4j.exception;
2   
3   /**
4    * Created by Yz on 2017/3/15.
5    * HttpResponseException  --  异常类
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           * 100 Continue
29           */
30          public static final HttpResponseStatus CONTINUE = newStatus(100, "Continue");
31  
32          /**
33           * 101 Switching Protocols
34           */
35          public static final HttpResponseStatus SWITCHING_PROTOCOLS = newStatus(101, "Switching Protocols");
36  
37          /**
38           * 102 Processing (WebDAV, RFC2518)
39           */
40          public static final HttpResponseStatus PROCESSING = newStatus(102, "Processing");
41  
42          /**
43           * 200 OK
44           */
45          public static final HttpResponseStatus OK = newStatus(200, "OK");
46  
47          /**
48           * 201 Created
49           */
50          public static final HttpResponseStatus CREATED = newStatus(201, "Created");
51  
52          /**
53           * 202 Accepted
54           */
55          public static final HttpResponseStatus ACCEPTED = newStatus(202, "Accepted");
56  
57          /**
58           * 203 Non-Authoritative Information (since HTTP/1.1)
59           */
60          public static final HttpResponseStatus NON_AUTHORITATIVE_INFORMATION =
61                  newStatus(203, "Non-Authoritative Information");
62  
63          /**
64           * 204 No Content
65           */
66          public static final HttpResponseStatus NO_CONTENT = newStatus(204, "No Content");
67  
68          /**
69           * 205 Reset Content
70           */
71          public static final HttpResponseStatus RESET_CONTENT = newStatus(205, "Reset Content");
72  
73          /**
74           * 206 Partial Content
75           */
76          public static final HttpResponseStatus PARTIAL_CONTENT = newStatus(206, "Partial Content");
77  
78          /**
79           * 207 Multi-Status (WebDAV, RFC2518)
80           */
81          public static final HttpResponseStatus MULTI_STATUS = newStatus(207, "Multi-Status");
82  
83          /**
84           * 300 Multiple Choices
85           */
86          public static final HttpResponseStatus MULTIPLE_CHOICES = newStatus(300, "Multiple Choices");
87  
88          /**
89           * 301 Moved Permanently
90           */
91          public static final HttpResponseStatus MOVED_PERMANENTLY = newStatus(301, "Moved Permanently");
92  
93          /**
94           * 302 Found
95           */
96          public static final HttpResponseStatus FOUND = newStatus(302, "Found");
97  
98          /**
99           * 303 See Other (since HTTP/1.1)
100          */
101         public static final HttpResponseStatus SEE_OTHER = newStatus(303, "See Other");
102 
103         /**
104          * 304 Not Modified
105          */
106         public static final HttpResponseStatus NOT_MODIFIED = newStatus(304, "Not Modified");
107 
108         /**
109          * 305 Use Proxy (since HTTP/1.1)
110          */
111         public static final HttpResponseStatus USE_PROXY = newStatus(305, "Use Proxy");
112 
113         /**
114          * 307 Temporary Redirect (since HTTP/1.1)
115          */
116         public static final HttpResponseStatus TEMPORARY_REDIRECT = newStatus(307, "Temporary Redirect");
117 
118         /**
119          * 400 Bad Request
120          */
121         public static final HttpResponseStatus BAD_REQUEST = newStatus(400, "Bad Request");
122 
123         /**
124          * 401 Unauthorized
125          */
126         public static final HttpResponseStatus UNAUTHORIZED = newStatus(401, "Unauthorized");
127 
128         /**
129          * 402 Payment Required
130          */
131         public static final HttpResponseStatus PAYMENT_REQUIRED = newStatus(402, "Payment Required");
132 
133         /**
134          * 403 Forbidden
135          */
136         public static final HttpResponseStatus FORBIDDEN = newStatus(403, "Forbidden");
137 
138         /**
139          * 404 Not Found
140          */
141         public static final HttpResponseStatus NOT_FOUND = newStatus(404, "Not Found");
142 
143         /**
144          * 405 Method Not Allowed
145          */
146         public static final HttpResponseStatus METHOD_NOT_ALLOWED = newStatus(405, "Method Not Allowed");
147 
148         /**
149          * 406 Not Acceptable
150          */
151         public static final HttpResponseStatus NOT_ACCEPTABLE = newStatus(406, "Not Acceptable");
152 
153         /**
154          * 407 Proxy Authentication Required
155          */
156         public static final HttpResponseStatus PROXY_AUTHENTICATION_REQUIRED =
157                 newStatus(407, "Proxy Authentication Required");
158 
159         /**
160          * 408 Request Timeout
161          */
162         public static final HttpResponseStatus REQUEST_TIMEOUT = newStatus(408, "Request Timeout");
163 
164         /**
165          * 409 Conflict
166          */
167         public static final HttpResponseStatus CONFLICT = newStatus(409, "Conflict");
168 
169         /**
170          * 410 Gone
171          */
172         public static final HttpResponseStatus GONE = newStatus(410, "Gone");
173 
174         /**
175          * 411 Length Required
176          */
177         public static final HttpResponseStatus LENGTH_REQUIRED = newStatus(411, "Length Required");
178 
179         /**
180          * 412 Precondition Failed
181          */
182         public static final HttpResponseStatus PRECONDITION_FAILED = newStatus(412, "Precondition Failed");
183 
184         /**
185          * 413 Request Entity Too Large
186          */
187         public static final HttpResponseStatus REQUEST_ENTITY_TOO_LARGE =
188                 newStatus(413, "Request Entity Too Large");
189 
190         /**
191          * 414 Request-URI Too Long
192          */
193         public static final HttpResponseStatus REQUEST_URI_TOO_LONG = newStatus(414, "Request-URI Too Long");
194 
195         /**
196          * 415 Unsupported Media Type
197          */
198         public static final HttpResponseStatus UNSUPPORTED_MEDIA_TYPE = newStatus(415, "Unsupported Media Type");
199 
200         /**
201          * 416 Requested Range Not Satisfiable
202          */
203         public static final HttpResponseStatus REQUESTED_RANGE_NOT_SATISFIABLE =
204                 newStatus(416, "Requested Range Not Satisfiable");
205 
206         /**
207          * 417 Expectation Failed
208          */
209         public static final HttpResponseStatus EXPECTATION_FAILED = newStatus(417, "Expectation Failed");
210 
211         /**
212          * 421 Misdirected Request
213          * <p>
214          * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-http2-15#section-9.1.2">421 Status Code</a>
215          */
216         public static final HttpResponseStatus MISDIRECTED_REQUEST = newStatus(421, "Misdirected Request");
217 
218         /**
219          * 422 Unprocessable Entity (WebDAV, RFC4918)
220          */
221         public static final HttpResponseStatus UNPROCESSABLE_ENTITY = newStatus(422, "Unprocessable Entity");
222 
223         /**
224          * 423 Locked (WebDAV, RFC4918)
225          */
226         public static final HttpResponseStatus LOCKED = newStatus(423, "Locked");
227 
228         /**
229          * 424 Failed Dependency (WebDAV, RFC4918)
230          */
231         public static final HttpResponseStatus FAILED_DEPENDENCY = newStatus(424, "Failed Dependency");
232 
233         /**
234          * 425 Unordered Collection (WebDAV, RFC3648)
235          */
236         public static final HttpResponseStatus UNORDERED_COLLECTION = newStatus(425, "Unordered Collection");
237 
238         /**
239          * 426 Upgrade Required (RFC2817)
240          */
241         public static final HttpResponseStatus UPGRADE_REQUIRED = newStatus(426, "Upgrade Required");
242 
243         /**
244          * 428 Precondition Required (RFC6585)
245          */
246         public static final HttpResponseStatus PRECONDITION_REQUIRED = newStatus(428, "Precondition Required");
247 
248         /**
249          * 429 Too Many Requests (RFC6585)
250          */
251         public static final HttpResponseStatus TOO_MANY_REQUESTS = newStatus(429, "Too Many Requests");
252 
253         /**
254          * 431 Request Header Fields Too Large (RFC6585)
255          */
256         public static final HttpResponseStatus REQUEST_HEADER_FIELDS_TOO_LARGE =
257                 newStatus(431, "Request Header Fields Too Large");
258 
259         /**
260          * 500 Internal Server Error
261          */
262         public static final HttpResponseStatus INTERNAL_SERVER_ERROR = newStatus(500, "Internal Server Error");
263 
264         /**
265          * 501 Not Implemented
266          */
267         public static final HttpResponseStatus NOT_IMPLEMENTED = newStatus(501, "Not Implemented");
268 
269         /**
270          * 502 Bad Gateway
271          */
272         public static final HttpResponseStatus BAD_GATEWAY = newStatus(502, "Bad Gateway");
273 
274         /**
275          * 503 Service Unavailable
276          */
277         public static final HttpResponseStatus SERVICE_UNAVAILABLE = newStatus(503, "Service Unavailable");
278 
279         /**
280          * 504 Gateway Timeout
281          */
282         public static final HttpResponseStatus GATEWAY_TIMEOUT = newStatus(504, "Gateway Timeout");
283 
284         /**
285          * 505 HTTP Version Not Supported
286          */
287         public static final HttpResponseStatus HTTP_VERSION_NOT_SUPPORTED =
288                 newStatus(505, "HTTP Version Not Supported");
289 
290         /**
291          * 506 Variant Also Negotiates (RFC2295)
292          */
293         public static final HttpResponseStatus VARIANT_ALSO_NEGOTIATES = newStatus(506, "Variant Also Negotiates");
294 
295         /**
296          * 507 Insufficient Storage (WebDAV, RFC4918)
297          */
298         public static final HttpResponseStatus INSUFFICIENT_STORAGE = newStatus(507, "Insufficient Storage");
299 
300         /**
301          * 510 Not Extended (RFC2774)
302          */
303         public static final HttpResponseStatus NOT_EXTENDED = newStatus(510, "Not Extended");
304 
305         /**
306          * 511 Network Authentication Required (RFC6585)
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          * Creates a new instance with the specified {@code code} and its {@code reasonPhrase}.
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                 // Check prohibited characters.
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 }