-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreamClient.java
350 lines (293 loc) · 12.8 KB
/
StreamClient.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.workfront.api;
/*
* Copyright (c) 2011 AtTask, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.HostnameVerifier; // located in jsee.jar which is located in your jre/lib directory
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class StreamClient {
private static final String METH_DELETE = "DELETE";
private static final String METH_GET = "GET";
private static final String METH_POST = "POST";
private static final String METH_PUT = "PUT";
private static final String PATH_LOGIN = "/login";
private static final String PATH_LOGOUT = "/logout";
private static final String PATH_SEARCH = "/search";
private static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public static final String NEW_LINE = "\r\n";
private String hostname;
private String apiKey;
private String sessionID;
public StreamClient (String hostname) {
this.hostname = hostname;
}
public StreamClient (String hostname, String apiKey) {
this.hostname = hostname;
this.apiKey = apiKey;
}
public JSONObject login (String username, String password) throws StreamClientException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("username", username);
params.put("password", password);
return (JSONObject) request(PATH_LOGIN, params, null, METH_GET);
}
public boolean logout () throws StreamClientException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("sessionID", sessionID);
JSONObject result = (JSONObject) request(PATH_LOGOUT, params, null, METH_GET);
try {
return result.getBoolean("success");
}
catch (JSONException e) {
throw new StreamClientException(e);
}
}
public JSONObject uploadUrl(String url) throws StreamClientException {
Map<String, Object> query = new HashMap<String, Object>();
query.put("uri",url);
return (JSONObject) request("/upload",query, null, METH_GET);
}
public JSONObject export (Map<String, Object> query) throws StreamClientException {
return (JSONObject) request("/", query, null, METH_GET);
}
public JSONArray search (String objCode, Map<String, Object> query) throws StreamClientException {
return search(objCode, query, (Set<String>) null);
}
public JSONArray search (String objCode, Map<String, Object> query, String[] fields) throws StreamClientException {
return search(objCode, query, new HashSet<String>(Arrays.asList(fields)));
}
public JSONArray search (String objCode, Map<String, Object> query, Set<String> fields) throws StreamClientException {
return (JSONArray) request("/"+objCode+PATH_SEARCH, query, fields, METH_GET);
}
public JSONObject get (String objCode, String objID) throws StreamClientException {
return get(objCode, objID, (Set<String>) null);
}
public JSONObject get (String objCode, String objID, String[] fields) throws StreamClientException {
return get(objCode, objID, new HashSet<String>(Arrays.asList(fields)));
}
public JSONObject get (String objCode, String objID, Set<String> fields) throws StreamClientException {
return (JSONObject) request("/"+objCode+"/"+objID, null, fields, METH_GET);
}
public JSONObject post (String objCode, Map<String, Object> message) throws StreamClientException {
return post(objCode, message, (Set<String>) null);
}
public JSONObject post (String objCode, Map<String, Object> message, String[] fields) throws StreamClientException {
return post(objCode, message, new HashSet<String>(Arrays.asList(fields)));
}
public JSONObject post (String objCode, Map<String, Object> message, Set<String> fields) throws StreamClientException {
return (JSONObject) request("/"+objCode, message, fields, METH_POST);
}
public JSONObject post (String objCode, Map<String, Object> params, Map<String, Object> updates, Set<String> fields) throws StreamClientException {
params.put("updates", new JSONObject(updates).toString());
return (JSONObject) request("/"+objCode, params, fields, METH_POST);
}
public JSONObject put (String objCode, String objID, Map<String, Object> message) throws StreamClientException {
return put(objCode, objID, message, (Set<String>) null);
}
public JSONObject put (String objCode, String objID, Map<String, Object> message, String[] fields) throws StreamClientException {
return put(objCode, objID, message, new HashSet<String>(Arrays.asList(fields)));
}
public JSONObject put (String objCode, String objID, Map<String, Object> message, Set<String> fields) throws StreamClientException {
Map<String, Object> params = new HashMap<String, Object>();
return put(objCode,objID,params,message,fields);
}
public JSONObject put (String objCode, String objID, Map<String, Object> params, Map<String, Object> updates, Set<String> fields) throws StreamClientException {
params.put("updates", new JSONObject(updates).toString());
return (JSONObject) request("/"+objCode+"/"+objID, params, fields, METH_PUT);
}
public boolean delete (String objCode, String objID) throws StreamClientException {
return delete(objCode, objID, false);
}
public boolean delete (String objCode, String objID, boolean force) throws StreamClientException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("force", force);
JSONObject result = (JSONObject) request("/"+objCode+"/"+objID, params, null, METH_DELETE);
try {
return result.getBoolean("success");
}
catch (JSONException e) {
throw new StreamClientException(e);
}
}
public JSONObject upload(File file) throws StreamClientException {
String name = this.apiKey != null ? "apiKey" : "sessionID";
String value = this.apiKey != null ? this.apiKey : this.sessionID;
return (JSONObject) request("/upload?" + name + "=" + value, null, null, "POST", 0, file);
}
private Object request (String path, Map<String, Object> params, Set<String> fields, String method) throws StreamClientException {
return request(path, params, fields, method, 0);
}
private Object request (String path, Map<String, Object> params, Set<String> fields, String method, int retryCount) throws StreamClientException {
return request(path, params, fields, method, retryCount, null);
}
private Object request (String path, Map<String, Object> params, Set<String> fields, String method, int retryCount, File file) throws StreamClientException {
HttpURLConnection conn = null;
int responseCode = -1;
try {
String authenticationParam = "";
if (apiKey != null) {
authenticationParam += "apiKey=" + apiKey;
} else if (sessionID != null) {
authenticationParam += "sessionID=" + sessionID;
}
String methodParam = "method=" + method;
String query = authenticationParam + "&" + methodParam;
if (params != null) {
for (String key : params.keySet()) {
if (params.get(key) instanceof String[]) {
String[] paramVal = (String[]) params.get(key);
for (int i = 0; i < paramVal.length; i++) {
String val = paramVal[i];
query += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(val, "UTF-8");
}
} else {
query += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(String.valueOf(params.get(key)), "UTF-8");
}
}
}
if (fields != null) {
query += "&fields=";
for (String field : fields) {
query += URLEncoder.encode(field, "UTF-8") + ",";
}
query = query.substring(0, query.lastIndexOf(","));
}
conn = createConnection(hostname + path, method);
String boundary = Long.toHexString(System.currentTimeMillis());
if (file != null) {
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("User-Agent", "Workfront Java StreamClient");
}
// Send request
OutputStream outputStream = conn.getOutputStream();
Writer out = new OutputStreamWriter(outputStream);
if (file != null) {
addFileToRequest(boundary, out, outputStream, file);
} else {
out.write(query);
}
out.flush();
out.close();
// Get response code
responseCode = conn.getResponseCode();
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
// Decode JSON
JSONObject result = new JSONObject(response.toString());
// Verify result
if (result.has("error")) {
throw new StreamClientException(result.getJSONObject("error").getString("message"));
}
else if (!result.has("data")) {
throw new StreamClientException("Invalid response from server");
}
// Manage the session
if (path.equals(PATH_LOGIN)) {
sessionID = result.getJSONObject("data").getString("sessionID");
}
else if (path.equals(PATH_LOGOUT)) {
sessionID = null;
}
return result.get("data");
} catch(ConnectException connectException) {
throw new StreamClientException("Unable to connect to " + hostname + path);
} catch (IOException e) {
//getErrorStream() can return null if no error data was sent back
if (conn.getErrorStream() != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
copyStream(conn.getErrorStream(), out);
} catch (IOException e1) {
// Removed printStackTrace call
}
throw new StreamClientException(new String(out.toByteArray()));
} else {
//I believe this use case happens when the we are sending to many requests at one time...
if (retryCount < 3) {
return request(path, params, fields, method, ++retryCount);
} else {
throw new StreamClientException("An error happened but no error data was sent... 3 time... Response Code = " + responseCode);
}
}
} catch (Exception e) {
throw new StreamClientException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private void copyStream(InputStream in, OutputStream out) throws IOException {
try {
byte[] buffer = new byte[8192];
int count;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
} finally {
in.close();
out.close();
}
}
private HttpURLConnection createConnection (String spec, String method) throws IOException {
URL url = new URL(spec);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setHostnameVerifier(HOSTNAME_VERIFIER);
}
conn.setAllowUserInteraction(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(60000);
conn.setReadTimeout(300000);
return conn;
}
private void addFileToRequest(String boundary, Writer out, OutputStream binaryStream, File file) throws IOException {
out.append("--" + boundary).append(NEW_LINE);
out.append("Content-Disposition: form-data; name=\"uploadedFile\"; filename=\"" + file.getName() + "\"").append(NEW_LINE);
out.append("Content-Type: " + URLConnection.guessContentTypeFromName(file.getName())).append(NEW_LINE);
out.append("Content-Transfer-Encoding: binary").append(NEW_LINE).append(NEW_LINE);
out.flush();
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
binaryStream.write(buffer, 0, bytesRead);
}
binaryStream.flush();
inputStream.close();
out.append(NEW_LINE);
out.flush();
out.append("--").append(boundary).append("--").append(NEW_LINE);
}
}