jersey - How do I getAbsolutePathBuilder() outside of @Context -


i have simple jax-rs app written in jersey 2.13 use emit "links" using uribuilder derived through uriinfo injected via @context annotation.

@context private uriinfo uriinfo; ... uribuilder uribuilder = uriinfo.getabsolutepathbuilder(); string prid = uuid.randomuuid().tostring(); uri pruri = uribuilder.path(prid).build(); ... builder.putlink(new link("self", pruri.tostring())); 

i find in of supporting classes need means of getting uribuilder derived uriinfo.getabsolutepathbuilder() without having benefit of using @context annotation. how do this? there someway can ask resource directly builder? how statically asking, what, class extends resourceconfig builder?

how can getabsolutepathbuilder() outside of @context?

you capture request in filter using containerrequestfilter , put uriinfo object thread local variable. below possible solution, not 100% complete not provide uriinfoholder class needs hold thread-local instance of uriinfo object. see how this, can @ spring's localecontextholder class example.

@provider public class myfilter implements containerrequestfilter {      @override     public void filter(containerrequestcontext requestcontext)             throws ioexception {          uriinfo uriinfo = requestcontext.geturiinfo();         if (uriinfo != null)             uriinfoholder.seturiinfo(uriinfo);      } } 

then in of other classes on thread invoked resource, can uriinfo (and absolutepathbuilder) calling method uriinfoholder.geturiinfo(). see parallel localecontextholder.getlocale() method inspiration.

this answers question, negligent if did not point out imo best practice pass "absolutepathbuilder" classes (ideally part of service method call) if information needed classes. pulling things environment have shown above result in hard find bugs years down road when new people don't understand how code works.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -