Пример #1
1
 private String getDate(String timeStampStr, String format) {
   DateFormat formatter = new SimpleDateFormat(format);
   long timeStamp = Long.parseLong(timeStampStr);
   Calendar calendar = Calendar.getInstance();
   calendar.setTimeInMillis(timeStamp);
   return formatter.format(calendar.getTime());
 }
Пример #2
0
  private List<Long> parseUsersIdsFromFile(String listFilePath) throws ServletException {

    List<Long> ids = new ArrayList<Long>();
    try {
      BufferedReader fileReader = new BufferedReader(new FileReader(listFilePath));
      String currentLine = fileReader.readLine();
      while (currentLine != null) {
        LOGGER.debug(currentLine);
        long id = Long.parseLong(currentLine);
        ids.add(id);
        currentLine = fileReader.readLine();
      }
      fileReader.close();
    } catch (FileNotFoundException e) {
      String emsg = "cant find file '" + listFilePath + "'";
      throw new ServletException(emsg, e);
    } catch (IOException e) {
      String emsg = "cant read from file '" + listFilePath + "'";
      throw new ServletException(emsg, e);
    }
    return ids;
  }
Пример #3
0
  private void setupRoutes() {
    get(
        "/transactionservice/transaction/:id",
        (req, res) -> {
          try {
            long id = Long.parseLong(req.params(":id"));
            Transaction tx = store.getTxById(id);
            if (tx == null) {
              res.status(HTTP_NOT_FOUND);
              return errorUnknownIdBody;
            }
            return gson.toJson(tx);
          } catch (Exception e) {
            res.status(HTTP_REQUEST_ERROR);
            return errorInvalidBody;
          }
        });

    // Queries for a type will always succeed: If there is no transaction, the (valid) response is
    // an empty list.
    get(
        "/transactionservice/types/:type",
        (req, res) -> gson.toJson(store.getTxIdsByType(req.params(":type"))));

    get(
        "/transactionservice/sum/:id",
        (req, res) -> {
          try {
            long id = Long.parseLong(req.params(":id"));
            double sum = store.getSum(id);
            JsonObject resJson = new JsonObject();
            resJson.addProperty("sum", sum);
            return resJson;
          } catch (TransactionNotFoundException e) {
            res.status(HTTP_NOT_FOUND);
            return errorUnknownIdBody;
          } catch (Exception e) {
            res.status(HTTP_REQUEST_ERROR);
            return errorInvalidBody;
          }
        });

    put(
        "/transactionservice/transaction/:id",
        (req, res) -> {
          try {
            JsonObject input = (JsonObject) new JsonParser().parse(req.body());
            long id = Long.parseLong(req.params(":id"));
            double amount = input.get("amount").getAsDouble();
            // TODO: Specify and enforce allowed characters in the type string. Currently, all
            // strings are valid.
            String type = input.get("type").getAsString();

            // Parent ID is optional; set to zero if not provided
            long parentId = 0L;
            if (input.has("parent")) {
              parentId = input.get("parent").getAsLong();
            }

            store.addTx(id, amount, type, parentId);
          } catch (DuplicateTransactionException e) {
            res.status(HTTP_CONFLICT);
            return errorDuplicateBody;
          } catch (InvalidParentTransactionException e) {
            res.status(HTTP_REQUEST_ERROR);
            return errorInvalidParent;
          } catch (Exception e) {
            // Something else went wrong (e.g. unparseable request format)
            res.status(HTTP_REQUEST_ERROR);
            return errorInvalidBody;
          }

          // If everything went OK, the resource has now been created: Return the appropriate HTTP
          // status
          res.status(HTTP_CREATED);
          return okBody;
        });
  }