public synchronized String transferFromBankToPlayer(Request request, Response response) { lamport.Add(); if (banks.containsKey(request.params(":gameid"))) { Collection<Account> accounts = banks.get(request.params(":gameid")); for (Account account : accounts) { if (account.getPlayer().getId().equals(request.params(":to"))) { int am = Integer.parseInt(request.params(":amount")); account.AddAmount(am); banks.put(request.params(":gameid"), accounts); String uri = localUri + "transfers/" + UniqueId++; Transfer trans = new Transfer( request.params(":from"), request.params(":to"), am, request.body(), "", uri); Event event = new Event( "bank transfer", request.body(), request.body(), uri, account.getPlayer().getId()); transfers.add(trans); BanksService.sendUpdate(trans, request.params(":gameid")); response.status(201); return gson.toJson(event); } } response.status(404); return "Player not found."; } else { response.status(404); return "Game not found."; } }
@Override public Object handle(final Request request, final Response response) throws Exception { if (!CreateGameDTO.isValidRequestJson(request.body())) { response.status(400); return BAD_JSON_MESSAGE; } final CommandExecutionResult result = GamesController.createGame(new CreateGameDTO(request.body())); if (result.errorOccurred()) { response.status(result.getStatus()); } else { response.status(200); } // set any new cookies if (result.hasNewCookies()) { Map<String, String> cookies = result.getNewCookies(); for (String key : cookies.keySet()) { Cookie cookie = new Cookie(key, cookies.get(key)); cookie.setPath("/"); response.raw().addCookie(cookie); } } return result.getBody(); }
@Override public Object handle(Request request, Response response) throws Exception { try { String serviceName = request.params(":serviceName"); deleteServiceJob(serviceName, user, password, tenant, dbClient); response.status(HttpConstants.HTTP_OK); response.type(HttpConstants.APPLICATION_JSON); return "Deletion scheduled for service " + serviceName; } catch (Exception e) { response.status(HTTP_BAD_REQUEST); return e.toString(); } }
private String upsertUser(Request request, Response response) throws UsersRepositoryException { try { return userService.upsertUser( request.params(USER_NAME), jsonExtractor.parse(request.body()).getPassword(), response); } catch (JsonExtractException e) { LOGGER.info("Error while deserializing addUser request", e); response.status(400); return Constants.EMPTY_BODY; } catch (IllegalArgumentException e) { LOGGER.info("Invalid user path", e); response.status(400); return Constants.EMPTY_BODY; } }
private String removeUser(Request request, Response response) { String username = request.params(USER_NAME); try { userService.removeUser(username); response.status(204); return Constants.EMPTY_BODY; } catch (UsersRepositoryException e) { response.status(204); return "The user " + username + " does not exists"; } catch (IllegalArgumentException e) { LOGGER.info("Invalid user path", e); response.status(400); return Constants.EMPTY_BODY; } }
@Override public void status(int statusCode) { delegate.status(statusCode); }
public synchronized String updateAccounts(Request request, Response response) { Transaction json = gson.fromJson(request.body(), Transaction.class); if (json.getTimestamp() > lamport.getTime()) { lamport.Update(json.getTimestamp()); lamport.Add(); Unirest.get(replication + "/sync/all"); return "replacing database."; } else { lamport.Add(); for (Transaction transaction : transactions) { if (transaction.getTransfer().equals(json.getTransfer()) && transaction.getTimestamp() == json.getTimestamp()) { return "failure, transfer already exists."; } } if (banks.containsKey(request.params(":gameid"))) { Collection<Account> accounts = banks.get(request.params(":gameid")); for (Account account : accounts) { if (account.getPlayer().getId().equals(request.params(":from"))) { int am = Integer.parseInt(request.params(":amount")); if (account.getSaldo() >= am) { account.SubtractAmount(am); banks.put(request.params(":gameid"), accounts); String uri = localUri + "transfers/" + UniqueId++; Transfer trans = new Transfer( request.params(":from"), request.params(":to"), am, request.body(), "", uri); Event event = new Event( "bank transfer", request.body(), request.body(), uri, account.getPlayer().getId()); transfers.add(trans); Transaction transaction = new Transaction(trans, lamport.getTime()); transactions.add(transaction); BanksService.sendUpdate(trans, request.params(":gameid")); response.status(201); return gson.toJson(event); } else { response.status(403); return "Insufficent funds."; } } } response.status(404); return "Player not found."; } // from Bank to Player else if (json.getTransfer().getFrom().equals("bank")) { if (banks.containsKey(request.params(":gameid"))) { Collection<Account> accounts = banks.get(request.params(":gameid")); for (Account account : accounts) { if (account.getPlayer().getId().equals(request.params(":to"))) { int am = Integer.parseInt(request.params(":amount")); account.AddAmount(am); banks.put(request.params(":gameid"), accounts); String uri = localUri + "transfers/" + UniqueId++; Transfer trans = new Transfer( request.params(":from"), request.params(":to"), am, request.body(), "", uri); Event event = new Event( "bank transfer", request.body(), request.body(), uri, account.getPlayer().getId()); transfers.add(trans); BanksService.sendUpdate(trans, request.params(":gameid")); response.status(201); return gson.toJson(event); } } response.status(404); return "Player not found."; } // from Player to Player else { Collection<Event> events = new ArrayList<Event>(); boolean transactionSuccessful = false; Transfer trans = null; Collection<Account> accounts = banks.get(request.params(":gameid")); Collection<Account> copy = new ArrayList<>(); copy.addAll(accounts); for (Account account : copy) { if (account.getPlayer().getId().equals(request.params(":from"))) { int am = Integer.parseInt(request.params(":amount")); if (account.getSaldo() >= am) { account.SubtractAmount(am); break; } else { response.status(403); return "Insufficent funds."; } } } for (Account account : copy) { if (account.getPlayer().getId().equals(request.params(":to"))) { int am = Integer.parseInt(request.params(":amount")); account.AddAmount(am); String uri = localUri + "transfers/" + UniqueId++; trans = new Transfer( request.params(":from"), request.params(":to"), am, request.body(), "", uri); Event event = new Event( "bank transfer", request.body(), request.body(), uri, account.getPlayer().getId()); events.add(event); transactionSuccessful = true; } } if (transactionSuccessful) { // transfer was successful, time to "commit" the changes! banks.put(request.params(":gameid"), copy); transfers.add(trans); Transaction transaction = new Transaction(trans, lamport.getTime()); transactions.add(transaction); BanksService.sendUpdate(trans, request.params(":gameid")); response.status(201); return gson.toJson(events); } else { // OH OH! Something went wrong! But we actually didn't change anything, so we're good. response.status(404); return "Player not found."; } } } else { return "Game not found."; } } }