From 2adc116ef0a2d281111644c153604543078f7995 Mon Sep 17 00:00:00 2001 From: antelder Date: Thu, 22 Jul 2010 10:30:49 +0000 Subject: Tag 2.0-Beta1-RC1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@966566 13f79535-47bb-0310-9956-ffa450edef68 --- .../JAXRSOperationSelectorInterceptor.java | 256 +++++++++++++++++++++ .../JAXRSOperationSelectorProviderFactory.java | 52 +++++ .../JAXRSOperationSelectorReferenceProvider.java | 50 ++++ .../JAXRSOperationSelectorServiceProvider.java | 57 +++++ .../provider/RPCOperationSelectorInterceptor.java | 203 ++++++++++++++++ .../RPCOperationSelectorProviderFactory.java | 52 +++++ .../RPCOperationSelectorReferenceProvider.java | 50 ++++ .../RPCOperationSelectorServiceProvider.java | 57 +++++ 8 files changed, 777 insertions(+) create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorReferenceProvider.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorServiceProvider.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorProviderFactory.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorReferenceProvider.java create mode 100644 sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorServiceProvider.java (limited to 'sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector') diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java new file mode 100644 index 0000000000..928b80a044 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java @@ -0,0 +1,256 @@ +/* + * 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.operationselector.jaxrs.provider; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.StringWriter; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.util.List; + +import javax.activation.DataSource; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; + +import org.apache.tuscany.sca.common.http.HTTPContext; +import org.apache.tuscany.sca.common.http.HTTPUtil; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +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.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * JAXRS operation selector Interceptor. + * + * @version $Rev$ $Date$ +*/ +public class JAXRSOperationSelectorInterceptor implements Interceptor { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpoint endpoint; + + private RuntimeComponentService service; + private InterfaceContract interfaceContract; + private List serviceOperations; + + private Invoker next; + + public JAXRSOperationSelectorInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + this.endpoint = endpoint; + + this.service = (RuntimeComponentService)endpoint.getService(); + this.interfaceContract = service.getInterfaceContract(); + this.serviceOperations = service.getInterfaceContract().getInterface().getOperations(); + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + public Message invoke(Message msg) { + try { + HTTPContext bindingContext = (HTTPContext)msg.getBindingContext(); + + // By-pass the operation selector + if (bindingContext == null) { + return getNext().invoke(msg); + } + + String path = URLDecoder.decode(HTTPUtil.getRequestPath(bindingContext.getHttpRequest()), "UTF-8"); + + if (path.startsWith("/")) { + path = path.substring(1); + } + + List operations = + filterOperationsByHttpMethod(interfaceContract, bindingContext.getHttpRequest().getMethod()); + + Operation operation = findOperation(path, operations); + + final JavaOperation javaOperation = (JavaOperation)operation; + final Method method = javaOperation.getJavaMethod(); + + if (path != null && path.length() > 0) { + if (method.getAnnotation(Path.class) != null) { + msg.setBody(new Object[] {path}); + } + } + + // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity parameter + Class[] paramTypes = method.getParameterTypes(); + if (paramTypes.length == 1) { + Class type = paramTypes[0]; + InputStream is = (InputStream)((Object[])msg.getBody())[0]; + Object target = convert(is, bindingContext.getHttpRequest().getContentType(), type); + msg.setBody(new Object[] {target}); + } else if (paramTypes.length == 0) { + msg.setBody(null); + } + + msg.setOperation(operation); + + return getNext().invoke(msg); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private Object convert(InputStream content, String contentType, Class type) { + if (type == DataSource.class) { + return type.cast(new InputStreamDataSource(content, contentType)); + } else if (type == InputStream.class) { + return type.cast(content); + } else if (type == String.class) { + try { + StringWriter sw = new StringWriter(); + InputStreamReader reader = new InputStreamReader(content, "UTF-8"); + char[] buf = new char[8192]; + while (true) { + int size = reader.read(buf); + if (size < 0) { + break; + } + sw.write(buf, 0, size); + } + return type.cast(sw.toString()); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } else if (type == byte[].class) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buf = new byte[8192]; + while (true) { + int size = content.read(buf); + if (size < 0) { + break; + } + bos.write(buf, 0, size); + } + return type.cast(bos.toByteArray()); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } else { + return content; + } + } + + public static final class InputStreamDataSource implements DataSource { + + public static final String DEFAULT_TYPE = "application/octet-stream"; + + private final InputStream in; + private final String ctype; + + public InputStreamDataSource(InputStream in) { + this(in, null); + } + + public InputStreamDataSource(InputStream in, String ctype) { + this.in = in; + this.ctype = (ctype != null) ? ctype : DEFAULT_TYPE; + } + + public String getContentType() { + return ctype; + } + + public String getName() { + return null; + } + + public InputStream getInputStream() throws IOException { + return in; + } + + public OutputStream getOutputStream() throws IOException { + return null; + } + + } + + /** + * Find the operation from the component service contract + * @param componentService + * @param http_method + * @return + */ + private static List filterOperationsByHttpMethod(InterfaceContract interfaceContract, String http_method) { + List operations = null; + + if (http_method.equalsIgnoreCase("get")) { + operations = (List)interfaceContract.getInterface().getAttributes().get(GET.class); + } else if (http_method.equalsIgnoreCase("put")) { + operations = (List)interfaceContract.getInterface().getAttributes().get(PUT.class); + } else if (http_method.equalsIgnoreCase("post")) { + operations = (List)interfaceContract.getInterface().getAttributes().get(POST.class); + } else if (http_method.equalsIgnoreCase("delete")) { + operations = (List)interfaceContract.getInterface().getAttributes().get(DELETE.class); + } + + return operations; + } + + /** + * Find the operation from the component service contract + * @param componentService + * @param http_method + * @return + */ + private Operation findOperation(String path, List operations) { + Operation operation = null; + + for (Operation op : operations) { + final JavaOperation javaOperation = (JavaOperation)op; + final Method method = javaOperation.getJavaMethod(); + + if (path != null && path.length() > 0) { + if (method.getAnnotation(Path.class) != null) { + operation = op; + break; + } + } else { + if (method.getAnnotation(Path.class) == null) { + operation = op; + break; + } + } + } + + return operation; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorProviderFactory.java new file mode 100644 index 0000000000..d057f1a852 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorProviderFactory.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.operationselector.jaxrs.provider; + +import org.apache.tuscany.sca.binding.rest.operationselector.jaxrs.JAXRSOperationSelector; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +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; + +/** + * JAXRS operation selector Provider Factory. + * + * @version $Rev$ $Date$ +*/ +public class JAXRSOperationSelectorProviderFactory implements OperationSelectorProviderFactory{ + private ExtensionPointRegistry extensionPoints; + + public JAXRSOperationSelectorProviderFactory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + } + public OperationSelectorProvider createReferenceOperationSelectorProvider(RuntimeEndpointReference endpointReference) { + return new JAXRSOperationSelectorReferenceProvider(extensionPoints, endpointReference); + } + + public OperationSelectorProvider createServiceOperationSelectorProvider(RuntimeEndpoint endpoint) { + return new JAXRSOperationSelectorServiceProvider(extensionPoints, endpoint); + } + + public Class getModelType() { + return JAXRSOperationSelector.class; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorReferenceProvider.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorReferenceProvider.java new file mode 100644 index 0000000000..c560a13ae2 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorReferenceProvider.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 org.apache.tuscany.sca.binding.rest.operationselector.jaxrs.provider; + +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.runtime.RuntimeEndpointReference; + +/** + * JAXRS operation selector Reference Provider. + * + * @version $Rev$ $Date$ +*/ +public class JAXRSOperationSelectorReferenceProvider implements OperationSelectorProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpointReference endpointReference; + + public JAXRSOperationSelectorReferenceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpointReference endpointReference ) { + this.extensionPoints = extensionPoints; + this.endpointReference = endpointReference; + } + + public Interceptor createInterceptor() { + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_OPERATION_SELECTOR; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorServiceProvider.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorServiceProvider.java new file mode 100644 index 0000000000..105c84ebda --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorServiceProvider.java @@ -0,0 +1,57 @@ +/* + * 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.operationselector.jaxrs.provider; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.rest.operationselector.jaxrs.JAXRSOperationSelector; +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.runtime.RuntimeEndpoint; + +/** + * JAXRS operation selector Service Provider. + * + * @version $Rev$ $Date$ +*/ +public class JAXRSOperationSelectorServiceProvider implements OperationSelectorProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpoint endpoint; + + private Binding binding; + + public JAXRSOperationSelectorServiceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + this.endpoint = endpoint; + this.binding = endpoint.getBinding(); + } + + public Interceptor createInterceptor() { + if(binding.getOperationSelector() != null && binding.getOperationSelector() instanceof JAXRSOperationSelector) { + return new JAXRSOperationSelectorInterceptor(extensionPoints, endpoint); + } + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_OPERATION_SELECTOR; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java new file mode 100644 index 0000000000..0df6e5e0fb --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java @@ -0,0 +1,203 @@ +/* + * 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.operationselector.rpc.provider; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.QueryParam; + +import org.apache.tuscany.sca.common.http.HTTPContext; +import org.apache.tuscany.sca.common.http.HTTPUtil; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.databinding.SimpleTypeMapper; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +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.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * RPC operation selector Interceptor. + * + * @version $Rev$ $Date$ +*/ +public class RPCOperationSelectorInterceptor implements Interceptor { + private ExtensionPointRegistry extensionPoints; + private SimpleTypeMapper simpleTypeMapper; + + private RuntimeEndpoint endpoint; + + private RuntimeComponentService service; + private InterfaceContract interfaceContract; + private List serviceOperations; + + private Invoker next; + + public RPCOperationSelectorInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + + UtilityExtensionPoint utilityExtensionPoint = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + this.simpleTypeMapper = utilityExtensionPoint.getUtility(SimpleTypeMapper.class); + + this.endpoint = endpoint; + + this.service = (RuntimeComponentService)endpoint.getService(); + this.interfaceContract = service.getInterfaceContract(); + this.serviceOperations = service.getInterfaceContract().getInterface().getOperations(); + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + public Message invoke(Message msg) { + try { + HTTPContext bindingContext = (HTTPContext)msg.getBindingContext(); + + if(! "get".equalsIgnoreCase(bindingContext.getHttpRequest().getMethod())) { + throw new RuntimeException("RPC Invocation only allowed over HTTP GET operations"); + } + + String path = URLDecoder.decode(HTTPUtil.getRequestPath(bindingContext.getHttpRequest()), "UTF-8"); + + if (path.startsWith("/")) { + path = path.substring(1); + } + + + String operationName = bindingContext.getHttpRequest().getParameter("method"); + Operation operation = findOperation( operationName ); + + if (operation == null) { + throw new RuntimeException("Invalid Operation '" + operationName + "'" ); + } + + final JavaOperation javaOperation = (JavaOperation)operation; + final Method method = javaOperation.getJavaMethod(); + + List messageParameters = new ArrayList(); + for(int i=0; i clazz = method.getParameterTypes()[i]; + TypeInfo typeInfo = simpleTypeMapper.getXMLType(clazz); + Object v = simpleTypeMapper.toJavaObject(typeInfo.getQName(), values[0], null); + messageParameters.add(v); + } else { + //process value, making necessary map from string to expected value + Class clazz = (method.getParameterTypes()[i]).getComponentType(); + TypeInfo typeInfo = simpleTypeMapper.getXMLType(clazz); + + + Object objectArray = Array.newInstance(clazz, values.length); + for (int count = 0; count < values.length; ++count) { + Object v = simpleTypeMapper.toJavaObject(typeInfo.getQName(), values[count], null); + Array.set(objectArray, count, v); + } + + messageParameters.add(objectArray); + } + } + } + } + + Object[] body = new Object[messageParameters.size()]; + messageParameters.toArray(body); + + msg.setBody(body); + msg.setOperation(operation); + + Message responseMessage = getNext().invoke(msg); + + //set Cache-Control to no-cache to avoid intermediary + //proxy/reverse-proxy caches and always hit the server + //that would identify if the value was current or not + bindingContext.getHttpResponse().setHeader("Cache-Control", "no-cache"); + bindingContext.getHttpResponse().setHeader("Expires", new Date(0).toGMTString()); + + + String eTag = HTTPUtil.calculateHashETag(responseMessage.getBody().toString().getBytes("UTF-8")); + + // Test request for predicates. + String predicate = bindingContext.getHttpRequest().getHeader( "If-Match" ); + if (( predicate != null ) && ( !predicate.equals(eTag) )) { + // No match, should short circuit + bindingContext.getHttpResponse().sendError(HttpServletResponse.SC_PRECONDITION_FAILED); + } + predicate = bindingContext.getHttpRequest().getHeader( "If-None-Match" ); + if (( predicate != null ) && ( predicate.equals(eTag) )) { + // Match, should short circuit + bindingContext.getHttpResponse().sendError(HttpServletResponse.SC_NOT_MODIFIED); + } + + bindingContext.getHttpResponse().addHeader("ETag", eTag); + + return responseMessage; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Find the operation from the component service contract + * @param componentService + * @param method + * @return + */ + private Operation findOperation(String method) { + if (method.contains(".")) { + method = method.substring(method.lastIndexOf(".") + 1); + } + + List operations = endpoint.getComponentServiceInterfaceContract().getInterface().getOperations(); + + Operation result = null; + for (Operation o : operations) { + if (o.getName().equalsIgnoreCase(method)) { + result = o; + break; + } + } + + return result; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorProviderFactory.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorProviderFactory.java new file mode 100644 index 0000000000..011e89e7cc --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorProviderFactory.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.operationselector.rpc.provider; + +import org.apache.tuscany.sca.binding.rest.operationselector.rpc.RPCOperationSelector; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +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; + +/** + * RPC operation selector Provider Factory. + * + * @version $Rev$ $Date$ +*/ +public class RPCOperationSelectorProviderFactory implements OperationSelectorProviderFactory{ + private ExtensionPointRegistry extensionPoints; + + public RPCOperationSelectorProviderFactory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + } + public OperationSelectorProvider createReferenceOperationSelectorProvider(RuntimeEndpointReference endpointReference) { + return new RPCOperationSelectorReferenceProvider(extensionPoints, endpointReference); + } + + public OperationSelectorProvider createServiceOperationSelectorProvider(RuntimeEndpoint endpoint) { + return new RPCOperationSelectorServiceProvider(extensionPoints, endpoint); + } + + public Class getModelType() { + return RPCOperationSelector.class; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorReferenceProvider.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorReferenceProvider.java new file mode 100644 index 0000000000..c87b2fc21d --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorReferenceProvider.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 org.apache.tuscany.sca.binding.rest.operationselector.rpc.provider; + +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.runtime.RuntimeEndpointReference; + +/** + * RPC operation selector Reference Provider. + * + * @version $Rev$ $Date$ +*/ +public class RPCOperationSelectorReferenceProvider implements OperationSelectorProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpointReference endpointReference; + + public RPCOperationSelectorReferenceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpointReference endpointReference ) { + this.extensionPoints = extensionPoints; + this.endpointReference = endpointReference; + } + + public Interceptor createInterceptor() { + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_OPERATION_SELECTOR; + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorServiceProvider.java b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorServiceProvider.java new file mode 100644 index 0000000000..e4a003d4b5 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta1-RC1/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorServiceProvider.java @@ -0,0 +1,57 @@ +/* + * 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.operationselector.rpc.provider; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.rest.operationselector.rpc.RPCOperationSelector; +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.runtime.RuntimeEndpoint; + +/** + * RPC operation selector Service Provider. + * + * @version $Rev$ $Date$ +*/ +public class RPCOperationSelectorServiceProvider implements OperationSelectorProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpoint endpoint; + + private Binding binding; + + public RPCOperationSelectorServiceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + this.endpoint = endpoint; + this.binding = endpoint.getBinding(); + } + + public Interceptor createInterceptor() { + if(binding.getOperationSelector() != null && binding.getOperationSelector() instanceof RPCOperationSelector) { + return new RPCOperationSelectorInterceptor(extensionPoints, endpoint); + } + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_OPERATION_SELECTOR; + } +} -- cgit v1.2.3