java ee - Replace path parameter dynamically -
i have @provider should replace path variable this:
@provider @priority(value = 1) public class securitycheckrequestfilter implements containerrequestfilter { @override public void filter(containerrequestcontext requestcontext) throws ioexception { //code requestcontext.geturiinfo().getpathparameters().putsingle("userid", somenewuserid); } }
when debug it, path variable "userid" seems replaced, @ endpoint later in workflow (for example /user/{userid}
), old value appears again. nothing replaced. no information behaviour in latest resteasy doc, in very old resteasy doc, there information, getpathparameters()
returns unmodifiable list. if unmodifiable, why can replace value within provider then? nevertheless, value not replaced. how can overwrite existing path parameter new value?
(sure, add new userid header parameter , fetch information later in endpoint, that's not solution)
i have solution now. it's little bit cumbersome works. first of all, had add additional annotation @prematching
. causes provider executed earlier in request, can manipulate path before processed. disadvantage not able use pathparameters
anymore, because placeholder {userid}
not set @ time. instead had somehow extract information path path segments:
list<pathsegment> pathsegments = requestcontext.geturiinfo().getpathsegments();
unfortunately, had rebuild whole path new (including query parameters!) change userid.
string fullpath = requestcontext.geturiinfo().getpath(); multivaluedmap<string, string> queryparameters = requestcontext.geturiinfo().getqueryparameters(true); string modifiedpath = fullpath.replacefirst(obfuscationid, userid); uribuilder uribuilder = uribuilder.frompath(modifiedpath); queryparameters.foreach((k,v) -> { uribuilder.queryparam(k, v.get(0)); } ); requestcontext.setrequesturi(uribuilder.build());
Comments
Post a Comment