java - custom annotation based interceptor filter jersey -
i using using jersey rest client. want write custom annotation intercept request can validate user , throw error accordingly. can annotate functions want validate. there 2 problems around it.
as far have seen annotations inject property before actual call. how can control while function has been called. need access @context httpservletrequest request part userid , make db call validate..
classes going annotated annotation parser need know before hand. how can make independent of that. spring annotations.
@mycustomfilter @get @produces(mediatype.application_json) @path("/{programid}/processcount") public response getprograminitializedcount(@pathparam("programid") string programid, @context httpservletrequest request) { //this line moved annotation filter. string userid = wfmanagerutil.getcurrentuser(request);
any ideas around how can achieved.
first edit : research found out how it. not happening. can me in finding out why.
annotation :
@target({elementtype.method, elementtype.type}) @retention(retentionpolicy.runtime) public @interface validateuser { }
filter :
public class validateuserfilter implements containerrequestfilter { @override public containerrequest filter(containerrequest request) throws webapplicationexception{ string user = request.getheadervalue(user_header); //rest of code. } }
binding :
@provider public class resourcefilterbindingfeature implements dynamicfeature { @override public void configure(resourceinfo resourceinfo, featurecontext context) { if (resourceinfo.getresourcemethod().isannotationpresent(validateuser.class)) { context.register(commonresourcefilter.class); } } }
and using :
@path("/v1.0/programs") public class programsresource { @validateuser @get @produces(mediatype.application_json) @path("/{programid}/initialization/progress/") public response getprograminitializedcount(@pathparam("programid") string programid, @context httpservletrequest request) throws workflowmanagerexception { //code } }
pom :
<dependency> <groupid>javax.ws.rs</groupid> <artifactid>javax.ws.rs-api</artifactid> <version>2.0.1</version> </dependency> <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-core</artifactid> <version>1.8</version> <!-- <scope>provided</scope> --> </dependency> <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-server</artifactid> <version>1.8</version> <!-- <scope>provided</scope> --> </dependency>
where possibly doing wrong. filter class not being invoked.
Comments
Post a Comment