diff options
Diffstat (limited to '')
3 files changed, 72 insertions, 1 deletions
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<RESTBinding> CONTEXT = new ThreadLocal<RESTBinding>(); + + 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"> - <component name="CustomerService"> + <component name="DummyComponent"> + <implementation.java class="services.customer.CustomerServiceImpl"/> + <service name="CustomerService"> + <tuscany:binding.rest uri="http://localhost:8085/Dummy"> + <tuscany:wireFormat.xml /> + <tuscany:operationSelector.jaxrs /> + <tuscany:http-headers> + <tuscany:header name="Cache-Control" value="no-cache"/> + <tuscany:header name="Expires" value="-1"/> + <tuscany:header name="X-Tuscany" value="dummy"/> + <tuscany:header name="X-TuscanyComponent" value="DummyComponent"/> + </tuscany:http-headers> + </tuscany:binding.rest> + </service> + </component> + + + <component name="CustomerComponent"> <implementation.java class="services.customer.CustomerServiceImpl"/> <service name="CustomerService"> <tuscany:binding.rest uri="http://localhost:8085/Customer"> @@ -32,6 +49,7 @@ <tuscany:header name="Cache-Control" value="no-cache"/> <tuscany:header name="Expires" value="-1"/> <tuscany:header name="X-Tuscany" value="tuscany"/> + <tuscany:header name="X-TuscanyComponent" value="CustomerComponent"/> </tuscany:http-headers> </tuscany:binding.rest> </service> |