summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/binding-jsonp-runtime/src
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-07-28 05:39:17 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-07-28 05:39:17 +0000
commit5a4d0d8e8caa72f12687a9d80104235e2af033b2 (patch)
tree0d9ca0e118d059689ab3c1a62d23d8d980a6ee4d /java/sca/modules/binding-jsonp-runtime/src
parentb285236148619a04052dcf7c3750f90739a4228d (diff)
Update to correctly order the service request arguments
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@798407 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java45
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java4
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java2
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java9
4 files changed, 54 insertions, 6 deletions
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
index f61b6c493e..406018a974 100644
--- a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
@@ -22,13 +22,19 @@ package org.apache.tuscany.sca.binding.jsonp.runtime;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Operation;
@@ -63,18 +69,18 @@ public class JSONPServlet extends GenericServlet {
* Turn the request into JSON
*/
protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException {
- String jsonRequest = "[";
List<DataType> types = operation.getInputType().getLogical();
int typesIndex = 0;
- for (Enumeration<?> ns = servletRequest.getParameterNames(); ns.hasMoreElements(); ) {
- String name = (String) ns.nextElement();
+ String jsonRequest = "";
+ for (String name : getOrderedParameterNames(servletRequest)) {
if (!name.startsWith("_") && !"callback".equals(name)) {
if (jsonRequest.length() > 1) {
jsonRequest += ", ";
}
-
+
+ // automatically quote string parammeters so clients work in the usual javascript way
if (typesIndex < types.size() && String.class.equals(types.get(typesIndex).getGenericType())) {
jsonRequest += "\"" + servletRequest.getParameter(name) + "\"";
} else {
@@ -83,8 +89,35 @@ public class JSONPServlet extends GenericServlet {
}
}
- jsonRequest += "]";
- return jsonRequest;
+
+ return "[" + jsonRequest + "]";
+ }
+
+ /**
+ * Get the request parameter names in the correct order.
+ * The request args need to be in the correct order to invoke the service so work out the
+ * order from the order in the query string. Eg, the url:
+ * http://localhost:8085/HelloWorldService/sayHello2?first=petra&last=arnold&callback=foo"
+ * should invoke:
+ * sayHello2("petra", "arnold")
+ * so the parameter names should be ordered: "first", "last"
+ */
+ protected SortedSet<String> getOrderedParameterNames(ServletRequest servletRequest) {
+ final String queryString = ((HttpServletRequest)servletRequest).getQueryString();
+
+ SortedSet<String> sortedNames = new TreeSet<String>(new Comparator<String>(){
+ public int compare(String o1, String o2) {
+ int i = queryString.indexOf(o1);
+ int j = queryString.indexOf(o2);
+ return i - j;
+ }});
+
+ Set<String> parameterNames = servletRequest.getParameterMap().keySet();
+ for (String name : parameterNames) {
+ sortedNames.add(name);
+ }
+
+ return sortedNames;
}
/**
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java
index 25a5cb24e0..99f2958d0d 100644
--- a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldImpl.java
@@ -24,5 +24,9 @@ public class HelloWorldImpl implements HelloWorldService {
public String sayHello(String name) {
return "Hello " + name;
}
+
+ public String sayHello2(String firstName, String lastName) {
+ return "Hello " + firstName + " " + lastName;
+ }
}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java
index 59b4bd0d57..b252eddbed 100644
--- a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldService.java
@@ -25,4 +25,6 @@ public interface HelloWorldService {
String sayHello(String name);
+ String sayHello2(String firstName, String lastName);
+
}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java
index d817aff0ff..622dbd4897 100644
--- a/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java
+++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java
@@ -46,6 +46,15 @@ public class BindingTestCase {
}
+ @Test
+ public void testTwoArgs() throws MalformedURLException, IOException {
+ URL url = new URL("http://localhost:8085/HelloWorldService/sayHello2?first=petra&last=arnold&callback=foo");
+ BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
+ String response = br.readLine();
+ Assert.assertEquals("foo(\"Hello petra arnold\");", response);
+
+ }
+
@BeforeClass
public static void init() throws Exception {
JettyServer.portDefault = 8085;