You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by mr...@apache.org on 2009/01/30 23:33:07 UTC

svn commit: r739437 - in /ode/sandbox/simpel: ./ src/main/java/org/apache/ode/rest/ src/main/java/org/apache/ode/rest/datam/ src/test/java/org/apache/ode/rest/

Author: mriou
Date: Fri Jan 30 22:33:06 2009
New Revision: 739437

URL: http://svn.apache.org/viewvc?rev=739437&view=rev
Log:
Producing HTML in response to a form-enc request.

Added:
    ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/
    ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/FEJOML.java
Modified:
    ode/sandbox/simpel/Rakefile
    ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java
    ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java

Modified: ode/sandbox/simpel/Rakefile
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/Rakefile?rev=739437&r1=739436&r2=739437&view=diff
==============================================================================
--- ode/sandbox/simpel/Rakefile (original)
+++ ode/sandbox/simpel/Rakefile Fri Jan 30 22:33:06 2009
@@ -93,5 +93,6 @@
     ODE, LOG4J, WSDL4J, ASM, JERSEY, JAVAX.rest, JETTY, GERONIMO.transaction, XERCES,
     file(_("lib/e4x-grammar-0.2.jar")), ANTLR, ANTLR_TEMPLATE, file(_("lib/rhino-1.7R2pre-patched.jar"))
   test.using :fork => :each
+  test.exclude 'SingleshotTest'
   package :jar
 end

Modified: ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java?rev=739437&r1=739436&r2=739437&view=diff
==============================================================================
--- ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java (original)
+++ ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java Fri Jan 30 22:33:06 2009
@@ -5,8 +5,11 @@
 import org.apache.ode.embed.ServerLifecycle;
 import org.apache.ode.utils.GUID;
 import org.apache.ode.utils.DOMUtils;
+import org.apache.ode.rest.datam.FEJOML;
 import org.w3c.dom.Element;
 import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
 
 import javax.ws.rs.*;
 import javax.ws.rs.core.Response;
@@ -45,7 +48,6 @@
             } else {
                 return Response.status(200)
                         .entity(unwrapResponse(mex.getResponse().getMessage()))
-                        .type("application/xml")
                         .header("Location", _root+mex.getResource().getUrl())
                         .build();
             }
@@ -53,7 +55,7 @@
         else return Response.status(405).header("Allow", _resource.methods()).build();
     }
 
-    public Response post(Element msgElmt) {
+    public Response post(Element msgElmt, String targetFormat) {
         if (_resource.post) {
             RESTInMessageExchange mex = _serverLifecyle.getServer().createMessageExchange(
                     _resource.toResource("POST"), new GUID().toString());
@@ -89,84 +91,63 @@
                     b = Response.status(201).header("Location", _root + mex.getResource().getUrl());
                 else
                     b = Response.status(200);
-                return b.entity(unwrapResponse(mex.getResponse().getMessage()))
-                        .type("application/xml")
+                return b.entity(FEJOML.fromXML(unwrapResponse(mex.getResponse().getMessage()), targetFormat))
                         .build();
             }
         }
         else return Response.status(405).header("Allow", _resource.methods()).build();
     }
 
