99import java .io .UnsupportedEncodingException ;
1010import java .math .BigInteger ;
1111import java .net .ConnectException ;
12+ import java .net .HttpURLConnection ;
1213import java .net .SocketTimeoutException ;
14+ import java .net .URL ;
1315import java .net .URLEncoder ;
1416import java .security .Key ;
1517import java .security .MessageDigest ;
1618import java .security .NoSuchAlgorithmException ;
1719import java .util .ArrayList ;
1820import java .util .Arrays ;
21+ import java .util .Enumeration ;
1922import java .util .HashMap ;
23+ import java .util .Hashtable ;
2024import java .util .Iterator ;
2125import java .util .List ;
2226import java .util .UUID ;
3438import org .json .JSONException ;
3539import 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 ) {
0 commit comments