From 016d91d73396d778f544123efbd7ff81426a4e36 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 20 May 2009 06:22:42 +0000 Subject: Properly handle SMD requests in JSON-RPC wire format/operation selector git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@776580 13f79535-47bb-0310-9956-ffa450edef68 --- .../JSONRPCOperationSelectorInterceptor.java | 6 +++ .../provider/JSONRPCWireFormatInterceptor.java | 44 ++++++++++++++++- .../provider/JSONRPCWireFormatServiceProvider.java | 2 +- .../wireformat/jsonrpc/provider/JavaToSmd.java | 56 ++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JavaToSmd.java (limited to 'branches/sca-java-1.x/modules/binding-http-jsonrpc-runtime/src') 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(); + } + +} -- cgit v1.2.3