diff options
Diffstat (limited to 'branches/sca-java-1.x')
7 files changed, 136 insertions, 15 deletions
diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/pom.xml b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/pom.xml index 32dc4dc7dd..568d4eaaa6 100644 --- a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/pom.xml +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/pom.xml @@ -62,13 +62,6 @@ <dependency> <groupId>org.apache.tuscany.sca</groupId> - <artifactId>tuscany-host-jetty</artifactId> - <version>1.6-SNAPSHOT</version> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-databinding</artifactId> <version>1.6-SNAPSHOT</version> </dependency> @@ -77,7 +70,14 @@ <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-databinding-json</artifactId> <version>1.6-SNAPSHOT</version> - </dependency> + </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> <dependency> <groupId>com.metaparadigm</groupId> diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/operationselector/jsonrpc/provider/JSONRPCOperationSelectorInterceptor.java b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/operationselector/jsonrpc/provider/JSONRPCOperationSelectorInterceptor.java index 3e93414fce..37c76d88d3 100644 --- a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/operationselector/jsonrpc/provider/JSONRPCOperationSelectorInterceptor.java +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/operationselector/jsonrpc/provider/JSONRPCOperationSelectorInterceptor.java @@ -23,6 +23,7 @@ import java.io.CharArrayWriter; import java.util.List; import org.apache.tuscany.sca.binding.http.HTTPBinding; +import org.apache.tuscany.sca.binding.http.HTTPBindingContext; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Interceptor; import org.apache.tuscany.sca.invocation.Invoker; @@ -62,6 +63,11 @@ public class JSONRPCOperationSelectorInterceptor implements Interceptor { public Message invoke(Message msg) { + HTTPBindingContext bindingContext = msg.getBindingContext(); + if ("smd".equals(bindingContext.getHttpRequest().getQueryString())) { + return getNext().invoke(msg); + } + JSONObject jsonReq = null; String method = null; try { diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java index 8b128d3135..b314f69fcb 100644 --- a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java @@ -20,6 +20,9 @@ package org.apache.tuscany.sca.binding.http.wireformat.jsonrpc.provider; import org.apache.tuscany.sca.binding.http.HTTPBinding; +import org.apache.tuscany.sca.binding.http.HTTPBindingContext; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.invocation.Interceptor; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; @@ -42,12 +45,16 @@ public class JSONRPCWireFormatInterceptor implements Interceptor { private RuntimeWire runtimeWire; private HTTPBinding binding; + private InterfaceContract serviceContract; + private MessageFactory messageFactory; - public JSONRPCWireFormatInterceptor(HTTPBinding binding, RuntimeWire runtimeWire, MessageFactory messageFactory) { + public JSONRPCWireFormatInterceptor(HTTPBinding binding, RuntimeWire runtimeWire, InterfaceContract serviceContract, MessageFactory messageFactory) { this.binding = binding; this.runtimeWire = runtimeWire; + this.serviceContract = serviceContract; + this.messageFactory = messageFactory; } @@ -61,6 +68,25 @@ public class JSONRPCWireFormatInterceptor implements Interceptor { } public Message invoke(Message msg) { + HTTPBindingContext bindingContext = msg.getBindingContext(); + Message returnMessage = null; + + if ("smd".equals(bindingContext.getHttpRequest().getQueryString())) { + returnMessage = handleSMDInvocation(msg); + } else { + returnMessage = handleJSONRPCInvocation(msg); + } + + return returnMessage; + + } + + /** + * Handle regular JSON-RPC Invocation + * @param msg + * @return + */ + private Message handleJSONRPCInvocation (Message msg) { JSONObject jsonReq = (JSONObject) msg.getBody(); String method = null; Object[] args = null; @@ -112,9 +138,23 @@ public class JSONRPCWireFormatInterceptor implements Interceptor { Throwable exception = (Throwable)responseMessage.getBody(); return createJSONFaultMessage(exception); } - } + /** + * handles requests for the SMD descriptor for a service + */ + private Message handleSMDInvocation(Message msg) { + HTTPBindingContext bindingContext = msg.getBindingContext(); + + String serviceUrl = bindingContext.getHttpRequest().getRequestURL().toString(); + JavaInterface interfaze = (JavaInterface) serviceContract.getInterface(); + String smd = JavaToSmd.interfaceToSmd(interfaze.getJavaClass(), serviceUrl); + + Message smdResponseMessage = messageFactory.createMessage(); + smdResponseMessage.setBody(smd); + + return smdResponseMessage; + } /** * Create a Fault Message with a JSON representation of the current exception diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java index 4c163f8660..1454fbdff2 100644 --- a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java @@ -85,7 +85,7 @@ public class JSONRPCWireFormatServiceProvider implements WireFormatProvider { BindingRRB rrbBinding = (BindingRRB) binding; WireFormat wireFormat = rrbBinding.getRequestWireFormat(); if(wireFormat != null && wireFormat instanceof JSONRPCWireFormat) { - return new JSONRPCWireFormatInterceptor((HTTPBinding) binding, service.getRuntimeWire(binding), messageFactory); + return new JSONRPCWireFormatInterceptor((HTTPBinding) binding, service.getRuntimeWire(binding), serviceContract, messageFactory); } } diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JavaToSmd.java b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JavaToSmd.java new file mode 100644 index 0000000000..c67060f7b6 --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JavaToSmd.java @@ -0,0 +1,56 @@ +/* + * 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.http.wireformat.jsonrpc.provider; + +import java.lang.reflect.Method; + +/** + * Utility class to create a Simple Method Description (SMD) descriptor + * from a Java class. See http://dojo.jot.com/SMD. + * + * TODO: Change to work from the Tuscany Interface instead of a Java class + * + * @version $Rev$ $Date$ + */ +class JavaToSmd { + + static String interfaceToSmd(Class<?> klazz, String serviceUrl) { + String name = klazz.getSimpleName(); + Method[] methods = klazz.getMethods(); + + StringBuffer smdSb = new StringBuffer(); + smdSb.append("{\"SMDVersion\":\".1\",\"objectName\":\"" + name + "\",\"serviceType\":\"JSON-RPC\",\"serviceURL\":\""+ serviceUrl + "\",\"methods\":["); + for (int i = 0; i < methods.length; i++) { + if (i != 0) smdSb.append(","); + Class<?>[] params = methods[i].getParameterTypes(); + smdSb.append("{\"name\":\""+methods[i].getName() + "\",\"parameters\":["); + for (int j = 0; j < params.length; j++) { + if (j != 0) smdSb.append(","); + // right now Dojo doesn't look at the type value, so we'll default it to STRING + // also, since we can't introspect the method parameter names we'll just create an incrementing parameter name + smdSb.append("{\"name\":\"param" + j + "\",\"type\":\"STRING\"}"); + } + smdSb.append("]}"); + } + smdSb.append("]}"); + + return smdSb.toString(); + } + +} diff --git a/branches/sca-java-1.x/modules/binding-http-jsonrpc/pom.xml b/branches/sca-java-1.x/modules/binding-http-jsonrpc/pom.xml index ee0e8dfcf1..08f2e084bf 100644 --- a/branches/sca-java-1.x/modules/binding-http-jsonrpc/pom.xml +++ b/branches/sca-java-1.x/modules/binding-http-jsonrpc/pom.xml @@ -49,6 +49,13 @@ </dependency> <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> + + <dependency> <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-implementation-java-runtime</artifactId> <version>1.6-SNAPSHOT</version> diff --git a/branches/sca-java-1.x/samples/store/src/main/resources/store.composite b/branches/sca-java-1.x/samples/store/src/main/resources/store.composite index 571678839d..c60b6a1f76 100644 --- a/branches/sca-java-1.x/samples/store/src/main/resources/store.composite +++ b/branches/sca-java-1.x/samples/store/src/main/resources/store.composite @@ -28,13 +28,19 @@ <t:binding.http uri="/store"/> </service> <reference name="catalog" target="Catalog"> - <t:binding.jsonrpc/> + <t:binding.http> + <t:wireFormat.jsonrpc/> + <t:operationSelector.jsonrpc/> + </t:binding.http> </reference> <reference name="shoppingCart" target="ShoppingCart/Cart"> <t:binding.atom/> </reference> <reference name="shoppingTotal" target="ShoppingCart/Total"> - <t:binding.jsonrpc/> + <t:binding.http> + <t:wireFormat.jsonrpc/> + <t:operationSelector.jsonrpc/> + </t:binding.http> </reference> </component> @@ -42,7 +48,10 @@ <implementation.java class="services.FruitsCatalogImpl"/> <property name="currencyCode">USD</property> <service name="Catalog"> - <t:binding.jsonrpc/> + <t:binding.http> + <t:wireFormat.jsonrpc/> + <t:operationSelector.jsonrpc/> + </t:binding.http> </service> <reference name="currencyConverter" target="CurrencyConverter"/> </component> @@ -53,7 +62,10 @@ <t:binding.atom uri="/ShoppingCart/Cart"/> </service> <service name="Total"> - <t:binding.jsonrpc/> + <t:binding.http> + <t:wireFormat.jsonrpc/> + <t:operationSelector.jsonrpc/> + </t:binding.http> </service> </component> |