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