From d10dbf4a6cb9a549ba8edca1805592b09e5585b8 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 20 Jun 2012 00:31:38 +0000 Subject: TUSCANY-4065 - Properly retrieving binding context within the response handlers git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1351908 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/provider/ThreadBindingContext.java | 45 ++++++++++++++++++++++ .../binding/rest/provider/TuscanyRESTServlet.java | 8 ++++ .../src/test/resources/customer.composite | 20 +++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/ThreadBindingContext.java diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/ThreadBindingContext.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/ThreadBindingContext.java new file mode 100644 index 0000000000..0c9d070a4b --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/ThreadBindingContext.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.rest.provider; + +import org.apache.tuscany.sca.binding.rest.RESTBinding; + +public class ThreadBindingContext { + + private static final ThreadLocal CONTEXT = new ThreadLocal(); + + private ThreadBindingContext() { + } + + public static RESTBinding setBindingContext(RESTBinding binding) { + RESTBinding old = CONTEXT.get(); + CONTEXT.set(binding); + return old; + } + + public static RESTBinding getBindingContext() { + return CONTEXT.get(); + } + + public static void removeBindingContext() { + CONTEXT.remove(); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java index 35c46ac56b..17756a7f23 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java @@ -94,6 +94,7 @@ public class TuscanyRESTServlet extends RestServlet { try { //store in thread local ThreadHTTPContext.setHTTPContext(bindingContext); + ThreadBindingContext.setBindingContext(binding); // handle special ?wadl request String query = request.getQueryString(); @@ -105,6 +106,7 @@ public class TuscanyRESTServlet extends RestServlet { } finally { //remove ThreadHTTPContext.removeHTTPContext(); + ThreadBindingContext.removeBindingContext(); } } @@ -209,6 +211,10 @@ public class TuscanyRESTServlet extends RestServlet { /** * TuscanyResponseHandler + * + * The response handler is shared, that's why we need to get the + * binding from the thread context otherwise different components + * might get wrong binding configuration (e.g. headers) * * Required to support declarative HTTP Headers */ @@ -222,6 +228,8 @@ public class TuscanyRESTServlet extends RestServlet { return; } + RESTBinding binding = ThreadBindingContext.getBindingContext(); + //process declarative headers for(HTTPHeader header : binding.getHttpHeaders()) { //treat special headers that need to be calculated diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite index 7b33145afd..80636462ad 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite @@ -22,7 +22,24 @@ targetNamespace="http://customer" name="customer"> - + + + + + + + + + + + + + + + + + + @@ -32,6 +49,7 @@ + -- cgit v1.2.3