summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk
diff options
context:
space:
mode:
authorslaws <slaws@13f79535-47bb-0310-9956-ffa450edef68>2010-07-29 08:56:53 +0000
committerslaws <slaws@13f79535-47bb-0310-9956-ffa450edef68>2010-07-29 08:56:53 +0000
commite032cc484a057506f5db0dc5d1cea3106f756c17 (patch)
tree41229892021ad6ddb5efa61db855c50cd9433285 /sca-java-2.x/trunk
parenta197aec6efe9002535c12fd162eefeb917a41842 (diff)
TUSCANY-3635 - delegate the conversion to/from JSON to the databinding layer which has the correct information regarding the operation parameter and return types.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@980366 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml7
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java19
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java10
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java10
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java67
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java2
6 files changed, 106 insertions, 9 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml
index 37db92b980..7b9b31c35d 100644
--- a/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml
+++ b/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml
@@ -85,6 +85,13 @@
<version>2.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-databinding-json</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
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");