Throw a GfeHttpResponseException if an HTTP response has an error code

This commit is contained in:
Cameron Gutman 2014-01-09 23:43:59 -06:00
parent 616945a963
commit ade061bf3c
2 changed files with 35 additions and 2 deletions

View File

@ -0,0 +1,20 @@
package com.limelight.nvstream.http;
import java.io.IOException;
public class GfeHttpResponseException extends IOException {
private static final long serialVersionUID = 1543508830807804222L;
private int errorCode;
private String errorMsg;
public GfeHttpResponseException(int errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
@Override
public String getMessage() {
return "GFE error: "+errorMsg+" (Error code: "+errorCode+")";
}
}

View File

@ -42,6 +42,9 @@ public class NvHTTP {
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) { switch (eventType) {
case (XmlPullParser.START_TAG): case (XmlPullParser.START_TAG):
if (xpp.getName().equals("root")) {
verifyResponseStatus(xpp);
}
currentTag.push(xpp.getName()); currentTag.push(xpp.getName());
break; break;
case (XmlPullParser.END_TAG): case (XmlPullParser.END_TAG):
@ -59,6 +62,13 @@ public class NvHTTP {
return null; return null;
} }
private void verifyResponseStatus(XmlPullParser xpp) throws GfeHttpResponseException {
int statusCode = Integer.parseInt(xpp.getAttributeValue(XmlPullParser.NO_NAMESPACE, "status_code"));
if (statusCode != 200) {
throw new GfeHttpResponseException(statusCode, xpp.getAttributeValue(XmlPullParser.NO_NAMESPACE, "status_message"));
}
}
private InputStream openHttpConnection(String url) throws IOException { private InputStream openHttpConnection(String url) throws IOException {
URLConnection conn = new URL(url).openConnection(); URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setConnectTimeout(CONNECTION_TIMEOUT);
@ -102,7 +112,7 @@ public class NvHTTP {
return null; return null;
} }
public LinkedList<NvApp> getAppList() throws IOException, XmlPullParserException { public LinkedList<NvApp> getAppList() throws GfeHttpResponseException, IOException, XmlPullParserException {
InputStream in = openHttpConnection(baseUrl + "/applist?uniqueid=" + uniqueId); InputStream in = openHttpConnection(baseUrl + "/applist?uniqueid=" + uniqueId);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
@ -116,6 +126,9 @@ public class NvHTTP {
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) { switch (eventType) {
case (XmlPullParser.START_TAG): case (XmlPullParser.START_TAG):
if (xpp.getName().equals("root")) {
verifyResponseStatus(xpp);
}
currentTag.push(xpp.getName()); currentTag.push(xpp.getName());
if (xpp.getName().equals("App")) { if (xpp.getName().equals("App")) {
appList.addLast(new NvApp()); appList.addLast(new NvApp());