From 9d5106bd197cebed36a5701a243f35d056dbc615 Mon Sep 17 00:00:00 2001 From: antelder Date: Tue, 26 Oct 2010 14:17:45 +0000 Subject: Tag rc1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1027571 13f79535-47bb-0310-9956-ffa450edef68 --- ...TTPDefaultOperationSelectorProviderFactory.java | 60 +++ ...DefaultServiceOperationSelectorInterceptor.java | 85 ++++ .../HTTPDefaultWireFormatProviderFactory.java | 68 +++ .../HTTPDefaultWireFormatServiceInterceptor.java | 252 +++++++++++ .../format/HTTPXMLWireFormatProviderFactory.java | 70 ++++ .../HTTPXMLWireFormatServiceInterceptor.java | 182 ++++++++ .../http/provider/HTTPBindingListenerServlet.java | 462 +++++++++++++++++++++ .../http/provider/HTTPBindingProviderFactory.java | 64 +++ .../http/provider/HTTPBindingServiceServlet.java | 53 +++ .../sca/binding/http/provider/HTTPContext.java | 46 ++ .../http/provider/HTTPServiceBindingProvider.java | 226 ++++++++++ 11 files changed, 1568 insertions(+) create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultOperationSelectorProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultServiceOperationSelectorInterceptor.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatServiceInterceptor.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingServiceServlet.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPContext.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java (limited to 'sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca') diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultOperationSelectorProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultOperationSelectorProviderFactory.java new file mode 100644 index 0000000000..bf6ad75cd3 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultOperationSelectorProviderFactory.java @@ -0,0 +1,60 @@ +/* + * 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.format; + +import org.apache.tuscany.sca.binding.http.HTTPDefaultOperationSelector; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.OperationSelectorProvider; +import org.apache.tuscany.sca.provider.OperationSelectorProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +public class HTTPDefaultOperationSelectorProviderFactory implements OperationSelectorProviderFactory{ + + public HTTPDefaultOperationSelectorProviderFactory(ExtensionPointRegistry extensionPoints) { + } + + @Override + public Class getModelType() { + return null; + } + + @Override + public OperationSelectorProvider createReferenceOperationSelectorProvider(RuntimeEndpointReference endpointReference) { + return null; + } + + @Override + public OperationSelectorProvider createServiceOperationSelectorProvider(final RuntimeEndpoint endpoint) { + return new OperationSelectorProvider(){ + @Override + public Interceptor createInterceptor() { + return new HTTPDefaultServiceOperationSelectorInterceptor(endpoint); + } + + @Override + public String getPhase() { + return Phase.SERVICE_BINDING_OPERATION_SELECTOR; + }}; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultServiceOperationSelectorInterceptor.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultServiceOperationSelectorInterceptor.java new file mode 100644 index 0000000000..dc4f37dbad --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultServiceOperationSelectorInterceptor.java @@ -0,0 +1,85 @@ +/* + * 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.format; + +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.tuscany.sca.binding.http.provider.HTTPContext; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.Operation; +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; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Sets the operation based on the request path. + * + * From a url: http://localhost:8080/HelloworldComponent/Helloworld/sayHello?name=Petra + * where the component is HelloworldComponent and the service is Helloworld + * the path will be "/sayHello" so the operation is "sayHello". + * + * TODO: we could also do something similar to how the JMS binding supports + * a single "onMessage" method to get all requests, so perhaps this could + * also support impls with method: service(HttpServletRequest, HttpServletResponse) + */ +public class HTTPDefaultServiceOperationSelectorInterceptor implements Interceptor { + + private Invoker next; + private List operations; + + public HTTPDefaultServiceOperationSelectorInterceptor(RuntimeEndpoint endpoint) { + Interface serviceInterface = endpoint.getService().getInterfaceContract().getInterface(); + this.operations = serviceInterface.getOperations(); + } + + @Override + public Message invoke(Message msg) { + HTTPContext context = msg.getBindingContext(); + HttpServletRequest request = context.getRequest(); + String path = request.getPathInfo(); + if (path.startsWith("/")) { + path = path.substring(1); + } + + for (Operation op : operations) { + if (op.getName().equals(path)) { + msg.setOperation(op); + return next.invoke(msg); + } + } + + throw new ServiceRuntimeException("No matching operation: " + path); + } + + @Override + public void setNext(Invoker next) { + this.next = next; + } + + @Override + public Invoker getNext() { + return next; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatProviderFactory.java new file mode 100644 index 0000000000..45e74d8ec8 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatProviderFactory.java @@ -0,0 +1,68 @@ +/* + * 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.format; + +import org.apache.tuscany.sca.binding.http.HTTPDefaultWireFormat; +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.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +public class HTTPDefaultWireFormatProviderFactory implements WireFormatProviderFactory { + + public HTTPDefaultWireFormatProviderFactory(ExtensionPointRegistry extensionPoints) { + } + + @Override + public Class getModelType() { + return null; + } + + @Override + public WireFormatProvider createReferenceWireFormatProvider(RuntimeEndpointReference endpointReference) { + return null; + } + + @Override + public WireFormatProvider createServiceWireFormatProvider(final RuntimeEndpoint endpoint) { + return new WireFormatProvider() { + @Override + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + // TODO: Ideally this wants to set the databinding on a per request basis from the + // http content type and accept headers and so support things like json or xml etc, + // for now to get started just use json + interfaceContract.getInterface().resetDataBinding("JSON"); + return interfaceContract; + } + @Override + public Interceptor createInterceptor() { + return new HTTPDefaultWireFormatServiceInterceptor(endpoint); + } + @Override + public String getPhase() { + return Phase.SERVICE_BINDING_WIREFORMAT; + }}; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatServiceInterceptor.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatServiceInterceptor.java new file mode 100644 index 0000000000..fcbf8015f8 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPDefaultWireFormatServiceInterceptor.java @@ -0,0 +1,252 @@ +/* + * 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.format; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.StringTokenizer; +import java.util.TreeSet; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.binding.http.provider.HTTPContext; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +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; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Handles the default wire format for the http binding + * + * 1- determine the request and response format (xml, json, etc) from the + * binding config or content type header and accept headers + * - TODO: need a way to configure the databinding framework based on that format + * 2- get the request contents from the HttpServletRequest + * - for a post its just the request body + * - for a get need to convert the query string into a body based on the format (xml, json, etc) + * 3- send the request on down the wire + * 4- set the response contents in the HttpServletResponse + * (the databinding should already have put it in the correct format) + * + */ +public class HTTPDefaultWireFormatServiceInterceptor implements Interceptor { + + private Invoker next; + private String jsonpCallbackName = "callback"; + + public HTTPDefaultWireFormatServiceInterceptor(RuntimeEndpoint endpoint) { + } + + @Override + public void setNext(Invoker next) { + this.next = next; + } + + @Override + public Invoker getNext() { + return next; + } + + @Override + public Message invoke(Message msg) { + try { + return invokeResponse(getNext().invoke(invokeRequest(msg))); + } catch (IOException e) { + throw new ServiceRuntimeException(e); + } + } + + private Message invokeRequest(Message msg) throws IOException { + HTTPContext context = msg.getBindingContext(); + HttpServletRequest servletRequest = context.getRequest(); + if ("GET".equals(servletRequest.getMethod())) { + msg.setBody(getRequestFromQueryString(msg.getOperation(), servletRequest)); + } else { + msg.setBody(getRequestFromPost(msg.getOperation(), servletRequest)); + } + return msg; + } + + /** + * The data binding seems to be expecting an Object array of json strings so if the + * post data is a json array convert that to an array of strings + * TODO: should this be being done by the data binding framework? + */ + private Object[] getRequestFromPost(Operation operation, HttpServletRequest servletRequest) throws IOException { + List os = new ArrayList(); + String data = read(servletRequest); + if (data.length() > 0) { + if (data.startsWith("[") && data.endsWith("]")) { + data = data.substring(1, data.length()-1); + StringTokenizer st = new StringTokenizer(data, ","); + while (st.hasMoreElements()) { + os.add(st.nextElement()); + } + } else { + os.add(data); + } + } + return os.toArray(); + } + + private Message invokeResponse(Message msg) throws IOException { + HTTPContext context = msg.getBindingContext(); + HttpServletRequest servletRequest = context.getRequest(); + HttpServletResponse servletResponse = context.getResponse(); + + if (msg.isFault()) { + servletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.valueOf(msg.getBody())); + } else { + String response = getResponseAsString(servletRequest, servletResponse, msg.getBody()); + servletResponse.getOutputStream().println(response); + } + + return msg; + } + + /** + * Turn the request into a string array of JSON structures. The data binding + * layer will then convert each of the individual parameters into the appropriate + * types for the implementation interface + * + * From ML thread: http://apache.markmail.org/message/ix3vvyomronellmi + * 1- if the binding configuration contains a mapping from query parameter name to operation parameter then use that. + * 2- if the service interface or impl uses jaxrs annotations to name the parameters then use that mapping + * 3- if the query parameters are name arg0, arg1 etc than use those names for the mapping, + * 4- otherwise use the order in the query string. + */ + protected Object[] getRequestFromQueryString(Operation operation, ServletRequest servletRequest) { + + List types = operation.getInputType().getLogical(); + int typesIndex = 0; + + List jsonRequestArray = new ArrayList(); + + for (String name : getOrderedParameterNames(servletRequest)) { + String jsonRequest = ""; + // quote string parameters so clients work in the usual javascript way + if (typesIndex < types.size() && String.class.equals(types.get(typesIndex++).getGenericType())) { + String x = servletRequest.getParameter(name); + if (x.startsWith("\"") || x.startsWith("'")) { + jsonRequest += x; + } else { + if (x.contains("\"")) { + jsonRequest += "'" + x + "'"; + } else { + jsonRequest += "\"" + x + "\""; + } + } + } else { + jsonRequest += servletRequest.getParameter(name); + } + jsonRequestArray.add(jsonRequest); + } + + return jsonRequestArray.toArray(); + } + + /** + * Get the request parameter names in the correct order. + * Either the query parameters are named arg0, arg1, arg2 etc or else use the order + * from the order in the query string. Eg, the url: + * http://localhost:8085/HelloWorldService/sayHello2?first=petra&last=arnold&callback=foo" + * should invoke: + * sayHello2("petra", "arnold") + * so the parameter names should be ordered: "first", "last" + */ + protected List getOrderedParameterNames(ServletRequest servletRequest) { + List orderedNames = new ArrayList(); + Set parameterNames = servletRequest.getParameterMap().keySet(); + if (parameterNames.contains("arg0")) { + for (int i=0; i sortedNames = new TreeSet(new Comparator(){ + public int compare(String o1, String o2) { + int i = queryString.indexOf(o1); + int j = queryString.indexOf(o2); + return i - j; + }}); + for (String name : parameterNames) { + // ignore system and jsonpCallbackName parameters + if (!name.startsWith("_") && !name.equals(jsonpCallbackName)) { + sortedNames.add(name); + } + } + orderedNames.addAll(sortedNames); + } + return orderedNames; + } + + /** + * The databinding layer will have converted the return type into a JSON string so simply + * add wrap it for return. + */ + protected String getResponseAsString(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object response) { + String jsonResponse = response == null ? "" : response.toString(); + + if ("GET".equals(servletRequest.getMethod())) { + // handle JSONP callback name padding + String callback = servletRequest.getParameter(jsonpCallbackName); + if (callback != null && callback.length() > 1) { + jsonResponse = callback + "(" + jsonResponse + ");"; + } + } + + return jsonResponse; + } + + protected static String read(HttpServletRequest servletRequest) throws IOException { + InputStream is = servletRequest.getInputStream(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(is)); + StringBuffer sb = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + } + return sb.toString().trim(); + } finally { + if (reader != null) { + reader.close(); + } + } + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatProviderFactory.java new file mode 100644 index 0000000000..cb60101bb7 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatProviderFactory.java @@ -0,0 +1,70 @@ +/* + * 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.format; + +import org.apache.tuscany.sca.binding.http.HTTPXMLWireFormat; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; +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.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +public class HTTPXMLWireFormatProviderFactory implements WireFormatProviderFactory { + + private DOMHelper domHelper; + + public HTTPXMLWireFormatProviderFactory(ExtensionPointRegistry extensionPoints) { + this.domHelper = DOMHelper.getInstance(extensionPoints); + } + + @Override + public Class getModelType() { + return null; + } + + @Override + public WireFormatProvider createReferenceWireFormatProvider(RuntimeEndpointReference endpointReference) { + return null; + } + + @Override + public WireFormatProvider createServiceWireFormatProvider(final RuntimeEndpoint endpoint) { + return new WireFormatProvider() { + @Override + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + interfaceContract.getInterface().resetDataBinding(DOMDataBinding.NAME); + return interfaceContract; + } + @Override + public Interceptor createInterceptor() { + return new HTTPXMLWireFormatServiceInterceptor(endpoint, domHelper); + } + @Override + public String getPhase() { + return Phase.SERVICE_BINDING_WIREFORMAT; + }}; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java new file mode 100644 index 0000000000..8bd8b8b9f0 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java @@ -0,0 +1,182 @@ +/* + * 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.format; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.binding.http.provider.HTTPContext; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.util.FaultException; +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; +import org.oasisopen.sca.ServiceRuntimeException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; + +/** + * Handles the xml wire format for the http binding + */ +public class HTTPXMLWireFormatServiceInterceptor implements Interceptor { + + private Invoker next; + private DOMHelper domHelper; + + public HTTPXMLWireFormatServiceInterceptor(RuntimeEndpoint endpoint, DOMHelper domHelper) { + this.domHelper = domHelper; + } + + @Override + public void setNext(Invoker next) { + this.next = next; + } + + @Override + public Invoker getNext() { + return next; + } + + @Override + public Message invoke(Message msg) { + try { + return invokeResponse(getNext().invoke(invokeRequest(msg))); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } + + private Message invokeRequest(Message msg) throws IOException, SAXException { + HTTPContext context = msg.getBindingContext(); + HttpServletRequest servletRequest = context.getRequest(); + if ("GET".equals(servletRequest.getMethod())) { + msg.setBody(getRequestFromQueryString(msg.getOperation(), servletRequest)); + } else { + msg.setBody(new Object[]{domHelper.load(read(servletRequest))}); + } + return msg; + } + + private Message invokeResponse(Message msg) throws IOException { + HTTPContext context = msg.getBindingContext(); + HttpServletResponse servletResponse = context.getResponse(); + + servletResponse.setContentType("text/xml"); + + Object o = msg.getBody(); + if (msg.isFault()) { + String xml = domHelper.saveAsString((Node)((FaultException)o).getFaultInfo()); + servletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, xml); + } else { + String xml = ""; + if (o instanceof Element) { + xml = domHelper.saveAsString((Node)o); + } else if ((o instanceof Object[]) && ((Object[])o)[0] instanceof Node) { + xml = domHelper.saveAsString((Node)((Object[])o)[0]); + } else if (o != null) { + throw new IllegalStateException("expecting Node payload: " + o); + } + servletResponse.getOutputStream().println(xml); + } + + return msg; + } + + /** + * Turn the query request into XML. + */ + protected Object[] getRequestFromQueryString(Operation operation, ServletRequest servletRequest) throws IOException, SAXException { + List xmlRequestArray = new ArrayList(); + for (String name : getOrderedParameterNames(servletRequest)) { + xmlRequestArray.add(domHelper.load("<" + name + ">" + servletRequest.getParameter(name) + "")); + } + return xmlRequestArray.toArray(); + } + + /** + * Get the request parameter names in the correct order. + * Either the query parameters are named arg0, arg1, arg2 etc or else use the order + * from the order in the query string. Eg, the url: + * http://localhost:8085/HelloWorldService/sayHello2?first=petra&last=arnold&callback=foo" + * should invoke: + * sayHello2("petra", "arnold") + * so the parameter names should be ordered: "first", "last" + */ + protected List getOrderedParameterNames(ServletRequest servletRequest) { + List orderedNames = new ArrayList(); + Set parameterNames = servletRequest.getParameterMap().keySet(); + if (parameterNames.contains("arg0")) { + for (int i=0; i sortedNames = new TreeSet(new Comparator(){ + public int compare(String o1, String o2) { + int i = queryString.indexOf(o1); + int j = queryString.indexOf(o2); + return i - j; + }}); + for (String name : parameterNames) { + sortedNames.add(name); + } + orderedNames.addAll(sortedNames); + } + return orderedNames; + } + + protected static String read(HttpServletRequest servletRequest) throws IOException { + InputStream is = servletRequest.getInputStream(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(is)); + StringBuffer sb = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + } + return sb.toString(); + } finally { + if (reader != null) { + reader.close(); + } + } + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java new file mode 100644 index 0000000000..e2a3a5cf39 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java @@ -0,0 +1,462 @@ +/* + * 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.provider; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLDecoder; +import java.text.ParseException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.common.http.HTTPCacheContext; +import org.apache.tuscany.sca.common.http.HTTPContentTypeMapper; +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 requests to the + * target component implementation. + * + * @version $Rev$ $Date$ + */ +public class HTTPBindingListenerServlet extends HttpServlet { + private static final long serialVersionUID = 2865466417329430610L; + + transient private Binding binding; + + private MessageFactory messageFactory; + private Invoker getInvoker; + private Invoker conditionalGetInvoker; + private Invoker putInvoker; + private Invoker conditionalPutInvoker; + private Invoker postInvoker; + private Invoker conditionalPostInvoker; + private Invoker deleteInvoker; + private Invoker conditionalDeleteInvoker; + + /** + * Constructs a new HTTPServiceListenerServlet. + */ + public HTTPBindingListenerServlet(Binding binding, MessageFactory messageFactory) { + this.binding = binding; + this.messageFactory = messageFactory; + } + + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + super.service(request, response); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Get the request path + String pathInfo = request.getPathInfo(); + if (pathInfo == null || pathInfo.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; + } + String path = URLDecoder.decode(pathInfo, "UTF-8"); + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + + String id = path.substring(1); + + Message responseMessage = null; + HTTPCacheContext cacheContext = null; + try { + cacheContext = HTTPCacheContext.createCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // 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); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = getInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + if(response.getContentType() == null || response.getContentType().length() == 0){ + // Calculate content-type based on extension + String contentType = HTTPContentTypeMapper.getContentType(id); + if(contentType != null && contentType.length() >0) { + response.setContentType(contentType); + } + } + + // 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(); + } + + @Override + protected void doDelete(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); + + Message responseMessage = null; + HTTPCacheContext cacheContext = null; + try { + cacheContext = HTTPCacheContext.createCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalDeleteInvoker != null )) { + requestMessage.setBody(new Object[] {id, cacheContext}); + responseMessage = conditionalDeleteInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = deleteInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + 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(); + } + + @Override + protected void doPut(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); + + Message responseMessage = null; + HTTPCacheContext cacheContext = null; + try { + cacheContext = HTTPCacheContext.createCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPutInvoker != null )) { + requestMessage.setBody(new Object[] {id, cacheContext}); + responseMessage = conditionalPutInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = putInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + 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(); + } + + @Override + protected void doPost(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); + + Message responseMessage = null; + HTTPCacheContext cacheContext = null; + try { + cacheContext = HTTPCacheContext.createCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPostInvoker != null )) { + requestMessage.setBody(new Object[] {cacheContext}); + responseMessage = conditionalPostInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {}); + responseMessage = postInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + + // Test if the ETag and LastModified are returned as a cache context. + Object body = responseMessage.getBody(); + if ( body.getClass() == HTTPCacheContext.class ) { + // Transfer to header if so. + HTTPCacheContext cc = (HTTPCacheContext)responseMessage.getBody(); + if (( cc != null ) && ( cc.isEnabled() )) { + String eTag = cc.getETag(); + if ( eTag != null ) + response.setHeader( "ETag", cc.getETag() ); + String lastModified = cc.getLastModified(); + if ( lastModified != null) + response.setHeader( "LastModified", cc.getLastModified() ); + } + } + } + + /** + * @return the getInvoker + */ + public Invoker getGetInvoker() { + return getInvoker; + } + + /** + * @param getInvoker the getInvoker to set + */ + public void setGetInvoker(Invoker getInvoker) { + this.getInvoker = getInvoker; + } + + /** + * @return the conditionalGetInvoker + */ + public Invoker getConditionalGetInvoker() { + return conditionalGetInvoker; + } + + /** + * @param conditionalGetInvoker the conditionalGetInvoker to set + */ + public void setConditionalGetInvoker(Invoker conditionalGetInvoker) { + this.conditionalGetInvoker = conditionalGetInvoker; + } + + /** + * @return the putInvoker + */ + public Invoker getPutInvoker() { + return putInvoker; + } + + /** + * @param putInvoker the putInvoker to set + */ + public void setPutInvoker(Invoker putInvoker) { + this.putInvoker = putInvoker; + } + + /** + * @return the conditionalPutInvoker + */ + public Invoker getConditionalPutInvoker() { + return conditionalPutInvoker; + } + + /** + * @param conditionalPutInvoker the conditionalPutInvoker to set + */ + public void setConditionalPutInvoker(Invoker conditionalPutInvoker) { + this.conditionalPutInvoker = conditionalPutInvoker; + } + + /** + * @return the postInvoker + */ + public Invoker getPostInvoker() { + return postInvoker; + } + + /** + * @param postInvoker the postInvoker to set + */ + public void setPostInvoker(Invoker postInvoker) { + this.postInvoker = postInvoker; + } + + /** + * @return the conditionalPostInvoker + */ + public Invoker getConditionalPostInvoker() { + return conditionalPostInvoker; + } + + /** + * @param conditionalPostInvoker the conditionalPostInvoker to set + */ + public void setConditionalPostInvoker(Invoker conditionalPostInvoker) { + this.conditionalPostInvoker = conditionalPostInvoker; + } + + /** + * @return the deleteInvoker + */ + public Invoker getDeleteInvoker() { + return deleteInvoker; + } + + /** + * @param deleteInvoker the deleteInvoker to set + */ + public void setDeleteInvoker(Invoker deleteInvoker) { + this.deleteInvoker = deleteInvoker; + } + + /** + * @return the conditionalDeleteInvoker + */ + public Invoker getConditionalDeleteInvoker() { + return conditionalDeleteInvoker; + } + + /** + * @param conditionalDeleteInvoker the conditionalDeleteInvoker to set + */ + public void setConditionalDeleteInvoker(Invoker conditionalDeleteInvoker) { + this.conditionalDeleteInvoker = conditionalDeleteInvoker; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java new file mode 100644 index 0000000000..118a2c3bc4 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java @@ -0,0 +1,64 @@ +/* + * 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.provider; + +import org.apache.tuscany.sca.binding.http.HTTPBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostHelper; +import org.apache.tuscany.sca.invocation.MessageFactory; +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.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + + +/** + * Factory for HTTP binding providers. + * + * @version $Rev$ $Date$ + */ +public class HTTPBindingProviderFactory implements BindingProviderFactory { + private ExtensionPointRegistry extensionPoints; + private MessageFactory messageFactory; + private ServletHost servletHost; + + public HTTPBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + this.servletHost = ServletHostHelper.getServletHost(extensionPoints); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + messageFactory = modelFactories.getFactory(MessageFactory.class); + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpointReference) { + // Binding HTTP is currently NOT supporting References + return null; + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + return new HTTPServiceBindingProvider(endpoint, extensionPoints, messageFactory, servletHost); + } + + public Class getModelType() { + return HTTPBinding.class; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingServiceServlet.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingServiceServlet.java new file mode 100644 index 0000000000..686032571e --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingServiceServlet.java @@ -0,0 +1,53 @@ +/* + * 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.provider; + +import java.io.IOException; + +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.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +public class HTTPBindingServiceServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected transient MessageFactory messageFactory; + protected transient RuntimeEndpoint wire; + + public HTTPBindingServiceServlet(RuntimeEndpoint wire, MessageFactory messageFactory) { + this.wire = wire; + this.messageFactory = messageFactory; + } + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HTTPContext bindingContext = new HTTPContext(); + bindingContext.setRequest(request); + bindingContext.setResponse(response); + Message msg = messageFactory.createMessage(); + msg.setBindingContext(bindingContext); + wire.invoke(msg); + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPContext.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPContext.java new file mode 100644 index 0000000000..0a49823f06 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPContext.java @@ -0,0 +1,46 @@ +/* + * 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.provider; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Holder to pass servlet request and response between the Inteceptors + */ +public class HTTPContext { + + private HttpServletRequest request; + private HttpServletResponse response; + + public HttpServletRequest getRequest() { + return request; + } + public void setRequest(HttpServletRequest request) { + this.request = request; + } + public HttpServletResponse getResponse() { + return response; + } + public void setResponse(HttpServletResponse response) { + this.response = response; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java new file mode 100644 index 0000000000..e123c28e02 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java @@ -0,0 +1,226 @@ +/* + * 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.provider; + +import javax.servlet.Servlet; + +import org.apache.tuscany.sca.binding.http.HTTPBinding; +import org.apache.tuscany.sca.binding.http.HTTPDefaultOperationSelector; +import org.apache.tuscany.sca.binding.http.HTTPDefaultWireFormat; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.EndpointProvider; +import org.apache.tuscany.sca.provider.OperationSelectorProvider; +import org.apache.tuscany.sca.provider.OperationSelectorProviderFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * Implementation of an HTTP binding provider. + * + * @version $Rev$ $Date$ + */ +public class HTTPServiceBindingProvider implements EndpointProvider { + private ExtensionPointRegistry extensionPoints; + + private RuntimeEndpoint endpoint; + private HTTPBinding binding; + private MessageFactory messageFactory; + private OperationSelectorProvider osProvider; + private WireFormatProvider wfProvider; + private ServletHost servletHost; + private String servletMapping; + private InterfaceContract interfaceContract; + private boolean widget; + + private HTTPBindingListenerServlet bindingListenerServlet; + + public HTTPServiceBindingProvider(RuntimeEndpoint endpoint, + ExtensionPointRegistry extensionPoints, + MessageFactory messageFactory, + ServletHost servletHost) { + + this.endpoint = endpoint; + this.binding = (HTTPBinding)endpoint.getBinding(); + this.widget = "Widget".equals(binding.getName()); + + this.extensionPoints = extensionPoints; + this.messageFactory = messageFactory; + this.servletHost = servletHost; + + if (binding.getOperationSelector() == null && !widget) { + binding.setOperationSelector(new HTTPDefaultOperationSelector()); + } + if (binding.getRequestWireFormat() == null && !widget) { + binding.setRequestWireFormat(new HTTPDefaultWireFormat()); + } + if (binding.getResponseWireFormat() == null && !widget) { + binding.setResponseWireFormat(new HTTPDefaultWireFormat()); + } + + ProviderFactoryExtensionPoint providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); + + if (binding.getOperationSelector() != null) { + // Configure the interceptors for operation selection + OperationSelectorProviderFactory osProviderFactory = (OperationSelectorProviderFactory) providerFactories.getProviderFactory(binding.getOperationSelector().getClass()); + if (osProviderFactory != null) { + this.osProvider = osProviderFactory.createServiceOperationSelectorProvider(endpoint); + } + } + + if (binding.getRequestWireFormat() != null && binding.getResponseWireFormat() != null) { + // Configure the interceptors for wire format + WireFormatProviderFactory wfProviderFactory = (WireFormatProviderFactory) providerFactories.getProviderFactory(binding.getRequestWireFormat().getClass()); + if (wfProviderFactory != null) { + this.wfProvider = wfProviderFactory.createServiceWireFormatProvider(endpoint); + } + } + + //clone the service contract to avoid databinding issues + try { + interfaceContract = (InterfaceContract)endpoint.getComponentServiceInterfaceContract().clone(); + + // configure data binding + if (this.wfProvider != null) { + wfProvider.configureWireFormatInterfaceContract(interfaceContract); + } + } catch(CloneNotSupportedException e) { + // shouldn't happen + } + + servletMapping = binding.getURI(); + if (!servletMapping.endsWith("/")) { + servletMapping += "/"; + } + if (!servletMapping.endsWith("*")) { + servletMapping += "*"; + } + } + + public void start() { + if (widget) { + start1x(); + } else { + servletHost.addServletMapping(servletMapping, new HTTPBindingServiceServlet(endpoint, messageFactory)); + } + } + + public void start1x() { + // Get the invokers for the supported operations + Servlet servlet = null; + bindingListenerServlet = new HTTPBindingListenerServlet(binding, messageFactory ); + for (InvocationChain invocationChain : endpoint.getInvocationChains()) { + Operation operation = invocationChain.getTargetOperation(); + String operationName = operation.getName(); + if (operationName.equals("get")) { + Invoker getInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setGetInvoker(getInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalGet")) { + Invoker conditionalGetInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalGetInvoker(conditionalGetInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("delete")) { + Invoker deleteInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setDeleteInvoker(deleteInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalDelete")) { + Invoker conditionalDeleteInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalDeleteInvoker(conditionalDeleteInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("put")) { + Invoker putInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setPutInvoker(putInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalPut")) { + Invoker conditionalPutInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalPutInvoker(conditionalPutInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("post")) { + Invoker postInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setPostInvoker(postInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalPost")) { + Invoker conditionalPostInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalPostInvoker(conditionalPostInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("service")) { + Invoker serviceInvoker = invocationChain.getHeadInvoker(); +// servlet = new HTTPServiceListenerServlet(binding, serviceInvoker, messageFactory); + break; + } + } + if (servlet == null) { + throw new IllegalStateException("No get or service method found on the service"); + } + + // Create our HTTP service listener Servlet and register it with the + // Servlet host + servletMapping = binding.getURI(); + if (!servletMapping.endsWith("/")) { + servletMapping += "/"; + } + if (!servletMapping.endsWith("*")) { + servletMapping += "*"; + } + + servletHost.addServletMapping(servletMapping, servlet); + } + + public void stop() { + servletHost.removeServletMapping(servletMapping); + } + + public InterfaceContract getBindingInterfaceContract() { + return interfaceContract; + } + + public boolean supportsOneWayInvocation() { + return false; + } + + /** + * Add specific http interceptor to invocation chain + */ + public void configure() { + + if (widget) return; + + InvocationChain bindingChain = endpoint.getBindingInvocationChain(); + + if(osProvider != null) { + bindingChain.addInterceptor(Phase.SERVICE_BINDING_OPERATION_SELECTOR, osProvider.createInterceptor()); + } + + if (wfProvider != null) { + bindingChain.addInterceptor(Phase.SERVICE_BINDING_WIREFORMAT, wfProvider.createInterceptor()); + } + + } + +} -- cgit v1.2.3