I believe what you are asking is not possible at the application level. The specification talks only about the META-INF
folder, but the URL is implied as well. I am using the term "implied" here because let's think for a minute the consequences of a server allowing you to do what you want.
If a server allowed the /appcontext/META-INF
url to reach one of your filters/servlets then it has no way of knowing if you (the developer) would handle that case according to the specification or not. So strictly speaking, it does not fully conform to the specification. Even worse, it relies on the web developer to do that. If that was the case, then for every web application where the default behaviour was desired (i.e. META-INF
to not be accessible), a developer would have to implement a servlet/filter to do something that the server should be doing in the first place (!)
So I believe that what you ask can only be available at a server level and only if the server allows you that i.e. if the server gives you a configuration option to do that, or allows you to write your own interceptor class to handle the request in a way different then the default behaviour.
In Tomcat 7 for example META-INF
access is forbidden in the StandardContextValve class in its invoke()
method:
public final void invoke(Request request, Response response) throws IOException, ServletException {
// Disallow any direct access to resources under WEB-INF or META-INF
MessageBytes requestPathMB = request.getRequestPathMB();
if ((requestPathMB.startsWithIgnoreCase("/META-INF/", 0))
|| (requestPathMB.equalsIgnoreCase("/META-INF"))
|| (requestPathMB.startsWithIgnoreCase("/WEB-INF/", 0))
|| (requestPathMB.equalsIgnoreCase("/WEB-INF"))) {
notFound(response); // <-- Issues a response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
...
}
So you would have to create your own Valve
implementation (i.e. an AllowMetaInfAccessValve
that would basically be a copy of the above class without that "disallow" check), package it in a jar and put it in the <TOMCAT_HOME>/lib
folder.
Then in your server.xml
you would declare something like below. Using that approach URLs that attempt to access the META-INF
folder, would then reach your servlet and would be you own responsibility.
...
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
...
<Valve className="com.mypackage.valves.AllowMetaInfAccessValve" allow="true"/>
...
</Host>
...
Update: To clarify my reasoning a little more: It is true that a servlet could do many things other than serving files from META-INF
folder. But it could also do just that. Or even worse it would be sufficient for a web developer to just forget to forbid access, for the META-INF
folder to become accessible.
The point here though is not what the servlet would or would not do. The point is: should the people that implemented the server depend on the web developer to conform to the specification? My guess is that they wouldn't feel too comfortable with that thought. A specification is basically a set of rules. If you want to be able to say "my server follows that set of rules" you cannot depend on what a third person would do. Put yourself in their shoes. What would you do? Would you depend on some developer or make sure that your server follows the rules?
I believe that most people would make the same decision i.e. by default forbid access and provide an extension point of some sort. Now if this is a good decision or a bad one, only time will tell. After all, specifications evolve too. I hope it is more clear now