You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Garret Wilson <ga...@globalmentor.com> on 2019/10/11 14:06:52 UTC
efficient redirect map with embedded Tomcat
This is a question for Tomcat experts before I get started implementing
a new feature.
Let's say I'm embedding Tomcat to serve static files. At the time of
creation I know that certain paths, such as `foo/bar.txt`, should
redirect to other paths, such as `some/other.txt`. What's the best way
to configure Tomcat to do those redirects? I'm comfortable with
extending the source code.
Here are a couple of ideas that come to mind:
* I could create a redirect servlet and map different instances of it
to different targets in the context when I configure everything. But
in Tomcat's routing engine, is the most efficient way to do things?
(I assume that the servlet mappings can be placed "over" the default
servlet's path space, that is, cherry-pick paths for redirection,
falling back to the default file-serving servlet for non-redirect
paths.)
* I thought of patching into the default file servlet, overriding
`org.apache.catalina.WebResource`, and creating virtual
`RedirectResource` resources that don't correspond to any physical
file. However it's not obvious to me where I would create a
redirect. Maybe throw some redirect exception inside
`WebResource.getInputStream()`? (This is probably not correct. I'm
just brainstorming. The idea is sound if I knew where to put it.)
* Should I install a configured rewrite valve when I'm setting up
embedded Tomcat?
* Is there some other routing logic in Tomcat I could tap into most
efficiently, providing a known set of redirects?
Thanks for any guidance. I'm want to figure out the best way to attack
this before getting very deep in an implementation.
Garret
Re: efficient redirect map with embedded Tomcat
Posted by Felix Schumacher <fe...@internetallee.de>.
Am 15. Oktober 2019 15:18:07 MESZ schrieb Christopher Schultz <ch...@christopherschultz.net>:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA256
>
>Mark,
>
>On 10/14/19 04:50, Mark Thomas wrote:
>> On 13/10/2019 23:46, Garret Wilson wrote:
>>> On 10/13/2019 11:52 AM, Mark Thomas wrote:
>>>> That depends on how you define best. Simplest to implement?
>>>> Easiest to maintain? Minimum overhead?
>>>
>>> How about, "What best follows the spirit of the Tomcat
>>> architecture?"
>>>
>>> Or alternatively, "What would be most efficient (i.e. not slowing
>>> down normal requests)?"
>>>
>>>>
>>>> It also depends on how many redirects are you talking about as
>>>> well as what sort of % of the over all requests need to be
>>>> redirected.
>>>
>>> Let's say 100 resources need redirecting, to pick an arbitrary
>>> number.
>>>
>>> (The use case is simply to migrate some old URLs that have
>>> probably been indexed already or even linked on the web.
>>> Theoretically the entire site would need to redirect its old
>>> URLs, but probably only the pages.)
>>
>> For that use case I'd start with the RewriteValve.
>
>RewriteValve won't be the most efficient way to do this, because
>RewriteValve has its own overhead of mapping request URIs (and
>possibly other requirements) to arbitrary things-to-do.
>
>If you want it to be as fast as possible, then you need to write your
>own Servlet (or Filter), map all URIs-to-redirect to that servlet (in
>WEB-INF/web.xml) and then write Java code to do the mapping.
>
>The fastest possible implementation wouldn't be a bunch of
>"string".equals calls or a HashMap, but something more elaborate.
>
>But all that seems like a lot of work for something that CAN be
>accomplished by using RewriteValve, where you don't have to write new
>code and then baby sit it forever.
>
>> If you notice a performance impact then we should take a look with
>> a profiler and see if there is room for improvement in the
>> RewriteValve.
>
>If someone could write Javadoc for the
>org.apache.catalina.valves.rewrite.RewriteMap interface, that would be
>very helpful. I took a quick look at the code, and it's not clear to
>me what either of the two methods in that interface should actually do
>and/or return.
I have added some doc to the rewrite user guide. I try to add some Javadoc later.
One odd thing I noticed is that the method setParameters gets passed the first optional parameter, only.
Felix
>
>Also, Substitution.SubstitutionElement#evaluate(). That would help
>with the above.
>
>Rémy, is that originally your code? git blame says most of the code
>was committed by you.
>
>- -chris
>-----BEGIN PGP SIGNATURE-----
>Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
>
>iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl2lxw8ACgkQHPApP6U8
>pFjMphAAw8fHxiPzgNSMY0cIlcCrDGSYeU78c0oMj+UDfqIprj2tDDOXmeJ8i2/o
>H7ngAzcuFeAm6G965KBa3brpS4phHj2q1ZRk1Ww5IQikYX0EeeHl+6LjI3r+irR9
>cfm32Zlb8MWZ4JoqyGX2vhyzrlCokAXidlBhFLRMkP9gG6Bq1fICdTwpQ1/yTRVe
>FDqMVlbaBepB3mvZpbA2SH58+rz0wHPGZweZEo5KTFOUM0xqQ/2hxbdhJsRfAtbS
>muR1zyLBPhA8cNZYpUC88OhmNCoumI6laBdWcpZ3h9yMW/a7T5LnxeUCZNJxwdze
>MvYB8CFGC5h3a9DIcapkhAk3sebiFhWVXjQ0Icz3qK+RLoiKoDVH/YsG3RW20u+4
>XDFSAij+GrfL49gc9P4nZ0sUfAOvZt7NlzbnB3z5qK2ybAQ5wEXboNBV0vrHEVgc
>hwnv7ShW7sKyv5ywjloscFABoGPfXbn43iiFT7fE09vVJnIRZZydHpqjhXQNSJPR
>4N9lqsOnmiIVeuqC/IzY10QZoo1g3S6AnoqyX6mENuicOrWQbB1MRTgChdQdV8Xc
>w03shweUED8JW0LvJsFf089w1xlu4xJQkmoSFgU1DCCUnioKJcwXni1VQlyP3m5G
>Vfl4WKIm1HwX4fMyhaBX8JkxUBlt1/GCI8bG67IvcM0GtrV5wj8=
>=fBb1
>-----END PGP SIGNATURE-----
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Mark,
On 10/14/19 04:50, Mark Thomas wrote:
> On 13/10/2019 23:46, Garret Wilson wrote:
>> On 10/13/2019 11:52 AM, Mark Thomas wrote:
>>> That depends on how you define best. Simplest to implement?
>>> Easiest to maintain? Minimum overhead?
>>
>> How about, "What best follows the spirit of the Tomcat
>> architecture?"
>>
>> Or alternatively, "What would be most efficient (i.e. not slowing
>> down normal requests)?"
>>
>>>
>>> It also depends on how many redirects are you talking about as
>>> well as what sort of % of the over all requests need to be
>>> redirected.
>>
>> Let's say 100 resources need redirecting, to pick an arbitrary
>> number.
>>
>> (The use case is simply to migrate some old URLs that have
>> probably been indexed already or even linked on the web.
>> Theoretically the entire site would need to redirect its old
>> URLs, but probably only the pages.)
>
> For that use case I'd start with the RewriteValve.
RewriteValve won't be the most efficient way to do this, because
RewriteValve has its own overhead of mapping request URIs (and
possibly other requirements) to arbitrary things-to-do.
If you want it to be as fast as possible, then you need to write your
own Servlet (or Filter), map all URIs-to-redirect to that servlet (in
WEB-INF/web.xml) and then write Java code to do the mapping.
The fastest possible implementation wouldn't be a bunch of
"string".equals calls or a HashMap, but something more elaborate.
But all that seems like a lot of work for something that CAN be
accomplished by using RewriteValve, where you don't have to write new
code and then baby sit it forever.
> If you notice a performance impact then we should take a look with
> a profiler and see if there is room for improvement in the
> RewriteValve.
If someone could write Javadoc for the
org.apache.catalina.valves.rewrite.RewriteMap interface, that would be
very helpful. I took a quick look at the code, and it's not clear to
me what either of the two methods in that interface should actually do
and/or return.
Also, Substitution.SubstitutionElement#evaluate(). That would help
with the above.
Rémy, is that originally your code? git blame says most of the code
was committed by you.
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl2lxw8ACgkQHPApP6U8
pFjMphAAw8fHxiPzgNSMY0cIlcCrDGSYeU78c0oMj+UDfqIprj2tDDOXmeJ8i2/o
H7ngAzcuFeAm6G965KBa3brpS4phHj2q1ZRk1Ww5IQikYX0EeeHl+6LjI3r+irR9
cfm32Zlb8MWZ4JoqyGX2vhyzrlCokAXidlBhFLRMkP9gG6Bq1fICdTwpQ1/yTRVe
FDqMVlbaBepB3mvZpbA2SH58+rz0wHPGZweZEo5KTFOUM0xqQ/2hxbdhJsRfAtbS
muR1zyLBPhA8cNZYpUC88OhmNCoumI6laBdWcpZ3h9yMW/a7T5LnxeUCZNJxwdze
MvYB8CFGC5h3a9DIcapkhAk3sebiFhWVXjQ0Icz3qK+RLoiKoDVH/YsG3RW20u+4
XDFSAij+GrfL49gc9P4nZ0sUfAOvZt7NlzbnB3z5qK2ybAQ5wEXboNBV0vrHEVgc
hwnv7ShW7sKyv5ywjloscFABoGPfXbn43iiFT7fE09vVJnIRZZydHpqjhXQNSJPR
4N9lqsOnmiIVeuqC/IzY10QZoo1g3S6AnoqyX6mENuicOrWQbB1MRTgChdQdV8Xc
w03shweUED8JW0LvJsFf089w1xlu4xJQkmoSFgU1DCCUnioKJcwXni1VQlyP3m5G
Vfl4WKIm1HwX4fMyhaBX8JkxUBlt1/GCI8bG67IvcM0GtrV5wj8=
=fBb1
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Mark Thomas <ma...@apache.org>.
On 13/10/2019 23:46, Garret Wilson wrote:
> On 10/13/2019 11:52 AM, Mark Thomas wrote:
>> That depends on how you define best. Simplest to implement? Easiest to
>> maintain? Minimum overhead?
>
> How about, "What best follows the spirit of the Tomcat architecture?"
>
> Or alternatively, "What would be most efficient (i.e. not slowing down
> normal requests)?"
>
>>
>> It also depends on how many redirects are you talking about as well as
>> what sort of % of the over all requests need to be redirected.
>
> Let's say 100 resources need redirecting, to pick an arbitrary number.
>
> (The use case is simply to migrate some old URLs that have probably been
> indexed already or even linked on the web. Theoretically the entire site
> would need to redirect its old URLs, but probably only the pages.)
For that use case I'd start with the RewriteValve.
If you notice a performance impact then we should take a look with a
profiler and see if there is room for improvement in the RewriteValve.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Garret Wilson <ga...@globalmentor.com>.
On 10/15/2019 6:06 AM, Christopher Schultz wrote:
>
>> (The use case is simply to migrate some old URLs that have probably
>> been indexed already or even linked on the web. Theoretically the
>> entire site would need to redirect its old URLs, but probably only
>> the pages.)
> So, just to be sure, you are talking about HTTP 302 "redirect"
> responses, and not just mapping incoming URIs to some other resource,
> right?
Actually HTTP 301, because these represent resources that have
permanently moved to the new location.
But yes, in essence that's what we're talking about: when a request
comes in for an old path, we send back an HTTP redirect to the new path.
No content will be served at the old path.
Garret
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Garret,
On 10/13/19 18:46, Garret Wilson wrote:
> On 10/13/2019 11:52 AM, Mark Thomas wrote:
>> That depends on how you define best. Simplest to implement?
>> Easiest to maintain? Minimum overhead?
>
> How about, "What best follows the spirit of the Tomcat
> architecture?"
>
> Or alternatively, "What would be most efficient (i.e. not slowing
> down normal requests)?"
>
>>
>> It also depends on how many redirects are you talking about as
>> well as what sort of % of the over all requests need to be
>> redirected.
>
> Let's say 100 resources need redirecting, to pick an arbitrary
> number.
>
> (The use case is simply to migrate some old URLs that have probably
> been indexed already or even linked on the web. Theoretically the
> entire site would need to redirect its old URLs, but probably only
> the pages.)
So, just to be sure, you are talking about HTTP 302 "redirect"
responses, and not just mapping incoming URIs to some other resource,
right?
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl2lxDkACgkQHPApP6U8
pFh0YA//fWxPjGQpDEIQeTuzYE8trADwrJxoFiouLcwlF7/hpUgXioMsNHOjWapq
D4/c5eAJnwnUQl6EuCLHrA0alopP/l3G+yg1UeISy6D7ylC1ufuyORENNpOmwxhP
m9zEGhx5ahT7ag3WqMyH2a4ouWEv54s3DwRpujj4Al5n3OQYM60MNbnxi1905fZD
MhWDfWkj+D62SWqlKQqlqywAJJiR/6Wi+yTn+1nXrkcbbUMhPwkllgGLDDTCRC8u
ZRq2ADJgXx/jlv6hhHhK+3mdMunXF8H9n/b3Q2tZLLAWAy82HPS/ImLDbZN1TY2e
UMXbDaj1kDwrmsdvEKHqjIG8D0WY6haX3m8yc0XVVp9Otg2bbipNdTVJJal+dBrI
uVrKLYXnm5BVTDmb9/vPgE01nSA7NnJTC0HFx4yBlFjrD4THqnp9VgLr8CDx49+x
re3DpsYd/BKSHq/lemlP+PaVVb7idVh9QvWVLqpnKi8Z5wuRJWeANd7YW8wdhCcd
VNSUHwqVFCaFdjus361wY6QVa8IGsQtega90wtbsbT03H0EZ9caTvdTYNEXQZXvT
su1ggmduZl0eIjHtzhmWMghdDiwTly95OSjaLzc6ztNg3AFn0dLAUpUcM2CPlE8o
w2iBNbnC8n1ENidzLd9uIuNcGB5SFcZuHnipKYBq3XLTJrzHhmU=
=mDHW
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Garret Wilson <ga...@globalmentor.com>.
On 10/13/2019 11:52 AM, Mark Thomas wrote:
> That depends on how you define best. Simplest to implement? Easiest to
> maintain? Minimum overhead?
How about, "What best follows the spirit of the Tomcat architecture?"
Or alternatively, "What would be most efficient (i.e. not slowing down
normal requests)?"
>
> It also depends on how many redirects are you talking about as well as
> what sort of % of the over all requests need to be redirected.
Let's say 100 resources need redirecting, to pick an arbitrary number.
(The use case is simply to migrate some old URLs that have probably been
indexed already or even linked on the web. Theoretically the entire site
would need to redirect its old URLs, but probably only the pages.)
Garret
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Mark Thomas <ma...@apache.org>.
On 12/10/2019 20:35, Felix Schumacher wrote:
>
> Am 12.10.19 um 17:13 schrieb Garret Wilson:
>> Could somebody at least point me to the best place to wire in
>> site-level per-resource redirects in embedded Tomcat? I can create a
>> solution, I just need to know where it is best to start.
>
> Did you look at https://tomcat.apache.org/tomcat-9.0-doc/rewrite.html
That depends on how you define best. Simplest to implement? Easiest to
maintain? Minimum overhead?
It also depends on how many redirects are you talking about as well as
what sort of % of the over all requests need to be redirected.
I'd either start with the redirect valve as Felix suggests or - possibly
- a custom resource implementation.
Mark
>
> Felix
>
>>
>> Thanks,
>>
>> Garret
>>
>> On 10/11/2019 11:06 AM, Garret Wilson wrote:
>>> This is a question for Tomcat experts before I get started
>>> implementing a new feature.
>>>
>>> Let's say I'm embedding Tomcat to serve static files. At the time of
>>> creation I know that certain paths, such as `foo/bar.txt`, should
>>> redirect to other paths, such as `some/other.txt`. What's the best
>>> way to configure Tomcat to do those redirects? I'm comfortable with
>>> extending the source code.
>>>
>>> Here are a couple of ideas that come to mind:
>>>
>>> * I could create a redirect servlet and map different instances of it
>>> to different targets in the context when I configure everything. But
>>> in Tomcat's routing engine, is the most efficient way to do things?
>>> (I assume that the servlet mappings can be placed "over" the default
>>> servlet's path space, that is, cherry-pick paths for redirection,
>>> falling back to the default file-serving servlet for non-redirect
>>> paths.)
>>> * I thought of patching into the default file servlet, overriding
>>> `org.apache.catalina.WebResource`, and creating virtual
>>> `RedirectResource` resources that don't correspond to any physical
>>> file. However it's not obvious to me where I would create a
>>> redirect. Maybe throw some redirect exception inside
>>> `WebResource.getInputStream()`? (This is probably not correct. I'm
>>> just brainstorming. The idea is sound if I knew where to put it.)
>>> * Should I install a configured rewrite valve when I'm setting up
>>> embedded Tomcat?
>>> * Is there some other routing logic in Tomcat I could tap into most
>>> efficiently, providing a known set of redirects?
>>>
>>> Thanks for any guidance. I'm want to figure out the best way to
>>> attack this before getting very deep in an implementation.
>>>
>>> Garret
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Felix Schumacher <fe...@internetallee.de>.
Am 12.10.19 um 17:13 schrieb Garret Wilson:
> Could somebody at least point me to the best place to wire in
> site-level per-resource redirects in embedded Tomcat? I can create a
> solution, I just need to know where it is best to start.
Did you look at https://tomcat.apache.org/tomcat-9.0-doc/rewrite.html
Felix
>
> Thanks,
>
> Garret
>
> On 10/11/2019 11:06 AM, Garret Wilson wrote:
>> This is a question for Tomcat experts before I get started
>> implementing a new feature.
>>
>> Let's say I'm embedding Tomcat to serve static files. At the time of
>> creation I know that certain paths, such as `foo/bar.txt`, should
>> redirect to other paths, such as `some/other.txt`. What's the best
>> way to configure Tomcat to do those redirects? I'm comfortable with
>> extending the source code.
>>
>> Here are a couple of ideas that come to mind:
>>
>> * I could create a redirect servlet and map different instances of it
>> to different targets in the context when I configure everything. But
>> in Tomcat's routing engine, is the most efficient way to do things?
>> (I assume that the servlet mappings can be placed "over" the default
>> servlet's path space, that is, cherry-pick paths for redirection,
>> falling back to the default file-serving servlet for non-redirect
>> paths.)
>> * I thought of patching into the default file servlet, overriding
>> `org.apache.catalina.WebResource`, and creating virtual
>> `RedirectResource` resources that don't correspond to any physical
>> file. However it's not obvious to me where I would create a
>> redirect. Maybe throw some redirect exception inside
>> `WebResource.getInputStream()`? (This is probably not correct. I'm
>> just brainstorming. The idea is sound if I knew where to put it.)
>> * Should I install a configured rewrite valve when I'm setting up
>> embedded Tomcat?
>> * Is there some other routing logic in Tomcat I could tap into most
>> efficiently, providing a known set of redirects?
>>
>> Thanks for any guidance. I'm want to figure out the best way to
>> attack this before getting very deep in an implementation.
>>
>> Garret
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: efficient redirect map with embedded Tomcat
Posted by Garret Wilson <ga...@globalmentor.com>.
Could somebody at least point me to the best place to wire in site-level
per-resource redirects in embedded Tomcat? I can create a solution, I
just need to know where it is best to start.
Thanks,
Garret
On 10/11/2019 11:06 AM, Garret Wilson wrote:
> This is a question for Tomcat experts before I get started
> implementing a new feature.
>
> Let's say I'm embedding Tomcat to serve static files. At the time of
> creation I know that certain paths, such as `foo/bar.txt`, should
> redirect to other paths, such as `some/other.txt`. What's the best way
> to configure Tomcat to do those redirects? I'm comfortable with
> extending the source code.
>
> Here are a couple of ideas that come to mind:
>
> * I could create a redirect servlet and map different instances of it
> to different targets in the context when I configure everything. But
> in Tomcat's routing engine, is the most efficient way to do things?
> (I assume that the servlet mappings can be placed "over" the default
> servlet's path space, that is, cherry-pick paths for redirection,
> falling back to the default file-serving servlet for non-redirect
> paths.)
> * I thought of patching into the default file servlet, overriding
> `org.apache.catalina.WebResource`, and creating virtual
> `RedirectResource` resources that don't correspond to any physical
> file. However it's not obvious to me where I would create a
> redirect. Maybe throw some redirect exception inside
> `WebResource.getInputStream()`? (This is probably not correct. I'm
> just brainstorming. The idea is sound if I knew where to put it.)
> * Should I install a configured rewrite valve when I'm setting up
> embedded Tomcat?
> * Is there some other routing logic in Tomcat I could tap into most
> efficiently, providing a known set of redirects?
>
> Thanks for any guidance. I'm want to figure out the best way to attack
> this before getting very deep in an implementation.
>
> Garret
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org