/
Method.java
234 lines (205 loc) · 8.01 KB
/
Method.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
*/
package net.linkfluence.jspore;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.ning.http.client.RequestBuilder;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Describe a route specification.
*
* @author Nicolas Yzet <nyzet@linkfluence.net>
*/
public class Method {
private final static String VAR_PREFIX = ":";
private final static Pattern VAR_PATTERN = Pattern.compile("(" + VAR_PREFIX + "\\w+)");
private final String pathFormat;
public final String name;
public final String path;
public final String httpMethod;
public final ImmutableSet<Integer> expectedStatus;
public final ImmutableSet<String> requiredParams;
public final ImmutableSet<String> optionalParams;
public final ImmutableMap<String, String> headers;
public final ImmutableList<String> pathVarNames;
public final boolean authentication;
protected Method(String name, String path, String httpMethod,
Iterable<Integer> expectedStatus,
Iterable<String> requiredParams,
Iterable<String> optionalParams,
Map<String, String> headers,
boolean authentication) {
this.authentication = authentication;
this.name = name;
this.path = path;
this.httpMethod = httpMethod.toUpperCase();
this.expectedStatus = new ImmutableSet.Builder<Integer>()
.addAll(expectedStatus)
.build();
this.requiredParams = new ImmutableSet.Builder<String>()
.addAll(requiredParams)
.build();
this.optionalParams = new ImmutableSet.Builder<String>()
.addAll(optionalParams)
.build();
this.headers = new ImmutableMap.Builder<String, String>()
.putAll(headers)
.build();
Matcher variableMatcher = VAR_PATTERN.matcher(path);
List<String> varNames = new ArrayList<String>();
int fromIndex = 0;
String expr = path;
while (variableMatcher.find()) {
String varName = variableMatcher.group(1);
int varIdx = expr.indexOf(varName);
expr = expr.substring(0, varIdx) + "%s" + expr.substring(varIdx + varName.length());
varNames.add(varName.substring(1));
}
this.pathFormat = expr;
this.pathVarNames = new ImmutableList.Builder<String>()
.addAll(varNames)
.build();
}
private RequestBuilder buildRequestImpl(String baseUrl, Map<String, String> params) throws SporeException {
RequestBuilder builder = new RequestBuilder();
builder.setMethod(this.httpMethod);
Set<String> checkRequired = new HashSet<String>(this.requiredParams);
// fill path parameters
Object[] pathParams = new String[pathVarNames.size()];
Arrays.fill(pathParams, "");
for (int i = 0; i < pathVarNames.size(); i++) {
String val = params.get(pathVarNames.get(i));
if (val != null) {
pathParams[i] = val;
checkRequired.remove(pathVarNames.get(i));
}
}
String url;
String reqPath = String.format(this.pathFormat, pathParams);
if (baseUrl.endsWith("/") && reqPath.startsWith("/")) {
url = baseUrl + reqPath.substring(1);
} else {
url = baseUrl + reqPath;
}
builder.setUrl(url);
// add query parameter
for (Entry<String, String> e : params.entrySet()) {
builder.addParameter(e.getKey(), e.getValue());
checkRequired.remove(e.getKey());
}
if (!checkRequired.isEmpty()) {
Joiner joiner = Joiner.on(',');
throw new SporeException("Missing required parameters: "
+ joiner.join(checkRequired));
}
return builder;
}
public RequestBuilder buildRequest(String baseUrl, Map<String, String> params, String payload) throws SporeException {
RequestBuilder builder = buildRequestImpl(baseUrl, params);
if (!Strings.isNullOrEmpty(payload)) {
builder.setBody(payload);
}
return builder;
}
public RequestBuilder buildRequest(String baseUrl, Map<String, String> params, Object payload) throws SporeException {
RequestBuilder builder = buildRequestImpl(baseUrl, params);
if (payload != null) {
if (!Strings.isNullOrEmpty(payload.toString())) {
builder.setBody(payload.toString());
}
}
return builder;
}
public RequestBuilder buildRequest(String baseUrl, Map<String, String> params, byte[] payload) throws SporeException {
RequestBuilder builder = buildRequestImpl(baseUrl, params);
if (payload != null) {
builder.setBody(payload);
}
return builder;
}
public static class Builder {
public final String name;
public final String path;
public final String httpMethod;
private final Set<Integer> expectedStatus;
private final Set<String> requiredParams;
private final Set<String> optionalParams;
private final Map<String, String> headers;
private boolean authentication = false;
public Builder(String name, String path, String httpMethod) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
Preconditions.checkArgument(!Strings.isNullOrEmpty(httpMethod));
this.name = name;
this.path = path;
this.httpMethod = httpMethod.toUpperCase();
this.expectedStatus = new HashSet<Integer>();
this.optionalParams = new HashSet<String>();
this.requiredParams = new HashSet<String>();
this.headers = new HashMap<String, String>();
}
public Builder addExpectedStatus(int status) {
expectedStatus.add(status);
return this;
}
public Builder addOptionalParam(String param) {
optionalParams.add(param);
return this;
}
public Builder addRequiredParam(String param) {
requiredParams.add(param);
return this;
}
public Builder addRequiredParams(Collection<String> params) {
for (String param : params) {
addRequiredParam(param);
}
return this;
}
public Builder addOptionalParams(Collection<String> params) {
for (String param : params) {
addOptionalParam(param);
}
return this;
}
public Builder addExpectedStatuses(Collection<Integer> statuses) {
for (int status : statuses) {
addExpectedStatus(status);
}
return this;
}
/**
* if key exists replace with new value
* @param key
* @param value
*/
public Builder addHeader(String key, String value) {
this.headers.put(key, value);
return this;
}
/**
* If key already exist, replace with new value.
* @param headers
*/
public Builder addHeaders(Map<String, String> headers) {
this.headers.putAll(headers);
return this;
}
public Builder setAuthentication(boolean auth) {
this.authentication = auth;
return this;
}
public Method build(){
return new Method(this.name, this.path, this.httpMethod,
this.expectedStatus, this.requiredParams, this.optionalParams,
this.headers, this.authentication);
}
}
}