cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ckosta...@gpu.com
Subject Utils.getBasename() issue. Cocoon 1.7.2
Date Fri, 23 Jun 2000 17:16:13 GMT


Just wanted to share a problem I had with Utils.class in 1.7.2 and my work
around.  If you believe this
was caused by misconfiguration on my part or it has been fixed in subsequent
releases please ignore this post.   I am
using Windows Nt 4.0, Apache 1.3.11, Tomcat 3.1, Cocoon 1.7.2, Sun jdk 1.2 and
Sun jsdk 2.0.

Problem:
1. The initial request to the web server
(http://d0airpta20.myDomain.com/CustomerCare/CustomerInfo.xml)
was  processed correctly and the correct HTML output returned to the browser.
2.  The second request to a different Tomcat webApp on the same web server
(http://d0airpta20.myDomain.com/DaveG/tips.xml)
failed with a file not found error from the Utils class.  The file path
identified in the error message returned to the browser
indicated that Cocoon was actually looking in the directory of the previous
request (CustomerCare) for the file (tips.xml).

After some debugging I found that the code below, in Utils.getBasename() was
returning the wrong directory and causing a
file not found error.
     String resource = ((ServletContext) context).getRealPath(path);


Work around:
Prior to moving to a newer version of Cocoon,  I experimented with changes in
Utils.java and ended up with the following
Utils.getBasename() method:

public static String getBasename(HttpServletRequest request, Object context) {
         return request.getRealPath(request.getServletPath()).replace('\\','/');
}

Previously the method call looked like this:

public static String getBasename(HttpServletRequest request, Object context) {
    try {
            // detect if the engine supports at least Servlet API 2.2
            request.getContextPath();
            // we need to check this in case we've been included in a servlet or
 jsp
            String path = (String)
request.getAttribute("javax.servlet.include.servlet_path");
            // otherwise, we find it out ourselves
            if (path == null) path = request.getServletPath();

            // FIXME (SM): we should use getResource() instead when we are
            // able to handle remote resources.
            String resource = ((ServletContext) context).getRealPath(path);

            if (resource != null) {
                return resource.replace('\\', '/');
            } else {
                throw new RuntimeException("Cannot access non-file/war
resources");
            }
        } catch (NoSuchMethodError e) {
            // if there is no such method we must be in Servlet API 2.1
            if (request.getPathInfo() != null) {
                // this must be Apache JServ
                return request.getPathTranslated().replace('\\','/');
            } else {
                // otherwise use the deprecated method on all other servlet
engines.
                return
request.getRealPath(request.getRequestURI()).replace('\\', '/');
            }
        } catch (NullPointerException e) {
            // if there is no context set, we must be called from the command
line
            return request.getPathTranslated().replace('\\','/');
}

Hope this is of some help.  Thanks for a great product!  I'd be happy to provide
 my configuration information if the list anyone wants it.
Chris Kostaras



Mime
View raw message