Repository navigation
Expand file tree
/
Copy pathConnectionService.java
More file actions
185 lines (157 loc) · 4.33 KB
/
Copy pathConnectionService.java
File metadata and controls
185 lines (157 loc) · 4.33 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
/**
* Copyright (C) 2014 xuanhung2401.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cloud.google.util;
/**
* @author xuanhung2401
*
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;
public class ConnectionService {
private static final Logger log = Logger.getLogger(ConnectionService.class
.getName());
private String url = "";
private Map<String, String> headers = new HashMap<String, String>();
private String method = "GET";
private String requestBody = "";
private int acceptResponseCode = 200;
public ConnectionService() {
this.header("Content-Type", "application/json");
}
public ConnectionService url(String url) {
this.setUrl(url);
return this;
}
public String url() {
return this.url;
}
public ConnectionService acceptCode(int value) {
this.acceptResponseCode = value;
return this;
}
public ConnectionService header(String name, String value) {
this.headers.put(name, value);
return this;
}
public ConnectionService accessToken(String value) {
this.headers.put("Authorization", "Bearer " + value);
return this;
}
public Map<String, String> headers() {
return this.headers;
}
public String method() {
return this.method;
}
public ConnectionService method(String method) {
this.method = method;
return this;
}
public ConnectionService body(String body) {
this.requestBody = body;
return this;
}
public String body() {
return this.requestBody;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String get() {
this.method("GET");
return this.execute();
}
public String post() {
this.method("POST");
return this.execute();
}
/**
* Open connection to this.url, use gzip to send data. Check response code
* and return response string (using ByteArrayOutputStream to read)
* */
public String execute() {
HttpURLConnection connection = null;
try {
URL url = new URL(this.url);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod(this.method);
for (Entry<String, String> entry : headers.entrySet()) {
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
/**
* write request body, add parameter
* */
OutputStreamWriter wr = new OutputStreamWriter(
connection.getOutputStream());
wr.write(requestBody);
wr.flush();
connection.connect();
int statusCode = connection.getResponseCode();
InputStream stream = null;
if (statusCode != acceptResponseCode) {
stream = connection.getErrorStream();
} else {
stream = connection.getInputStream();
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int length;
byte[] b = new byte[2048];
while ((length = stream.read(b)) != -1) {
buffer.write(b, 0, length);
}
buffer.flush();
if (null != connection) {
connection.disconnect();
}
if (statusCode != acceptResponseCode) {
log.severe("Request return code : " + statusCode + "\n"
+ buffer.toString("UTF-8"));
return null;
} else {
return buffer.toString("UTF-8");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Return connectionservice object after set url = user parameter
* */
public static ConnectionService connect(String url) {
ConnectionService instance = new ConnectionService();
instance.setUrl(url);
return instance;
}
}