diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-jsonp-runtime/src')
5 files changed, 99 insertions, 9 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java index e68d8e676b..6b0bfec213 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java @@ -66,12 +66,13 @@ public class JSONPInvoker implements Invoker { public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException, EncoderException { String uri = endpoint.getBinding().getURI() + "/" + operation.getName(); - String[] jsonArgs = objectsToJSON((Object[])msg.getBody()); + //String[] jsonArgs = objectsToJSON((Object[])msg.getBody()); + String[] jsonArgs = objectsToJSONStrings((Object[])msg.getBody()); String responseJSON = invokeHTTPRequest(uri, jsonArgs); - Object response = jsonToObjects(responseJSON)[0]; - msg.setBody(response); + //Object response = jsonToObjects(responseJSON)[0]; + msg.setBody(responseJSON); return msg; } @@ -142,6 +143,7 @@ public class JSONPInvoker implements Invoker { return responseJSON.toString(); } +/* Not required now JSON conversion is delegated to databinding protected String[] objectsToJSON(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException { String[] jsonArgs = new String[msgArgs.length]; for (int i=0; i<msgArgs.length; i++) { @@ -151,11 +153,22 @@ public class JSONPInvoker implements Invoker { } return jsonArgs; } +*/ + + protected String[] objectsToJSONStrings(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException { + String[] jsonArgs = new String[msgArgs.length]; + for (int i=0; i<msgArgs.length; i++) { + jsonArgs[i] = msgArgs[i].toString(); + } + return jsonArgs; + } +/* Not required now JSON conversion is delegated to databinding protected Object[] jsonToObjects(String jsonRequest) throws JsonParseException, JsonMappingException, IOException { Class<?> c = new Object[0].getClass(); Object[] args = (Object[])mapper.readValue("[" + jsonRequest +"]", c); return args; } +*/ } diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java index 9682b33f61..eda70e275e 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java @@ -28,9 +28,17 @@ import org.apache.tuscany.sca.provider.ReferenceBindingProvider; public class JSONPReferenceBindingProvider implements ReferenceBindingProvider { private EndpointReference endpoint; + private InterfaceContract contract; public JSONPReferenceBindingProvider(EndpointReference endpoint) { this.endpoint = endpoint; + + try { + contract = (InterfaceContract)endpoint.getComponentReferenceInterfaceContract().clone(); + } catch (Exception ex){ + // we know this supports clone + } + contract.getInterface().resetDataBinding("JSON"); } public Invoker createInvoker(Operation operation) { return new JSONPInvoker(operation, endpoint); @@ -43,7 +51,7 @@ public class JSONPReferenceBindingProvider implements ReferenceBindingProvider { } public InterfaceContract getBindingInterfaceContract() { - return null; + return contract; } public boolean supportsOneWayInvocation() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java index 4e3f7321af..991aeb3a21 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java @@ -31,10 +31,18 @@ public class JSONPServiceBindingProvider implements ServiceBindingProvider { private RuntimeEndpoint endpoint; private ServletHost servletHost; + private InterfaceContract contract; public JSONPServiceBindingProvider(RuntimeEndpoint endpoint, ServletHost servletHost) { this.endpoint = endpoint; this.servletHost = servletHost; + + try { + contract = (InterfaceContract)endpoint.getComponentServiceInterfaceContract().clone(); + } catch (Exception ex){ + // we know this supports clone + } + contract.getInterface().resetDataBinding("JSON"); } public void start() { @@ -58,7 +66,7 @@ public class JSONPServiceBindingProvider implements ServiceBindingProvider { // TODO: Why are these two still on the ServiceBindingProvider interface? public InterfaceContract getBindingInterfaceContract() { - return null; + return contract; } public boolean supportsOneWayInvocation() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java index b782875948..aadb4d9853 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java +++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java @@ -22,6 +22,7 @@ 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.List; import java.util.Set; @@ -56,16 +57,19 @@ public class JSONPServlet extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { - String jsonRequest = getJSONRequest(servletRequest); - Object[] args = jsonToObjects(jsonRequest); + //String jsonRequest = getJSONRequest(servletRequest); + //Object[] args = jsonToObjects(jsonRequest); + Object[] args = getJSONRequestStringArray(servletRequest); Object response = invokeService(args); - String jsonResponse = getJSONResponse(servletRequest, response); + //String jsonResponse = getJSONResponse(servletRequest, response); + String jsonResponse = getJSONResponseAsString(servletRequest, response); servletResponse.getOutputStream().println(jsonResponse); } /** * Turn the request into JSON */ +/* Not required now JSON conversion is delegated to databinding protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException { List<DataType> types = operation.getInputType().getLogical(); @@ -96,6 +100,43 @@ public class JSONPServlet extends GenericServlet { return "[" + jsonRequest + "]"; } +*/ + + /** + * Turn the request into a string array of JSON structures. The Databinding + * layer will then convert each of the individual parameters into the appropriate + * types for the implementation interface + * + */ + protected Object[] getJSONRequestStringArray(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException { + + List<DataType> types = operation.getInputType().getLogical(); + int typesIndex = 0; + + List<String> jsonRequestArray = new ArrayList<String>(); + + for (String name : getOrderedParameterNames(servletRequest)) { + String jsonRequest = ""; + if (!name.startsWith("_") && !"callback".equals(name)) { + + // automatically quote string parameters so clients work in the usual javascript way + if (typesIndex < types.size() && String.class.equals(types.get(typesIndex).getGenericType())) { + String x = servletRequest.getParameter(name); + // TODO: do this more properly + if (!x.startsWith("\"")) { + jsonRequest += "\"" + x + "\""; + } else { + jsonRequest += x; + } + } else { + jsonRequest += servletRequest.getParameter(name); + } + jsonRequestArray.add(jsonRequest); + } + } + + return jsonRequestArray.toArray(); + } /** * Get the request parameter names in the correct order. @@ -127,6 +168,7 @@ public class JSONPServlet extends GenericServlet { /** * Turn the response object into JSON */ +/* Not required now JSON conversion is delegated to databinding protected String getJSONResponse(ServletRequest servletRequest, Object response) throws IOException, JsonParseException { ByteArrayOutputStream os = new ByteArrayOutputStream(); mapper.writeValue(os , response); @@ -139,15 +181,34 @@ public class JSONPServlet extends GenericServlet { return jsonResponse; } +*/ + + /** + * The databinding layer will have converterted the return type into a JSON string so simply + * add wrap it for return. + */ + protected String getJSONResponseAsString(ServletRequest servletRequest, Object response) throws IOException, JsonParseException { + String jsonResponse = response.toString(); + + String callback = servletRequest.getParameter("callback"); + if (callback != null && callback.length() > 1) { + jsonResponse = callback + "(" + jsonResponse + ");"; + } + + return jsonResponse; + } /** * Turn the request JSON into objects */ +/* Not required now JSON conversion is delegated to databinding protected Object[] jsonToObjects(String jsonRequest) throws IOException, JsonParseException, JsonMappingException { Class<?> c = new Object[0].getClass(); Object[] args = (Object[])mapper.readValue(jsonRequest, c); return args; } +*/ + /** * Send the request down the wire to invoke the service diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java index 5a8e5ac533..4e7bca0144 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java @@ -69,7 +69,7 @@ public class BindingTestCase { } @Test - @Ignore("TUSCANY-3635") + //@Ignore("TUSCANY-3635") public void testComplexParams() throws MalformedURLException, IOException { HelloWorldService client = node.getService(HelloWorldService.class, "HelloWorldClient"); |