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/04/19 16:20:24 UTC

programmatically setting MIME mappings for static-only site

I'm wanting to embed Tomcat to only serve static files (for the moment). 
That is, no JSP, etc. I also want to have the welcome files completely 
customizable.

So instead of calling `tomcat.addWebapp()`, I go the completely 
programmable route and call `tomcat.addContext()`, 
`context.createWrapper()`, etc. This makes my bypass the 
`DefaultWebXmlListener`, which would have called 
`initWebappDefaults(Context ctx)` to set up the welcome files and such 
myself.

But `initWebappDefaults()` also sets up the default MIME mappings. And 
`Tomcat.DEFAULT_MIME_MAPPINGS` is private.

So the situation seems to be that Tomcat forces me to choose between 
creating a full-fledged JSP server, or setting up all the MIME types 
with some list of my own. Maybe it would be good for me to have my own 
list eventually, but for now this seems like an artificial choice forced 
upon me.

Part of the problem seems to be that the (ancient?) code has the MIME 
mappings as a string array!! Heaven knows we don't want to expose that. 
It should really be turned into a read-only map and then exposed so we 
can use it.

Then of course I see the comment:

 > TODO: would a properties resource be better ? Or just parsing 
/etc/mime.types ?

To answer part of that question, we can't just parse `/etc/mime.types` 
because the embedded server might not even have an `/etc/mime.types` 
file. This should definitely be put into a properties resource, I would 
think.

So I've probably answered my own question; this is an old TODO that 
needs to be done, I suppose?

Garret


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Garret,

On 4/19/19 17:21, Garret Wilson wrote:
> On 4/19/2019 5:12 PM, Christopher Schultz wrote:
>>> On April 19, 2019 4:20:24 PM UTC, Garret Wilson 
>>> <ga...@globalmentor.com> wrote:
>>>> 
>>>> So I've probably answered my own question; this is an old
>>>> TODO that needs to be done, I suppose?
>>> Yep.
>> +1
>> 
>> This is a perfect "beginner" enhancement.
> 
> 
> Yay! I'd love to take that on.
> 
> Would you like to assign a GitHub ticket to me, or should I create
> one? I don't remember the details of the source control process,
> but you guys have described it before when talking about
> configuration. I'll review the other threads to remember what the
> GitHub status is, but if it's OK to assign me a GitHub ticket, that
> would sure be helpful (instead of my keeping track of emails).

Just a regular GitHub PR will be fine. Feel free to file a ticket
against either your fork or the primary ASF one first.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAly9xE8ACgkQHPApP6U8
pFgVvhAAi+iZ95aLlbES2oex0LcPYbu9kZLkE5EwPycQi0uIG2t0sZdRVCNl1m1O
RPgUqkABs9zbSezRyVr0zTjxyy3TYeiREYB3kSBDnKOIg/4tbcF6gyIvwf8gaElk
SI5gcGtC80QkQlc2RC+oqodCdPAJtHU2LpL+qGebarHR/8V7zpOb/ErNxKOsyCHa
rxBfvwxYQZyXu0ms5zAVUaL5jcL2by9D2S65Tqjh/C4Y+sj6HRVCt7UnSW45ivLg
CqBE9QcdLThbRp+rNIf7EIy29g1YMJnlk6aj/R4+zxvywtAridBFK8QAobkFs187
3b4002rxKBFatHbWpl8exw7Pb0XSiqfcyWF7OjRVRYVf+2kds6OxCRrMEJbu9nl3
BagyhuZ6XcqphzTGXNsK0OgiIPQGIp8JiSB0NDC9+6/nn9wKPw17lXed8iaWoM3O
1DvMXvMZfPpMuDDtX5XUkgCo3jtaMkD/1KSk8rsmn6BqrN0YAObaS9watMd+E8r2
0UZAtcyxD9pFNEAnGhSU+mD/7BB33wgsNvbw7q314771M3TDBoxhYDlCl1RAfw99
RwOq4q0eKEkUBeM77/sulcO1bGrrUZpCM1ctmHCbo/czkN6lxu/MOX4re6U6728n
fKY8XccIe5l4Q40OEpsBTLFr6AstVls0R3g5YKxRjJpQ4cxQWaQ=
=qFaS
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Garret Wilson <ga...@globalmentor.com>.
On 4/19/2019 5:12 PM, Christopher Schultz wrote:
>> On April 19, 2019 4:20:24 PM UTC, Garret Wilson
>> <ga...@globalmentor.com> wrote:
>>>
>>> So I've probably answered my own question; this is an old TODO
>>> that needs to be done, I suppose?
>> Yep.
> +1
>
> This is a perfect "beginner" enhancement.


