forked from alibaba/jvm-sandbox-repeater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identity.java
147 lines (125 loc) · 4.06 KB
/
Identity.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.alibaba.jvm.sandbox.repeater.plugin.domain;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Map;
/**
* {@link Identity} 定义一次{@link Invocation}的标志;
* <p>
*
* @author zhaoyb1990
*/
public class Identity implements java.io.Serializable{
private final static String QUERY_STRING_COLLECTOR = "?";
private final static String KEY_VALUE_SPLITTER = "&";
private final static String KEY_VALUE_COLLECTOR = "=";
private final static String HOST_SPLITTER = "://";
private final static String LOCATION_SPLITTER = "/";
/**
* 唯一资源定位标志
*/
private volatile String uri;
private transient String scheme;
private transient String location;
private transient String endpoint;
private transient Map<String, String> extra;
private Identity() {}
public Identity(String scheme, String location, String endpoint, Map<String, String> extra) {
this.scheme = scheme;
this.location = location;
this.endpoint = endpoint;
this.extra = extra;
StringBuilder sb = new StringBuilder();
sb.append(scheme).append(HOST_SPLITTER).append(Joiner.on("/").join(location, endpoint));
if (extra != null && !extra.isEmpty()) {
boolean firstKey = true;
for (Map.Entry<String, String> entry : extra.entrySet()) {
if (firstKey) {
firstKey = false;
sb.append(QUERY_STRING_COLLECTOR);
} else {
sb.append(KEY_VALUE_SPLITTER);
}
sb.append(entry.getKey()).append(KEY_VALUE_COLLECTOR).append(entry.getValue());
}
}
this.uri = sb.toString();
}
public Identity(String uri) {
this.uri = uri;
updateIfNecessary();
}
public String getUri() {
updateIfNecessary();
return uri;
}
public String getScheme() {
updateIfNecessary();
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
public String getLocation() {
updateIfNecessary();
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getEndpoint() {
updateIfNecessary();
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public Map<String, String> getExtra() {
return extra;
}
public void setExtra(Map<String, String> extra) {
this.extra = extra;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Identity) {
Identity identity = (Identity) obj;
return this.uri.equals(identity.uri);
} else {
return false;
}
}
private void update() {
try {
URI parsed = new URI(uri);
scheme = parsed.getScheme();
location = parsed.getAuthority();
endpoint = parsed.getPath();
String queryString = parsed.getQuery();
if (null != queryString) {
extra = Splitter.on(KEY_VALUE_SPLITTER)
.trimResults()
.withKeyValueSeparator(KEY_VALUE_COLLECTOR)
.split(queryString);
}
} catch (URISyntaxException e) {
String[] schemeSplit = uri.split(HOST_SPLITTER);
scheme = schemeSplit[0];
if (schemeSplit.length > 1) {
location = schemeSplit[1].split(LOCATION_SPLITTER)[0];
endpoint = schemeSplit[1].replace(location, "");
}
}
}
private void updateIfNecessary() {
if (scheme == null || location == null || endpoint == null) {
update();
}
}
@Override
public int hashCode() {
return Arrays.hashCode(new Object[]{uri, scheme, location, endpoint, extra});
}
}