You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2006/03/20 22:41:19 UTC

svn commit: r387318 - in /beehive/trunk/netui: src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/ src/tags-html/org/apache/beehive/netui/tags/divpanel/ src/tags-html/org/apache/beehive/netui/tags/tree/ test/webapps/drt/web/WEB-INF/

Author: ekoneil
Date: Mon Mar 20 13:41:17 2006
New Revision: 387318

URL: http://svn.apache.org/viewcvs?rev=387318&view=rev
Log:
Few more changes around the XmlHttpRequestServlet and Commands.

- implement the Command interface on the ErrorCRI
- implement loading of the ErrorCRI onto either the command chain (if present) or onto the RequestInterceptors (otherwise)
- move initialization of the Command chain into the XmlHttpRequestServlet.init() method.  Note, this should still move to a single location for framework-wide initialization.  Perhaps a ServletContextListener?

BB: self
Test: NetUI pass


Modified:
    beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/XmlHttpRequestServlet.java
    beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/divpanel/DivPanelCRI.java
    beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/TreeCRI.java
    beehive/trunk/netui/test/webapps/drt/web/WEB-INF/beehive-netui-config.xml

Modified: beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/XmlHttpRequestServlet.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/XmlHttpRequestServlet.java?rev=387318&r1=387317&r2=387318&view=diff
==============================================================================
--- beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/XmlHttpRequestServlet.java (original)
+++ beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/xmlhttprequest/XmlHttpRequestServlet.java Mon Mar 20 13:41:17 2006
@@ -22,6 +22,8 @@
 import org.apache.beehive.netui.core.chain.CatalogFactory;
 import org.apache.beehive.netui.core.chain.Catalog;
 import org.apache.beehive.netui.core.chain.Command;
+import org.apache.beehive.netui.core.chain.Context;
+import org.apache.beehive.netui.core.chain.Chain;
 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptorContext;
 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptor;
 import org.apache.beehive.netui.pageflow.interceptor.Interceptors;
