From 5a4d0d8e8caa72f12687a9d80104235e2af033b2 Mon Sep 17 00:00:00 2001 From: antelder Date: Tue, 28 Jul 2009 05:39:17 +0000 Subject: 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 --- .../sca/binding/jsonp/runtime/JSONPServlet.java | 45 +++++++++++++++++++--- .../src/test/java/helloworld/HelloWorldImpl.java | 4 ++ .../test/java/helloworld/HelloWorldService.java | 2 + .../src/test/java/test/BindingTestCase.java | 9 +++++ 4 files changed, 54 insertions(+), 6 deletions(-) (limited to 'java/sca/modules/binding-jsonp-runtime') 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 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 getOrderedParameterNames(ServletRequest servletRequest) { + final String queryString = ((HttpServletRequest)servletRequest).getQueryString(); + + SortedSet sortedNames = new TreeSet(new Comparator(){ + public int compare(String o1, String o2) { + int i = queryString.indexOf(o1); + int j = queryString.indexOf(o2); + return i - j; + }}); + + Set 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; -- cgit v1.2.3