Yay! I'd love to take that on.

Would you like to assign a GitHub ticket to me, or should I create one? 
I don't remember the details of the source control process, but you guys 
have described it before when talking about configuration. I'll review 
the other threads to remember what the GitHub status is, but if it's OK 
to assign me a GitHub ticket, that would sure be helpful (instead of my 
keeping track of emails).

Garret


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Garret,

On 4/19/19 15:12, Mark Thomas wrote:
> On April 19, 2019 4:20:24 PM UTC, Garret Wilson
> <ga...@globalmentor.com> wrote:
>> I'm wanting to embed Tomcat to only serve static files (for the 
>> moment). That is, no JSP, etc. I also want to have the welcome
>> files completely customizable.
>> 
>> So instead of calling `tomcat.addWebapp()`, I go the completely 
>> programmable route and call `tomcat.addContext()`, 
>> `context.createWrapper()`, etc. This makes my bypass the 
>> `DefaultWebXmlListener`, which would have called 
>> `initWebappDefaults(Context ctx)` to set up the welcome files and
>> such myself.
>> 
>> But `initWebappDefaults()` also sets up the default MIME
>> mappings. And `Tomcat.DEFAULT_MIME_MAPPINGS` is private.
>> 
>> So the situation seems to be that Tomcat forces me to choose
>> between creating a full-fledged JSP server, or setting up all the
>> MIME types with some list of my own. Maybe it would be good for
>> me to have my own list eventually, but for now this seems like an
>> artificial choice forced upon me.
>> 
>> Part of the problem seems to be that the (ancient?) code has the
>> MIME mappings as a string array!! Heaven knows we don't want to
>> expose that.
>> 
>> It should really be turned into a read-only map and then exposed
>> so we can use it.
>> 
>> Then of course I see the comment:
>> 
>>> TODO: would a properties resource be better ? Or just parsing
>> /etc/mime.types ?
>> 
>> To answer part of that question, we can't just parse
>> `/etc/mime.types` because the embedded server might not even have
>> an `/etc/mime.types` file. This should definitely be put into a
>> properties resource, I would
>> 
>> think.
>> 
>> So I've probably answered my own question; this is an old TODO
>> that needs to be done, I suppose?
> 
> Yep.

+1

This is a perfect "beginner" enhancement.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAly6K5wACgkQHPApP6U8
pFhxyRAAw2AkPD1gtbc9/0poWuZrJMBLeDY41G13V+3VGwAu+YsBTpUmyh24lv/6
YOjGiNLYvqYLoKMx//obH/dwK38OwzZVJcS6z91ip9E5Unu2s++X3tNlOh98+C69
xCIc09Q/9B4jCIwA2NrgQY5KBqRk6wPm+xcG6EvCBJ8jijDkaw9qs9TkvUI26sIs
6JR24wR6YipTG6r4DTpHl8Z0ltbgEA14rws9TvQUWvODizvFVEZroSVtIsF6NYBB
wiiA+JPuvcWHyouxoQCLtrTELkcAX2F7RskY+vaH8Yvr4lUiX+P6mPHuLh9X/CLF
yoaks+zc/wPiaGiECh08zw6d5HFvwvM7cseLWhDO+PCVj2HsoIxxLniaBRWmYF5t
QPdRFIc83Gz5Aq6TUNyxjIqNXZnN5Fz1pvxWitSK671APKLzpqI27mf90GF9+gqL
VVbROKlfpzacE98pv7UHeKT9v0rEo1Ramw29BwpshtDvhVgEIrcMID0/ExDdct4W
bd4NUY1PEW3ykB0HVKCDEPPQVecbuNEYFe7tgtxO8YZqq1adyZmGCQffaQEpa7z0
mWKBa8JAFDpDS2sQMDlLP2Lddu1z2wW8izdWt5ArFCSGZQpVJQh7XQY41Fy7bB0y
5lH7htGK0S2CrvX7UT4JpmNbvtQ1b4K/hb1wKI3O/ljKnUKXDNs=
=B/mc
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Mark Thomas <ma...@apache.org>.
On April 19, 2019 4:20:24 PM UTC, Garret Wilson <ga...@globalmentor.com> wrote:
>I'm wanting to embed Tomcat to only serve static files (for the
>moment). 
>That is, no JSP, etc. I also want to have the welcome files completely 
>customizable.
>
>So instead of calling `tomcat.addWebapp()`, I go the completely 
>programmable route and call `tomcat.addContext()`, 
>`context.createWrapper()`, etc. This makes my bypass the 
>`DefaultWebXmlListener`, which would have called 
>`initWebappDefaults(Context ctx)` to set up the welcome files and such 
>myself.
>
>But `initWebappDefaults()` also sets up the default MIME mappings. And 
>`Tomcat.DEFAULT_MIME_MAPPINGS` is private.
>
>So the situation seems to be that Tomcat forces me to choose between 
>creating a full-fledged JSP server, or setting up all the MIME types 
>with some list of my own. Maybe it would be good for me to have my own 
>list eventually, but for now this seems like an artificial choice
>forced 
>upon me.
>
>Part of the problem seems to be that the (ancient?) code has the MIME 
>mappings as a string array!! Heaven knows we don't want to expose that.
>
>It should really be turned into a read-only map and then exposed so we 
>can use it.
>
>Then of course I see the comment:
>
> > TODO: would a properties resource be better ? Or just parsing 
>/etc/mime.types ?
>
>To answer part of that question, we can't just parse `/etc/mime.types` 
>because the embedded server might not even have an `/etc/mime.types` 
>file. This should definitely be put into a properties resource, I would
>
>think.
>
>So I've probably answered my own question; this is an old TODO that 
>needs to be done, I suppose?

