diff options
2 files changed, 65 insertions, 47 deletions
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 061d615d3e..9e837485fb 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 @@ -31,12 +31,15 @@ import java.util.logging.Logger; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.binding.rest.RESTBinding; import org.apache.tuscany.sca.common.http.HTTPCacheContext; +import org.apache.tuscany.sca.common.http.HTTPContext; import org.apache.tuscany.sca.common.http.HTTPHeader; +import org.apache.tuscany.sca.common.http.ThreadHTTPContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.extensibility.ClassLoaderContext; import org.apache.wink.common.internal.registry.ProvidersRegistry; @@ -56,7 +59,6 @@ public class TuscanyRESTServlet extends RestServlet { private ExtensionPointRegistry registry; private RESTBinding binding; private Class<?> resourceClass; - private boolean fixed; public TuscanyRESTServlet(ExtensionPointRegistry registry, Binding binding, Class<?> resourceClass) { super(); @@ -65,6 +67,25 @@ public class TuscanyRESTServlet extends RestServlet { this.resourceClass = resourceClass; } + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //create context + HTTPContext bindingContext = new HTTPContext(); + bindingContext.setHttpRequest(request); + bindingContext.setHttpResponse(response); + + + try { + //store in thread local + ThreadHTTPContext.setHTTPContext(bindingContext); + super.service(request, response); + } finally { + //remove + ThreadHTTPContext.removeHTTPContext(); + } + } + + public void init() throws ServletException { ClassLoader cl = ClassLoaderContext.setContextClassLoader(Thread.currentThread().getContextClassLoader(), @@ -134,54 +155,10 @@ public class TuscanyRESTServlet extends RestServlet { return config; } - /* - private synchronized void fixMediaTypes(DeploymentConfiguration config) { - if (fixed) { - return; - } - // FIXME: A hacky workaround for https://issues.apache.org/jira/browse/TUSCANY-3572 - ResourceRecord record = config.getResourceRegistry().getRecord(resourceClass); - - for (MethodMetadata methodMetadata : record.getMetadata().getResourceMethods()) { - String method = methodMetadata.getHttpMethod(); - if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { - methodMetadata.addConsumes(MediaType.APPLICATION_OCTET_STREAM_TYPE); - methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); - } - if (HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { - methodMetadata.addProduces(MediaType.APPLICATION_OCTET_STREAM_TYPE); - methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); - } - } - for (MethodMetadata methodMetadata : record.getMetadata().getSubResourceMethods()) { - String method = methodMetadata.getHttpMethod(); - if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { - methodMetadata.addConsumes(MediaType.APPLICATION_OCTET_STREAM_TYPE); - methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); - } - if (HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { - methodMetadata.addProduces(MediaType.APPLICATION_OCTET_STREAM_TYPE); - methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); - } - } - fixed = true; - } - - @Override - public RequestProcessor getRequestProcessor() { - RequestProcessor processor = super.getRequestProcessor(); - // The 1st call returns null - if (processor != null) { - fixMediaTypes(processor.getConfiguration()); - } - return processor; - } - */ - /** * TuscanyResponseHandler * - * Required to support declartive HTTP Headers + * Required to support declarative HTTP Headers */ class TuscanyResponseHandler implements ResponseHandler { public void handleResponse(MessageContext context, HandlersChain chain) throws Throwable { @@ -216,5 +193,4 @@ public class TuscanyRESTServlet extends RestServlet { } } - } diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/ThreadHTTPContext.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/ThreadHTTPContext.java new file mode 100644 index 0000000000..fb1206f005 --- /dev/null +++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/ThreadHTTPContext.java @@ -0,0 +1,42 @@ +/* + * 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.common.http; + +public class ThreadHTTPContext { + + private static final ThreadLocal<HTTPContext> CONTEXT = new ThreadLocal<HTTPContext>(); + + private ThreadHTTPContext() { + } + + public static HTTPContext setHTTPContext(HTTPContext context) { + HTTPContext old = CONTEXT.get(); + CONTEXT.set(context); + return old; + } + + public static HTTPContext getHTTPContext() { + return CONTEXT.get(); + } + + public static void removeHTTPContext() { + CONTEXT.remove(); + } +} |