forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientCore.java
More file actions
105 lines (87 loc) · 3.51 KB
/
Copy pathHttpClientCore.java
File metadata and controls
105 lines (87 loc) · 3.51 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
package com.pubnub.api;
import static com.pubnub.api.PubnubError.*;
import static com.pubnub.api.PubnubError.getErrorObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.io.*;
import org.json.*;
class HttpClientCore extends HttpClient {
PubnubCn1Connection connection;
private int requestTimeout = 310000;
private int connectionTimeout = 5000;
protected static Logger log = new Logger(Worker.class);
private void init() {
connection = new PubnubCn1Connection();
}
public HttpClientCore(int connectionTimeout, int requestTimeout, Hashtable headers) {
init();
this.setRequestTimeout(requestTimeout);
this.setConnectionTimeout(connectionTimeout);
this._headers = headers;
}
public int getRequestTimeout() {
return requestTimeout;
}
public void setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public HttpResponse fetch(String url) throws PubnubException, IOException {
return fetch(url, null);
}
public synchronized HttpResponse fetch(String url, Hashtable headers)
throws PubnubException, IOException {
IOException excp = null;
PubnubCn1Response pcr = null;
try {
pcr = connection.fetch(url, requestTimeout, PubnubUtil.hashtableClone(headers,_headers));
} catch (IOException ex) {
excp = ex;
}
String page = pcr.getResponse();
switch (pcr.getResponseStatusCode()) {
case HttpUtil.HTTP_FORBIDDEN:
throw new PubnubException(getErrorObject(PNERROBJ_FORBIDDEN, page));
case HttpUtil.HTTP_UNAUTHORIZED:
throw new PubnubException(getErrorObject(PNERROBJ_UNAUTHORIZED, page));
case HttpUtil.HTTP_BAD_REQUEST:
try {
JSONArray jsarr = new JSONArray(page);
String error = jsarr.get(1).toString();
throw new PubnubException(getErrorObject(PNERROBJ_BAD_REQUEST, 1, error));
} catch (JSONException e) {
JSONObject jso;
try {
jso = new JSONObject(page);
throw new PubnubException(getErrorObject(PNERROBJ_BAD_REQUEST, 2, jso.toString()));
} catch (JSONException e1) {
throw new PubnubException(getErrorObject(PNERROBJ_INVALID_JSON, 2));
}
}
case HttpUtil.HTTP_BAD_GATEWAY:
throw new PubnubException(getErrorObject(PNERROBJ_BAD_GATEWAY, url));
case HttpUtil.HTTP_CLIENT_TIMEOUT:
throw new PubnubException(getErrorObject(PNERROBJ_CLIENT_TIMEOUT, url));
case HttpUtil.HTTP_GATEWAY_TIMEOUT:
throw new PubnubException(getErrorObject(PNERROBJ_GATEWAY_TIMEOUT, url));
case HttpUtil.HTTP_INTERNAL_ERROR:
throw new PubnubException(getErrorObject(PNERROBJ_INTERNAL_ERROR, url));
default:
if (excp != null) throw excp;
break;
}
return new HttpResponse(pcr.getResponseStatusCode(), pcr.getResponse());
}
public void shutdown() {
if (connection != null) connection.disconnect();
}
}