FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/java/src1/com/pubnub/api/HttpClientCore.java at master · longfeiteng/java · GitHub
longfeiteng
/
java
Public
forked from
ringcentral/pubnub-jtools
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
java
/
src1
/
com
/
pubnub
/
api
/
HttpClientCore.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
259 lines (227 loc) · 9.08 KB
Breadcrumbs
java
/
java
/
src1
/
com
/
pubnub
/
api
/
HttpClientCore.java
Copy path
File metadata and controls
259 lines (227 loc) · 9.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
pubnub
.
api
;
import
java
.
io
.
ByteArrayOutputStream
;
import
java
.
util
.
zip
.
GZIPInputStream
;
import
java
.
io
.
IOException
;
import
java
.
io
.
InputStream
;
import
java
.
net
.
HttpURLConnection
;
import
java
.
net
.
MalformedURLException
;
import
java
.
net
.
ProtocolException
;
import
java
.
net
.
SocketTimeoutException
;
import
java
.
net
.
URL
;
import
java
.
util
.
Enumeration
;
import
java
.
util
.
Hashtable
;
import
org
.
json
.
JSONArray
;
import
org
.
json
.
JSONException
;
import
org
.
json
.
JSONObject
;
import
com
.
pubnub
.
api
.
PubnubException
;
import
static
com
.
pubnub
.
api
.
PubnubError
.*;
class
HttpClientCore
extends
HttpClient
{
private
int
requestTimeout
=
310000
;
private
int
connectionTimeout
=
5000
;
HttpURLConnection
connection
;
protected
static
Logger
log
=
new
Logger
(
Worker
.
class
);
private
void
init
() {
HttpURLConnection
.
setFollowRedirects
(
true
);
}
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
boolean
isRedirect
(
int
rc
) {
return
(
rc
==
HttpURLConnection
.
HTTP_MOVED_PERM
||
rc
==
HttpURLConnection
.
HTTP_MOVED_TEMP
||
rc
==
HttpURLConnection
.
HTTP_SEE_OTHER
);
}
public
boolean
checkResponse
(
int
rc
) {
return
(
rc
==
HttpURLConnection
.
HTTP_OK
||
isRedirect
(
rc
));
}
public
boolean
checkResponseSuccess
(
int
rc
) {
return
(
rc
==
HttpURLConnection
.
HTTP_OK
);
}
private
static
String
readInput
(
InputStream
in
)
throws
IOException
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
byte
bytes
[] =
new
byte
[
1024
];
int
n
=
in
.
read
(
bytes
);
while
(
n
!= -
1
) {
out
.
write
(
bytes
,
0
,
n
);
n
=
in
.
read
(
bytes
);
}
return
new
String
(
out
.
toString
());
}
public
HttpResponse
fetch
(
String
url
)
throws
PubnubException
,
SocketTimeoutException
{
return
fetch
(
url
,
null
);
}
public
synchronized
HttpResponse
fetch
(
String
url
,
Hashtable
headers
)
throws
PubnubException
,
SocketTimeoutException
{
URL
urlobj
=
null
;
log
.
verbose
(
"FETCHING URL : "
+
url
);
try
{
urlobj
=
new
URL
(
url
);
}
catch
(
MalformedURLException
e3
) {
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_MALFORMED_URL
,
url
));
}
try
{
connection
= (
HttpURLConnection
)
urlobj
.
openConnection
();
}
catch
(
IOException
e2
) {
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_URL_OPEN
,
url
));
}
try
{
connection
.
setRequestMethod
(
"GET"
);
}
catch
(
ProtocolException
e1
) {
throw
new
PubnubException
(
PNERROBJ_PROTOCOL_EXCEPTION
);
}
if
(
_headers
!=
null
) {
Enumeration
en
=
_headers
.
keys
();
while
(
en
.
hasMoreElements
()) {
String
key
= (
String
)
en
.
nextElement
();
String
val
= (
String
)
_headers
.
get
(
key
);
connection
.
addRequestProperty
(
key
,
val
);
}
}
if
(
headers
!=
null
) {
Enumeration
en
=
headers
.
keys
();
while
(
en
.
hasMoreElements
()) {
String
key
= (
String
)
en
.
nextElement
();
String
val
= (
String
)
headers
.
get
(
key
);
connection
.
addRequestProperty
(
key
,
val
);
}
}
connection
.
setReadTimeout
(
requestTimeout
);
connection
.
setConnectTimeout
(
connectionTimeout
);
/*
try {
connection.connect();
} catch (SocketTimeoutException e) {
throw e;
} catch (IOException e) {
throw new PubnubException(getErrorObject(PNERROBJ_CONNECT_EXCEPTION, url + " : " + e.toString()));
}
*/
int
rc
=
HttpURLConnection
.
HTTP_INTERNAL_ERROR
;
try
{
rc
=
connection
.
getResponseCode
();
}
catch
(
SocketTimeoutException
ste
) {
throw
ste
;
}
catch
(
IOException
e
) {
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_HTTP_RC_ERROR
,
url
+
" : "
+
e
.
toString
()));
}
InputStream
is
=
null
;
String
encoding
=
connection
.
getContentEncoding
();
if
(
encoding
==
null
|| !
encoding
.
equals
(
"gzip"
)) {
try
{
is
=
connection
.
getInputStream
();
}
catch
(
IOException
e
) {
if
(
rc
==
HttpURLConnection
.
HTTP_OK
)
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_GETINPUTSTREAM
,
1
,
url
));
is
=
connection
.
getErrorStream
();
}
}
else
{
try
{
is
=
new
GZIPInputStream
(
connection
.
getInputStream
());
}
catch
(
IOException
e
) {
if
(
rc
==
HttpURLConnection
.
HTTP_OK
)
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_GETINPUTSTREAM
,
2
,
url
));
is
=
connection
.
getErrorStream
();
}
}
String
page
=
null
;
try
{
page
=
readInput
(
is
);
}
catch
(
IOException
e
) {
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_READINPUT
,
url
));
}
if
(
is
!=
null
) {
try
{
is
.
close
();
}
catch
(
IOException
e
) {
}
}
log
.
verbose
(
"URL = "
+
url
+
", Status Code : "
+
rc
+
", : RESPONSE = "
+
page
);
switch
(
rc
) {
case
HttpURLConnection
.
HTTP_FORBIDDEN
:
{
JSONObject
payload
=
null
;
String
message
=
null
;
try
{
JSONObject
pageJso
=
new
JSONObject
(
page
);
message
=
pageJso
.
getString
(
"message"
);
payload
=
pageJso
.
getJSONObject
(
"payload"
);
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_FORBIDDEN
,
message
,
payload
));
}
catch
(
JSONException
e2
) {}
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_FORBIDDEN
,
page
));
}
case
HttpURLConnection
.
HTTP_UNAUTHORIZED
:
{
JSONObject
payload
=
null
;
String
message
=
null
;
try
{
JSONObject
pageJso
=
new
JSONObject
(
page
);
message
=
pageJso
.
getString
(
"message"
);
payload
=
pageJso
.
getJSONObject
(
"payload"
);
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_FORBIDDEN
,
message
,
payload
));
}
catch
(
JSONException
e2
) {}
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_UNAUTHORIZED
,
page
));
}
case
HttpURLConnection
.
HTTP_BAD_REQUEST
:
{
JSONObject
payload
=
null
;
String
message
=
null
;
try
{
JSONObject
pageJso
=
new
JSONObject
(
page
);
message
=
pageJso
.
getString
(
"message"
);
payload
=
pageJso
.
getJSONObject
(
"payload"
);
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_BAD_REQUEST
,
message
,
payload
));
}
catch
(
JSONException
e2
) {}
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_BAD_REQUEST
,
page
));
}
case
HttpURLConnection
.
HTTP_NOT_FOUND
:
{
JSONObject
payload
=
null
;
String
message
=
null
;
try
{
JSONObject
pageJso
=
new
JSONObject
(
page
);
message
=
pageJso
.
getString
(
"message"
);
payload
=
pageJso
.
getJSONObject
(
"payload"
);
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_BAD_REQUEST
,
message
,
payload
));
}
catch
(
JSONException
e2
) {}
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_NOT_FOUND_ERROR
,
page
));
}
case
HttpURLConnection
.
HTTP_BAD_GATEWAY
:
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_BAD_GATEWAY
,
url
));
case
HttpURLConnection
.
HTTP_CLIENT_TIMEOUT
:
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_CLIENT_TIMEOUT
,
url
));
case
HttpURLConnection
.
HTTP_GATEWAY_TIMEOUT
:
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_GATEWAY_TIMEOUT
,
url
));
case
HttpURLConnection
.
HTTP_INTERNAL_ERROR
:
throw
new
PubnubException
(
getErrorObject
(
PNERROBJ_INTERNAL_ERROR
,
url
+
" : "
+
rc
));
default
:
break
;
}
return
new
HttpResponse
(
rc
,
page
);
}
public
boolean
isOk
(
int
rc
) {
return
(
rc
==
HttpURLConnection
.
HTTP_OK
);
}
public
void
shutdown
() {
if
(
connection
!=
null
) {
try
{
connection
.
disconnect
();
}
catch
(
Exception
e
) {
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL