Skip to content

Instantly share code, notes, and snippets.

@slomo
Forked from miguelcardo/read-result.java
Last active December 1, 2015 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slomo/fa2b915f28400b123ab1 to your computer and use it in GitHub Desktop.
Save slomo/fa2b915f28400b123ab1 to your computer and use it in GitHub Desktop.
Check operation result
// handles response from the Read MIFARE Data message
post(new Route(Constants.readBlockCallbackUrl) {
@Override
public Object handle(Request request, Response response) {
JSONObject jsonParameters = (JSONObject) JSONValue.parse(request.body());
String operationId = (String) jsonParameters.get("operationId");
String sessionId = pendingOperations.get(operationId);
pendingOperations.remove(operationId);
int statusCode = ((Long)jsonParameters.get("statusCode")).intValue();
if (statusCode != HTTP_OK)
{
handleFailure(sessionId);
} else {
// parse the counter value from the read block, increment
// unless it is the first time the card is used, then initialize to one
// store counter and checksum in the session state map
JSONArray blocks = (JSONArray) jsonParameters.get("blocks");
long counter = Utils.decodeCounter(blocks);
sessionMap.get(sessionId).setCounter(counter);
if (sessionMap.get(sessionId).isFirstTime()) {
sessionMap.get(sessionId).setCounter(1);
if (cardClient.writeCounterInCard(1, sessionId, pendingOperations) != HTTP_OK) handleFailure(sessionId);
} else {
counter ++;
if (cardClient.writeCounterInCard(counter, sessionId, pendingOperations) != HTTP_OK) handleFailure(sessionId);
}
}
response.status(HTTP_OK);
return "";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment