diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 08:08:53 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 08:08:53 +0000 |
commit | 7df85ffb3127adb374eb24db24ce3fb20d66ffc7 (patch) | |
tree | 89c2d8e75bef4031f16e0959ff6227d3166d10ba /java/sca/modules/binding-jsonp-runtime/src | |
parent | 4faf498c892a6f54e81094d3eda6f9dd2d3090a3 (diff) |
Start adding support for references in the JSONP binding
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@803021 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-jsonp-runtime/src')
5 files changed, 142 insertions, 8 deletions
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 index 01a128913b..f6442e00cd 100644 --- 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 @@ -19,9 +19,20 @@ package org.apache.tuscany.sca.binding.jsonp.runtime;
+import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.net.URLCodec;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.utils.URIUtils;
+import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.Invoker;
@@ -49,12 +60,12 @@ public class JSONPInvoker implements Invoker { return doInvoke(msg);
- } catch (IOException e) {
+ } catch (Exception e) {
throw new RuntimeException(e);
}
}
- public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException {
+ public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException, EncoderException {
String uri = endpoint.getBinding().getURI() + "/" + operation.getName();
String[] jsonArgs = objectsToJSON((Object[])msg.getBody());
@@ -66,9 +77,70 @@ public class JSONPInvoker implements Invoker { return msg;
}
- protected String invokeHTTPRequest(String uri, String[] jsonArgs) {
- // TODO Auto-generated method stub
- return null;
+ protected String invokeHTTPRequest(String url, String[] jsonArgs) throws IOException, EncoderException {
+
+ HttpClient httpclient = new DefaultHttpClient();
+
+
+ URLCodec uc = new URLCodec();
+ for (int i=0 ; i<jsonArgs.length; i++) {
+ if (i == 0) {
+ url += '?';
+ } else {
+ url += '&';
+ }
+ url += "arg" + i + "=";
+ url += uc.encode(jsonArgs[i]);
+ }
+
+ HttpGet httpget = new HttpGet(url);
+
+ HttpResponse response = httpclient.execute(httpget);
+
+ StringBuffer responseJSON = new StringBuffer();
+
+ HttpEntity entity = response.getEntity();
+
+ // If the response does not enclose an entity, there is no need
+ // to worry about connection release
+ if (entity != null) {
+ InputStream instream = entity.getContent();
+ try {
+
+ BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
+ String s = null;
+ while ((s = reader.readLine()) != null) {
+ responseJSON.append(s);
+ }
+
+ } catch (IOException ex) {
+
+ // In case of an IOException the connection will be released
+ // back to the connection manager automatically
+ throw ex;
+
+ } catch (RuntimeException ex) {
+
+ // In case of an unexpected exception you may want to abort
+ // the HTTP request in order to shut down the underlying
+ // connection and release it back to the connection manager.
+ httpget.abort();
+ throw ex;
+
+ } finally {
+
+ // Closing the input stream will trigger connection release
+ instream.close();
+
+ }
+
+ // When HttpClient instance is no longer needed,
+ // shut down the connection manager to ensure
+ // immediate deallocation of all system resources
+ httpclient.getConnectionManager().shutdown();
+ }
+
+ return responseJSON.toString();
}
protected String[] objectsToJSON(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException {
@@ -83,7 +155,7 @@ public class JSONPInvoker implements Invoker { protected Object[] jsonToObjects(String jsonRequest) throws JsonParseException, JsonMappingException, IOException {
Class<?> c = new Object[0].getClass();
- Object[] args = (Object[])mapper.readValue(jsonRequest, c);
+ 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/JSONPServlet.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java index c84aee7d9f..edacc2d374 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 @@ -80,7 +80,13 @@ public class JSONPServlet extends GenericServlet { // 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) + "\""; + String x = servletRequest.getParameter(name); + // TODO: do this more properly + if (!x.startsWith("\"")) { + jsonRequest += "\"" + x + "\""; + } else { + jsonRequest += x; + } } else { jsonRequest += servletRequest.getParameter(name); } diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldClient.java b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldClient.java new file mode 100644 index 0000000000..5ee1f167ea --- /dev/null +++ b/java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldClient.java @@ -0,0 +1,37 @@ +/*
+ * 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 helloworld;
+
+import org.oasisopen.sca.annotation.Reference;
+
+public class HelloWorldClient implements HelloWorldService {
+
+ @Reference
+ public HelloWorldService ref;
+
+ public String sayHello(String name) {
+ return ref.sayHello(name);
+ }
+
+ public String sayHello2(String firstName, String lastName) {
+ return ref.sayHello2(firstName, 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 be46c1dee7..efec56bbe3 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 @@ -18,6 +18,8 @@ */ package test; +import helloworld.HelloWorldService; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -55,6 +57,16 @@ public class BindingTestCase { } + @Test + public void testReference() throws MalformedURLException, IOException { + + HelloWorldService client = node.getService(HelloWorldService.class, "HelloWorldClient"); + + Assert.assertEquals("Hello beate", client.sayHello("beate")); + Assert.assertEquals("Hello beate arnold", client.sayHello2("beate", "arnold")); + + } + @BeforeClass public static void init() throws Exception { JettyServer.portDefault = 8085; diff --git a/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite b/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite index 3ca0b4763e..d058e4869f 100644 --- a/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite +++ b/java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite @@ -29,4 +29,11 @@ </service> </component> + <component name="HelloWorldClient"> + <implementation.java class="helloworld.HelloWorldClient"/> + <reference name="ref" > + <tuscany:binding.jsonp uri="http://localhost:8085/HelloWorldComponent/HelloWorldService"/> + </reference> + </component> + </composite> |