Yep.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Konstantin,

On 4/19/19 19:05, Konstantin Kolinko wrote:
> пт, 19 апр. 2019 г. в 19:29, Garret Wilson
> <ga...@globalmentor.com>:>
>> I'm wanting to embed Tomcat to only serve static files (for the
>> moment). That is, no JSP, etc. I also want to have the welcome
>> files completely customizable.
>> 
>> So instead of calling `tomcat.addWebapp()`, I go the completely 
>> programmable route and call `tomcat.addContext()`, 
>> `context.createWrapper()`, etc. This makes my bypass the 
>> `DefaultWebXmlListener`, which would have called 
>> `initWebappDefaults(Context ctx)` to set up the welcome files and
>> such myself.
>> 
>> But `initWebappDefaults()` also sets up the default MIME
>> mappings. And `Tomcat.DEFAULT_MIME_MAPPINGS` is private.
> 
> Also note that the value is not synch'ed with the default list in 
> conf/web.xml. E.g  the following entries from the top of the
> default list are missing
> 
> <mime-mapping> <extension>123</extension> 
> <mime-type>application/vnd.lotus-1-2-3</mime-type> </mime-mapping> 
> <mime-mapping> <extension>7z</extension> 
> <mime-type>application/x-7z-compressed</mime-type> </mime-mapping>
> 
> (and many others)
> 
> Some years ago the list in web.xml was synch'ed to the similar file
> in Apache HTTPD, but the list used by embedded Tomcat was not
> updated.
> 
> Previous discussion: http://markmail.org/message/gjkixk7wysopyztp
> 
>> So the situation seems to be that Tomcat forces me to choose
>> between creating a full-fledged JSP server, or setting up all the
>> MIME types with some list of my own. Maybe it would be good for
>> me to have my own list eventually, but for now this seems like an
>> artificial choice forced upon me.
>> 
>> Part of the problem seems to be that the (ancient?) code has the
>> MIME mappings as a string array!! Heaven knows we don't want to
>> expose that. It should really be turned into a read-only map and
>> then exposed so we can use it.
> 
> An array is a bad API, but generally it is faster to create an
> array. All the time used to create a map goes to waste if it is
> only accessed sequentially, like it is done here.
> 
> The only place where the values are used is
> 
> for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) { 
> ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++], 
> DEFAULT_MIME_MAPPINGS[i++]); }
> 
> There are some people who ask for options to make Tomcat to start
> up faster (e.g. in a "serverless" environment when you start a
> process on demand and pay for execution time). Not being careful
> here may negatively affect the startup time. The current 
> 'DEFAULT_MIME_MAPPINGS' field is a static one and is always
> created when the class is loaded, regardless of whether it will be
> used or not.
> 
> 
>> Then of course I see the comment:
>> 
>>> TODO: would a properties resource be better ? Or just parsing
>> /etc/mime.types ?
>> 
>> To answer part of that question, we can't just parse
>> `/etc/mime.types` because the embedded server might not even have
>> an `/etc/mime.types` file. This should definitely be put into a
>> properties resource, I would think.
> 
> For reference, 
> https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.type
s
>
> 
>> So I've probably answered my own question; this is an old TODO
>> that needs to be done, I suppose?

Maybe it makes sense to do the following:

1. Create a separate, simple file. No XML, etc.

2. Create a new method for loading the default mime types. It can be
called if/when necessary but is not always called.

