pedronveloso
pedronveloso

Reputation: 965

PlayFramework 2.2 Java Action Composition

I've been using Action composition on my Play! apps until now, and they worked fine. However with the recent 2.2.0 update they no longer work and I don't know how to updated them correctly.

That this action for example:

public class ChatMsgValidation extends Action<ChatMsgValidation.ValidChatMsg> {

@With(ChatMsgValidation.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidChatMsg {
}


public Result call(Http.Context ctx) throws Throwable {

    Utils.debugFunctionCall("ValidChatMsg() " + ctx.toString());

    // validate we got the "player" parameter
    JsonNode jsonRequest = request().body().asJson();
    if (!WSUtils.validateJSONField(Constants.JSON_MSG, jsonRequest)) {
        return badRequest(WSUtils.simpleMissingFieldMsg(Constants.JSON_MSG));
    }

    RequestParser requestParser = new RequestParser(request());
    String chatMsg = requestParser.getMessage();

    if (chatMsg.isEmpty()) {
        return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.cannot.be.empty.error"), FailConstants.REASON_EMPTY));
    }


    if (chatMsg.length() < Constants.MIN_CHAT_MESSAGE_LENGTH) {
        return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("query.lower.limit.error"), FailConstants.REASON_TOO_SHORT));
    }

    if (chatMsg.length() > Constants.MAX_CHAT_MESSAGE_LENGTH) {
        return badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.too.long.error"), FailConstants.REASON_TOO_LONG));
    }

    return delegate.call(ctx);
}
}

Problem is now the "call" method should return "Promise" instead of "Result", and I can't figure out a way to return a simple JSON message without doing a LOT of code, useless code because I'm creating dummy functions just to have Promises. There has to be a better way I'm not seeing, please advise.

Upvotes: 6

Views: 2496

Answers (1)

pedronveloso
pedronveloso

Reputation: 965

I figured out a better solution for my problem. It is as follows:

return F.Promise.pure((SimpleResult) badRequest(WSUtils.simpleFailureMsgWithReason(Messages.get("message.cannot.be.empty.error"), FailConstants.REASON_EMPTY)));

Upvotes: 9

Related Questions