Skip to content

Commit

Permalink
changes to return correct error in case of 403 and 401
Browse files Browse the repository at this point in the history
  • Loading branch information
Devendra committed Jul 4, 2013
1 parent bd132fc commit 0d72d41
Show file tree
Hide file tree
Showing 33 changed files with 327 additions and 66 deletions.
Binary file modified android/Pubnub-Android-3.5.2.jar
Binary file not shown.
Binary file modified android/examples/PubnubExample/libs/Pubnub-Android-3.5.2.jar
Binary file not shown.
Binary file modified android/examples/SubscribeAtBoot/libs/Pubnub-Android-3.5.2.jar
Binary file not shown.
Binary file modified blackberry/Pubnub-BlackBerry-3.5.2.jar
Binary file not shown.
Binary file modified codenameone/pubnub-codenameone-3.5.2.cn1lib
Binary file not shown.
93 changes: 57 additions & 36 deletions codenameone/src/com/codename1/io/PubnubCn1Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}

44 changes: 41 additions & 3 deletions codenameone/src/com/pubnub/api/HttpClientCore.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +11,7 @@

import com.codename1.impl.CodenameOneImplementation;
import com.codename1.io.*;
import org.json.*;

class HttpClientCore extends HttpClient {

Expand Down Expand Up @@ -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());
}

Expand Down
14 changes: 10 additions & 4 deletions codenameone/src/com/pubnub/api/SubscribeWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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++;
Expand All @@ -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;
}
Expand Down
Binary file modified j2me/Pubnub-MicroEdition-3.5.2.jar
Binary file not shown.
Binary file modified java/Pubnub-StandardEdition-3.5.2.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion java/doc/allclasses-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 17:15:29 IST 2013 -->
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 23:21:17 IST 2013 -->
<TITLE>
All Classes
</TITLE>
Expand Down
2 changes: 1 addition & 1 deletion java/doc/allclasses-noframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 17:15:29 IST 2013 -->
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 23:21:17 IST 2013 -->
<TITLE>
All Classes
</TITLE>
Expand Down
2 changes: 1 addition & 1 deletion java/doc/com/pubnub/api/Callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 17:15:28 IST 2013 -->
<!-- Generated by javadoc (build 1.6.0_37) on Thu Jul 04 23:21:16 IST 2013 -->
<TITLE>
Callback
</TITLE>
Expand Down
Loading

0 comments on commit 0d72d41

Please sign in to comment.