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/29 23:52:19 UTC

svn commit: r739063 - in /ode/sandbox/simpel: Rakefile src/main/java/org/apache/ode/rest/ProcessWebResource.java src/test/java/org/apache/ode/rest/RestfulSimPELTest.java src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java

Author: mriou
Date: Thu Jan 29 22:52:18 2009
New Revision: 739063

URL: http://svn.apache.org/viewvc?rev=739063&view=rev
Log:
Simple support for form url-encoded when submitted by a browser. Still some work required to reply with HTML. Would be nice to also support invocation with form url-encoded to be able to call all the simple browser-based services out there.

Added:
    ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java
      - copied, changed from r739062, ode/sandbox/simpel/src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java
Removed:
    ode/sandbox/simpel/src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java
Modified:
    ode/sandbox/simpel/Rakefile
    ode/sandbox/simpel/src/main/java/org/apache/ode/rest/ProcessWebResource.java

Modified: ode/sandbox/simpel/Rakefile
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/Rakefile?rev=739063&r1=739062&r2=739063&view=diff
==============================================================================
--- ode/sandbox/simpel/Rakefile (original)
+++ ode/sandbox/simpel/Rakefile Thu Jan 29 22:52:18 2009
@@ -24,6 +24,7 @@
 NEXT_VERSION = "1.2"
 
 ANTLR               = "org.antlr:antlr:jar:3.0.1"
+ANTLR_TEMPLATE      = "org.antlr:stringtemplate:jar:3.0"
 ASM                 = "asm:asm:jar:3.1"
 COMMONS             = struct(
   :logging          =>"commons-logging:commons-logging:jar:1.1",
@@ -40,7 +41,7 @@
   :resource         =>"org.apache.geronimo.specs:geronimo-j2ee-connector_1.5_spec:jar:1.0",
   :rest             =>"javax.ws.rs:jsr311-api:jar:1.0"
 )
