diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-03 19:46:13 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-06-03 19:46:13 +0000 |
commit | 9cd4f93ec6e3aca85f803edd21debe29f0df7b7c (patch) | |
tree | 88a894b8e97755024b97d583d6e28d902b51cde3 | |
parent | b3b5154bd66c4e0f34754bd06d4b6abea9ad0794 (diff) |
TUSCANY-3583 - Making sure we recognize the headers configured declaratively in composite file when delegating to JAX-RS runtime
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@951132 13f79535-47bb-0310-9956-ffa450edef68
2 files changed, 46 insertions, 2 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java index 325aeff018..c8089904ae 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingInvoker.java @@ -22,7 +22,10 @@ package org.apache.tuscany.sca.binding.rest.provider; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.net.URI; +import java.util.Calendar; import java.util.Collections; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -52,6 +55,8 @@ import org.apache.tuscany.sca.assembly.WireFormat; import org.apache.tuscany.sca.binding.rest.RESTBinding; import org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat; import org.apache.tuscany.sca.binding.rest.wireformat.xml.XMLWireFormat; +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.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; @@ -222,6 +227,22 @@ public class RESTBindingInvoker implements Invoker { resource.contentType(getContentType()); resource.accept(getAccepts()); + + //handles declarative headers configured on the composite + 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())); + + resource.header("Expires", HTTPCacheContext.RFC822DateFormat.format( calendar.getTime() )); + } else { + //default behaviour to pass the header value to HTTP response + resource.header(header.getName(), header.getValue()); + } + } Object result = resource.invoke(httpMethod, responseType, entity); msg.setBody(result); diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java index 0ee63b1668..1dac8a2c9a 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java @@ -20,6 +20,9 @@ package org.apache.tuscany.sca.binding.rest.provider; import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; import javax.servlet.Servlet; import javax.servlet.ServletConfig; @@ -29,7 +32,10 @@ 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.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; @@ -44,7 +50,7 @@ public class RESTServiceListenerServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = -5543706958107836637L; - transient private Binding binding; + transient private RESTBinding binding; transient private ServletConfig config; transient private MessageFactory messageFactory; transient private Invoker serviceInvoker; @@ -53,7 +59,7 @@ public class RESTServiceListenerServlet extends HttpServlet implements Servlet { * Constructs a new HTTPServiceListenerServlet. */ public RESTServiceListenerServlet(Binding binding, Invoker serviceInvoker, MessageFactory messageFactory) { - this.binding = binding; + this.binding = (RESTBinding) binding; this.serviceInvoker = serviceInvoker; this.messageFactory = messageFactory; } @@ -90,6 +96,23 @@ public class RESTServiceListenerServlet extends HttpServlet implements Servlet { //throw new ServletException((Throwable)responseMessage.getBody()); Throwable e = (Throwable)responseMessage.getBody(); ((HttpServletResponse)response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); + } else { + //handles declarative headers configured on the composite + 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())); + + response.setHeader("Expires", HTTPCacheContext.RFC822DateFormat.format( calendar.getTime() )); + } else { + //default behaviour to pass the header value to HTTP response + response.setHeader(header.getName(), header.getValue()); + } + + } } } } |