Skip to content

Commit e3acfd6

Browse files
author
Devendra
committed
replacing ning with native java.net url lib
1 parent 1c04b62 commit e3acfd6

6 files changed

Lines changed: 82 additions & 81 deletions

File tree

‎java/3.3/Pubnub-3.3.jar‎

-15.1 KB
Binary file not shown.

‎java/3.3/build.xml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<project basedir="." default="build" name="pubnub">
33
<property environment="env" />
4+
<property name="pubnub_jar_file_name" value="Pubnub-3.3.jar"/>
45
<property name="target" value="1.5" />
56
<property name="source" value="1.5" />
67
<path id="pubnub.classpath">
78
<pathelement location="bin" />
8-
<pathelement location="lib/async-http-client-1.6.5-shaded.jar" />
99
<pathelement location="lib/junit.jar" />
1010
<pathelement location="lib/json.jar" />
1111
<pathelement location="lib/slf4j-nop-1.6.2.jar" />
@@ -21,9 +21,10 @@
2121
</target>
2222
<target name="clean">
2323
<delete dir="bin" />
24+
<delete file="${pubnub_jar_file_name}" />
2425
</target>
2526
<target depends="clean" name="cleanall" />
26-
<target depends="build-subprojects,build-project,create-examples-jar,create-tests-jar" name="build" />
27+
<target depends="build-subprojects,build-project,create-api-jar,create-examples-jar,create-tests-jar" name="build" />
2728
<target name="build-subprojects" />
2829
<target depends="init" name="build-project">
2930
<echo message="${ant.project.name}: ${ant.file}" />
@@ -116,5 +117,11 @@
116117
<target name="create-tests-jar">
117118
<create-jar jar.type="tests" jar.classname="com.pubnub.tests.PubnubUnitTestRunner" jar.filename="PubnubUnitTest.jar" />
118119
</target>
120+
<target depends="build-project" name="create-api-jar">
121+
<jar destfile="${pubnub_jar_file_name}"
122+
basedir="bin"
123+
includes="com/pubnub/api/**"
124+
/>
125+
</target>
119126

120127
</project>
-1.11 MB
Binary file not shown.

‎java/3.3/src/com/pubnub/api/Pubnub.java‎

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
import java.io.UnsupportedEncodingException;
1010
import java.math.BigInteger;
1111
import java.net.ConnectException;
12+
import java.net.HttpURLConnection;
1213
import java.net.SocketTimeoutException;
14+
import java.net.URL;
1315
import java.net.URLEncoder;
1416
import java.security.Key;
1517
import java.security.MessageDigest;
1618
import java.security.NoSuchAlgorithmException;
1719
import java.util.ArrayList;
1820
import java.util.Arrays;
21+
import java.util.Enumeration;
1922
import java.util.HashMap;
23+
import java.util.Hashtable;
2024
import java.util.Iterator;
2125
import java.util.List;
2226
import java.util.UUID;
@@ -34,14 +38,6 @@
3438
import org.json.JSONException;
3539
import org.json.JSONObject;
3640

37-
import com.ning.http.client.AsyncCompletionHandler;
38-
import com.ning.http.client.AsyncHttpClient;
39-
import com.ning.http.client.AsyncHttpClientConfig;
40-
import com.ning.http.client.AsyncHttpClientConfig.Builder;
41-
import com.ning.http.client.PerRequestConfig;
42-
import com.ning.http.client.Request;
43-
import com.ning.http.client.RequestBuilder;
44-
import com.ning.http.client.Response;
4541

4642

