java - Servlet Filter not working when exception is thrown -
what trying do?
i trying generate new timestamped tokens server side client can use in subsequent request
what have tried?
i have servlet filter wraps around rest
calls , looks like
@webfilter(urlpatterns = "/rest/secure") public class securityfilter implements filter { private static final pattern pattern = pattern.compile(":"); private static final logger logger = loggerfactory.getlogger(securityfilter.class); @override public void init(final filterconfig filterconfig) throws servletexception { //logger.info("initializing securityfilter"); } @override public void dofilter(final servletrequest request, final servletresponse response, final filterchain chain) throws ioexception, servletexception { final httpservletresponse httpservletresponse = (httpservletresponse) response; final string authtoken = getauthenticationheadervalue((httpservletrequest) request); try { validateauthtoken(authtoken); } catch (illegalargumentexception tokennotvalidexception) { logger.error("invalid token"); httpservletresponse.senderror(401); } try { chain.dofilter(request, response); } catch (exception e) { logger.error("exception: " + e.getmessage()); }finally { final string newauthtoken = generatenewauthtoken(authtoken); httpservletresponse.addheader(auth_token, newauthtoken); logger.info("added new security token: " + newauthtoken); } }
and in 1 of endpoints do
@put public response updateuser() { throw new illegalargumentexception("just test purposes"); }
i using resteasy
rest
based work.
and using seam rest
library map server exceptions rest
based exceptions
@exceptionmapping.list({ @exceptionmapping(exceptiontype = illegalargumentexception.class, status = 400, useexceptionmessage = true), @exceptionmapping(exceptiontype = persistenceexception.class, status = 400, useexceptionmessage = true), @exceptionmapping(exceptiontype = constraintviolationexception.class, status = 400, useexceptionmessage = true), @exceptionmapping(exceptiontype = validationexception.class, status = 400, useexceptionmessage = true), @exceptionmapping(exceptiontype = noresultexception.class, status = 404, useexceptionmessage = true), @exceptionmapping(exceptiontype = illegalstateexception.class, status = 406, useexceptionmessage = true), @exceptionmapping(exceptiontype = noclassdeffounderror.class, status = 404, useexceptionmessage = true), @exceptionmapping(exceptiontype = unsupportedoperationexception.class, status = 400, useexceptionmessage = true), }) @applicationpath("/rest") public class marketapplicationconfiguration extends application { }
problem?
- when endpoint throws exception, callback never returned filter code.
- when use try/catch/finally
follows
try { chain.dofilter(request, response); } catch (exception e) { logger.error("exception: " + e.getmessage()); }finally { final string newauthtoken = generatenewauthtoken(authtoken); httpservletresponse.addheader(auth_token, newauthtoken); logger.info("added new security token: " + newauthtoken); }
- can test illegalargumentexception
mapped http 400
based on seam rest
exception mapping, never returned securityfilter
code in case of server exceptions.
required?
- want generate server tokens when application throws exception(s) client can use them
- how can, in case of exceptions, can route response through securityfilter
?
i think should use purpose own exceptions handler can defined in web.xml , in case of exception should process inside exception handler , not in filter.
you can more details in article "servlets - exception handling"
Comments
Post a Comment