Reputation: 3170
I am Using Play 2.3 (Java).
I have a onRequest
method inside my Global.java
file.
Following some answers on Stackoverflow and other resources, my Global.java
is like -
private static ArrayList<String> admin_users = new ArrayList<String>();
public static ArrayList<String> getAdmin_users() {
return admin_users;
}
public static void setAdmin_users(ArrayList<String> admin_users) {
Global.admin_users = admin_users;
}
@Override
public void onStart(play.Application arg0) {
ArrayList<String> admins = new ArrayList<String>();
admins.add("aahuja");
admins.add("chlr");
admins.add("bobba");
setAdmin_users(admins);
}
private static ArrayList<String> admin_users = new ArrayList<String>();
public static ArrayList<String> getAdmin_users() {
return admin_users;
}
public static void setAdmin_users(ArrayList<String> admin_users) {
Global.admin_users = admin_users;
}
@Override
public void onStart(play.Application arg0) {
ArrayList<String> admins = new ArrayList<String>();
admins.add("aahuja");
admins.add("chlr");
admins.add("bobba");
setAdmin_users(admins);
}
private class ActionWrapper extends Action.Simple {
private String user;
public ActionWrapper(Action<?> action, String user) {
this.delegate = action;
this.user = user;
}
@Override
public Promise<Result> call(Http.Context ctx) throws java.lang.Throwable {
Promise<Result> result = this.delegate.call(ctx);
ctx.args.put("Name", this.user);
}
@Override
public Action<?> onRequest(Http.Request request, java.lang.reflect.Method actionMethod) {
if(request.getHeader("OriginalName") != null){
if(getAdmin_users().contains(request.getHeader("OriginalName"))){
if(request.hasHeader("IMPERSONATE") && request.getHeader("IMPERSONATE").equals("true")){
return new ActionWrapper(super.onRequest(request, actionMethod), request.getHeader("IMPERSONATE-IDENTITY"));
}
}
return new ActionWrapper(super.onRequest(request, actionMethod), request.getHeader("OriginalName"));
}
else
return super.onRequest(request, actionMethod);
}
Now, inside my controller file, I have a method like this -
public static Promise<Result> all() {
String name = (String) ctx().args.get("Name");
System.out.println(name);
// rest of code
}
Now when I pass a request to this application with the header information like -
OriginalName: abcd
, I get a value of null
inside my controller printed out.
Is ctx
the correct way to pass data? If not whats the correct way.
I am trying to take the redundant logic out of the main business logic.
The same can also be achieved if we are able to modify the header information when it gets passed from the interceptor to the controller. But I cant find any suitable way of doing so.
Upvotes: 3
Views: 758
Reputation: 407
Try to put the ctx.args.put() before calling the delegate:
@Override
public Promise<Result> call(Http.Context ctx) throws java.lang.Throwable {
ctx.args.put("Name", this.user);
Promise<Result> result = this.delegate.call(ctx);
}
You are setting the value afther your action run. So you cannot access the value inside your action.
Upvotes: 1