forked from SeanShubin/hello-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestValue.java
64 lines (57 loc) · 1.93 KB
/
RequestValue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.seanshubin.hello.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
class RequestValue {
final String method;
final String path;
final String query;
final List<Header> headers;
RequestValue(String method, String path, String query, List<Header> headers) {
this.method = method;
this.path = path;
this.query = query;
this.headers = Collections.unmodifiableList(new ArrayList<>(headers));
}
@Override
public String toString() {
return "RequestValue{" +
"method='" + method + '\'' +
", path='" + path + '\'' +
", query='" + query + '\'' +
", headers=" + headers +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RequestValue that = (RequestValue) o;
return Objects.equals(method, that.method) &&
Objects.equals(path, that.path) &&
Objects.equals(query, that.query) &&
Objects.equals(headers, that.headers);
}
@Override
public int hashCode() {
return Objects.hash(method, path, query, headers);
}
String singleQueryParameter(String name) {
String[] pairs = query.split("&");
List<String> matches = new ArrayList<>();
for (String pair : pairs) {
String[] keyValue = pair.split("=");
String key = keyValue[0];
if (name.equals(key)) {
String value = keyValue[1];
matches.add(value);
}
}
if (matches.size() == 1) {
return matches.get(0);
} else {
throw new RuntimeException(String.format("Expected exactly one parameter matching '%s', got %d", name, matches.size()));
}
}
}