]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/http/HttpStatus.java
FS utils throws IOException
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / http / HttpStatus.java
1 package org.argeo.cms.http;
2
3 /**
4 * Standard HTTP response status codes (including WebDav ones).
5 *
6 * @see "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"
7 */
8 public enum HttpStatus {
9 // Successful responses (200–299)
10 OK(200, "OK"), //
11 NO_CONTENT(204, "No Content"), //
12 MULTI_STATUS(207, "Multi-Status"), // WebDav
13 // Client error responses (400–499)
14 UNAUTHORIZED(401, "Unauthorized"), //
15 FORBIDDEN(403, "Forbidden"), //
16 NOT_FOUND(404, "Not Found"), //
17 // Server error responses (500-599)
18 INTERNAL_SERVER_ERROR(500, "Internal Server Error"), //
19 NOT_IMPLEMENTED(501, "Not Implemented"), //
20 ;
21
22 private final int code;
23 private final String reasonPhrase;
24
25 HttpStatus(int statusCode, String reasonPhrase) {
26 this.code = statusCode;
27 this.reasonPhrase = reasonPhrase;
28 }
29
30 public int getCode() {
31 return code;
32 }
33
34 public String getReasonPhrase() {
35 return reasonPhrase;
36 }
37
38 /**
39 * The status line, as defined by RFC2616.
40 *
41 * @see "https://www.rfc-editor.org/rfc/rfc2616#section-6.1"
42 */
43 public String getStatusLine(String httpVersion) {
44 return httpVersion + " " + code + " " + reasonPhrase;
45 }
46
47 public static HttpStatus parseStatusLine(String statusLine) {
48 try {
49 String[] arr = statusLine.split(" ");
50 int code = Integer.parseInt(arr[1]);
51 for (HttpStatus status : values()) {
52 if (status.getCode() == code)
53 return status;
54 }
55 } catch (Exception e) {
56 throw new IllegalArgumentException("Invalid status line: " + statusLine, e);
57 }
58 throw new IllegalArgumentException("Unkown status code: " + statusLine);
59 }
60
61 @Override
62 public String toString() {
63 return code + " " + reasonPhrase;
64 }
65
66 }