|
24 | 24 | /** An immutable request to an http server. */ |
25 | 25 | public final class Request { |
26 | 26 |
|
| 27 | + public enum HttpMethod { |
| 28 | + GET, |
| 29 | + HEAD, |
| 30 | + POST, |
| 31 | + PUT, |
| 32 | + DELETE, |
| 33 | + CONNECT, |
| 34 | + OPTIONS, |
| 35 | + TRACE, |
| 36 | + PATCH |
| 37 | + } |
| 38 | + |
27 | 39 | /** |
28 | 40 | * No parameters can be null except {@code body} and {@code charset}. All parameters must be |
29 | 41 | * effectively immutable, via safe copies, not mutating or otherwise. |
| 42 | + * |
| 43 | + * @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)} |
30 | 44 | */ |
31 | 45 | public static Request create( |
32 | 46 | String method, |
33 | 47 | String url, |
34 | 48 | Map<String, Collection<String>> headers, |
35 | 49 | byte[] body, |
36 | 50 | Charset charset) { |
37 | | - return new Request(method, url, headers, body, charset); |
| 51 | + checkNotNull(method, "httpMethod of %s", method); |
| 52 | + HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase()); |
| 53 | + return create(httpMethod, url, headers, body, charset); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Builds a Request. All parameters must be effectively immutable, via safe copies. |
| 58 | + * |
| 59 | + * @param httpMethod for the request. |
| 60 | + * @param url for the request. |
| 61 | + * @param headers to include. |
| 62 | + * @param body of the request, can be {@literal null} |
| 63 | + * @param charset of the request, can be {@literal null} |
| 64 | + * @return a Request |
| 65 | + */ |
| 66 | + public static Request create( |
| 67 | + HttpMethod httpMethod, |
| 68 | + String url, |
| 69 | + Map<String, Collection<String>> headers, |
| 70 | + byte[] body, |
| 71 | + Charset charset) { |
| 72 | + return new Request(httpMethod, url, headers, body, charset); |
38 | 73 | } |
39 | 74 |
|
40 | | - private final String method; |
| 75 | + private final HttpMethod httpMethod; |
41 | 76 | private final String url; |
42 | 77 | private final Map<String, Collection<String>> headers; |
43 | 78 | private final byte[] body; |
44 | 79 | private final Charset charset; |
45 | 80 |
|
46 | 81 | Request( |
47 | | - String method, |
| 82 | + HttpMethod method, |
48 | 83 | String url, |
49 | 84 | Map<String, Collection<String>> headers, |
50 | 85 | byte[] body, |
51 | 86 | Charset charset) { |
52 | | - this.method = checkNotNull(method, "method of %s", url); |
| 87 | + this.httpMethod = checkNotNull(method, "httpMethod of %s", method.name()); |
53 | 88 | this.url = checkNotNull(url, "url"); |
54 | 89 | this.headers = checkNotNull(headers, "headers of %s %s", method, url); |
55 | 90 | this.body = body; // nullable |
56 | 91 | this.charset = charset; // nullable |
57 | 92 | } |
58 | 93 |
|
59 | | - /* Method to invoke on the server. */ |
| 94 | + /** |
| 95 | + * Http Method for this request. |
| 96 | + * |
| 97 | + * @return the HttpMethod string |
| 98 | + * @deprecated @see {@link #httpMethod()} |
| 99 | + */ |
60 | 100 | public String method() { |
61 | | - return method; |
| 101 | + return httpMethod.name(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Http Method for the request. |
| 106 | + * |
| 107 | + * @return the HttpMethod. |
| 108 | + */ |
| 109 | + public HttpMethod httpMethod() { |
| 110 | + return this.httpMethod; |
62 | 111 | } |
63 | 112 |
|
64 | 113 | /* Fully resolved URL including query. */ |
@@ -93,7 +142,7 @@ public byte[] body() { |
93 | 142 | @Override |
94 | 143 | public String toString() { |
95 | 144 | StringBuilder builder = new StringBuilder(); |
96 | | - builder.append(method).append(' ').append(url).append(" HTTP/1.1\n"); |
| 145 | + builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n"); |
97 | 146 | for (String field : headers.keySet()) { |
98 | 147 | for (String value : valuesOrEmpty(headers, field)) { |
99 | 148 | builder.append(field).append(": ").append(value).append('\n'); |
|
0 commit comments