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