diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules')
4 files changed, 96 insertions, 9 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF index 79141569a0..4ea6d9618b 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF @@ -51,4 +51,5 @@ Import-Package: javax.jws, Bundle-SymbolicName: org.apache.tuscany.sca.binding.rest.runtime Bundle-DocURL: http://www.apache.org/ Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 +Require-Bundle: org.apache.wink.server diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java index eaf830ceb5..ec607e9e13 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java @@ -266,7 +266,7 @@ public class RESTServiceBindingProvider implements EndpointProvider { if (isJAXRS) { application = new SimpleApplication(interfaze); - TuscanyRESTServlet restServlet = new TuscanyRESTServlet(extensionPoints, application.resourceClass); + TuscanyRESTServlet restServlet = new TuscanyRESTServlet(extensionPoints, binding, application.resourceClass); servletMapping = registerServlet(restServlet); 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 f40f69b218..fb9c245f75 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 @@ -20,24 +20,43 @@ package org.apache.tuscany.sca.binding.rest.provider; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; import java.util.Enumeration; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; +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 javax.ws.rs.HttpMethod; +import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerFactory; -import javax.xml.validation.SchemaFactory; +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.HTTPHeader; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.extensibility.ClassLoaderContext; import org.apache.wink.common.internal.registry.ProvidersRegistry; import org.apache.wink.common.internal.registry.metadata.MethodMetadata; +import org.apache.wink.server.handlers.HandlersChain; +import org.apache.wink.server.handlers.HandlersFactory; +import org.apache.wink.server.handlers.MessageContext; +import org.apache.wink.server.handlers.RequestHandler; +import org.apache.wink.server.handlers.ResponseHandler; import org.apache.wink.server.internal.DeploymentConfiguration; import org.apache.wink.server.internal.RequestProcessor; +import org.apache.wink.server.internal.handlers.CheckLocationHeaderHandler; import org.apache.wink.server.internal.registry.ResourceRecord; import org.apache.wink.server.internal.servlet.RestServlet; @@ -45,14 +64,18 @@ import org.apache.wink.server.internal.servlet.RestServlet; * */ public class TuscanyRESTServlet extends RestServlet { + private static final Logger logger = Logger.getLogger(TuscanyRESTServlet.class.getName()); + private static final long serialVersionUID = 89997233133964915L; private ExtensionPointRegistry registry; + private RESTBinding binding; private Class<?> resourceClass; private boolean fixed; - public TuscanyRESTServlet(ExtensionPointRegistry registry, Class<?> resourceClass) { + public TuscanyRESTServlet(ExtensionPointRegistry registry, Binding binding, Class<?> resourceClass) { super(); this.registry = registry; + this.binding = (RESTBinding) binding; this.resourceClass = resourceClass; } @@ -70,7 +93,16 @@ public class TuscanyRESTServlet extends RestServlet { } } } + + /** + * Create Tuscany own DeploymentConfiguration in order to be able to + * add ResponseHandler to the Wink HandlerChain + */ + public DeploymentConfiguration createDeploymentConfiguration() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + return new TuscanyDeploymentConfiguration(); + } + @SuppressWarnings({"unchecked", "rawtypes"}) @Override public DeploymentConfiguration getDeploymentConfiguration() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { @@ -117,7 +149,7 @@ public class TuscanyRESTServlet extends RestServlet { ProvidersRegistry providers = config.getProvidersRegistry(); providers.addProvider(new DataBindingJAXRSReader(registry), 0.001, true); providers.addProvider(new DataBindingJAXRSWriter(registry), 0.001, true); - + return config; } @@ -162,5 +194,59 @@ public class TuscanyRESTServlet extends RestServlet { } return processor; } + + /** + * TuscanyDeploymentConfiguration + * + * Required to inject TuscanyResponseHandler to the HandlerChain + */ + class TuscanyDeploymentConfiguration extends DeploymentConfiguration { + + @Override + protected List<ResponseHandler> initResponseUserHandlers() { + List<ResponseHandler> list = super.initResponseUserHandlers(); + list.add(new TuscanyResponseHandler()); + return list; + } + } + + /** + * TuscanyResponseHandler + * + * Required to support declartive HTTP Headers + */ + class TuscanyResponseHandler implements ResponseHandler { + public void handleResponse(MessageContext context, HandlersChain chain) throws Throwable { + + // assert response is not committed + final HttpServletResponse httpResponse = context.getAttribute(HttpServletResponse.class); + if (httpResponse.isCommitted()) { + logger.log(Level.FINE, "The response is already committed. Nothing to do."); + return; + } + + //process declarative headers + for(HTTPHeader header : binding.getHttpHeaders()) { + //treat special headers that need to be calculated + if(header.getName().equalsIgnoreCase("Expires")) { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTime(new Date()); + + calendar.add(Calendar.HOUR, Integer.parseInt(header.getValue())); + + httpResponse.setHeader("Expires", HTTPCacheContext.RFC822DateFormat.format( calendar.getTime() )); + } else { + //default behaviour to pass the header value to HTTP response + httpResponse.setHeader(header.getName(), header.getValue()); + } + } + + chain.doChain(context); + } + + public void init(Properties props) { + + } + } } diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java index a7cbda20f4..c6e487596b 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java @@ -88,8 +88,8 @@ public class CustomerServiceTestCase { //System.out.println(">>>" + response.getText()); Assert.assertEquals(200, response.getResponseCode()); -// Assert.assertEquals("no-cache", response.getHeaderField("Cache-Control")); -// Assert.assertEquals("tuscany", response.getHeaderField("X-Tuscany")); + Assert.assertEquals("no-cache", response.getHeaderField("Cache-Control")); + Assert.assertEquals("tuscany", response.getHeaderField("X-Tuscany")); Assert.assertEquals(GET_RESPONSE, response.getText()); } |