/
APICaller.java
255 lines (201 loc) · 8.33 KB
/
APICaller.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
package com.face4j.facebook.http;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import com.face4j.facebook.exception.FacebookException;
import com.face4j.facebook.util.JSONToObjectTransformer;
/**
* APICaller will make http requests, obtain that response and return it without processing. Basically, the raw response is returned by every method.
* @author nischal.shetty
*
*/
public class APICaller implements APICallerInterface {
private static final APICaller caller = new APICaller();
private static HttpClient httpClient = null;
private APICaller(){
}
private synchronized static HttpClient getHttpClient() {
if(null==httpClient){
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 15);
connectionManager.getParams().setMaxTotalConnections(15);
httpClient = new HttpClient(connectionManager);
String username = null;
String password = null;
String host = null;
int port = -1;
Credentials credentials = null;
//Check if username and password exists in any resource file
try {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("face4j.properties");
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();
username = properties.getProperty("client.proxy.username");
password = properties.getProperty("client.proxy.password");
host = properties.getProperty("client.proxy.host");
if(properties.getProperty("client.proxy.port") != null){
port = Integer.parseInt(properties.getProperty("client.proxy.port"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(username != null || password != null){
credentials = new UsernamePasswordCredentials(username, password);
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
}
if(username != null || password != null || host != null || port > -1){
try {
httpClient.getState().setCredentials( new AuthScope(host, port), credentials);
} finally {
}
}
}
return httpClient;
}
/*public static void main(String[] args) {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("face4j.properties");
try {
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
System.out.println(key + " : " + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
public static APICaller getInstance(){
return caller;
}
public String getData(String url, NameValuePair[] nameValuePairs) throws FacebookException{
HttpClient client = APICaller.getHttpClient();
String response = null;
//This part is when the nameValuePairs is null indicating the params are most probably in the url
String urlSplit[] = null;
if(nameValuePairs==null){
urlSplit = url.split("\\?");
url = urlSplit[0];
if(urlSplit.length > 1){
nameValuePairs = getNameValuePairs(urlSplit[1]);
}
}
GetMethod getMethod = null;
try{
getMethod = new GetMethod(url);
if(nameValuePairs!=null){
getMethod.setQueryString(nameValuePairs);
}
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
//FacebookError error = new FacebookError(statusCode, "I guess you are not permitted to access this url. HTTP status code:"+statusCode, null);
response = getMethod.getResponseBodyAsString();
throw new FacebookException(JSONToObjectTransformer.getError(response, statusCode));
}
response = getMethod.getResponseBodyAsString();
} catch (HttpException e) {
throw new FacebookException("Http Exception while calling facebook!",e);
} catch (IOException e) {
throw new FacebookException("IO Exception while calling facebook!",e);
} finally {
getMethod.releaseConnection();
}
//if response string contains accessToken=xxx remove it!
//response = Util.replaceAccessToken(response, nameValuePairs);
return response;
}
public String postData(String url, NameValuePair[] nameValuePairs) throws FacebookException {
HttpClient client = APICaller.getHttpClient();
String response = null;
PostMethod postMethod = null;
try {
postMethod = new PostMethod(url);
if (nameValuePairs != null) {
postMethod.setQueryString(nameValuePairs);
}
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
//FacebookError error = new FacebookError(statusCode, "I guess you are not permitted to access this url. HTTP status code:"+statusCode, null);
response = postMethod.getResponseBodyAsString();
throw new FacebookException(JSONToObjectTransformer.getError(response, statusCode));
}
response = postMethod.getResponseBodyAsString();
} catch (HttpException e) {
throw new FacebookException("Http Exception while calling facebook!", e);
} catch (IOException e) {
throw new FacebookException("IO Exception while calling facebook!", e);
} finally {
postMethod.releaseConnection();
}
return response;
}
public String deleteData(String url, NameValuePair[] nameValuePairs) throws FacebookException {
HttpClient client = APICaller.getHttpClient();
String response = null;
DeleteMethod deleteMethod = null;
try {
deleteMethod = new DeleteMethod(url);
if (nameValuePairs != null) {
deleteMethod.setQueryString(nameValuePairs);
}
int statusCode = client.executeMethod(deleteMethod);
if (statusCode != HttpStatus.SC_OK) {
response = deleteMethod.getResponseBodyAsString();
throw new FacebookException(JSONToObjectTransformer.getError(response, statusCode));
}
response = deleteMethod.getResponseBodyAsString();
} catch (HttpException e) {
throw new FacebookException("Http Exception while calling facebook!", e);
} catch (IOException e) {
throw new FacebookException("IO Exception while calling facebook!", e);
} finally {
deleteMethod.releaseConnection();
}
return response;
}
private NameValuePair[] getNameValuePairs(String urlParams){
String[] params = urlParams.split("&");
NameValuePair[] nameValuePair = new NameValuePair[params.length];
NameValuePair valuePair = null;
String[] tempParamPair = null;
for(int i=0;i <params.length;i++){
tempParamPair = params[i].split("=");
valuePair = new NameValuePair(tempParamPair[0], tempParamPair[1]);
nameValuePair[i] = valuePair;
}
return nameValuePair;
}
//TODO: delete data method
/*public static void main(String[] args) {
APICaller client = APICaller.getInstance();
//https://graph.facebook.com/me
//access_token=118528751499071|4d6f2914cdc4d0177cdb5a66-544232058|cIgUXhJ5bnj5i5orhdqqrVUh3TA.
try {
client.getData("https://graph.facebook.com/me?access_token=118528751499071|4d6f2914cdc4d0177cdb5a66-544232058|cIgUXhJ5bnj5i5orhdqqrVUh3TA.");
} catch (Exception e) {
e.printStackTrace();
}
}*/
}