Example #1
0
  private int validateId(final Token idToken) {
    if (idToken.getType() != TokenType.INTEGER) {
      throw new CommandArgumentExcpetion("Id must be a number!");
    }

    final Integer id = idToken.asInteger();

    if (id < 0) {
      throw new CommandArgumentExcpetion("Id must not be negative!");
    }

    if (id > getInbox().count() - 1) {
      throw new CommandArgumentExcpetion(
          String.format("Id must not be less than %d!", getInbox().count()));
    }

    return id;
  }
Example #2
0
  private void answerMessage() {
    final Token idToken = getArguments().get(0);
    final int id = validateId(idToken);
    final Message message = getInbox().get(id);
    final Token messageBodyToken = getArguments().get(1);
    final Message answer =
        Messaging.newTextMessage(newLocalAddress(), message.getFrom(), messageBodyToken.asString());

    try {
      Messaging.newSender().send(answer);
    } catch (IOException ex) {
      throw new CommandRuntimeException(
          String.format("Can't send message to %s!", message.getFrom()), ex);
    }

    answer.markAsRead();
    getApplicationContext().getOutbox().put(answer);
  }