diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-12 06:21:38 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-12 06:21:38 +0000 |
commit | ee14a5168606daa06748d32216535f96e8a7416a (patch) | |
tree | 7341ec21d139be2d67cda33727d5079c93a7ba70 /sca-java-2.x/trunk | |
parent | 0bf4df347d4933da8535f9649c9a095d60700a02 (diff) |
Adding generated eTag to RPC style invocations
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@943389 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk')
2 files changed, 52 insertions, 9 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java index 20878696d4..0eb81cfda5 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java @@ -23,14 +23,15 @@ import java.lang.annotation.Annotation; 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.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; @@ -76,11 +77,16 @@ public class RPCOperationSelectorInterceptor implements Interceptor { 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 ); @@ -92,12 +98,6 @@ public class RPCOperationSelectorInterceptor implements Interceptor { final JavaOperation javaOperation = (JavaOperation)operation; final Method method = javaOperation.getJavaMethod(); - List<DataType> operationInputType = operation.getInputType().getLogical(); - int size = operationInputType.size(); - for (int i = 0; i < size; i++) { - System.out.println(operationInputType.get(i)); - } - List<Object> messageParameters = new ArrayList<Object>(); for(int i=0; i<method.getParameterTypes().length; i++) { for(Annotation annotation : method.getParameterAnnotations()[i]) { @@ -110,7 +110,6 @@ public class RPCOperationSelectorInterceptor implements Interceptor { } else { messageParameters.add(values); } - } } } @@ -121,7 +120,32 @@ public class RPCOperationSelectorInterceptor implements Interceptor { msg.setBody(body); msg.setOperation(operation); - return getNext().invoke(msg); + 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); } diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java index 3dc9f5891d..f810970869 100644 --- a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java +++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPUtil.java @@ -19,6 +19,9 @@ package org.apache.tuscany.sca.common.http; +import java.math.BigInteger; +import java.security.MessageDigest; + import javax.servlet.http.HttpServletRequest; /** @@ -71,4 +74,20 @@ public class HTTPUtil { return contextRoot; } + + public static String calculateHashETag(byte[] content) { + String eTag = "invalid"; + try { + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + byte[] digest = messageDigest.digest(content); + BigInteger number = new BigInteger(1, digest); + StringBuffer sb = new StringBuffer('0'); + sb.append(number.toString(16)); + eTag = sb.toString(); + } catch(Exception e) { + //ignore, we will return random etag + eTag = Integer.toString((new java.util.Random()).nextInt(Integer.MAX_VALUE)); + } + return eTag; + } } |