3. Hook something into the build process for re-generating the
mime-mappings for the default conf/web.xml file so everything is
always in sync.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAly9xOgACgkQHPApP6U8
pFhIkQ//Qhps4Jglmk79vWMRaVnZtiWyh5aW7ST4Zy7lid2ZcOzJne1Aw/U24hCX
c9yzvsHVweqql5INZAx+ADDY+IiQU8xZlxbQ9F/2jocQP76NuldWw09DvEiCj44P
no570sr0vxM8PK7ZDNYOc03DfSFPVpXFNBeydPX1bcumtayGnykZoLmcKY6yXyAu
tl8fjDvkBb/zDrJE/amzb2sbUkP3RKrzMP58zeqPehVSH1lsLAOqALxE5BaQPREo
WISWb3XfMp5H7DwQLAfa/6NiDT6Zpqaafwe3MHwiUuVfE7MIfg5L3h2rPH2vPdVe
vTmStRhq9VPwX+I9zCVKMbwigTgxYXxHdpseoLeLz/RhZvny/gumiWNTvH4/u4yT
PY6Q+OxfznFSeixDmD9GF14ZFQb4eJtLFpR8bi53bZ5lb2utGThdzyL5iZ8WICKb
4AQtLY1uh3EkMdz79g5HZKWEcVK5Q0RAU1Wp0bBmtl59jWN2SuHxe/1pm5+PrHxT
TI/rJI3Zr/dGaAbwD3DbY2E5bbPGH0vD0E3k9h8cR5YY1TTnne6wTswNMOFaSMIQ
ewlgxSDBrnHeg2RY/fr+rVe9yA9h2BFS5JPwQxtX8dk4w4FptPanCEUrNb+Xdc4U
zz2bUsxELDctO9ElgmP+6xKMZTFGF5x9ZWAYPa9dAT3h+bxdvx4=
=AsuI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programmatically setting MIME mappings for static-only site

Posted by Konstantin Kolinko <kn...@gmail.com>.
пт, 19 апр. 2019 г. в 19:29, Garret Wilson <ga...@globalmentor.com>:>
> I'm wanting to embed Tomcat to only serve static files (for the moment).
> That is, no JSP, etc. I also want to have the welcome files completely
> customizable.
>
> So instead of calling `tomcat.addWebapp()`, I go the completely
> programmable route and call `tomcat.addContext()`,
> `context.createWrapper()`, etc. This makes my bypass the
> `DefaultWebXmlListener`, which would have called
> `initWebappDefaults(Context ctx)` to set up the welcome files and such
> myself.
>
> But `initWebappDefaults()` also sets up the default MIME mappings. And
> `Tomcat.DEFAULT_MIME_MAPPINGS` is private.

Also note that the value is not synch'ed with the default list in
conf/web.xml. E.g  the following entries from the top of the default
list are missing

    <mime-mapping>
        <extension>123</extension>
        <mime-type>application/vnd.lotus-1-2-3</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>7z</extension>
        <mime-type>application/x-7z-compressed</mime-type>
    </mime-mapping>

(and many others)

Some years ago the list in web.xml was synch'ed to the similar file in
Apache HTTPD, but the list used by embedded Tomcat was not updated.

Previous discussion:
http://markmail.org/message/gjkixk7wysopyztp

> So the situation seems to be that Tomcat forces me to choose between
> creating a full-fledged JSP server, or setting up all the MIME types
> with some list of my own. Maybe it would be good for me to have my own
> list eventually, but for now this seems like an artificial choice forced
> upon me.
>
> Part of the problem seems to be that the (ancient?) code has the MIME
> mappings as a string array!! Heaven knows we don't want to expose that.
> It should really be turned into a read-only map and then exposed so we
> can use it.

An array is a bad API, but generally it is faster to create an array.
All the time used to create a map goes to waste if it is only accessed
sequentially, like it is done here.

The only place where the values are used is

        for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) {
            ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++],
                    DEFAULT_MIME_MAPPINGS[i++]);
        }

There are some people who ask for options to make Tomcat to start up
faster (e.g. in a "serverless" environment when you start a process on
demand and pay for execution time). Not being careful here may
negatively affect the startup time. The current
'DEFAULT_MIME_MAPPINGS' field is a static one and is always created
when the class is loaded, regardless of whether it will be used or
not.


> Then of course I see the comment:
>
>  > TODO: would a properties resource be better ? Or just parsing
> /etc/mime.types ?
>
> To answer part of that question, we can't just parse `/etc/mime.types`
> because the embedded server might not even have an `/etc/mime.types`
> file. This should definitely be put into a properties resource, I would
> think.

For reference,
https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

> So I've probably answered my own question; this is an old TODO that
> needs to be done, I suppose?
>

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org