This repository was archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
131 lines (109 loc) · 4.28 KB
/
Copy pathClient.java
File metadata and controls
131 lines (109 loc) · 4.28 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
package org.twocloud.java2cloud;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.joda.time.DateTime;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
public class Client {
public Client() {
}
public Response get(String endpoint, String device) throws IOException {
return get(endpoint, null, null, device);
}
public Response get(String endpoint, String username, String secret, String device) throws IOException {
return doRequest("GET", endpoint, username, secret, null, device);
}
public Response post(String endpoint, String body, String device) throws IOException {
return post(endpoint, null, null, body, device);
}
public Response post(String endpoint, String username, String secret, String body, String device) throws IOException {
return doRequest("POST", endpoint, username, secret, body, device);
}
public Response put(String endpoint, String body, String device) throws IOException {
return put(endpoint, null, null, body, device);
}
public Response put(String endpoint, String username, String secret, String body, String device) throws IOException {
return doRequest("PUT", endpoint, username, secret, body, device);
}
public Response delete(String endpoint, String device) throws IOException {
return delete(endpoint, null, null, device);
}
public Response delete(String endpoint, String username, String secret, String device) throws IOException {
return doRequest("DELETE", endpoint, username, secret, null, device);
}
private Response doRequest(String method, String endpoint, final String username, final String secret, String body, String device) throws IOException {
URL url = new URL(endpoint);
Response resp;
Map<String, List<String>> headers = null;
byte[] b;
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if(username != null && secret != null) {
String encodeMe = username + ":" + secret;
urlConnection.addRequestProperty("Authorization", "Basic "+Base64.encodeBase64String(encodeMe.getBytes()));
}
urlConnection.addRequestProperty("User-Agent", "java2cloud | 0.1.0");
urlConnection.addRequestProperty("API-Version", "1");
urlConnection.addRequestProperty("From", device);
urlConnection.setRequestMethod(method);
urlConnection.setInstanceFollowRedirects(false);
try {
if(body != null) {
urlConnection.setDoOutput(true);
//urlConnection.setFixedLengthStreamingMode(body.getBytes().length);
urlConnection.setRequestProperty("Content-Length", body.getBytes().length+"");
urlConnection.getOutputStream().write(body.getBytes());
}
if(urlConnection.getResponseCode() < 300) {
BufferedInputStream in = new BufferedInputStream(urlConnection.getInputStream());
b = readStream(in);
headers = urlConnection.getHeaderFields();
} else {
BufferedInputStream in = new BufferedInputStream(urlConnection.getErrorStream());
b = readStream(in);
}
resp = parseJson(b);
} finally {
urlConnection.disconnect();
}
if(headers != null && headers.containsKey("Warning")) {
resp.subscriptionExpired = true;
} else {
resp.subscriptionExpired = false;
}
return resp;
}
private Response parseJson(byte[] b) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
Gson gson = builder.create();
Response resp = null;
try {
resp = gson.fromJson(new String(b, "UTF-8"), Response.class);
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resp;
}
private static byte[] readStream(InputStream in)
throws IOException {
byte[] buf = new byte[1024];
int count = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
while ((count = in.read(buf)) != -1)
out.write(buf, 0, count);
return out.toByteArray();
}
}