From b082c70417773a30d071643146bf67875932cf0d Mon Sep 17 00:00:00 2001 From: lresende Date: Sun, 25 Apr 2010 17:52:22 +0000 Subject: Initial implementation for JSON wire format for REST binding git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@937850 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding-rest-runtime/META-INF/MANIFEST.MF | 1 + .../trunk/modules/binding-rest-runtime/pom.xml | 25 +++++ .../rest/provider/RESTBindingListenerServlet.java | 28 ++++-- .../rest/provider/RESTGetListenerServlet.java | 94 ----------------- .../json/provider/JSONWireFormatInterceptor.java | 52 ++++++++++ .../provider/JSONWireFormatProviderFctory.java | 48 +++++++++ .../provider/JSONWireFormatReferenceProvider.java | 49 +++++++++ .../provider/JSONWireFormatServiceProvider.java | 102 +++++++++++++++++++ ....tuscany.sca.provider.WireFormatProviderFactory | 19 ++++ .../sca/binding/rest/RESTBindingCacheTestCase.java | 4 +- .../wireformat/json/CatalogServiceTestCase.java | 80 +++++++++++++++ .../wireformat/json/JSONWireFormatTestCase.java | 24 +++++ .../src/test/java/services/Catalog.java | 27 +++++ .../src/test/java/services/CurrencyConverter.java | 29 ++++++ .../test/java/services/CurrencyConverterImpl.java | 38 +++++++ .../src/test/java/services/FruitsCatalogImpl.java | 52 ++++++++++ .../src/test/java/services/Item.java | 50 ++++++++++ .../src/test/resources/store.composite | 40 ++++++++ .../src/test/resources/test.composite | 7 -- .../rest/wireformat/json/JSONWireFormat.java | 43 ++++++++ .../wireformat/json/JSONWireFormatFactory.java | 34 +++++++ .../json/impl/JSONWireFormatFactoryImpl.java | 36 +++++++ .../wireformat/json/impl/JSONWireFormatImpl.java | 44 ++++++++ ...ding.rest.wireformat.json.JSONWireFormatFactory | 19 ++++ .../json/impl/JSONWireFormatProcessorTestCase.java | 111 +++++++++++++++++++++ 25 files changed, 947 insertions(+), 109 deletions(-) delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatProviderFctory.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatReferenceProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatServiceProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatTestCase.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormat.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatFactory.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatFactoryImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormatFactory create mode 100644 sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatProcessorTestCase.java (limited to 'sca-java-2.x') diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF index 7ec5487d22..666d2d3112 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Import-Package: javax.servlet, javax.servlet.http, org.apache.tuscany.sca.assembly;version="2.0.0", org.apache.tuscany.sca.binding.rest;version="2.0.0", + org.apache.tuscany.sca.binding.rest.wireformat.json, org.apache.tuscany.sca.common.http;version="2.0.0", org.apache.tuscany.sca.core;version="2.0.0", org.apache.tuscany.sca.host.http;version="2.0.0", diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml index ec672f1690..021ecdba70 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml @@ -36,6 +36,12 @@ 2.0-SNAPSHOT + + org.apache.tuscany.sca + tuscany-data-api + 2.0-SNAPSHOT + + org.apache.tuscany.sca tuscany-common-http @@ -48,6 +54,18 @@ 2.0-SNAPSHOT + + org.apache.tuscany.sca + tuscany-databinding + 2.0-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-databinding-json + 2.0-SNAPSHOT + + org.apache.tuscany.sca tuscany-host-http @@ -82,6 +100,13 @@ test + + httpunit + httpunit + 1.6.1 + test + + junit junit diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java index 5739fbea77..3b0b69bcd7 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java @@ -92,17 +92,33 @@ public class RESTBindingListenerServlet extends HttpServlet { RESTCacheContext cacheContext = null; try { cacheContext = RESTCacheContext.getCacheContextFromRequest(request); - } catch (ParseException e) { + } catch (ParseException e) { + + } + + if (path == null || path.length() == 0 || path.equals("/")) { + } // Route message based on availability of cache info and cache methods - if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalGetInvoker != null )) { - requestMessage.setBody(new Object[] {id, cacheContext}); - responseMessage = conditionalGetInvoker.invoke(requestMessage); + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalGetInvoker != null )) { + if(id != null && id.length() > 0) { + requestMessage.setBody(new Object[] {id, cacheContext}); + } else { + requestMessage.setBody(new Object[] {cacheContext}); + } + + responseMessage = conditionalGetInvoker.invoke(requestMessage); } else { - requestMessage.setBody(new Object[] {id}); - responseMessage = getInvoker.invoke(requestMessage); + if(id != null && id.length() > 0) { + requestMessage.setBody(new Object[] {id}); + } else { + //requestMessage.setBody(new Object[] {id}); + } + + responseMessage = getInvoker.invoke(requestMessage); } + if (responseMessage.isFault()) { Object body = responseMessage.getBody(); diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java deleted file mode 100644 index b08fa4b201..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.rest.provider; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URLDecoder; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; - -/** - * Servlet responsible for dispatching HTTP GET requests to the - * target component implementation. - * - * @version $Rev$ $Date$ - */ -public class RESTGetListenerServlet extends HttpServlet { - private static final long serialVersionUID = 2865466417329430610L; - - private MessageFactory messageFactory; - private Invoker getInvoker; - - /** - * Constructs a new HTTPServiceListenerServlet. - */ - public RESTGetListenerServlet(Invoker getInvoker, MessageFactory messageFactory) { - this.getInvoker = getInvoker; - this.messageFactory = messageFactory; - } - - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - String id = path.substring(1); - requestMessage.setBody(new Object[] {id}); - Message responseMessage = getInvoker.invoke(requestMessage); - if (responseMessage.isFault()) { - throw new ServletException((Throwable)responseMessage.getBody()); - } - - // Write the response from the service implementation to the response - // output stream - InputStream is = (InputStream)responseMessage.getBody(); - OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - for (;;) { - int n = is.read(buffer); - if (n <= 0) - break; - os.write(buffer, 0, n); - } - os.flush(); - os.close(); - - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java new file mode 100644 index 0000000000..e5ca19b78f --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java @@ -0,0 +1,52 @@ +/* + * 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.rest.wireformat.json.provider; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * JSON wire format Interceptor. + * + * @version $Rev$ $Date$ +*/ +public class JSONWireFormatInterceptor implements Interceptor { + private Invoker next; + + public JSONWireFormatInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + public Message invoke(Message msg) { + return getNext().invoke(msg); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatProviderFctory.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatProviderFctory.java new file mode 100644 index 0000000000..8abc979c02 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatProviderFctory.java @@ -0,0 +1,48 @@ +/* + * 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.rest.wireformat.json.provider; + +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + + +public class JSONWireFormatProviderFctory implements WireFormatProviderFactory{ + private ExtensionPointRegistry extensionPoints; + + public JSONWireFormatProviderFctory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + } + public WireFormatProvider createReferenceWireFormatProvider(RuntimeEndpointReference endpointReference) { + return new JSONWireFormatReferenceProvider(extensionPoints, endpointReference); + } + + public WireFormatProvider createServiceWireFormatProvider(RuntimeEndpoint endpoint) { + return new JSONWireFormatServiceProvider(extensionPoints, endpoint); + } + + public Class getModelType() { + return JSONWireFormat.class; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatReferenceProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatReferenceProvider.java new file mode 100644 index 0000000000..e1dce660ca --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatReferenceProvider.java @@ -0,0 +1,49 @@ +/* + * 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.rest.wireformat.json.provider; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +public class JSONWireFormatReferenceProvider implements WireFormatProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpointReference endpointReference; + + public JSONWireFormatReferenceProvider(ExtensionPointRegistry extensionPoints,RuntimeEndpointReference endpointReference ) { + this.extensionPoints = extensionPoints; + this.endpointReference = endpointReference; + } + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + return null; + } + + public Interceptor createInterceptor() { + return null; + } + + public String getPhase() { + return Phase.REFERENCE_BINDING_WIREFORMAT; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatServiceProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatServiceProvider.java new file mode 100644 index 0000000000..b4d59bd5ad --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatServiceProvider.java @@ -0,0 +1,102 @@ +/* + * 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.rest.wireformat.json.provider; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.javabeans.SimpleJavaDataBinding; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +public class JSONWireFormatServiceProvider implements WireFormatProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpoint endpoint; + + private InterfaceContract serviceContract; + private Binding binding; + + public JSONWireFormatServiceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + this.endpoint = endpoint; + this.binding = endpoint.getBinding(); + + } + + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + serviceContract = interfaceContract; + + //set JSON databinding + setDataBinding(serviceContract.getInterface()); + + //make JSON databinding default + serviceContract.getInterface().resetDataBinding(JSONDataBinding.NAME); + + return serviceContract; + } + + public Interceptor createInterceptor() { + if(binding.getRequestWireFormat() != null && binding.getRequestWireFormat() instanceof JSONWireFormat) { + return new JSONWireFormatInterceptor(extensionPoints, endpoint); + } + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_WIREFORMAT; + } + + + /** + * Utility method to reset data binding for the interface contract + * @param interfaze + */ + @SuppressWarnings({"deprecation", "unchecked"}) + private void setDataBinding(Interface interfaze) { + List operations = interfaze.getOperations(); + for (Operation operation : operations) { + operation.setDataBinding(JSONDataBinding.NAME); + DataType> inputType = operation.getInputType(); + if (inputType != null) { + List logical = inputType.getLogical(); + for (DataType inArg : logical) { + if (!SimpleJavaDataBinding.NAME.equals(inArg.getDataBinding())) { + inArg.setDataBinding(JSONDataBinding.NAME); + } + } + } + DataType outputType = operation.getOutputType(); + if (outputType != null) { + if (!SimpleJavaDataBinding.NAME.equals(outputType.getDataBinding())) { + outputType.setDataBinding(JSONDataBinding.NAME); + } + } + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory new file mode 100644 index 0000000000..7400523fb6 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory @@ -0,0 +1,19 @@ +# 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. + +# Implementation class for the wire format provider factory +org.apache.tuscany.sca.binding.rest.wireformat.json.provider.JSONWireFormatProviderFctory;model=org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat \ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java index e68646c8a5..4db7fd64dc 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java @@ -107,8 +107,8 @@ public class RESTBindingCacheTestCase { int index = 0; String content = ""; String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); + "If-Modified-Since", dateFormat.format(new Date(0)), + content.getBytes().length, content); os.write(request.getBytes()); os.flush(); diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java new file mode 100644 index 0000000000..9967fe4d71 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java @@ -0,0 +1,80 @@ +/* + * 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.rest.wireformat.json; + +import java.net.Socket; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.ContributionLocationHelper; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import services.Catalog; +import services.Item; + +@Ignore +public class CatalogServiceTestCase { + private static Node node; + private static Catalog catalogService; + + @BeforeClass + public static void init() throws Exception { + try { + String contribution = ContributionLocationHelper.getContributionLocation(CatalogServiceTestCase.class); + node = NodeFactory.newInstance().createNode("store.composite", new Contribution("catalog", contribution)); + node.start(); + + catalogService = node.getService(Catalog.class, "Catalog"); + Assert.assertNotNull(catalogService); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void destroy() throws Exception { + if(node != null) { + node.stop(); + } + } + + @Test + public void testPing() throws Exception { + new Socket("127.0.0.1", 8085); + System.in.read(); + } + + @Test + public void testNewsService() throws Exception { + Item[] items = catalogService.get(); + + Assert.assertNotNull(items); + Assert.assertTrue(items.length > 0); + + for(int pos = 0; pos < items.length; pos++) { + System.out.println(">>> Item[" + pos + "] - " + items[pos].getName() + " - " + items[pos].getPrice()); + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatTestCase.java new file mode 100644 index 0000000000..5159cf6893 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatTestCase.java @@ -0,0 +1,24 @@ +/* + * 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.rest.wireformat.json; + +public class JSONWireFormatTestCase { + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java new file mode 100644 index 0000000000..b5e504fe11 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java @@ -0,0 +1,27 @@ +/* + * 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 services; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface Catalog { + Item[] get(); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java new file mode 100644 index 0000000000..a064f3dd69 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java @@ -0,0 +1,29 @@ +/* + * 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 services; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface CurrencyConverter { + public double getConversion(String fromCurrenycCode, String toCurrencyCode, double amount); + + public String getCurrencySymbol(String currencyCode); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java new file mode 100644 index 0000000000..c354aed447 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java @@ -0,0 +1,38 @@ +/* + * 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 services; + +public class CurrencyConverterImpl implements CurrencyConverter { + public double getConversion(String fromCurrencyCode, String toCurrencyCode, double amount) { + if (toCurrencyCode.equals("USD")) + return amount; + else if (toCurrencyCode.equals("EUR")) + return ((double)Math.round(amount * 0.7256 * 100)) /100; + return 0; + } + + public String getCurrencySymbol(String currencyCode) { + if (currencyCode.equals("USD")) + return "$"; + else if (currencyCode.equals("EUR")) + return "E"; //"€"; + return "?"; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java new file mode 100644 index 0000000000..d132a24b00 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java @@ -0,0 +1,52 @@ +/* + * 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 services; + +import java.util.ArrayList; +import java.util.List; + +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +public class FruitsCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + private List catalog = new ArrayList(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.add(new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99))); + catalog.add(new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55))); + catalog.add(new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55))); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java new file mode 100644 index 0000000000..fe32cfc828 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java @@ -0,0 +1,50 @@ +/* + * 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 services; + +public class Item { + private String name; + private String price; + + public Item() { + } + + public Item(String name, String price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite new file mode 100644 index 0000000000..72219337b6 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite @@ -0,0 +1,40 @@ + + + + + + + USD + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite index 9fbce7c427..835633ac6d 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite @@ -23,13 +23,6 @@ xmlns:sr="http://sample/test" name="test"> - - diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormat.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormat.java new file mode 100644 index 0000000000..335ef66abf --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormat.java @@ -0,0 +1,43 @@ +/* + * 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.rest.wireformat.json; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.WireFormat; + +/** + * JSON Wireformat model for REST Binding + * + * @version $Rev$ $Date$ + */ +public interface JSONWireFormat extends WireFormat { + + /** + * QName representing the JSON Wireformat for REST Binding + */ + public static final QName REST_WIREFORMAT_JSON_QNAME = new QName(SCA11_TUSCANY_NS, "wireFormat.jsonrpc"); + + /** + * Return the QName identifying the wire format + * @return the QName identifying the wire format + */ + QName getSchemaName(); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatFactory.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatFactory.java new file mode 100644 index 0000000000..f297320b39 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/JSONWireFormatFactory.java @@ -0,0 +1,34 @@ +/* + * 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.rest.wireformat.json; + +/** + * JSON Wireformat factory for REST Binding + * + * @version $Rev$ $Date$ + */ +public interface JSONWireFormatFactory { + + /** + * Create a new JSON wireformat for REST Binding + * @return the new JSON wire format + */ + JSONWireFormat createRESTWireFormatJSON(); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatFactoryImpl.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatFactoryImpl.java new file mode 100644 index 0000000000..0ddeb8e557 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatFactoryImpl.java @@ -0,0 +1,36 @@ +/* + * 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.rest.wireformat.json.impl; + +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormatFactory; + +/** + * JSON Wireformat factory implementation for REST Binding + * + * @version $Rev$ $Date$ + */ +public class JSONWireFormatFactoryImpl implements JSONWireFormatFactory { + + public JSONWireFormat createRESTWireFormatJSON() { + return new JSONWireFormatImpl(); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatImpl.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatImpl.java new file mode 100644 index 0000000000..2ce3b95453 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatImpl.java @@ -0,0 +1,44 @@ +/* + * 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.rest.wireformat.json.impl; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; + +/** + * JSON Wireformat implementation for REST Binding + * + * @version $Rev$ $Date$ + */ +public class JSONWireFormatImpl implements JSONWireFormat { + + public QName getSchemaName() { + return JSONWireFormat.REST_WIREFORMAT_JSON_QNAME; + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + // no op + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormatFactory b/sca-java-2.x/trunk/modules/binding-rest/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormatFactory new file mode 100644 index 0000000000..8cc248c7d1 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/resources/META-INF/services/org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormatFactory @@ -0,0 +1,19 @@ +# 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. + +# Implementation class for model factory +org.apache.tuscany.sca.binding.rest.wireformat.json.impl.JSONWireFormatFactoryImpl \ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatProcessorTestCase.java b/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatProcessorTestCase.java new file mode 100644 index 0000000000..da7fb9e871 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/impl/JSONWireFormatProcessorTestCase.java @@ -0,0 +1,111 @@ +/* + * 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.rest.wireformat.json.impl; + +import java.io.ByteArrayOutputStream; +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.WireFormat; +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; +import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * JSON wire format processor tests to verify properly processing of + * wireFormat content in binding configuration in the composite file + * + * @version $Rev$ $Date$ + */ +public class JSONWireFormatProcessorTestCase { + + public static final String BINDING_WITH_WIRE_FORMAT = + "" + + "" + + ""; + + private static XMLInputFactory inputFactory; + private static XMLOutputFactory outputFactory; + private static ExtensibleStAXArtifactProcessor staxProcessor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + + context = new ProcessorContext(extensionPoints); + + StAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(extensionPoints); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory); + + } + + /** + * Tests the APIs: + * public WireFormat getRequstWireFormat(); + * public WireFormat getResponseWireFormat(); + * + * @throws Exception + */ + @Test + public void testWireFormat() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_WIRE_FORMAT)); + + RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context); + Assert.assertNotNull(binding); + + WireFormat requestWireFormat = binding.getRequestWireFormat(); + Assert.assertEquals(JSONWireFormat.class, requestWireFormat.getClass().getInterfaces()[0]); + + WireFormat responseWireFormat = binding.getResponseWireFormat(); + Assert.assertEquals(JSONWireFormat.class, responseWireFormat.getClass().getInterfaces()[0]); + } + + @Test + public void testWriteWireFormat() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(BINDING_WITH_WIRE_FORMAT)); + + RESTBinding binding = (RESTBinding)staxProcessor.read(reader, context); + Assert.assertNotNull(binding); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(binding, bos, context); + + // used for debug comparison + System.out.println(BINDING_WITH_WIRE_FORMAT); + System.out.println(bos.toString()); + + Assert.assertEquals(BINDING_WITH_WIRE_FORMAT, bos.toString()); + + } +} -- cgit v1.2.3