4743
/**
@@ -80,13 +76,12 @@ public class Pubnub {
8076
private boolean SSL = false;
8177
private String sessionUUID = "";
8278
private String parameters = "";
83-
private AsyncHttpClient ahc = null;
84-
private RequestBuilder rb = null;
8579
private int PUBNUB_WEBREQUEST_CALLBACK_INTERVAL = 310000;
8680
private int PUBNUB_NETWORK_CHECK_RETRIES = 50;
8781
private int PUBNUB_WEBREQUEST_RETRY_INTERVAL = 10000;
8882
private int PUBNUB_FAST_WEBREQUEST_TIMEOUT = 5000;
8983
private int PUBNUB_DEFAULT_CONN_TIMEOUT = 10000;
84+
private HttpURLConnection connection;
9085

9186
/** Sets wait time for Presence/Subscribe API
9287
* @param interval Time interval in milliseconds for which the API's like Presence and Subscribe will block
@@ -167,6 +162,8 @@ private Request getRequest(List<String> url_components) {
167162

168163
@SuppressWarnings({ "deprecation"})
169164
private Request getRequest(List<String> url_components, int requestTimeout) {
165+
166+
Request req = new Request();
170167
StringBuilder url = new StringBuilder();
171168
Iterator<String> url_iterator = url_components.iterator();
172169
String request_for = url_components.get(0);
@@ -185,24 +182,26 @@ private Request getRequest(List<String> url_components, int requestTimeout) {
185182
if (request_for.equals("v2") && request_type.equals("history"))
186183
url.append(parameters);
187184

188-
rb = new RequestBuilder("GET");
189-
190-
rb.addHeader("V", "3.3");
191-
rb.addHeader("User-Agent", "Java");
192-
rb.addHeader("Accept-Encoding", "gzip");
193-
rb.setUrl(url.toString());
194-
185+
req.setUrl(url.toString());
186+
Hashtable headers = new Hashtable();
187+
headers.put("V", "3.3");
188+
headers.put("User-Agent", "Java");
189+
headers.put("Accept-Encoding", "gzip");
190+
req.setHeaders(headers);
191+
req.setConnectionTimeout(PUBNUB_DEFAULT_CONN_TIMEOUT);
195192
if (requestTimeout > 0) {
196-
PerRequestConfig prc = new PerRequestConfig();
197-
prc.setRequestTimeoutInMs(requestTimeout);
198-
rb.setPerRequestConfig(prc);
199-
}
200-
return rb.build();
193+
req.setRequestTimeout(requestTimeout);
194+
} else {
195+
req.setRequestTimeout(PUBNUB_FAST_WEBREQUEST_TIMEOUT);
196+
}
197+
return req;
201198
}
202199

203200
protected void finalize() {
204-
if (ahc != null)
205-
ahc.close();
201+
if (connection != null) {
202+
connection.disconnect();
203+
connection = null;
204+
}
206205
}
207206

208207
public String sessionUUID() {
@@ -294,10 +293,7 @@ private void init(String publish_key, String subscribe_key,
294293
} else {
295294
this.ORIGIN = "http://" + this.ORIGIN;
296295
}
297-
Builder cb = new AsyncHttpClientConfig.Builder();
298-
cb.setRequestTimeoutInMs(PUBNUB_WEBREQUEST_CALLBACK_INTERVAL);
299-
cb.setConnectionTimeoutInMs(PUBNUB_DEFAULT_CONN_TIMEOUT);
300-
ahc = new AsyncHttpClient(cb.build());
296+
301297
}
302298

303299
/**
@@ -558,7 +554,7 @@ private void _subscribe(HashMap<String, Object> args)
558554
return;
559555

560556
// Wait for Message
561-
JSONArray response = _request(new PubnubHttpRequest(getRequest(url), errorMessages));
557+
JSONArray response = _request(new PubnubHttpRequest(getRequest(url, PUBNUB_WEBREQUEST_CALLBACK_INTERVAL), errorMessages));
562558
// Stop Connection?
563559

564560
if (subscriptions.get(channel) != null
@@ -960,6 +956,21 @@ public void unsubscribe(HashMap<String, Object> args) {
960956
}
961957

962958

959+
960+
private String readInput(InputStream in) throws IOException {
961+
ByteArrayOutputStream out = new ByteArrayOutputStream();
962+
byte bytes[] = new byte[1024];
963+
964+
int n = in.read(bytes);
965+
966+
while (n != -1) {
967+
out.write(bytes, 0, n);
968+
n = in.read(bytes);
969+
}
970+
971+
return new String(out.toString());
972+
}
973+
963974
/**
964975
* @param List
965976
* <String> request of url directories.
@@ -970,47 +981,40 @@ private JSONArray _request(PubnubHttpRequest phr) {
970981
String json = "";
971982

972983
try {
973-
// Execute Request
974-
Future<String> f = ahc.executeRequest(phr.request(),
975-
new AsyncCompletionHandler<String>() {
976-
977-
@Override
978-
public String onCompleted(Response r) throws Exception {
979-
980-
String ce = r.getHeader("Content-Encoding");
981-
InputStream resulting_is = null;
982-
InputStream is = r.getResponseBodyAsStream();
983-
984-
if (ce != null && ce.equalsIgnoreCase("gzip")) {
985-
// Decoding using 'gzip'
986-
987-
try {
988-
resulting_is = new GZIPInputStream(is);
989-
} catch (IOException e) {
990-
resulting_is = is;
991-
} catch (Exception e) {
992-
resulting_is = is;
993-
}
994-
} else {
995-
// Default (encoding is null OR 'identity')
996-
resulting_is = is;
997-
}
998-
999-
String line = "", json = "";
1000-
BufferedReader reader = new BufferedReader(
1001-
new InputStreamReader(resulting_is, "UTF8"));
1002-
1003-
// Read JSON Message
1004-
while ((line = reader.readLine()) != null) {
1005-
json += line;
1006-
}
1007-
1008-
reader.close();
1009-
return json;
984+
URL urlobj = new URL(phr.request().getUrl());
985+
connection = (HttpURLConnection) urlobj.openConnection();
986+
connection.setRequestMethod("GET");
987+
Hashtable _headers = phr.request().getHeaders();
988+
if (_headers != null) {
989+
Enumeration en = _headers.keys();
990+
while (en.hasMoreElements()) {
991+
String key = (String) en.nextElement();
992+
String val = (String) _headers.get(key);
993+
connection.addRequestProperty(key, val);
1010994
}
1011-
});
1012-
json = f.get();
1013-
995+
}
996+
997+
connection.setReadTimeout(phr.request().getRequestTimeout());
998+
connection.setConnectTimeout(phr.request().getRequestTimeout());
999+
connection.connect();
1000+
InputStream is;
1001+
if(connection.getContentEncoding() == null || !connection.getContentEncoding().equals("gzip")) {
1002+
is = connection.getInputStream();
1003+
1004+
} else {
1005+
is = new GZIPInputStream(connection.getInputStream());
1006+
}
1007+
json = readInput(is);
1008+
1009+
// Parse JSON String
1010+
if (json.contains("uuids")) {
1011+
return new JSONArray().put(json);
1012+
}
1013+
try {
1014+
return new JSONArray(json);
1015+
} catch (JSONException e) {
1016+
return new JSONArray().put("Error: Failed JSON Parsing.");
1017+
}
10141018
}
10151019
catch (ConnectException e) {
10161020
return new JSONArray().put("0").put("0").put("Network Connect Error");
@@ -1026,16 +1030,6 @@ public String onCompleted(Response r) throws Exception {
10261030
}
10271031
return jsono;
10281032
}
1029-
1030-
// Parse JSON String
1031-
if (json.contains("uuids")) {
1032-
return new JSONArray().put(json);
1033-
}
1034-
try {
1035-
return new JSONArray(json);
1036-
} catch (JSONException e) {
1037-
return new JSONArray().put("Error: Failed JSON Parsing.");
1038-
}
10391033
}
10401034

10411035
private String _encodeURIcomponent(String s) {

‎java/3.3/test/PubnubExample.jar‎

-998 KB
Binary file not shown.

‎java/3.3/test/PubnubUnitTest.jar‎

-998 KB
Binary file not shown.

0 commit comments

Comments
 (0)