summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-08-10 07:50:40 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-08-10 07:50:40 +0000
commit7cd4170d0b649adfa0f9fb5db9cfa16d4f5aa776 (patch)
tree4736ea632401176816dd2bcec82641b02510d128 /java
parentc048e2d876b174ec04fd7ab020bdf06a1c310af8 (diff)
Start adding support for JSONP references. Doesn't work yet, still need to implement the http inovke bit using something like HttpClient
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802681 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java5
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java90
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java54
3 files changed, 146 insertions, 3 deletions
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java
index f686d9d771..90daedb647 100644
--- a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java
@@ -47,9 +47,8 @@ public class JSONPBindingProviderFactory implements BindingProviderFactory<JSONP
return JSONPBinding.class;
}
- public ReferenceBindingProvider createReferenceBindingProvider(EndpointReference arg0) {
- // TODO Auto-generated method stub
- return null;
+ public ReferenceBindingProvider createReferenceBindingProvider(EndpointReference endpoint) {
+ return new JSONPReferenceBindingProvider(endpoint);
}
public ServiceBindingProvider createServiceBindingProvider(Endpoint endpoint) {
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java
new file mode 100644
index 0000000000..01a128913b
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.jsonp.runtime;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.tuscany.sca.assembly.EndpointReference;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.invocation.Message;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+
+public class JSONPInvoker implements Invoker {
+
+ protected Operation operation;
+ protected EndpointReference endpoint;
+
+ protected ObjectMapper mapper; // TODO: share mapper btw invoker and servlet or move to databinding
+
+ public JSONPInvoker(Operation operation, EndpointReference endpoint) {
+ this.operation = operation;
+ this.endpoint = endpoint;
+ this.mapper = new ObjectMapper();
+ }
+
+ public Message invoke(Message msg) {
+ try {
+
+ return doInvoke(msg);
+
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException {
+ String uri = endpoint.getBinding().getURI() + "/" + operation.getName();
+ String[] jsonArgs = objectsToJSON((Object[])msg.getBody());
+
+ String responseJSON = invokeHTTPRequest(uri, jsonArgs);
+
+ Object response = jsonToObjects(responseJSON)[0];
+ msg.setBody(response);
+
+ return msg;
+ }
+
+ protected String invokeHTTPRequest(String uri, String[] jsonArgs) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ protected String[] objectsToJSON(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException {
+ String[] jsonArgs = new String[msgArgs.length];
+ for (int i=0; i<msgArgs.length; i++) {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ mapper.writeValue(os , msgArgs[i]);
+ jsonArgs[i] = os.toString();
+ }
+ return jsonArgs;
+ }
+
+ 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/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java
new file mode 100644
index 0000000000..3bb0c48861
--- /dev/null
+++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.jsonp.runtime;
+
+import org.apache.tuscany.sca.assembly.Endpoint;
+import org.apache.tuscany.sca.assembly.EndpointReference;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+
+public class JSONPReferenceBindingProvider implements ReferenceBindingProvider {
+
+ private EndpointReference endpoint;
+
+ public JSONPReferenceBindingProvider(EndpointReference endpoint) {
+ this.endpoint = endpoint;
+ }
+ public Invoker createInvoker(Operation operation) {
+ return new JSONPInvoker(operation, endpoint);
+ }
+
+ public void start() {
+ }
+
+ public void stop() {
+ }
+
+ public InterfaceContract getBindingInterfaceContract() {
+ return null;
+ }
+
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
+
+}