@@ -42,43 +44,27 @@
 import java.util.List;
 
 /**
- * Servlet to handle XMLHttpRequests sent from pages.
+ * Servlet to handle XMLHttpRequests sent from a client.
  */ 
 public class XmlHttpRequestServlet extends HttpServlet
 {
-    private static final Logger logger = Logger.getInstance(XmlHttpRequestServlet.class);
+    private static final Logger LOG = Logger.getInstance(XmlHttpRequestServlet.class);
+    private static final String COMMAND_XHR = "xhr-commands";
 
     public void init() throws ServletException
     {
         ServletContext ctxt = getServletContext();
-        RequestInterceptorContext.init(ctxt);
-        // TODO: does ErrorCRI really need to be an interceptor?
-        RequestInterceptorContext.addInterceptor(ctxt, new ErrorCRI());
-    }
 
-    public void doGet(HttpServletRequest request,
-                      HttpServletResponse response)
-        throws IOException, ServletException
-    {
-        //System.err.println("Inside the XmlHppRequestServlet:" + request.getRequestURI());
-
-        // create an XML empty document, that isn't cached on the client
-        /* todo: move this response manipulation into the CRIs */
-        response.setContentType("text/xml");
-        response.setHeader("Pragma", "No-cache");
-        response.setHeader("Cache-Control", "no-cache");
-        
-        // Register the default URLRewriter
-        URLRewriterService.registerURLRewriter(0, request, new DefaultURLRewriter());
+        /* Initialize the Command chain and add the ErrorCRI */
 
-        ServletContext ctxt = getServletContext();
-        Command xhrServletCommand = null;
+        Chain xhrServletCommand = null;
         CatalogConfig catalogConfig = ConfigUtil.getConfig().getCatalogConfig();
         if(catalogConfig != null) {
             /*
             todo: neaten up this initialization process.  because of the separation between
                   parsing and configuration, this is a second step.
-                  Need to put this somewhere in the framework.
+                  Need to put this somewhere in the framework, preferably in a single
+                  place that initializes the PageFlow runtime.
              */
             CatalogFactory catalogFactory = CatalogFactory.getInstance();
             if(catalogFactory.getCatalog() == null)
@@ -86,14 +72,46 @@
 
             assert catalogFactory != null;
             Catalog catalog = catalogFactory.getCatalog();
-            if(catalog != null)
-                xhrServletCommand = catalog.getCommand("xhr-commands");
+            if(catalog != null) {
+                xhrServletCommand = (Chain)catalog.getCommand(COMMAND_XHR);
+            }
+
+            if(xhrServletCommand != null) {
+                xhrServletCommand.addCommand(new ErrorCRI());
+            }
+        }
+
+        /*
+        For compatibility, add the ErrorCRI to the list of global Interceptors only if the
+        Command for the .xhr servlet wasn't found.  Note, this code will <em>disappear</em>
+        in a subsequent release of Beehive.
+         */
+        if(xhrServletCommand != null) {
+            RequestInterceptorContext.init(ctxt);
+            RequestInterceptorContext.addInterceptor(ctxt, new ErrorCRI());
+        }
+    }
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        //System.err.println("Inside the XmlHppRequestServlet:" + request.getRequestURI());
+
+        // Register the default URLRewriter
+        URLRewriterService.registerURLRewriter(0, request, new DefaultURLRewriter());
+
+        ServletContext ctxt = getServletContext();
+        Command xhrServletCommand = null;
+        CatalogFactory catalogFactory = CatalogFactory.getInstance();
+        if(catalogFactory != null && catalogFactory.getCatalog() != null) {
+            xhrServletCommand = catalogFactory.getCatalog().getCommand(COMMAND_XHR);
         }
 
         // execute the Command if found or the interceptors if found
         if(xhrServletCommand != null) {
             /* todo: add a chain to create the Context object */
             WebChainContext webChainContext = new WebChainContext(ctxt, request, response);
+
             try {
                 xhrServletCommand.execute(webChainContext);
             }
@@ -119,19 +137,53 @@
         */
     }
 
-    public void doPost(HttpServletRequest request,
-                      HttpServletResponse response)
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException
     {
         doGet(request, response);
     }
 
-    class ErrorCRI extends RequestInterceptor
+    /**
+     * Implementation of a {@link RequestInterceptor} and a {@link Command} that handle errors
+     * sent from the client.
+     *
+     * Note, this class <i>should not</i> be referenced by user code; it is added by the framework.
+     */
+    class ErrorCRI
+        extends RequestInterceptor
+        implements Command
     {
-        public void preRequest(RequestInterceptorContext ctxt, InterceptorChain chain) throws InterceptorException
-        {
-            // Create the command by striping off the context path and the extension
+        public void preRequest(RequestInterceptorContext ctxt, InterceptorChain chain)
+            throws InterceptorException {
+
             HttpServletRequest request = ctxt.getRequest();
+            String cmd = parseCommand(request);
+            render(cmd, request);
+            chain.continueChain();
+        }
+
+        public void postRequest(RequestInterceptorContext context, InterceptorChain chain)
+            throws InterceptorException {
+            chain.continueChain();
+        }
+
+        public boolean execute(Context context)
+            throws Exception {
+
+            assert context != null;
+            assert context instanceof WebChainContext;
+            assert ((WebChainContext)context).getServletRequest() instanceof HttpServletRequest;
+            assert ((WebChainContext)context).getServletResponse() instanceof HttpServletResponse;
+
+            WebChainContext webChainContext = (WebChainContext)context;
+            HttpServletRequest request = (HttpServletRequest)webChainContext.getServletRequest();
+
+            String cmd = parseCommand(request);
+            return render(cmd, request);
+        }
+
+        private String parseCommand(HttpServletRequest request) {
+            // Create the command by striping off the context path and the extension
             String cmd = request.getRequestURI();
             String ctxtPath = request.getContextPath();
 
@@ -142,23 +194,37 @@
                 if (idx != -1) {
                     cmd = cmd.substring(0, idx);
                 }
+            }
+            catch (RuntimeException e) {
+                LOG.error("Runtime Error creating XmlRequestServlet Command:" + e.getClass().getName(),e);
 
+                // set the command string to null to prevent the ErrorCRI command handler from executing
+                cmd = null;
+            }
+
+            return cmd;
+        }
+
+        private boolean render(String cmd, HttpServletRequest request) {
+            try {
                 if ("netuiError".equals(cmd)) {
                     String error = request.getParameter("error");
-                    logger.error("NetUI JavaScript Error:" + error);
-                    System.err.println("NetUI JavaScript Error:" + error);
+                    LOG.error("NetUI JavaScript Error:" + error);
+                    //System.err.println("NetUI JavaScript Error:" + error);
+                    return true;
                 }
             }
             catch (RuntimeException e) {
-                logger.error("Runtime Error creating XmlRequestServlet Command:" + e.getClass().getName(),e);
+                LOG.error("Runtime Error creating XmlRequestServlet Command:" + e.getClass().getName(),e);
             }
 
-            chain.continueChain();
+            /*
+             If the command wasn't a "netuiError", don't assume that it was handled and let
+             any additional Commands have a chance to take some action in response to
+             an error.
+              */
+            return false;
         }
 
-        public void postRequest(RequestInterceptorContext context, InterceptorChain chain) throws InterceptorException
-        {
-            chain.continueChain();
-        }
     }
 }

