Skip to content

Commit 0d72d41

Browse files
author
Devendra
committed
changes to return correct error in case of 403 and 401
1 parent bd132fc commit 0d72d41

33 files changed

Lines changed: 327 additions & 66 deletions

android/Pubnub-Android-3.5.2.jar

156 Bytes
Binary file not shown.
156 Bytes
Binary file not shown.
156 Bytes
Binary file not shown.
160 Bytes
Binary file not shown.
992 Bytes
Binary file not shown.

codenameone/src/com/codename1/io/PubnubCn1Connection.java

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,72 @@
77

88
import com.codename1.impl.CodenameOneImplementation;
99
import com.pubnub.api.HttpUtil;
10+
import java.io.UnsupportedEncodingException;
11+
//import java.util.logging.Level;
12+
//import java.util.logging.Logger;
1013

1114
public class PubnubCn1Connection {
1215

13-
private CodenameOneImplementation impl = Util.getImplementation();
14-
private Object connection = null;
15-
private InputStream input = null;
16-
private byte[] data = null;
17-
18-
public PubnubCn1Response fetch(String url, int timeout, Hashtable _headers) throws IOException {
19-
int rc = 0;
20-
String page = null;
21-
int contentLength = 0;
22-
23-
while (HttpUtil.isRedirect(rc) || rc == 0) {
24-
connection = impl.connect(url, true, false, timeout);
25-
impl.setPostRequest(connection, false);
26-
27-
if(_headers != null) {
28-
Enumeration e = _headers.keys();
29-
while(e.hasMoreElements()) {
30-
String k = (String)e.nextElement();
31-
String value = (String)_headers.get(k);
32-
impl.setHeader(connection, k, value);
33-
}
34-
}
35-
rc = impl.getResponseCode(connection);
36-
if(HttpUtil.isRedirect(rc)) {
37-
String uri = impl.getHeaderField("location", connection);
38-
if(!(uri.startsWith("http://") || uri.startsWith("https://"))) {
16+
private CodenameOneImplementation impl = Util.getImplementation();
17+
private Object connection = null;
18+
private InputStream input = null;
19+
20+
private byte[] data = null;
21+
22+
public PubnubCn1Response fetch(String url, int timeout, Hashtable _headers) throws UnsupportedEncodingException, IOException {
23+
int rc = 0;
24+
String page = null;
25+
int contentLength = 0;
26+
27+
while (HttpUtil.isRedirect(rc) || rc == 0) {
28+
try {
29+
connection = impl.connect(url, true, false, timeout);
30+
} catch (IOException ex) {
31+
throw ex;
32+
}
33+
impl.setPostRequest(connection, false);
34+
35+
if (_headers != null) {
36+
Enumeration e = _headers.keys();
37+
while (e.hasMoreElements()) {
38+
String k = (String) e.nextElement();
39+
String value = (String) _headers.get(k);
40+
impl.setHeader(connection, k, value);
41+
}
42+
}
43+
try {
44+
rc = impl.getResponseCode(connection);
45+
} catch (IOException ex) {
46+
throw ex;
47+
}
48+
if (HttpUtil.isRedirect(rc)) {
49+
String uri;
50+
try {
51+
uri = impl.getHeaderField("location", connection);
52+
} catch (IOException ex) {
53+
throw ex;
54+
}
55+
if (!(uri.startsWith("http://") || uri.startsWith("https://"))) {
3956
url = Util.relativeToAbsolute(url, uri);
4057
} else {
4158
url = uri;
4259
}
4360
}
44-
}
45-
contentLength = impl.getContentLength(connection);
46-
input = impl.openInputStream(connection);
47-
data = Util.readInputStream(input);
48-
input.close();
61+
}
62+
contentLength = impl.getContentLength(connection);
63+
try {
64+
input = impl.openInputStream(connection);
65+
data = Util.readInputStream(input);
66+
input.close();
67+
} catch (IOException ex) {
68+
return new PubnubCn1Response(rc, ex.getMessage() );
69+
}
70+
4971
input = null;
50-
return new PubnubCn1Response(rc, new String(data, "UTF-8"));
51-
}
52-
public void disconnect() {
5372

54-
}
73+
return new PubnubCn1Response(rc, new String(data, "UTF-8"));
74+
}
5575

76+
public void disconnect() {
77+
}
5678
}
57-

codenameone/src/com/pubnub/api/HttpClientCore.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.pubnub.api;
22

3-
import static com.pubnub.api.PubnubError.PNERROBJ_READINPUT;
3+
import static com.pubnub.api.PubnubError.*;
44
import static com.pubnub.api.PubnubError.getErrorObject;
55

66
import java.io.ByteArrayOutputStream;
@@ -11,6 +11,7 @@
1111

1212
import com.codename1.impl.CodenameOneImplementation;
1313
import com.codename1.io.*;
14+
import org.json.*;
1415

1516
class HttpClientCore extends HttpClient {
1617

@@ -55,8 +56,45 @@ public HttpResponse fetch(String url) throws PubnubException, IOException {
5556

5657
public synchronized HttpResponse fetch(String url, Hashtable headers)
5758
throws PubnubException, IOException {
58-
System.out.println(url);
59-
PubnubCn1Response pcr = connection.fetch(url, requestTimeout, headers);
59+
IOException excp = null;
60+
PubnubCn1Response pcr = null;
61+
try {
62+
pcr = connection.fetch(url, requestTimeout, headers);
63+
} catch (IOException ex) {
64+
excp = ex;
65+
}
66+
String page = pcr.getResponse();
67+
switch (pcr.getResponseStatusCode()) {
68+
case HttpUtil.HTTP_FORBIDDEN:
69+
throw new PubnubException(getErrorObject(PNERROBJ_FORBIDDEN, page));
70+
case HttpUtil.HTTP_UNAUTHORIZED:
71+
throw new PubnubException(getErrorObject(PNERROBJ_UNAUTHORIZED, page));
72+
case HttpUtil.HTTP_BAD_REQUEST:
73+
try {
74+
JSONArray jsarr = new JSONArray(page);
75+
String error = jsarr.get(1).toString();
76+
throw new PubnubException(getErrorObject(PNERROBJ_BAD_REQUEST, 1, error));
77+
} catch (JSONException e) {
78+
JSONObject jso;
79+
try {
80+
jso = new JSONObject(page);
81+
throw new PubnubException(getErrorObject(PNERROBJ_BAD_REQUEST, 2, jso.toString()));
82+
} catch (JSONException e1) {
83+
throw new PubnubException(getErrorObject(PNERROBJ_INVALID_JSON, 2));
84+
}
85+
}
86+
case HttpUtil.HTTP_BAD_GATEWAY:
87+
throw new PubnubException(getErrorObject(PNERROBJ_BAD_GATEWAY, url));
88+
case HttpUtil.HTTP_CLIENT_TIMEOUT:
89+
throw new PubnubException(getErrorObject(PNERROBJ_CLIENT_TIMEOUT, url));
90+
case HttpUtil.HTTP_GATEWAY_TIMEOUT:
91+
throw new PubnubException(getErrorObject(PNERROBJ_GATEWAY_TIMEOUT, url));
92+
case HttpUtil.HTTP_INTERNAL_ERROR:
93+
throw new PubnubException(getErrorObject(PNERROBJ_INTERNAL_ERROR, url));
94+
default:
95+
if (excp != null) throw excp;
96+
break;
97+
}
6098
return new HttpResponse(pcr.getResponseStatusCode(), pcr.getResponse());
6199
}
62100

codenameone/src/com/pubnub/api/SubscribeWorker.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import static com.pubnub.api.PubnubError.*;
77

88
class SubscribeWorker extends AbstractSubscribeWorker {
9-
9+
private Exception excp = null;
1010
SubscribeWorker(Vector _requestQueue, int connectionTimeout,
1111
int requestTimeout, int maxRetries, int retryInterval, Hashtable headers) {
1212
super(_requestQueue, connectionTimeout, requestTimeout,
@@ -48,12 +48,12 @@ void process(HttpRequest hreq) {
4848
break;
4949
5050
} */catch (PubnubException e) {
51-
51+
excp = e;
5252
switch(e.getPubnubError().errorCode) {
5353
case PNERR_FORBIDDEN:
5454
case PNERR_UNAUTHORIZED:
5555
log.verbose("Authentication Failure : " + e.toString());
56-
currentRetryAttempt = 1;
56+
currentRetryAttempt++;
5757
break;
5858
default:
5959
log.verbose("Retry Attempt : " + ((currentRetryAttempt == maxRetries)?"last":currentRetryAttempt)
@@ -63,6 +63,7 @@ void process(HttpRequest hreq) {
6363
}
6464

6565
} catch (Exception e) {
66+
excp = e;
6667
log.verbose("Retry Attempt : " + ((currentRetryAttempt == maxRetries)?"last":currentRetryAttempt)
6768
+ " Exception in Fetch : " + e.toString());
6869
currentRetryAttempt++;
@@ -80,7 +81,12 @@ void process(HttpRequest hreq) {
8081
log.verbose("Exhausted number of retries");
8182
hreq.getResponseHandler().handleTimeout(hreq);
8283
} else {
83-
hreq.getResponseHandler().handleError(hreq, getErrorObject(PNERROBJ_HTTP_ERROR, 1));
84+
85+
if (excp != null && excp instanceof PubnubException && ((PubnubException) excp).getPubnubError() != null) {
86+
hreq.getResponseHandler().handleError(hreq, ((PubnubException) excp).getPubnubError());
87+
} else {
88+
hreq.getResponseHandler().handleError(hreq, getErrorObject(PNERROBJ_HTTP_ERROR, 1));
89+
}
8490
}
8591
return;
8692
}

j2me/Pubnub-MicroEdition-3.5.2.jar

157 Bytes
Binary file not shown.
156 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)