diff options
Diffstat (limited to '')
4 files changed, 270 insertions, 0 deletions
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java new file mode 100644 index 0000000000..7c7a827571 --- /dev/null +++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPBindingProviderFactory.java @@ -0,0 +1,63 @@ +/*
+ * 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.jsonp.runtime;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.binding.jsonp.JSONPBinding;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint;
+import org.apache.tuscany.sca.provider.BindingProviderFactory;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+
+public class JSONPBindingProviderFactory implements BindingProviderFactory<JSONPBinding> {
+
+ private ServletHost servletHost;
+
+ public JSONPBindingProviderFactory(ExtensionPointRegistry extensionPoints) {
+ ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class);
+ List<ServletHost> hosts = servletHosts.getServletHosts();
+ if (!hosts.isEmpty()) {
+ this.servletHost = hosts.get(0);
+ }
+ }
+
+ public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ JSONPBinding binding) {
+ return null;
+ }
+
+ public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ JSONPBinding binding) {
+ return new JSONPServiceBindingProvider(component, service, binding, servletHost);
+ }
+
+ public Class<JSONPBinding> getModelType() {
+ return JSONPBinding.class;
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java new file mode 100644 index 0000000000..25f1d8cc51 --- /dev/null +++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java @@ -0,0 +1,74 @@ +/*
+ * 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.jsonp.runtime;
+
+import org.apache.tuscany.sca.binding.jsonp.JSONPBinding;
+import org.apache.tuscany.sca.host.http.ServletHost;
+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.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+
+public class JSONPServiceBindingProvider implements ServiceBindingProvider {
+
+ private RuntimeComponentService service;
+ private JSONPBinding binding;
+ private ServletHost servletHost;
+
+ public JSONPServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ JSONPBinding binding,
+ ServletHost servletHost) {
+ this.service = service;
+ this.binding = binding;
+ this.servletHost = servletHost;
+ }
+
+ public void start() {
+ RuntimeWire wire = service.getRuntimeWire(binding);
+ Interface serviceInterface = service.getInterfaceContract().getInterface();
+ for (Operation op : serviceInterface.getOperations()) {
+ JSONPServlet servlet = new JSONPServlet(wire, op);
+ String path = service.getName() + "/" + op.getName();
+ servletHost.addServletMapping(path, servlet);
+ }
+ }
+
+ public void stop() {
+ Interface serviceInterface = service.getInterfaceContract().getInterface();
+ for (Operation op : serviceInterface.getOperations()) {
+ String path = service.getName() + "/" + op.getName();
+ servletHost.removeServletMapping(path);
+ }
+ }
+
+ // TODO: Why are these two still on the ServiceBindingProvider interface?
+ public InterfaceContract getBindingInterfaceContract() {
+ return null;
+ }
+
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
+
+}
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 new file mode 100644 index 0000000000..6521e336e6 --- /dev/null +++ b/java/sca/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java @@ -0,0 +1,114 @@ +/*
+ * 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.jsonp.runtime;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Enumeration;
+
+import javax.servlet.GenericServlet;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+
+public class JSONPServlet extends GenericServlet {
+ private static final long serialVersionUID = 1L;
+
+ protected transient RuntimeWire wire;
+ protected transient Operation operation;
+ protected transient ObjectMapper mapper;
+
+ public JSONPServlet(RuntimeWire wire, Operation operation) {
+ this.wire = wire;
+ this.operation = operation;
+ this.mapper = new ObjectMapper();
+ }
+
+ @Override
+ public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
+ String jsonRequest = getJSONRequest(servletRequest);
+ Object[] args = jsonToObjects(jsonRequest);
+ Object response = invokeService(args);
+ String jsonResponse = getJSONResponse(servletRequest, response);
+ servletResponse.getOutputStream().println(jsonResponse);
+ }
+
+ /**
+ * Turn the request into JSON
+ */
+ protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException {
+ String jsonRequest = "[";
+ for (Enumeration<?> ns = servletRequest.getParameterNames(); ns.hasMoreElements(); ) {
+ String name = (String) ns.nextElement();
+ if (!name.startsWith("_") && !"callback".equals(name)) {
+ if (jsonRequest.length() > 1) {
+ jsonRequest += ", ";
+ }
+ //jsonRequest += name + ":" + servletRequest.getParameter(name);
+ jsonRequest += "\""+ servletRequest.getParameter(name) + "\"";
+ }
+ }
+ jsonRequest += "]";
+ return jsonRequest;
+ }
+
+ /**
+ * Turn the response object into JSON
+ */
+ protected String getJSONResponse(ServletRequest servletRequest, Object response) throws IOException, JsonParseException {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ mapper.writeValue(os , response);
+ String jsonResponse = os.toString();
+
+ String callback = servletRequest.getParameter("callback");
+ if (callback != null && callback.length() > 1) {
+ jsonResponse = callback + "(" + jsonResponse + ");";
+ }
+
+ return jsonResponse;
+ }
+
+ /**
+ * Turn the request JSON into objects
+ */
+ protected Object[] jsonToObjects(String jsonRequest) throws IOException, JsonParseException, JsonMappingException {
+ Class<?> c = new Object[0].getClass();
+ Object[] args = (Object[])mapper.readValue(jsonRequest, c);
+ return args;
+ }
+
+ /**
+ * Send the request down the wire to invoke the service
+ */
+ protected Object invokeService(Object[] args) {
+ try {
+ return wire.invoke(operation, args);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
\ No newline at end of file diff --git a/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory b/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory new file mode 100644 index 0000000000..3efe5535ce --- /dev/null +++ b/java/sca/modules/binding-jsonp-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory @@ -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 binding extension
+org.apache.tuscany.sca.binding.jsonp.runtime.JSONPBindingProviderFactory;model=org.apache.tuscany.sca.binding.jsonp.JSONPBinding
|