-    private String unwrapResponse(Element resp) {
+    private Node unwrapResponse(Element resp) {
         Element partElmt = DOMUtils.getFirstChildElement(DOMUtils.getFirstChildElement(resp));
         Element unwrapped = DOMUtils.getFirstChildElement(partElmt);
-        if (unwrapped == null) return partElmt.getTextContent();
-        else return DOMUtils.domToString(unwrapped);
+        if (unwrapped == null)
+            return partElmt.getOwnerDocument().createTextNode(partElmt.getTextContent());
+        else return unwrapped;
     }
 
-    @GET @Produces("application/xml") @Path("{sub : .*}")
+    @GET @Path("{sub : .*}")
+    @Produces("application/xml")
     public Response getSub() {
         return get();
     }
 
-    @POST @Consumes("application/xml")
+    @POST
+    @Consumes("application/xml") @Produces("application/xml")
     public Response post(String content) {
         try {
             Element msgElmt = null;
             if (content.length() > 0) msgElmt = DOMUtils.stringToDOM(content);
-            return post(msgElmt);
+            return post(msgElmt, FEJOML.XML);
         } catch (Exception e) {
             return Response.status(400).entity("Couldn't parse XML request.").type("text/plain").build();
         }
     }
 
-    @POST @Consumes("application/xml") @Path("{sub : .*}")
+    @POST @Path("{sub : .*}")
+    @Consumes("application/xml") @Produces("application/xml")
     public Response postSub(String content) {
         try {
             Element msgElmt = null;
             if (content.length() > 0) msgElmt = DOMUtils.stringToDOM(content);
-            return post(msgElmt);
+            return post(msgElmt, FEJOML.XML);
         } catch (Exception e) {
             return Response.status(400).entity("Couldn't parse XML request.").type("text/plain").build();
         }
     }
 
-    @POST @Consumes("application/x-www-form-urlencoded")
+    @POST
+    @Consumes("application/x-www-form-urlencoded") @Produces("text/html")
     public Response postEnc(MultivaluedMap<String, String> formParams) {
-        return post(formToXml(formParams));
+        return post(FEJOML.formToXML(formParams), FEJOML.FUE);
     }
 
-    @POST @Consumes("application/x-www-form-urlencoded") @Path("{sub : .*}")
+    @POST @Path("{sub : .*}")
+    @Consumes("application/x-www-form-urlencoded") @Produces("text/html")
     public Response postSubEnc(MultivaluedMap<String, String> formParams) {
-        return post(formToXml(formParams));
+        return post(FEJOML.formToXML(formParams), FEJOML.FUE);
     }
 
-    private Element formToXml(MultivaluedMap<String, String> formParams) {
-        Document doc = DOMUtils.newDocument();
-        Element form = doc.createElement("form");
-        doc.appendChild(form);
-        for (Map.Entry<String, List<String>> param : formParams.entrySet()) {
-            if (param.getValue().size() == 1) {
-                Element k = doc.createElement(param.getKey());
-                k.setTextContent(param.getValue().get(0));
-                form.appendChild(k);
-            } else {
-                // TODO support Rails encoding conventions as well
-                Element container = doc.createElement(pluralize(param.getKey()));
-                for (String s : param.getValue()) {
-                    Element e = doc.createElement(param.getKey());
-                    e.setTextContent(s);
-                    container.appendChild(e);
-                }
-            }
-        }
-        return form;
-    }
-
-    private String pluralize(String s) {
-        // Doing it the lazy way for now TODO get all pluralization rules from Rails
-        return s + "s";
-    }
 
 //    @GET @Produces("application/xhtml+xml")
 //    public String getXHTML() {

Added: ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/FEJOML.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/FEJOML.java?rev=739437&view=auto
==============================================================================
--- ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/FEJOML.java (added)
+++ ode/sandbox/simpel/src/main/java/org/apache/ode/rest/datam/FEJOML.java Fri Jan 30 22:33:06 2009
@@ -0,0 +1,83 @@
+package org.apache.ode.rest.datam;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.apache.ode.utils.DOMUtils;
+
+import javax.ws.rs.core.MultivaluedMap;
+import java.util.Map;
+import java.util.List;
+
+/**
+ * This ugly name stands for Form Encoded - JSON - X(HT)ML in case you were wondering, which are all
+ * the formats this attempts to translate. They're all based on a common data model that's a sane subset
+ * of each format.
+ * TODO this is barely better than a mock
+ */
+public class FEJOML {
+
+    public static final String JSON = "application/json";
+    public static final String XML = "application/xml";
+    public static final String XHTML = "application/xhtml+xml";
+    public static final String FUE = "application/x-www-form-urlencoded";
+
+    public static String convert(String in, String from, String to) {
+        throw new UnsupportedOperationException("not yet");
+    }
+
+    public static Element formToXML(MultivaluedMap<String, String> formParams) {
+        Document doc = DOMUtils.newDocument();
+        Element form = doc.createElement("form");
+        doc.appendChild(form);
+        for (Map.Entry<String, List<String>> param : formParams.entrySet()) {
+            if (param.getValue().size() == 1) {
+                Element k = doc.createElement(param.getKey());
+                k.setTextContent(param.getValue().get(0));
+                form.appendChild(k);
+            } else {
+                // TODO support Rails encoding conventions as well
+                Element container = doc.createElement(pluralize(param.getKey()));
+                for (String s : param.getValue()) {
+                    Element e = doc.createElement(param.getKey());
+                    e.setTextContent(s);
+                    container.appendChild(e);
+                }
+            }
+        }
+        return form;
+    }
+
+    public static String fromXML(Node in, String to) {
+        if (to.equals(XML)) return DOMUtils.domToString(in);
+        else if (to.equals(FUE)) return xmlToHtml(in);
+        else throw new UnsupportedOperationException("Not yet");
+    }
+
+    private static String xmlToHtml(Node in) {
+        StringBuffer html = new StringBuffer();
+        html.append("<html>\n    <body>\n");
+        if (in.getNodeType() == Node.TEXT_NODE || in.getTextContent() != null && in.getTextContent().trim().length() > 0)
+            html.append("<p>").append(in.getTextContent()).append("</p>");
+        else {
+            // todo this is super quick and dirty
+            NodeList children = in.getChildNodes();
+            for (int m = 0; m < children.getLength(); m++) {
+                if (children.item(m).getNodeType() == Node.ELEMENT_NODE) {
+                    Element child = (Element) children.item(m);
+                    html.append("        <p><label>").append(child.getNodeName())
+                            .append("</label>: ").append(child.getTextContent());
+                }
+            }
+        }
+        html.append("    </body>\n</html>");
+        return html.toString();
+    }
+
+    private static String pluralize(String s) {
+        // Doing it the lazy way for now TODO get all pluralization rules from Rails
+        return s + "s";
+    }
+
+}

Modified: ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java?rev=739437&r1=739436&r2=739437&view=diff
==============================================================================
--- ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java (original)
+++ ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java Fri Jan 30 22:33:06 2009
@@ -267,6 +267,8 @@
         ClientConfig cc = new DefaultClientConfig();
         Client c = Client.create(cc);
 
+        Thread.sleep(10000000);
+
         WebResource wr = c.resource("http://localhost:3434/hello-form");
         ClientResponse resp = wr.path("/").accept("application/x-www-form-urlencoded").type("application/x-www-form-urlencoded")
                 .post(ClientResponse.class, "firstname=foo&lastname=bar");