Get url params in action compositions

I have an Interceptor (Action composition) defined for my controllers. I need to access url parameters in request

i.e. to enter conf below, how do I access the Id parameter inside my Action composition?

GET /jobs/:id controllers.JobManager.getlist(id: Int)

my action method hook class only refers to my Http.Context object. while accessing the body of the request is obvious that the URL is not.

+4
source share
1 answer

Take it out yourself. In your example, the path is 6 characters long plus the id length.

String path = ctx.request().path();
String id = path.substring(6, path.length());

This decision depends on the length of the route. In addition, you can pass the start and end parameters to your action:

@With({ ArgsAction.class })
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Args {

    int start() default -1;
    int end() default -1;

}

action :

public class ArgsAction extends Action<Args> {

    @Override
    public Promise<Result> call(Context ctx) throws Throwable {
        final int start = configuration.start();
        final int end = configuration.end();
        if (start != -1) {
            final String path = ctx.request().path();
            String arg = null;
            if (end != -1) {
                arg = path.substring(start, end);
            } else {
                arg = path.substring(start, path.length());
            }
            // Do something with arg...
        }
        return delegate.call(ctx);
    }
}

JobManager:

@Args(start = 6)
public getlist(Integer id) {
    return ok();
}
+1

All Articles