title: Action composition and async in Play Framework 2.5 in Java
body: <p>I want to create an action that I can use with the <code>@With</code> annotation style. This action will need to proceed to an RPC call so if I understood correctly the documentation I should rather put this in an async way.</p>
  
  <p>This is what I tried to do until now:</p>
  
  <pre><code>public class GetUserIdAction extends play.mvc.Action.Simple {
  
    @Override
    public CompletionStage&lt;Result&gt; call(Http.Context context) {
      String token = "";
  
      if (StringUtils.isEmpty(token)) {
        return delegate.call(context);
      }
  
      CompletionStage&lt;Http.Context&gt; promiseOfUpdatedContext = CompletableFuture.supplyAsync(() -&gt; setUserIdForToken(context, token));
  
      return promiseOfUpdatedContext.thenApply(ctx -&gt; delegate.call(ctx));
    }
  
    private Http.Context setUserIdForToken(Http.Context context, String token) {
      context.args.put("user_id", authenticationManager.getUserIdForToken(token));
      // The AuthenticationManager is issuing an RPC call and thus may take some time to complete.
      return context;
    }
  }
  </code></pre>
  
  <p>Set aside the fact that <code>token</code> is always empty and <code>authenticationManager</code> is not set, this is just a quick meaningless example, my IDE is complaining on the <code>thenApply</code> part. For what I understand, it is expecting a <code>CompletionStage&lt;Result&gt;</code> and gets something more like a <code>CompletionStage&lt;CompletionStage&lt;Result&gt;&gt;</code>.</p>
  
  <p>What is a way to deal with it? Cause here all I want is to put some information in the Context and then continue the <code>delegate.call</code> chain.</p>
  
  <p>Or maybe I'm trying to do something stupid and composed actions are already asynchronous?</p>