protected String acquireLocalURLString()

in velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewImportSupport.java [172:239]


    protected String acquireLocalURLString(String url) throws IOException
    {
        // URL is local, so we must be an HTTP request
        if (!(request instanceof HttpServletRequest
            && response instanceof HttpServletResponse))
        {
            throw new IOException("Local import from non-HTTP request not allowed");
        }

        // retrieve an appropriate ServletContext
        // normalize the URL if we have an HttpServletRequest
        if (!url.startsWith("/"))
        {
            String sp = ((HttpServletRequest)request).getServletPath();
            url = sp.substring(0, sp.lastIndexOf('/')) + '/' + url;
        }

        // strip the session id from the url
        url = stripSession(url);

        // According to the 3.1 Servlet API specification, the query string parameters of the URL to include
        // take *precedence* over the original query string parameters. It means that:
        // - we must merge both query strings
        // - we must set aside the cached request toolbox during the include
        url = mergeQueryStrings(url);
        Object parentToolbox = request.getAttribute(Toolbox.KEY);
        request.removeAttribute(Toolbox.KEY);

        // from this context, get a dispatcher
        RequestDispatcher rd = application.getRequestDispatcher(url);
        if (rd == null)
        {
            throw new IOException("Couldn't get a RequestDispatcher for \""
                + url + "\"");
        }

        // include the resource, using our custom wrapper
        ImportResponseWrapper irw =
            new ImportResponseWrapper((HttpServletResponse)response);
        try
        {
            rd.include(request, irw);
        }
        catch (IOException ex)
        {
            throw new IOException("Problem importing the local URL \"" + url + "\": " + ex.getMessage(), ex);
        }
        catch (ServletException se)
        {
            throw new IOException("Problem importing the local URL \"" + url + "\": " + se.getMessage(), se);

        }
        finally
        {
            request.setAttribute(Toolbox.KEY, parentToolbox);
        }
        /* let RuntimeExceptions go through */

        // disallow inappropriate response codes per JSTL spec
        if (irw.getStatus() < 200 || irw.getStatus() > 299)
        {
            throw new IOException("Invalid response code '" + irw.getStatus()
                + "' for \"" + url + "\"");
        }

        // recover the response String from our wrapper
        return irw.getString();
    }