diff --git a/android/Pubnub-Android-3.5.2.jar b/android/Pubnub-Android-3.5.2.jar index cd09e9215..ab939dcce 100644 Binary files a/android/Pubnub-Android-3.5.2.jar and b/android/Pubnub-Android-3.5.2.jar differ diff --git a/android/examples/PubnubExample/libs/Pubnub-Android-3.5.2.jar b/android/examples/PubnubExample/libs/Pubnub-Android-3.5.2.jar index cd09e9215..ab939dcce 100644 Binary files a/android/examples/PubnubExample/libs/Pubnub-Android-3.5.2.jar and b/android/examples/PubnubExample/libs/Pubnub-Android-3.5.2.jar differ diff --git a/android/examples/SubscribeAtBoot/libs/Pubnub-Android-3.5.2.jar b/android/examples/SubscribeAtBoot/libs/Pubnub-Android-3.5.2.jar index cd09e9215..ab939dcce 100644 Binary files a/android/examples/SubscribeAtBoot/libs/Pubnub-Android-3.5.2.jar and b/android/examples/SubscribeAtBoot/libs/Pubnub-Android-3.5.2.jar differ diff --git a/blackberry/Pubnub-BlackBerry-3.5.2.jar b/blackberry/Pubnub-BlackBerry-3.5.2.jar index dd8c1a6e8..350a0005a 100644 Binary files a/blackberry/Pubnub-BlackBerry-3.5.2.jar and b/blackberry/Pubnub-BlackBerry-3.5.2.jar differ diff --git a/codenameone/pubnub-codenameone-3.5.2.cn1lib b/codenameone/pubnub-codenameone-3.5.2.cn1lib index e8d2badc5..4f99279af 100644 Binary files a/codenameone/pubnub-codenameone-3.5.2.cn1lib and b/codenameone/pubnub-codenameone-3.5.2.cn1lib differ diff --git a/codenameone/src/com/codename1/io/PubnubCn1Connection.java b/codenameone/src/com/codename1/io/PubnubCn1Connection.java index 4444a06a6..c18faea7e 100644 --- a/codenameone/src/com/codename1/io/PubnubCn1Connection.java +++ b/codenameone/src/com/codename1/io/PubnubCn1Connection.java @@ -7,51 +7,72 @@ import com.codename1.impl.CodenameOneImplementation; import com.pubnub.api.HttpUtil; +import java.io.UnsupportedEncodingException; +//import java.util.logging.Level; +//import java.util.logging.Logger; public class PubnubCn1Connection { - private CodenameOneImplementation impl = Util.getImplementation(); - private Object connection = null; - private InputStream input = null; - private byte[] data = null; - - public PubnubCn1Response fetch(String url, int timeout, Hashtable _headers) throws IOException { - int rc = 0; - String page = null; - int contentLength = 0; - - while (HttpUtil.isRedirect(rc) || rc == 0) { - connection = impl.connect(url, true, false, timeout); - impl.setPostRequest(connection, false); - - if(_headers != null) { - Enumeration e = _headers.keys(); - while(e.hasMoreElements()) { - String k = (String)e.nextElement(); - String value = (String)_headers.get(k); - impl.setHeader(connection, k, value); - } - } - rc = impl.getResponseCode(connection); - if(HttpUtil.isRedirect(rc)) { - String uri = impl.getHeaderField("location", connection); - if(!(uri.startsWith("http://") || uri.startsWith("https://"))) { + private CodenameOneImplementation impl = Util.getImplementation(); + private Object connection = null; + private InputStream input = null; + + private byte[] data = null; + + public PubnubCn1Response fetch(String url, int timeout, Hashtable _headers) throws UnsupportedEncodingException, IOException { + int rc = 0; + String page = null; + int contentLength = 0; + + while (HttpUtil.isRedirect(rc) || rc == 0) { + try { + connection = impl.connect(url, true, false, timeout); + } catch (IOException ex) { + throw ex; + } + impl.setPostRequest(connection, false); + + if (_headers != null) { + Enumeration e = _headers.keys(); + while (e.hasMoreElements()) { + String k = (String) e.nextElement(); + String value = (String) _headers.get(k); + impl.setHeader(connection, k, value); + } + } + try { + rc = impl.getResponseCode(connection); + } catch (IOException ex) { + throw ex; + } + if (HttpUtil.isRedirect(rc)) { + String uri; + try { + uri = impl.getHeaderField("location", connection); + } catch (IOException ex) { + throw ex; + } + if (!(uri.startsWith("http://") || uri.startsWith("https://"))) { url = Util.relativeToAbsolute(url, uri); } else { url = uri; } } - } - contentLength = impl.getContentLength(connection); - input = impl.openInputStream(connection); - data = Util.readInputStream(input); - input.close(); + } + contentLength = impl.getContentLength(connection); + try { + input = impl.openInputStream(connection); + data = Util.readInputStream(input); + input.close(); + } catch (IOException ex) { + return new PubnubCn1Response(rc, ex.getMessage() ); + } + input = null; - return new PubnubCn1Response(rc, new String(data, "UTF-8")); - } - public void disconnect() { - } + return new PubnubCn1Response(rc, new String(data, "UTF-8")); + } + public void disconnect() { + } } - diff --git a/codenameone/src/com/pubnub/api/HttpClientCore.java b/codenameone/src/com/pubnub/api/HttpClientCore.java index 66489ab7b..aa42a92cc 100644 --- a/codenameone/src/com/pubnub/api/HttpClientCore.java +++ b/codenameone/src/com/pubnub/api/HttpClientCore.java @@ -1,6 +1,6 @@ package com.pubnub.api; -import static com.pubnub.api.PubnubError.PNERROBJ_READINPUT; +import static com.pubnub.api.PubnubError.*; import static com.pubnub.api.PubnubError.getErrorObject; import java.io.ByteArrayOutputStream; @@ -11,6 +11,7 @@ import com.codename1.impl.CodenameOneImplementation; import com.codename1.io.*; +import org.json.*; class HttpClientCore extends HttpClient { @@ -55,8 +56,45 @@ public HttpResponse fetch(String url) throws PubnubException, IOException { public synchronized HttpResponse fetch(String url, Hashtable headers) throws PubnubException, IOException { - System.out.println(url); - PubnubCn1Response pcr = connection.fetch(url, requestTimeout, headers); + IOException excp = null; + PubnubCn1Response pcr = null; + try { + pcr = connection.fetch(url, requestTimeout, 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()); } diff --git a/codenameone/src/com/pubnub/api/SubscribeWorker.java b/codenameone/src/com/pubnub/api/SubscribeWorker.java index 7ca1b5df6..98abc93e1 100644 --- a/codenameone/src/com/pubnub/api/SubscribeWorker.java +++ b/codenameone/src/com/pubnub/api/SubscribeWorker.java @@ -6,7 +6,7 @@ import static com.pubnub.api.PubnubError.*; class SubscribeWorker extends AbstractSubscribeWorker { - + private Exception excp = null; SubscribeWorker(Vector _requestQueue, int connectionTimeout, int requestTimeout, int maxRetries, int retryInterval, Hashtable headers) { super(_requestQueue, connectionTimeout, requestTimeout, @@ -48,12 +48,12 @@ void process(HttpRequest hreq) { break; } */catch (PubnubException e) { - + excp = e; switch(e.getPubnubError().errorCode) { case PNERR_FORBIDDEN: case PNERR_UNAUTHORIZED: log.verbose("Authentication Failure : " + e.toString()); - currentRetryAttempt = 1; + currentRetryAttempt++; break; default: log.verbose("Retry Attempt : " + ((currentRetryAttempt == maxRetries)?"last":currentRetryAttempt) @@ -63,6 +63,7 @@ void process(HttpRequest hreq) { } } catch (Exception e) { + excp = e; log.verbose("Retry Attempt : " + ((currentRetryAttempt == maxRetries)?"last":currentRetryAttempt) + " Exception in Fetch : " + e.toString()); currentRetryAttempt++; @@ -80,7 +81,12 @@ void process(HttpRequest hreq) { log.verbose("Exhausted number of retries"); hreq.getResponseHandler().handleTimeout(hreq); } else { - hreq.getResponseHandler().handleError(hreq, getErrorObject(PNERROBJ_HTTP_ERROR, 1)); + + if (excp != null && excp instanceof PubnubException && ((PubnubException) excp).getPubnubError() != null) { + hreq.getResponseHandler().handleError(hreq, ((PubnubException) excp).getPubnubError()); + } else { + hreq.getResponseHandler().handleError(hreq, getErrorObject(PNERROBJ_HTTP_ERROR, 1)); + } } return; } diff --git a/j2me/Pubnub-MicroEdition-3.5.2.jar b/j2me/Pubnub-MicroEdition-3.5.2.jar index 5f2a28ac2..f0102c872 100644 Binary files a/j2me/Pubnub-MicroEdition-3.5.2.jar and b/j2me/Pubnub-MicroEdition-3.5.2.jar differ diff --git a/java/Pubnub-StandardEdition-3.5.2.jar b/java/Pubnub-StandardEdition-3.5.2.jar index 3ecc5112d..63382763f 100644 Binary files a/java/Pubnub-StandardEdition-3.5.2.jar and b/java/Pubnub-StandardEdition-3.5.2.jar differ diff --git a/java/doc/allclasses-frame.html b/java/doc/allclasses-frame.html index 1e0fe879d..03b8f040c 100644 --- a/java/doc/allclasses-frame.html +++ b/java/doc/allclasses-frame.html @@ -2,7 +2,7 @@
- +static int
HTTP_BAD_GATEWAY
+
+static int
HTTP_BAD_REQUEST
+
+static int
HTTP_CLIENT_TIMEOUT
+
+static int
HTTP_FORBIDDEN
+
+static int
HTTP_GATEWAY_TIMEOUT
+
+static int
HTTP_INTERNAL_ERROR
+
+static int
HTTP_MOVED_PERM
static int
HTTP_SEE_OTHER
+static int
HTTP_UNAUTHORIZED
+
+public static final int HTTP_FORBIDDEN+
+public static final int HTTP_UNAUTHORIZED+
+public static final int HTTP_BAD_REQUEST+
+public static final int HTTP_BAD_GATEWAY+
+public static final int HTTP_CLIENT_TIMEOUT+
+public static final int HTTP_GATEWAY_TIMEOUT+
+public static final int HTTP_INTERNAL_ERROR+
public static final int
HTTP_BAD_GATEWAY
502
public static final int
HTTP_BAD_REQUEST
400
public static final int
HTTP_CLIENT_TIMEOUT
408
public static final int
HTTP_FORBIDDEN
403
public static final int
HTTP_GATEWAY_TIMEOUT
504
public static final int
HTTP_INTERNAL_ERROR
500
public static final int
HTTP_MOVED_PERM
HTTP_SEE_OTHER
303
public static final int
HTTP_UNAUTHORIZED
401