Jetty How to add a servlet to multiple ServletContextHandler or apply ContainerRequestFilter to...
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 Handler{wrapper,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 Handler{contexts,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
add a comment |
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 Handler{wrapper,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 Handler{contexts,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
1
" I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? TheContainerRequestFilter
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 multipleServletContextHandler
configured. The approach I took was to useContainerRequestFilter
.
– 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 yourServletContextHandler
instances discovered and deployed via theDeploymentManager
? or are they manually added to aHandlerList
(or the olderHandlerCollection
)?
– Joakim Erdfelt
Nov 15 '18 at 10:58
@JoakimErdfelt they are manually added toHandlerCollection
– tryingToLearn
Nov 16 '18 at 3:18
add a comment |
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 Handler{wrapper,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 Handler{contexts,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
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 Handler{wrapper,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 Handler{contexts,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
java jersey jetty embedded-jetty
edited Nov 15 '18 at 9:59
tryingToLearn
asked Nov 15 '18 at 4:54
tryingToLearntryingToLearn
2,75622850
2,75622850
1
" I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? TheContainerRequestFilter
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 multipleServletContextHandler
configured. The approach I took was to useContainerRequestFilter
.
– 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 yourServletContextHandler
instances discovered and deployed via theDeploymentManager
? or are they manually added to aHandlerList
(or the olderHandlerCollection
)?
– Joakim Erdfelt
Nov 15 '18 at 10:58
@JoakimErdfelt they are manually added toHandlerCollection
– tryingToLearn
Nov 16 '18 at 3:18
add a comment |
1
" I need this ContainerRequestFilter to be added to all ServletContextHandler" - What does this mean? TheContainerRequestFilter
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 multipleServletContextHandler
configured. The approach I took was to useContainerRequestFilter
.
– 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 yourServletContextHandler
instances discovered and deployed via theDeploymentManager
? or are they manually added to aHandlerList
(or the olderHandlerCollection
)?
– Joakim Erdfelt
Nov 15 '18 at 10:58
@JoakimErdfelt they are manually added toHandlerCollection
– 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
add a comment |
1 Answer
1
active
oldest
votes
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));
}
This isn't 1 instance of your filter for allServletContextHandlers
, like your question asked, this is a uniqueRequestInterceptor
instance perServletContextHandler
.
– Joakim Erdfelt
Nov 15 '18 at 10:56
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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));
}
This isn't 1 instance of your filter for allServletContextHandlers
, like your question asked, this is a uniqueRequestInterceptor
instance perServletContextHandler
.
– Joakim Erdfelt
Nov 15 '18 at 10:56
add a comment |
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));
}
This isn't 1 instance of your filter for allServletContextHandlers
, like your question asked, this is a uniqueRequestInterceptor
instance perServletContextHandler
.
– Joakim Erdfelt
Nov 15 '18 at 10:56
add a comment |
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));
}
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));
}
answered Nov 15 '18 at 9:59
tryingToLearntryingToLearn
2,75622850
2,75622850
This isn't 1 instance of your filter for allServletContextHandlers
, like your question asked, this is a uniqueRequestInterceptor
instance perServletContextHandler
.
– Joakim Erdfelt
Nov 15 '18 at 10:56
add a comment |
This isn't 1 instance of your filter for allServletContextHandlers
, like your question asked, this is a uniqueRequestInterceptor
instance perServletContextHandler
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 useContainerRequestFilter
.– 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 theDeploymentManager
? or are they manually added to aHandlerList
(or the olderHandlerCollection
)?– Joakim Erdfelt
Nov 15 '18 at 10:58
@JoakimErdfelt they are manually added to
HandlerCollection
– tryingToLearn
Nov 16 '18 at 3:18