-JERSEY              = group("jersey-server", "jersey-client", "jersey-core", :under=>"com.sun.jersey", :version=>"1.0")
+JERSEY              = group("jersey-server", "jersey-client", "jersey-core", :under=>"com.sun.jersey", :version=>"1.0.1")
 JETTY               = group("jetty", "jetty-util", "servlet-api-2.5", :under=>"org.mortbay.jetty", :version=>"6.1.11")
 LOG4J               = "log4j:log4j:jar:1.2.15"
 ODE                 = group("ode-bpel-api", "ode-bpel-compiler", "ode-bpel-dao", "ode-runtimes", 
@@ -90,7 +91,7 @@
   compile.enhance([task('tweak_antlr')])
   compile.with HSQLDB, JAVAX.resource, JAVAX.transaction, COMMONS.lang, COMMONS.logging,
     ODE, LOG4J, WSDL4J, ASM, JERSEY, JAVAX.rest, JETTY, GERONIMO.transaction, XERCES,
-    file(_("lib/e4x-grammar-0.2.jar")), ANTLR, file(_("lib/rhino-1.7R2pre-patched.jar"))
+    file(_("lib/e4x-grammar-0.2.jar")), ANTLR, ANTLR_TEMPLATE, file(_("lib/rhino-1.7R2pre-patched.jar"))
   test.using :fork => :each
   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=739063&r1=739062&r2=739063&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 Thu Jan 29 22:52:18 2009
@@ -10,6 +10,11 @@
 
 import javax.ws.rs.*;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.Context;
+import java.util.List;
+import java.util.Map;
 
 public class ProcessWebResource {
 
@@ -47,27 +52,23 @@
         else return Response.status(405).header("Allow", _resource.methods()).build();
     }
 
-    @POST @Consumes("application/xml")
-    public Response post(String content) {
+    public Response post(Element msgElmt) {
         if (_resource.post) {
             RESTInMessageExchange mex = _serverLifecyle.getServer().createMessageExchange(
                     _resource.toResource("POST"), new GUID().toString());
             Message request = mex.createMessage(null);
-            if (content.length() > 0) {
-                try {
-                    // TODO support for http headers and parameters as additional parts
-                    Element msgElmt = DOMUtils.stringToDOM(content);
-                    Document doc = DOMUtils.newDocument();
-                    Element docElmt = doc.createElement("document");
-                    Element partElmt = doc.createElement("payload");
-                    doc.appendChild(docElmt);
-                    docElmt.appendChild(partElmt);
-                    partElmt.appendChild(doc.importNode(msgElmt, true));
-
-                    request.setMessage(docElmt);
-                } catch (Exception e) {
-                    return Response.status(400).entity("Couldn't parse XML request.").type("text/plain").build();
-                }
+            if (msgElmt != null) {
+                // TODO support for http headers and parameters as additional parts
+                Document doc = DOMUtils.newDocument();
+                Element docElmt = doc.createElement("document");
+                Element partElmt = doc.createElement("payload");
+                Element rootElmt = doc.createElement("root");
+                doc.appendChild(docElmt);
+                docElmt.appendChild(partElmt);
+                partElmt.appendChild(rootElmt);
+                rootElmt.appendChild(doc.importNode(msgElmt, true));
+
+                request.setMessage(docElmt);
                 mex.setRequest(request);
             }
             try {
@@ -101,36 +102,68 @@
         else return DOMUtils.domToString(unwrapped);
     }
 
-    // This sucks big time
-
-    @GET @Produces("application/xml") @Path("{subpath}")
+    @GET @Produces("application/xml") @Path("{sub : .*}")
     public Response getSub() {
         return get();
     }
 
-    @GET @Produces("application/xml") @Path("{subpath}/{sub1}")
-    public Response getSubSub() {
-        return get();
-    }
-
-    @GET @Produces("application/xml") @Path("{subpath}/{sub1}/{sub2}")
-    public Response getSubSubSub() {
-        return get();
+    @POST @Consumes("application/xml")
+    public Response post(String content) {
+        try {
+            Element msgElmt = null;
+            if (content.length() > 0) msgElmt = DOMUtils.stringToDOM(content);
+            return post(msgElmt);
+        } catch (Exception e) {
+            return Response.status(400).entity("Couldn't parse XML request.").type("text/plain").build();
+        }
     }
 
-    @POST @Consumes("application/xml") @Path("{subpath}")
+    @POST @Consumes("application/xml") @Path("{sub : .*}")
     public Response postSub(String content) {
-        return post(content);
+        try {
+            Element msgElmt = null;
+            if (content.length() > 0) msgElmt = DOMUtils.stringToDOM(content);
+            return post(msgElmt);
+        } catch (Exception e) {
+            return Response.status(400).entity("Couldn't parse XML request.").type("text/plain").build();
+        }
     }
 
-    @POST @Consumes("application/xml") @Path("{subpath}/{sub1}")
-    public Response postSubSub(String content) {
-        return post(content);
+    @POST @Consumes("application/x-www-form-urlencoded")
+    public Response postEnc(MultivaluedMap<String, String> formParams) {
+        return post(formToXml(formParams));
+    }
+
+    @POST @Consumes("application/x-www-form-urlencoded") @Path("{sub : .*}")
+    public Response postSubEnc(MultivaluedMap<String, String> formParams) {
+        return post(formToXml(formParams));
+    }
+
+    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;
     }
 
-    @POST @Consumes("application/xml") @Path("{subpath}/{sub1}/{sub2}")
-    public Response postSubSubSub(String content) {
-        return post(content);
+    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")

Copied: ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java (from r739062, ode/sandbox/simpel/src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java)
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java?p2=ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java&p1=ode/sandbox/simpel/src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java&r1=739062&r2=739063&rev=739063&view=diff
==============================================================================
--- ode/sandbox/simpel/src/test/java/org/apache/ode/simpel/RestfulSimPELTest.java (original)
+++ ode/sandbox/simpel/src/test/java/org/apache/ode/rest/RestfulSimPELTest.java Thu Jan 29 22:52:18 2009
@@ -1,4 +1,4 @@
-package org.apache.ode.simpel;
+package org.apache.ode.rest;
 
 import org.apache.ode.EmbeddedServer;
 import org.apache.ode.Descriptor;
@@ -103,7 +103,7 @@
         String location = createResponse.getMetadata().get("Location").get(0);
         assertTrue(createResponse.getStatus() == 201);
         assertTrue(location.matches(".*/counter/[0-9]*$"));
-        assertEquals(response, "3");
+        assertTrue(response.indexOf("<counter>3</counter>") > 0);
 
         // Requesting links
         WebResource instance = c.resource(location);
@@ -250,4 +250,31 @@
         assertEquals(response, "http://foo/bar");
     }
 
+    private static final String HELLO_FORM_WORLD =
+            "process HelloFormWorld { \n" +
+            "   receive(self) { |form| \n" +
+            "       helloXml = <hello>{\"Hello \" + form.firstname + \" \" + form.lastname}</hello>; \n" +
+            "       reply(helloXml); \n" +
+            "   }\n" +
+            "}";
+
+    public void testFormHelloWorld() throws Exception {
+        server.start();
+        Descriptor desc = new Descriptor();
+        desc.setAddress("/hello-form");
+        server.deploy(HELLO_FORM_WORLD, desc);
+
+        ClientConfig cc = new DefaultClientConfig();
+        Client c = Client.create(cc);
+
+        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");
+        String response = resp.getEntity(String.class);
+        System.out.println("=> " + response);
+        assertTrue(response.indexOf("Hello foo bar") > 0);
+        assertTrue(resp.getMetadata().get("Location").get(0), resp.getMetadata().get("Location").get(0).matches(".*/hello-form/[0-9]*"));
+        assertTrue(resp.getStatus() == 201);
+    }
+
 }