0

I'm currently developing a web service with Play2 and I have a problem with action composition.

Here is one of the methods available for my web service :

@Authenticated(Secured.class)
@BodyParser.Of(BodyParser.Json.class)
public static Result createObject() {
    try {
        JsonNode json = request().body().asJson();

        // Retrieve user from request
        User user;
        try {
            user = getUserFromRequest();
        }
        catch (BeanNotFoundException e) {
            return badRequest(Messages.get("userNotFound"));
        }

        // Retrieve owner from user
        Owner owner;
        try {
            owner = getOwnerFromUser(user);
        }
        catch (BeanNotFoundException e) {
            return badRequest(Messages.get("ownerNotFound"));
        }

        // Create the object
        // Here is the code using User and Owner previously found
    }
    catch (BeanValidationException e) {
        return badRequest(JsonUtils.beanValidationMessagesToJson(e));
    }
}

The problem is that I have to repeat the code to retrieve the user and the owner in each method of my web service.

How can I use action composition to do that, since I'm calling the methods in the middle of my main action ? I read the documentation http://www.playframework.com/documentation/2.1.1/JavaActionsComposition but I don't understand how to change the behavior of the action with a simple annotation ?

Thank you

c4k
  • 4,270
  • 4
  • 40
  • 65

1 Answers1

0

There are examples of Play Java action composition here:

https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/play-java/app/controllers/Application.java

Christopher Hunt
  • 2,071
  • 1
  • 16
  • 20
  • Thanks for the answer, but I don't see how I can adapt this example with my use case... How can I create an annotation using the request ? – c4k May 29 '13 at 23:19