Skip to content

Commit

Permalink
Add KegbotApiException parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed May 29, 2014
1 parent e762499 commit bc21566
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
74 changes: 70 additions & 4 deletions kegtab/src/main/java/org/kegbot/api/KegbotApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.kegbot.api;

import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.google.common.base.Joiner;
Expand All @@ -38,6 +39,8 @@

import org.apache.http.NameValuePair;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.kegbot.app.KegbotApplication;
import org.kegbot.app.config.AppConfiguration;
Expand Down Expand Up @@ -216,25 +219,84 @@ private Request.Builder newRequest(final String path) {

private JsonNode requestJson(Request request) throws KegbotApiException {
final Response response;
final long startTime = SystemClock.elapsedRealtime();
try {
response = mClient.newCall(request).execute();
} catch (IOException e) {
Log.w(TAG, String.format("--> %s %s [ERR]", request.method(), request.urlString()));
throw new KegbotApiException(e);
}
final long endTime = SystemClock.elapsedRealtime();

final int responseCode = response.code();
final String logMessage = String.format("--> %s %s [%s] %sms", request.method(), request.urlString(),
responseCode, endTime - startTime);
if (responseCode >= 200 && responseCode < 300) {
Log.d(TAG, logMessage);
} else {
Log.w(TAG, logMessage);
}
final ResponseBody body = response.body();

final JsonNode rootNode;
try {
try {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode rootNode = mapper.readValue(body.byteStream(), JsonNode.class);
return rootNode;
rootNode = mapper.readValue(body.byteStream(), JsonNode.class);
} finally {
body.close();
}
} catch (JsonParseException e) {
throw new KegbotApiMalformedResponseException(e);
} catch (JsonMappingException e) {
throw new KegbotApiMalformedResponseException(e);
} catch (IOException e) {
throw new KegbotApiException(e);
}

boolean success = false;
try {
// Handle structural errors.
if (!rootNode.has("meta")) {
throw new KegbotApiMalformedResponseException("Response is missing 'meta' field.");
}
final JsonNode meta = rootNode.get("meta");
if (!meta.isContainerNode()) {
throw new KegbotApiMalformedResponseException("'meta' field is wrong type.");
}

final String message;
if (rootNode.has("error") && rootNode.get("error").has("message")) {
message = rootNode.get("error").get("message").getTextValue();
} else {
message = null;
}

// Handle HTTP errors.
if (responseCode < 200 || responseCode >= 400) {
switch (responseCode) {
case 401:
throw new NotAuthorizedException(message);
case 404:
throw new KegbotApi404(message);
case 405:
throw new MethodNotAllowedException(message);
default:
if (message != null) {
throw new KegbotApiServerError(message);
} else {
throw new KegbotApiServerError("Server error, response code=" + responseCode);
}
}
}

success = true;
return rootNode;
} finally {
if (!success) {
Log.d(TAG, "Response JSON was: " + rootNode.toString());
}
}
}

private JsonNode getJson(String path) throws KegbotApiException {
Expand Down Expand Up @@ -384,8 +446,12 @@ public void deleteTap(KegTap tap) {
@Override
public AuthenticationToken getAuthToken(String authDevice, String tokenValue)
throws KegbotApiException {
return getSingleProto("/auth-tokens/" + authDevice + "/" + tokenValue + "/",
AuthenticationToken.newBuilder());
try {
return getSingleProto("/auth-tokens/" + authDevice + "/" + tokenValue + "/",
AuthenticationToken.newBuilder());
} catch (KegbotApi404 e) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2014 Bevbot LLC <[email protected]>
*
* This file is part of the Kegtab package from the Kegbot project. For
* more information on Kegtab or Kegbot, see <http://kegbot.org/>.
*
* Kegtab is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, version 2.
*
* Kegtab is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with Kegtab. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kegbot.api;

/** Thrown when the server does not return proper JSON. */
public class KegbotApiMalformedResponseException extends KegbotApiException {

public KegbotApiMalformedResponseException() {
super();
}

public KegbotApiMalformedResponseException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}

public KegbotApiMalformedResponseException(String detailMessage) {
super(detailMessage);
}

public KegbotApiMalformedResponseException(Throwable throwable) {
super(throwable);
}

}
3 changes: 1 addition & 2 deletions kegtab/src/main/java/org/kegbot/backend/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public User createUser(String username, String email, String password, String im
/**
* Returns the authentication token record for the given token.
*
* @return the token record
* @throws NotFoundException if there is no record for this token.
* @return the token record, or {@code null} if not assigned
*/
public AuthenticationToken getAuthToken(String authDevice, String tokenValue)
throws BackendException;
Expand Down
2 changes: 1 addition & 1 deletion kegtab/src/main/java/org/kegbot/backend/LocalBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Keg endKeg(Keg keg) throws BackendException {
@Override
public AuthenticationToken getAuthToken(String authDevice, String tokenValue)
throws BackendException {
throw new NotFoundException("No token.");
return null; // Not Implemented
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ private User fetchUserForToken(AuthenticationToken token) throws BackendExceptio
org.kegbot.proto.Models.AuthenticationToken tok = mApi.getAuthToken(token
.getAuthDevice(), token.getTokenValue());
Log.d(TAG, "Got auth token: " + tok);
if (tok == null) {
throw new NotFoundException("Unknown token.");
}
if (!tok.getEnabled()) {
throw new NotFoundException("Token not enabled.");
} else if (!tok.hasUser()) {
Expand Down

0 comments on commit bc21566

Please sign in to comment.