[Play-Java] Conversion 2.5 -> 2.8

Dear

I’m trying to convert the following Play 2.5 code into Play 2.8:

   public class Perms extends Action.Simple {
      @Inject
       private UsersRepository users;

    @Override
    public CompletionStage<Result> call(Http.Context ctx) {
        Users user = users.findById(session("id"));
        List<Permissions> perms = user.getPermissions();
        ctx.args.put("perms", perms);
        return delegate.call(ctx);
    }

    public static List<Permissions> current() {
        return (List<Permissions>) Http.Context.current().args.get("perms");
    }

The following code seems to have errors because IntelliJ gives me the message that the put() method wants a TypedKey key.

 public class Perms extends Action.Simple {

    @Inject
    private UserRepository userRepository;

    @Override
    public CompletionStage<Result> call(Http.Request request) {
        User user = userRepository.findByUserId(request.session().get("id").orElse(null));
        List<Permissions> perms = user.getPermissions();
        request.attrs().put("perms", perms);
        return delegate.call(request);
    }

    public static List<Permissions> current(Http.Request request) {
        return (List<Permissions>) request.attrs().get("perms");
    }

}

How can I solve it?

Thanks

}

I tried as follow:

public class Perms extends Action.Simple {

    @Inject
    private UserRepository userRepository;

    @Override
    public CompletionStage<Result> call(Http.Request request) {


        User user = userRepository.findByUserId(request.session().get("id").orElse(null));
        List<Permissions> perms = user.getPermissions();
        TypedKey tkey = TypedKey.create("perms");
        request.attrs().put(tkey, perms);
        return delegate.call(request);
    }

    public static List<Permissions> current(Http.Request request) {
        TypedKey tkey = TypedKey.create("perms");
        return (List<Permissions>) request.attrs().get(tkey);
    }

}

Is it correct?

No. TypedKey should be a singleton instance, i.e. static final. The string parameter taken by the create method is not an ID for the TypedKey. It’s simply a human readable name and it’s completely optional.

Also, the request attrs is immutable. So calling put doesn’t modify the original attrs. It creates a modified copy.

You need to do something like this:

	public class Perms extends Action.Simple {

		public static final TypedKey<List<Permissions>> PERMS_KEY = TypedKey.create();

	    @Override
	    public CompletionStage<Result> call(Http.Request request) {
	        List<Permissions> perms = Collections.emptyList();
	        // this creates a new copy of the request with the added attr
	        return delegate.call(request.addAttr(PERMS_KEY, perms));
	    }

	    public static List<Permissions> current(Http.Request request) {
	        return request.attrs().get(PERMS_KEY);
	    }

	}

Thank you very much.