Modified: beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/divpanel/DivPanelCRI.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/divpanel/DivPanelCRI.java?rev=387318&r1=387317&r2=387318&view=diff
==============================================================================
--- beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/divpanel/DivPanelCRI.java (original)
+++ beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/divpanel/DivPanelCRI.java Mon Mar 20 13:41:17 2006
@@ -28,6 +28,7 @@
 import org.apache.beehive.netui.core.chain.web.WebChainContext;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 public class DivPanelCRI
     extends AbstractClientRequestInterceptor
@@ -41,9 +42,11 @@
         assert context != null;
         assert context instanceof WebChainContext;
         assert ((WebChainContext)context).getServletRequest() instanceof HttpServletRequest;
+        assert ((WebChainContext)context).getServletResponse() instanceof HttpServletResponse;
 
         WebChainContext webChainContext = (WebChainContext)context;
         HttpServletRequest request = (HttpServletRequest)webChainContext.getServletRequest();
+        HttpServletResponse response = (HttpServletResponse)webChainContext.getServletResponse();
 
         // Create the command by striping off the context path and the extension
         String uri = request.getRequestURI();
@@ -51,12 +54,13 @@
 
         String cmd = getCommand(uri, ctxtPath);
 
-        return handleCommand(cmd, request);
+        return handleCommand(cmd, request, response);
     }
 
     public void preRequest(RequestInterceptorContext ctxt, InterceptorChain chain)
     {
         HttpServletRequest request = ctxt.getRequest();
+        HttpServletResponse response = ctxt.getResponse();
 
         // Create the command by striping off the context path and the extension
         String uri = request.getRequestURI();
@@ -64,7 +68,7 @@
 
         String cmd = getCommand(uri, ctxtPath);
 
-        handleCommand(cmd, request);
+        handleCommand(cmd, request, response);
     }
 
     public void postRequest(RequestInterceptorContext context, InterceptorChain chain) throws InterceptorException
@@ -72,21 +76,26 @@
         chain.continueChain();
     }
 
-    private boolean handleCommand(String command, HttpServletRequest request) {
+    private boolean handleCommand(String command, HttpServletRequest request, HttpServletResponse response) {
         if (SWITCH_PAGE.equals(command)) {
-            handlePageSwitch(request);
+            handlePageSwitch(request, response);
             return true;
         }
         else return false;
     }
 
-    private void handlePageSwitch(HttpServletRequest req)
+    private void handlePageSwitch(HttpServletRequest request, HttpServletResponse response)
     {
-        String dp = req.getParameter("divPanel");
-        String fp = req.getParameter("firstPage");
+        // create an XML empty document, that isn't cached on the client        
+        response.setContentType("text/xml");
+        response.setHeader("Pragma", "No-cache");
+        response.setHeader("Cache-Control", "no-cache");
+
+        String dp = request.getParameter("divPanel");
+        String fp = request.getParameter("firstPage");
         //System.err.println("DivPanel Command: switch, DivPanel:" + dp + " Node:" + fp);
 
-        NameService ns = NameService.instance(req.getSession());
+        NameService ns = NameService.instance(request.getSession());
         assert(ns != null);
 
         // get the DivPanel from the name service

Modified: beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/TreeCRI.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/TreeCRI.java?rev=387318&r1=387317&r2=387318&view=diff
==============================================================================
--- beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/TreeCRI.java (original)
+++ beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/TreeCRI.java Mon Mar 20 13:41:17 2006
@@ -67,15 +67,14 @@
         assert context != null;
         assert context instanceof WebChainContext;
         assert ((WebChainContext)context).getServletRequest() instanceof HttpServletRequest;
+        assert ((WebChainContext)context).getServletResponse() instanceof HttpServletResponse;
 
         WebChainContext webChainContext = (WebChainContext)context;
         HttpServletRequest request = (HttpServletRequest)webChainContext.getServletRequest();
+        HttpServletResponse response = (HttpServletResponse)webChainContext.getServletResponse();
 
         String cmd = parseCommand(request.getRequestURI());
-        return render(request,
-            webChainContext.getServletResponse(),
-            webChainContext.getServletContext(),
-            cmd);
+        return render(request, response, webChainContext.getServletContext(), cmd);
     }
 
     /**
@@ -127,7 +126,7 @@
     }
 
     private boolean render(HttpServletRequest request,
-                           ServletResponse response,
+                           HttpServletResponse response,
                            ServletContext servletContext,
                            String cmd) {
 
@@ -146,9 +145,14 @@
 
     private void handleExpandCollapse(boolean expand,
                                       HttpServletRequest req,
-                                      ServletResponse response,
+                                      HttpServletResponse response,
                                       ServletContext ctxt)
     {
+        // create an XML empty document, that isn't cached on the client
+        response.setContentType("text/xml");
+        response.setHeader("Pragma", "No-cache");
+        response.setHeader("Cache-Control", "no-cache");
+
         String tree = req.getParameter("tree");
         String node = req.getParameter("node");
         String expandSvr = req.getParameter("expandOnServer");

Modified: beehive/trunk/netui/test/webapps/drt/web/WEB-INF/beehive-netui-config.xml
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/test/webapps/drt/web/WEB-INF/beehive-netui-config.xml?rev=387318&r1=387317&r2=387318&view=diff
==============================================================================
--- beehive/trunk/netui/test/webapps/drt/web/WEB-INF/beehive-netui-config.xml (original)
+++ beehive/trunk/netui/test/webapps/drt/web/WEB-INF/beehive-netui-config.xml Mon Mar 20 13:41:17 2006
@@ -182,6 +182,23 @@
        <id-javascript>legacy</id-javascript>
     </jsp-tag-config>
 
+    <!-- This request-interceptors block is used to ensure that the RequestInterceptor pattern
+         continues to work.  For subsequent Beehive releases, the preferred pattern is 
+         to use the Command/Chain Catalog
+      -->
+<!--
+    <request-interceptors>
+        <global>
+            <request-interceptor>
+                <interceptor-class>org.apache.beehive.netui.tags.tree.TreeCRI</interceptor-class>
+            </request-interceptor>
+            <request-interceptor>
+                <interceptor-class>org.apache.beehive.netui.tags.divpanel.DivPanelCRI</interceptor-class>
+            </request-interceptor>
+        </global>
+    </request-interceptors>
+-->
+
     <catalog>
         <chain name="xhr-commands">
             <command>
@@ -192,5 +209,4 @@
             </command>
         </chain>
     </catalog>
-
 </netui-config>