-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDroidDropNoteClient.java
More file actions
executable file
·474 lines (362 loc) · 13.3 KB
/
DroidDropNoteClient.java
File metadata and controls
executable file
·474 lines (362 loc) · 13.3 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package com.brightkeep.dropionotes;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.HttpGet;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Collection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class DroidDropNoteClient {
String apiKey;
String version="1.0";
String format="xml";
String token=null;
// format becomes api.dropio.com/prefix/dropName/suffix? parm1=1...
TreeMap<String,String> standardParms=new TreeMap<String,String>();
HttpResponse restResponse;
HttpClient restClient;
String apiServer = "http://api.drop.io";
String uploadServer = "http://assets.drop.io/upload";
String callId;
TreeMap<String,String> dropParms=new TreeMap<String,String>();
int connectionTimeout=10000;
public DroidDropNoteClient( ){
standardParms.put("version", version);
standardParms.put("format", format);
}
public DroidDropNoteClient( String apiKey){
this();
this.apiKey=apiKey;
standardParms.put("api_key", apiKey);
}
public DroidDropNoteClient( String apiKey, String token){
this(apiKey);
this.token=token;
standardParms.put("token", token);
}
public DroidDropNoteClient( String apiKey, String token, String versionParm, String formatParm){
this(apiKey,token);
version=versionParm;
format=formatParm;
standardParms.put("version", version);
standardParms.put("format", format);
}
public HttpResponse getResponse(boolean doPost, String dropPrefix, String dropName,String dropSuffix, String assetPrefix,String assetName, String assetSuffix, String commentId, TreeMap<String,String> parms)
throws Exception, ClientProtocolException, UnsupportedEncodingException{
String currentKey;
String currentValue;
String sigParms=""; //String used for creating signature
StringBuffer ub=new StringBuffer();
TreeMap<String,String>restParms = new TreeMap<String,String>();
restParms.putAll(standardParms);
restParms.putAll(parms);
Collection<String> c = restParms.keySet();
Iterator<String> itr = c.iterator();
if (itr.hasNext()){
currentKey = (String)itr.next();
currentValue = restParms.get(currentKey);
currentValue = URLEncoder.encode(currentValue, "UTF-8");
sigParms = sigParms +currentKey+"="+currentValue;
ub.append(sigParms);
}
while(itr.hasNext()){
currentKey = (String)itr.next();
currentValue = restParms.get(currentKey);
currentValue = URLEncoder.encode(currentValue, "UTF-8");
sigParms = "&"+currentKey+"="+currentValue;
ub.append(sigParms);
}
String postUrl=apiServer;
if (dropPrefix!=null){
postUrl+="/"+dropPrefix;
}
if (dropName!=null){
postUrl+="/"+dropName;
}
if (dropSuffix!=null){
postUrl+="/"+dropSuffix;
}
if (assetPrefix!=null){
postUrl+="/"+assetPrefix;
}
if (assetName!=null){
postUrl+="/"+assetName;
}
if (assetSuffix!=null){
postUrl+="/"+assetSuffix;
}
if (commentId!=null){
postUrl+="/"+commentId;
}
postUrl+="/";
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
connectionTimeout);
HttpConnectionParams.setSoTimeout(httpParams,
connectionTimeout);
restClient = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost(postUrl+"?"+ub.toString());
HttpGet get = new HttpGet(postUrl+"?"+ub.toString());
if (doPost){
restResponse = restClient.execute(post);
}else{
restResponse = restClient.execute(get);
}
return(restResponse);
}
public String call(boolean doPost, String prefix, String dropName,String suffix, String assetPrefix, String assetName, String assetSuffix, String commentId, TreeMap<String,String> parms)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
HttpResponse thisResponse;
String errorReturn;
String errorMessage;
thisResponse = this.getResponse(doPost, prefix,dropName,suffix,assetPrefix, assetName,assetSuffix, commentId, parms);
if (thisResponse==null){
throw new Exception("No Response");
}
int status= restResponse.getStatusLine().getStatusCode();
switch (status){
case 400:
case 403:
case 404:
case 500:
errorReturn = EntityUtils.toString(thisResponse.getEntity());
errorMessage = getErrorMsg(errorReturn);
if (errorMessage == null) errorMessage = "Server response was "+status;
throw new Exception(errorMessage);
case 200: return EntityUtils.toString(thisResponse.getEntity());
default: errorMessage = "Server response was "+status;
throw new Exception(errorMessage);
}
}
public String getDrop(String dropName, String password)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (password !=null){
dropParms.put("token", password);
}
return (this.call(false, "drops",dropName,null,null,null,null,null,dropParms));
}
public String getDrop(String dropName)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (dropName ==null) throw new Exception("Drop Name required to get Drop");
return (this.call(false, "drops",dropName,null,null,null,null,null,dropParms));
}
public String getAssetList(String dropName, String page)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{ // review, need paging
dropParms.clear();
if (dropName ==null) throw new Exception("Drop Name required to get Asset List");
if (page !=null){
dropParms.put("page", page);
}
return (this.call(false, "drops",dropName,"assets",null, null,null,null,dropParms));
}
public String getAsset(String dropName, String assetName)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (dropName ==null) throw new Exception("Drop Name required to get Asset");
if (assetName ==null) throw new Exception("Asset Name required to get Asset");
return (this.call(false, "drops",dropName,"assets",assetName,null,null,null,dropParms));
}
public String getAssetEmbed(String dropName, String assetName)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
return (this.call(false, "drops",dropName,"assets",assetName,"embed_code",null,null,dropParms));
}
public String createNote(String dropName, String title, String contents)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
//dropParms.put("name", dropName);
if (title!=null)
dropParms.put("title", title);
if (contents==null)
throw new Exception ("Content required when creating a note");
else{
dropParms.put("contents", contents); //required
return (this.call(true, "drops",dropName, "assets",null,null,null,null,dropParms));
}
}
public String createLink(String dropName, String title, String description, String url)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (title!=null)
dropParms.put("title", title);
if (description!=null)
dropParms.put("description", description);
if (url==null)
throw new Exception ("URL required when creating a link");
else{
dropParms.put("url", url); //required
return (this.call(true, "drops",dropName, "assets",null,null,null,null,dropParms));
}
}
public String createDrop(String dropName, String guestAdd, String guestDelete, String guestComment, String expiration, String guestPassword, String adminPassword)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (dropName!=null){
dropParms.put("name",dropName );
}
if (guestComment!=null){
dropParms.put("guests_can_comment", guestComment );
}
if (guestAdd!=null){
dropParms.put("guests_can_add", guestAdd );
}
if (guestDelete!=null){
dropParms.put("guests_can_delete", guestDelete );
}
if (guestPassword!=null){
dropParms.put("password",guestPassword );
}
if (adminPassword!=null){
dropParms.put("admin_password",adminPassword );
}
if (expiration!=null){
dropParms.put("expiration_length",expiration );
}
return (this.call(true, "drops",null,null,null,null,null,null,dropParms));
}
public String updateDrop(String dropName, String guestAdd, String guestDelete, String guestComment, String expiration, String guestPassword, String adminPassword)
throws Exception, ClientProtocolException, UnsupportedEncodingException, ParseException{
dropParms.clear();
if (dropName==null){
throw new Exception ("Drop name required when updating a drop");
}
if (guestComment!=null){
dropParms.put("guests_can_comment", guestComment );
}
if (guestAdd!=null){
dropParms.put("guests_can_add", guestAdd );
}
if (guestDelete!=null){
dropParms.put("guests_can_delete", guestDelete );
}
if (guestPassword!=null){
dropParms.put("password",guestPassword );
}
if (adminPassword!=null){
dropParms.put("admin_password",adminPassword );
}
if (expiration!=null){
dropParms.put("expiration_length",expiration );
}
return (this.call(true, "drops",dropName,null,null,null,null,null,dropParms));
}
public void setRequestParms(TreeMap<String,String> parms){
TreeMap<String,String>requestParms = new TreeMap<String,String>();
requestParms.putAll(standardParms);
requestParms.putAll(parms);
}
public void setToken(String token){
this.token=token;
standardParms.put("token", token);
}
public String getToken(){
return(this.token);
}
public void setFormat(String formatParm){
this.format=formatParm;
standardParms.put("format", format);
}
public void setVersion(String versionParm){
this.version=versionParm;
standardParms.put("version", version);
}
public void setApiKey(String apiKey){
this.apiKey=apiKey;
standardParms.put("api_key", apiKey);
}
public String getApiKey(){
return(this.apiKey);
}
public String getErrorMsg(String response, String format){
if (format.equals("XML")){
return getErrorMsg(response);
}
try {
JSONObject error = new JSONObject(response);
return error.getString("error_msg");
} catch (JSONException e) {
return null;
}
}
// this works for XML
public String getErrorMsg(String response){
String sTag = "<message>";
String eTag = "</message>";
int c1 = response.indexOf(sTag);
int c2 = response.indexOf(eTag);
if (c1 != -1 && c2 != -1) {
return(response.substring(c1 + sTag.length(), c2));
}
return null;
}
// might need this later
public String getErrorFormat(String response){
int offset=-1;
offset=response.indexOf("xml version");
if (offset==-1){
return "JSON";
}
return "XML";
}
public static Drop getDropfromString(String dropData) throws Exception{
// Use SAX to parse the drop
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
DropHandler dropHandler = new DropHandler();
xr.setContentHandler(dropHandler);
InputSource s = new InputSource(new StringReader(dropData));
xr.parse(s);
//Drop created
Drop createdDrop=dropHandler.getDrop();
if (createdDrop==null) throw new Exception ("Drop is null");
return (createdDrop);
}
public static Asset getAssetfromString(String assetData) throws Exception{
// Use SAX to parse the drop
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
AssetHandler assetHandler = new AssetHandler();
xr.setContentHandler(assetHandler);
InputSource s = new InputSource(new StringReader(assetData));
xr.parse(s);
//Drop created
Asset asset=assetHandler.getAsset();
if (asset==null) throw new Exception ("Asset is null");
return (asset);
}
public static List<Asset> getAssetListfromString(String assetData) throws Exception{
// Use SAX to parse the drop
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
AssetHandler assetHandler = new AssetHandler();
xr.setContentHandler(assetHandler);
InputSource s = new InputSource(new StringReader(assetData));
xr.parse(s);
//Drop created
List<Asset> assets=assetHandler.getAssetList();
if (assets==null) throw new Exception ("Asset is null");
return (assets);
}
}