Commits

plightbo  committed b7351a2

* xwork 1.0.5 released
* exploratory ValidationServlet

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@638573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 7cf848a

Comments (0)

Files changed (4)

File lib/core/libraries.txt

 oscore.jar - OSCore version 2.2.4
 pell-multipart.jar - Pell Multipart File Upload version ???
 velocity-dep.jar - Velocity (plus dependencies) version 1.3.1
-xwork.jar - XWork version 1.0.4
+xwork.jar - XWork version 1.0.5

File lib/core/xwork.jar

Binary file modified.

File src/java/com/opensymphony/webwork/validators/ValidationServlet.java

+package com.opensymphony.webwork.validators;
+
+import com.opensymphony.xwork.*;
+import com.opensymphony.xwork.config.entities.ActionConfig;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * User: plightbo
+ * Date: Dec 11, 2004
+ * Time: 6:17:58 PM
+ */
+public class ValidationServlet extends HttpServlet {
+    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
+        try {
+            String action = null;
+            String namespace = null;
+            HashMap params = null;
+            res.setContentType("text/xml");
+            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+            Document doc = db.parse(req.getInputStream());
+
+            Element r = doc.getDocumentElement();
+            action = r.getAttribute("a");
+            namespace = r.getAttribute("ns");
+            params = new HashMap();
+            NodeList ps = r.getElementsByTagName("p");
+            for (int i = 0; i < ps.getLength(); i++) {
+                Node n = ps.item(i);
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    Element p = (Element) n;
+                    String name = p.getAttribute("n");
+                    Node child = p.getFirstChild();
+                    String value;
+                    if (child != null) {
+                        value = child.getNodeValue();
+                        params.put(name, value);
+                    }
+                }
+            }
+
+            Action a = null;
+            HashMap ctx = new HashMap();
+            ctx.put(ActionContext.PARAMETERS, params);
+            ValidatorActionProxy proxy = new ValidatorActionProxy(namespace, action, ctx);
+            proxy.execute();
+            a = proxy.getAction();
+
+            PrintWriter out = res.getWriter();
+            out.println("<?xml version=\"1.0\"?>");
+            out.println("<es>");
+
+            if (a instanceof ValidationAware) {
+                ValidationAware va = (ValidationAware) a;
+                Map fe = va.getFieldErrors();
+                for (Iterator iterator = fe.entrySet().iterator(); iterator.hasNext();) {
+                    Map.Entry entry = (Map.Entry) iterator.next();
+                    String name = (String) entry.getKey();
+                    List errors = (List) entry.getValue();
+                    for (Iterator iterator1 = errors.iterator(); iterator1.hasNext();) {
+                        String error = (String) iterator1.next();
+                        out.println("<e n=\"" + name + "\">" + error + "</e>");
+                    }
+                }
+            }
+
+            out.println("</es>");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    static class ValidatorActionInvocation extends DefaultActionInvocation {
+        protected ValidatorActionInvocation(ActionProxy proxy, Map extraContext) throws Exception {
+            super(proxy, extraContext, true);
+        }
+
+        protected String invokeAction(Action action, ActionConfig actionConfig) throws Exception {
+            return Action.NONE; // don't actually execute the action
+        }
+    }
+
+    static class ValidatorActionProxy extends DefaultActionProxy {
+        protected ValidatorActionProxy(String namespace, String actionName, Map extraContext) throws Exception {
+            super(namespace, actionName, extraContext, false);
+        }
+
+        protected void prepare() throws Exception {
+            invocation = new ValidatorActionInvocation(this, extraContext);
+        }
+    }
+}

File src/webapp/WEB-INF/web.xml

     </servlet>
 
     <servlet>
+        <servlet-name>validation</servlet-name>
+        <servlet-class>com.opensymphony.webwork.validators.ValidationServlet</servlet-class>
+    </servlet>
+
+    <servlet>
         <servlet-name>freemarker</servlet-name>
         <servlet-class>com.opensymphony.webwork.views.freemarker.FreemarkerServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
     </servlet-mapping>
 
     <servlet-mapping>
+        <servlet-name>validation</servlet-name>
+        <url-pattern>/validation</url-pattern>
+    </servlet-mapping>
+
+    <servlet-mapping>
         <servlet-name>velocity</servlet-name>
         <url-pattern>*.vm</url-pattern>
     </servlet-mapping>