Jetty How to add a servlet to multiple ServletContextHandler or apply ContainerRequestFilter to ContextHandlerCollection










0















I need to intercept all the requests when there are multiple ServletContextHandler configured.



I have multiple ServletContextHandler in a ContextHandlerCollection and a ContainerRequestFilter.
I need this ContainerRequestFilter to be added to all ServletContextHandler
Only way I could find of adding the ContainerRequestFilter was through ResourceConfig. So I did this:



ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyContainerRequestFilter.class);
ServletHolder s = new ServletHolder(new ServletContainer(resourceConfig));
for (Handler context : contextHandlers)
((ServletContextHandler)context).addServlet(s, "/*");



which results in:




java.lang.IllegalStateException: Multiple servlets map to path: /*:
org.eclipse.jetty.proxy.ProxyServlet$Transparent-56c0a61e




What is the right way to do this?



I also looked into handlers and tried following but it overrides all the other servlets contained in ContextHandlerCollection i.e., when I call /api (exists in one of the ServletContextHandler in ContextHandlerCollection), I get 404 because of context.setContextPath("/"); below, but then this request filter needs to be applied on base path anyway.



HandlerWrapper wrapper = new HandlerWrapper();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(RequestInterceptor.class);
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
wrapper.setHandler(context)
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlerwrapper,contexts);


I also tried adding filter to above collection:



HandlerWrapper wrapper = new HandlerWrapper();
FilterHolder filter = new FilterHolder(MyContainerRequestFilter.class); // had to implment filter interface
wrapper.addFilterWithMapping(filter, "/*", EnumSet.allOf(DispatcherType.class)) ;
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlercontexts,wrapper);


In this case request does come to the filter but I get following exception:




Could not send response error 500: java.lang.IllegalStateException:
Committed Committed before 404 null











share|improve this question



















  • 1





    " I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

    – Paul Samsotha
    Nov 15 '18 at 5:49












  • @PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

    – tryingToLearn
    Nov 15 '18 at 6:02











  • "intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:54











  • Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:58











  • @JoakimErdfelt they are manually added to HandlerCollection

    – tryingToLearn
    Nov 16 '18 at 3:18















0















I need to intercept all the requests when there are multiple ServletContextHandler configured.



I have multiple ServletContextHandler in a ContextHandlerCollection and a ContainerRequestFilter.
I need this ContainerRequestFilter to be added to all ServletContextHandler
Only way I could find of adding the ContainerRequestFilter was through ResourceConfig. So I did this:



ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyContainerRequestFilter.class);
ServletHolder s = new ServletHolder(new ServletContainer(resourceConfig));
for (Handler context : contextHandlers)
((ServletContextHandler)context).addServlet(s, "/*");



which results in:




java.lang.IllegalStateException: Multiple servlets map to path: /*:
org.eclipse.jetty.proxy.ProxyServlet$Transparent-56c0a61e




What is the right way to do this?



I also looked into handlers and tried following but it overrides all the other servlets contained in ContextHandlerCollection i.e., when I call /api (exists in one of the ServletContextHandler in ContextHandlerCollection), I get 404 because of context.setContextPath("/"); below, but then this request filter needs to be applied on base path anyway.



HandlerWrapper wrapper = new HandlerWrapper();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(RequestInterceptor.class);
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
wrapper.setHandler(context)
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlerwrapper,contexts);


I also tried adding filter to above collection:



HandlerWrapper wrapper = new HandlerWrapper();
FilterHolder filter = new FilterHolder(MyContainerRequestFilter.class); // had to implment filter interface
wrapper.addFilterWithMapping(filter, "/*", EnumSet.allOf(DispatcherType.class)) ;
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlercontexts,wrapper);


In this case request does come to the filter but I get following exception:




Could not send response error 500: java.lang.IllegalStateException:
Committed Committed before 404 null











share|improve this question



















  • 1





    " I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

    – Paul Samsotha
    Nov 15 '18 at 5:49












  • @PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

    – tryingToLearn
    Nov 15 '18 at 6:02











  • "intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:54











  • Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:58











  • @JoakimErdfelt they are manually added to HandlerCollection

    – tryingToLearn
    Nov 16 '18 at 3:18













0












0








0








I need to intercept all the requests when there are multiple ServletContextHandler configured.



I have multiple ServletContextHandler in a ContextHandlerCollection and a ContainerRequestFilter.
I need this ContainerRequestFilter to be added to all ServletContextHandler
Only way I could find of adding the ContainerRequestFilter was through ResourceConfig. So I did this:



ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyContainerRequestFilter.class);
ServletHolder s = new ServletHolder(new ServletContainer(resourceConfig));
for (Handler context : contextHandlers)
((ServletContextHandler)context).addServlet(s, "/*");



which results in:




java.lang.IllegalStateException: Multiple servlets map to path: /*:
org.eclipse.jetty.proxy.ProxyServlet$Transparent-56c0a61e




What is the right way to do this?



I also looked into handlers and tried following but it overrides all the other servlets contained in ContextHandlerCollection i.e., when I call /api (exists in one of the ServletContextHandler in ContextHandlerCollection), I get 404 because of context.setContextPath("/"); below, but then this request filter needs to be applied on base path anyway.



HandlerWrapper wrapper = new HandlerWrapper();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(RequestInterceptor.class);
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
wrapper.setHandler(context)
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlerwrapper,contexts);


I also tried adding filter to above collection:



HandlerWrapper wrapper = new HandlerWrapper();
FilterHolder filter = new FilterHolder(MyContainerRequestFilter.class); // had to implment filter interface
wrapper.addFilterWithMapping(filter, "/*", EnumSet.allOf(DispatcherType.class)) ;
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlercontexts,wrapper);


In this case request does come to the filter but I get following exception:




Could not send response error 500: java.lang.IllegalStateException:
Committed Committed before 404 null











share|improve this question
















I need to intercept all the requests when there are multiple ServletContextHandler configured.



I have multiple ServletContextHandler in a ContextHandlerCollection and a ContainerRequestFilter.
I need this ContainerRequestFilter to be added to all ServletContextHandler
Only way I could find of adding the ContainerRequestFilter was through ResourceConfig. So I did this:



ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyContainerRequestFilter.class);
ServletHolder s = new ServletHolder(new ServletContainer(resourceConfig));
for (Handler context : contextHandlers)
((ServletContextHandler)context).addServlet(s, "/*");



which results in:




java.lang.IllegalStateException: Multiple servlets map to path: /*:
org.eclipse.jetty.proxy.ProxyServlet$Transparent-56c0a61e




What is the right way to do this?



I also looked into handlers and tried following but it overrides all the other servlets contained in ContextHandlerCollection i.e., when I call /api (exists in one of the ServletContextHandler in ContextHandlerCollection), I get 404 because of context.setContextPath("/"); below, but then this request filter needs to be applied on base path anyway.



HandlerWrapper wrapper = new HandlerWrapper();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(RequestInterceptor.class);
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
wrapper.setHandler(context)
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlerwrapper,contexts);


I also tried adding filter to above collection:



HandlerWrapper wrapper = new HandlerWrapper();
FilterHolder filter = new FilterHolder(MyContainerRequestFilter.class); // had to implment filter interface
wrapper.addFilterWithMapping(filter, "/*", EnumSet.allOf(DispatcherType.class)) ;
HandlerCollection handlers = new HandlerCollection(true);
handlers.setHandlers(new Handlercontexts,wrapper);


In this case request does come to the filter but I get following exception:




Could not send response error 500: java.lang.IllegalStateException:
Committed Committed before 404 null








java jersey jetty embedded-jetty






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:59







tryingToLearn

















asked Nov 15 '18 at 4:54









tryingToLearntryingToLearn

2,76632850




2,76632850







  • 1





    " I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

    – Paul Samsotha
    Nov 15 '18 at 5:49












  • @PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

    – tryingToLearn
    Nov 15 '18 at 6:02











  • "intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:54











  • Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:58











  • @JoakimErdfelt they are manually added to HandlerCollection

    – tryingToLearn
    Nov 16 '18 at 3:18












  • 1





    " I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

    – Paul Samsotha
    Nov 15 '18 at 5:49












  • @PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

    – tryingToLearn
    Nov 15 '18 at 6:02











  • "intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:54











  • Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

    – Joakim Erdfelt
    Nov 15 '18 at 10:58











  • @JoakimErdfelt they are manually added to HandlerCollection

    – tryingToLearn
    Nov 16 '18 at 3:18







1




1





" I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

– Paul Samsotha
Nov 15 '18 at 5:49






" I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? The ContainerRequestFilter has no affiliation with the ServletContextHandler. It is only a Jersey component and can only be registered with Jersey. The error you are seeing is coming from your loop, where you are trying to register multiple Jersey servlets with the same mapping /*. What exactly are you tryin to accomplish? Often people show HOW they are trying to accomplish something, and forget to tell us the WHAT. And sometime this leads to an XY problem

– Paul Samsotha
Nov 15 '18 at 5:49














@PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

– tryingToLearn
Nov 15 '18 at 6:02





@PaulSamsotha Apologies for ambiguity.Simply put, I need to intercept all the requests when there are multiple ServletContextHandler configured. The approach I took was to use ContainerRequestFilter.

– tryingToLearn
Nov 15 '18 at 6:02













"intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

– Joakim Erdfelt
Nov 15 '18 at 10:54





"intercept all the requests" has multiple meanings. are you wanting behavior within a webapp? (complete with ServletContext behaviors?) or behavior server wide (and you don't care about the ServletContext)?

– Joakim Erdfelt
Nov 15 '18 at 10:54













Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

– Joakim Erdfelt
Nov 15 '18 at 10:58





Are your ServletContextHandler instances discovered and deployed via the DeploymentManager? or are they manually added to a HandlerList (or the older HandlerCollection)?

– Joakim Erdfelt
Nov 15 '18 at 10:58













@JoakimErdfelt they are manually added to HandlerCollection

– tryingToLearn
Nov 16 '18 at 3:18





@JoakimErdfelt they are manually added to HandlerCollection

– tryingToLearn
Nov 16 '18 at 3:18












1 Answer
1






active

oldest

votes


















0














I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter



And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:



Handler contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers)
((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*",
EnumSet.allOf(DispatcherType.class));






share|improve this answer























  • This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

    – Joakim Erdfelt
    Nov 15 '18 at 10:56










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312637%2fjetty-how-to-add-a-servlet-to-multiple-servletcontexthandler-or-apply-containerr%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter



And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:



Handler contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers)
((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*",
EnumSet.allOf(DispatcherType.class));






share|improve this answer























  • This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

    – Joakim Erdfelt
    Nov 15 '18 at 10:56















0














I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter



And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:



Handler contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers)
((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*",
EnumSet.allOf(DispatcherType.class));






share|improve this answer























  • This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

    – Joakim Erdfelt
    Nov 15 '18 at 10:56













0












0








0







I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter



And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:



Handler contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers)
((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*",
EnumSet.allOf(DispatcherType.class));






share|improve this answer













I could not do it with ContainerRequestFilter but I had to use javax.servlet.Filter



And the correct way to add a javax.servlet.Filter in my case (Multiple ServletContextHandler) is:



Handler contextHandlers = contexts.getHandlers();

for (Handler context : contextHandlers)
((ServletContextHandler)context).addFilter(RequestInterceptor.class, "/*",
EnumSet.allOf(DispatcherType.class));







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 9:59









tryingToLearntryingToLearn

2,76632850




2,76632850












  • This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

    – Joakim Erdfelt
    Nov 15 '18 at 10:56

















  • This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

    – Joakim Erdfelt
    Nov 15 '18 at 10:56
















This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

– Joakim Erdfelt
Nov 15 '18 at 10:56





This isn't 1 instance of your filter for all ServletContextHandlers, like your question asked, this is a unique RequestInterceptor instance per ServletContextHandler.

– Joakim Erdfelt
Nov 15 '18 at 10:56



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312637%2fjetty-how-to-add-a-servlet-to-multiple-servletcontexthandler-or-apply-containerr%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto