dev@grizzly.java.net

Issue 591 : possible solutions : Tous pour un et un pour tous.

From: Survivant 00 <survivant00_at_gmail.com>
Date: Wed, 20 May 2009 21:24:06 -0400

Hello it me again :)

I worked on that for few hours and here my observations. (PS.. we had the
problem with Jasper, and PHP and probably all framework that generated code
from files)

I created a blank project using the source of Tomcat 6 : Jasper + a little
testcase using GWS.

here my testcase

#1 - I created a folder structure like that

C:\workspace\JSPSupport\demo\jsp\index.jsp
C:\workspace\JSPSupport\src
C:\workspace\JSPSupport\lib

in lib I had to put : ant.jar and jasper-jdt.jar


the Junit look like that

public void testJSP_Jasper2() throws IOException {
        System.out.println("testJSP_Jasper");
        try {
            startGrizzlyWebServer(PORT);
            String[] aliases = new String[] { "*.jsp" };

            String context = "/";
            String servletPath = "demo/jsp";
            String rootFolder = "c:/workspace/JSPSupport/";

            ServletAdapter adapter = new ServletAdapter();
            Servlet servlet = (Servlet)
ClassLoaderUtil.load("org.apache.jasper.servlet.JspServlet");
            adapter.setServletInstance(servlet);

            adapter.setContextPath(context);
            adapter.setServletPath(servletPath);
            adapter.setRootFolder(rootFolder);

            gws.addGrizzlyAdapter(adapter, aliases);

            gws.start();

            String url = context + servletPath + "/index.jsp";
            HttpURLConnection conn = getConnection(url);
            assertEquals(HttpServletResponse.SC_OK,
getResponseCodeFromAlias(conn));

            String response = readResponse(conn).toString();
            assertEquals(url, response.trim());

        } finally {
            stopGrizzlyWebServer();
        }
    }


*Observations*

// this testcase failed because in GrizzlyAdapterChain we set
setHandleStaticResources(true); instead of using the next SA.
        // maybe we should call the StaticResourcesAdapter at the end when
no other servlet works ?

        //and we have also in GWS :
adapterChains.setHandleStaticResources(true);
        //should be able to force it to false ?

*If I force the setHandleStaticResources(false).. Jasper servlet will be
called* if not.. the staticResourceAdapter will handle the file.. and it's
not what we want

        /*
         if (!isRootConfigured && wrapper.startsWith("*.")){
                        isRootConfigured = true;
                        GrizzlyAdapter a = new
GrizzlyAdapter(getRootFolder()){
                            {
                                setHandleStaticResources(true); /// HERE
                            }

                            @Override
                            public void service(GrizzlyRequest request,
GrizzlyResponse response) {
                                try {

customizedErrorPage(request.getRequest(), response.getResponse());
                                } catch (Exception ex) {
                                    ;
                                }
                            }
                        };
                        mapper.addContext(LOCAL_HOST, ctx, a,
                                new String[]{"index.html", "index.htm"},
null);
         */


*After that I forced it to false.. Jasper was called*

/*
     * here what we got in Jasper

          Requested JSP has not been the target of a
          RequestDispatcher.include(). Reconstruct its path from the
          request's getServletPath() and getPathInfo()

        jspUri = request.getServletPath();
        String pathInfo = request.getPathInfo();
        if (pathInfo != null) {
            jspUri += pathInfo;
        }

*Results
        // jspUri = demo/jsp
        // pathInfo = /index.jsp*


PATCH HERE
         //JASPER want it to start with /
          if(!jspUri.startsWith("/")){
                    jspUri = "/" + jspUri;
                }
         //without the "/" Jasper will remove "d" from demo as path :)

JspEngine --> demo/jsp/index.jsp
ServletPath: demo/jsp
PathInfo: /index.jsp
RealPath: C:\workspace\JSPSupport\demo\jsp\index.jsp
RequestURI: /demo/jsp/index.jsp
QueryString: null
Request Params:

*PROBLEM with
com.sun.grizzly.http.servlet.ServletContextImpl.getResource(String path)*

        JASPER will ask for
        context.getResource(jspUri)

        com.sun.grizzly.http.servlet.ServletContextImpl.getResource(String
path)

we have that

        if (path == null || !path.startsWith("/")) {
            throw new MalformedURLException(path);
        }


        PATCH

        // Help the UrlClassLoader, which is not able to load resources
        // that contains '//'
        if (path.length() > 1 && path.startsWith("/")){
            path = path.substring(1);
        }


*THE COMPILATION PROCESS NOW*

        later Jasper try to compile the jsp.

        try to get resource from *
com.sun.grizzly.http.servlet.ServletContextImpl.java*

        where path = "/demo/jsp/index.jsp"

        public InputStream getResourceAsStream(String path) {

            path = normalize(path);
            if (path == null)
                return (null);

            return Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(path);
        }

        and return NULL.. why ? should it check into the rootfolder ? or
we have to put that folder ourself into the classpath ? (I do that in the
deployer.. but I'm not using it in this test.. will try that tomorrow.. too
tired)



any comments ?