You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Antonio <an...@gmail.com> on 2008/10/22 14:44:02 UTC

Throwing exception when including unexisting resource

Hi all,
I am a Tiles developer and I am struggling to resolve a problem in
Tiles, that is reflected from a "gap" in the servlet specifications:
https://issues.apache.org/struts/browse/TILES-320
What I want to do is to throw an exception whenever an included
(through the use of RequestDispatcher.include and PageContext.include)
resource is missing.
The problem is that RequestDispatcher.include and PageContext.include
do not throw anything if a resource is missing, they simple "go on"
rendering the page. The API specification does not say anything about
it:
http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html#include(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse)
http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/PageContext.html#include(java.lang.String)

I would like to avoid replication of code, if possible.

Any advice will be appreciated.

Thanks
Antonio

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


Re: Throwing exception when including unexisting resource

Posted by Tim Funk <fu...@joedog.org>.
getServletPath() should return the path of the requested resource. So if 
I call include("/foo.html") - getServletPath() at that point during 
invocation should be /foo.html.

servletContext.getResource(stuff) returns a URL if the resource exists. 
For servlets which map to some extension like *.do - 
servletContext.getResource(stuff) would return null since the controller 
would decode the URL into some action.


-Tim


Antonio wrote:
> 2008/10/22 Tim Funk <fu...@joedog.org>:
>>  if (null==servletContext.getResource(request.getServletPath())) {
> 
> Are you sure that it works? the "getServletPath" returns the path or
> the name of the servlet.
> Notice that anything could be included, from a JSP page to a called
> servlet with parameters.
> Anyway I think that the filter suggestion is good (thanks :-) ),
> probably a HTTP request wrapper that checks if at least 1 byte is
> written to the response could be useful.
> 
>> Then map the filter as needed (with any additional checks too)
> 
> Something like:
> <filter-mapping>
>         <filter-name>my_filter</filter-name>
>         <url-pattern>/*</url-pattern>
>         <dispatcher>INCLUDE</dispatcher>
> </filter-mapping>


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


Re: Throwing exception when including unexisting resource

Posted by Antonio <an...@gmail.com>.
2008/10/22 Tim Funk <fu...@joedog.org>:
> This would not work for includes since includes can't change the status or
> change any headers.

I think that I will try the "404 + 0 bytes" approach then.

Thanks
Antonio

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


Re: Throwing exception when including unexisting resource

Posted by Tim Funk <fu...@joedog.org>.
This would not work for includes since includes can't change the status 
or change any headers.

-Tim

Filip Hanik - Dev Lists wrote:
> you could also create a response wrapper in a filter, capture the 
> status/output,
> so if the servlet sets 404, you can react to it appropriately
> Filip
> 
> Antonio wrote:
>> 2008/10/22 Tim Funk <fu...@joedog.org>:
>>  
>>>  if (null==servletContext.getResource(request.getServletPath())) {
>>>     
>>
>> Are you sure that it works? the "getServletPath" returns the path or
>> the name of the servlet.
>> Notice that anything could be included, from a JSP page to a called
>> servlet with parameters.
>> Anyway I think that the filter suggestion is good (thanks :-) ),
>> probably a HTTP request wrapper that checks if at least 1 byte is
>> written to the response could be useful.
>>
>>  
>>> Then map the filter as needed (with any additional checks too)
>>>     
>>
>> Something like:
>> <filter-mapping>
>>         <filter-name>my_filter</filter-name>
>>         <url-pattern>/*</url-pattern>
>>         <dispatcher>INCLUDE</dispatcher>
>> </filter-mapping>


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


Re: Throwing exception when including unexisting resource

Posted by Antonio <an...@gmail.com>.
2008/10/22 Filip Hanik - Dev Lists <de...@hanik.com>:
> you could also create a response wrapper in a filter, capture the
> status/output,
> so if the servlet sets 404, you can react to it appropriately

You're right, thanks :-)

Antonio

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


Re: Throwing exception when including unexisting resource

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
you could also create a response wrapper in a filter, capture the 
status/output,
so if the servlet sets 404, you can react to it appropriately
Filip

Antonio wrote:
> 2008/10/22 Tim Funk <fu...@joedog.org>:
>   
>>  if (null==servletContext.getResource(request.getServletPath())) {
>>     
>
> Are you sure that it works? the "getServletPath" returns the path or
> the name of the servlet.
> Notice that anything could be included, from a JSP page to a called
> servlet with parameters.
> Anyway I think that the filter suggestion is good (thanks :-) ),
> probably a HTTP request wrapper that checks if at least 1 byte is
> written to the response could be useful.
>
>   
>> Then map the filter as needed (with any additional checks too)
>>     
>
> Something like:
> <filter-mapping>
>         <filter-name>my_filter</filter-name>
>         <url-pattern>/*</url-pattern>
>         <dispatcher>INCLUDE</dispatcher>
> </filter-mapping>
> ?
>
> Thanks
> Antonio
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>   


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


Re: Throwing exception when including unexisting resource

Posted by Antonio <an...@gmail.com>.
2008/10/22 Tim Funk <fu...@joedog.org>:
>  if (null==servletContext.getResource(request.getServletPath())) {

Are you sure that it works? the "getServletPath" returns the path or
the name of the servlet.
Notice that anything could be included, from a JSP page to a called
servlet with parameters.
Anyway I think that the filter suggestion is good (thanks :-) ),
probably a HTTP request wrapper that checks if at least 1 byte is
written to the response could be useful.

> Then map the filter as needed (with any additional checks too)

Something like:
<filter-mapping>
        <filter-name>my_filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>INCLUDE</dispatcher>
</filter-mapping>
?

Thanks
Antonio

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


Re: Throwing exception when including unexisting resource

Posted by Tim Funk <fu...@joedog.org>.
This can be done in a Filter. Horribly pseudo coded as:

doFilter(...) {
   if (null==servletContext.getResource(request.getServletPath())) {
      throw new ServletException("No file");
   }
   chain.doFilter(...);
}

Then map the filter as needed (with any additional checks too)

-Tim

Antonio wrote:
> Hi all,
> I am a Tiles developer and I am struggling to resolve a problem in
> Tiles, that is reflected from a "gap" in the servlet specifications:
> https://issues.apache.org/struts/browse/TILES-320
> What I want to do is to throw an exception whenever an included
> (through the use of RequestDispatcher.include and PageContext.include)
> resource is missing.
> The problem is that RequestDispatcher.include and PageContext.include
> do not throw anything if a resource is missing, they simple "go on"
> rendering the page. The API specification does not say anything about
> it:
> http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html#include(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse)
> http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/PageContext.html#include(java.lang.String)


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