summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-08-11 08:08:53 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-08-11 08:08:53 +0000
commit7df85ffb3127adb374eb24db24ce3fb20d66ffc7 (patch)
tree89c2d8e75bef4031f16e0959ff6227d3166d10ba
parent4faf498c892a6f54e81094d3eda6f9dd2d3090a3 (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
-rw-r--r--java/sca/distribution/all/src/main/release/bin/LICENSE1
-rw-r--r--java/sca/distribution/tomcat/tomcat-war/src/main/webapp/LICENSE5
-rw-r--r--java/sca/modules/binding-jsonp-runtime/pom.xml14
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java86
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java8
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/helloworld/HelloWorldClient.java37
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java12
-rw-r--r--java/sca/modules/binding-jsonp-runtime/src/test/resources/helloworld.composite7
8 files changed, 160 insertions, 10 deletions
diff --git a/java/sca/distribution/all/src/main/release/bin/LICENSE b/java/sca/distribution/all/src/main/release/bin/LICENSE
index 74b0f582fc..8bf0e2481a 100644
--- a/java/sca/distribution/all/src/main/release/bin/LICENSE
+++ b/java/sca/distribution/all/src/main/release/bin/LICENSE
@@ -253,6 +253,7 @@ The following components come under Apache Software License 2.0
geronimo-kernel-2.0.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-transaction-2.0.1.jar
+ httpclient-4.0.jar
httpcore-4.0-beta1.jar
httpcore-nio-4.0-beta1.jar
jackson-core-asl-1.0.1.jar
diff --git a/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/LICENSE b/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/LICENSE
index e90c7ea7b4..8874bffcc2 100644
--- a/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/LICENSE
+++ b/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/LICENSE
@@ -227,7 +227,7 @@ The following components come under Apache Software License 2.0
axis-ant-1.4.jar
cglib-2.2.jar
commons-collections-3.1.jar
- commons-codec-1.2.jar
+ commons-codec-1.3.jar,
commons-discovery-0.2.jar
commons-fileupload-1.2.jar
commons-httpclient-3.1.jar
@@ -237,7 +237,8 @@ The following components come under Apache Software License 2.0
geronimo-javamail_1.4_spec-1.2.jar
geronimo-jms_1.1_spec-1.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
- httpcore-4.0-beta1.jar
+ httpclient-4.0.jar,
+ httpcore-4.0.1.jar
httpcore-nio-4.0-beta1.jar
jackson-core-asl-1.0.1.jar
jackson-mapper-asl-1.0.1.jar
diff --git a/java/sca/modules/binding-jsonp-runtime/pom.xml b/java/sca/modules/binding-jsonp-runtime/pom.xml
index af2845c1cf..458120544b 100644
--- a/java/sca/modules/binding-jsonp-runtime/pom.xml
+++ b/java/sca/modules/binding-jsonp-runtime/pom.xml
@@ -28,6 +28,14 @@
<artifactId>tuscany-binding-jsonp-runtime</artifactId>
<name>Apache Tuscany SCA JSONP Binding Runtime</name>
+ <!-- Use a temp repo just until the HCC4 release gets uploaded -->
+ <repositories>
+ <repository>
+ <id>temp.hc.repo</id>
+ <url>http://people.apache.org/~antelder/httpcomponents/4.0/maven</url>
+ </repository>
+ </repositories>
+
<dependencies>
<dependency>
@@ -53,6 +61,12 @@
</dependency>
<dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.0</version>
+ </dependency>
+
+ <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
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>