diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-07 23:48:28 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-07 23:48:28 +0000 |
commit | 36e4e5cbb71849b33da69924169f94e3a5161b19 (patch) | |
tree | 56ccc951915bade478fc6c86aafd4a88ef66eb0b /sca-java-2.x/trunk | |
parent | b897076af57a1a61679d80375ea80d1eea767217 (diff) |
Adding support for declarative http headers in binding.rest
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@942274 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
8 files changed, 203 insertions, 19 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java index 3ba76f5777..d09e197388 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java @@ -24,6 +24,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URLDecoder; import java.text.ParseException; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -31,9 +34,11 @@ 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.HTTPContentTypeMapper; 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; @@ -49,7 +54,7 @@ public class RESTBindingListenerServlet extends HttpServlet { transient private MessageFactory messageFactory; - transient private Binding binding; + transient private RESTBinding binding; transient private Invoker bindingInvoker; private Invoker invoker; @@ -66,7 +71,7 @@ public class RESTBindingListenerServlet extends HttpServlet { * Constructs a new RESTServiceListenerServlet. */ public RESTBindingListenerServlet(Binding binding, Invoker bindingInvoker, MessageFactory messageFactory) { - this.binding = binding; + this.binding = (RESTBinding) binding; this.bindingInvoker = bindingInvoker; this.messageFactory = messageFactory; } @@ -137,10 +142,26 @@ public class RESTBindingListenerServlet extends HttpServlet { // return response to client if (responseMessage.isFault()) { // Turn a fault into an exception - //throw new ServletException((Throwable)responseMessage.getBody()); Throwable e = (Throwable)responseMessage.getBody(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); } else { + + 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()); + } + + } + //handle void operations write(response.getOutputStream(), responseMessage.getBody()); response.getOutputStream().flush(); 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 eb3f361571..c100e264a7 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 @@ -28,6 +28,10 @@ <tuscany:binding.rest uri="http://localhost:8085/Customer"> <tuscany:wireFormat.xml /> <tuscany:operationSelector.jaxrs /> + <tuscany:http-headers> + <tuscany:header name="Cache-Control" value="no-cache"/> + <tuscany:header name="Expires" value="-1"/> + </tuscany:http-headers> </tuscany:binding.rest> </service> </component> diff --git a/sca-java-2.x/trunk/modules/binding-rest/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-rest/META-INF/MANIFEST.MF index 4a8e729539..293d747f2a 100644 --- a/sca-java-2.x/trunk/modules/binding-rest/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/binding-rest/META-INF/MANIFEST.MF @@ -15,6 +15,7 @@ Import-Package: javax.servlet.http, javax.xml.stream,
org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.binding.rest;version="2.0.0",
+ org.apache.tuscany.sca.common.http;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0",
org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
org.apache.tuscany.sca.core;version="2.0.0",
diff --git a/sca-java-2.x/trunk/modules/binding-rest/pom.xml b/sca-java-2.x/trunk/modules/binding-rest/pom.xml index 03858f88b7..6c5ea2e44e 100644 --- a/sca-java-2.x/trunk/modules/binding-rest/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-rest/pom.xml @@ -37,6 +37,12 @@ </dependency> <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-common-http</artifactId> + <version>2.0-SNAPSHOT</version> + </dependency> + + <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/RESTBinding.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/RESTBinding.java index 01dafc80a6..dd01c37537 100644 --- a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/RESTBinding.java +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/RESTBinding.java @@ -19,9 +19,12 @@ package org.apache.tuscany.sca.binding.rest; +import java.util.List; + import javax.xml.namespace.QName; import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.common.http.HTTPHeader; /** * REST binding model. @@ -30,4 +33,6 @@ import org.apache.tuscany.sca.assembly.Binding; */ public interface RESTBinding extends Binding { QName TYPE = new QName(SCA11_TUSCANY_NS, "binding.rest"); + + public List<HTTPHeader> getHttpHeaders(); } diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/impl/RESTBindingImpl.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/impl/RESTBindingImpl.java index 887d0d9878..f6acc6934e 100644 --- a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/impl/RESTBindingImpl.java +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/impl/RESTBindingImpl.java @@ -19,11 +19,15 @@ package org.apache.tuscany.sca.binding.rest.impl; +import java.util.ArrayList; +import java.util.List; + import javax.xml.namespace.QName; import org.apache.tuscany.sca.assembly.OperationSelector; import org.apache.tuscany.sca.assembly.WireFormat; import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.common.http.HTTPHeader; /** @@ -35,6 +39,8 @@ class RESTBindingImpl implements RESTBinding { private String name; private String uri; + + private List<HTTPHeader> httpHeaders = new ArrayList<HTTPHeader>(); private WireFormat wireFormat; private OperationSelector operationSelector; @@ -47,18 +53,22 @@ class RESTBindingImpl implements RESTBinding { return name; } - public String getURI() { - return uri; - } - public void setName(String name) { this.name = name; } + public String getURI() { + return uri; + } + public void setURI(String uri) { this.uri = uri; } + public List<HTTPHeader> getHttpHeaders() { + return this.httpHeaders; + } + public boolean isUnresolved() { return false; } diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessor.java b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessor.java index 46e0696c69..bd6b1dd7a2 100644 --- a/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessor.java +++ b/sca-java-2.x/trunk/modules/binding-rest/src/main/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessor.java @@ -27,10 +27,12 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import org.apache.tuscany.sca.assembly.Base; import org.apache.tuscany.sca.assembly.OperationSelector; import org.apache.tuscany.sca.assembly.WireFormat; import org.apache.tuscany.sca.binding.rest.RESTBinding; import org.apache.tuscany.sca.binding.rest.RESTBindingFactory; +import org.apache.tuscany.sca.common.http.HTTPHeader; import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; @@ -48,7 +50,11 @@ import org.apache.tuscany.sca.core.FactoryExtensionPoint; * @version $Rev$ $Date$ */ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<RESTBinding> { + private static final QName HEADERS_QNAME = new QName(Base.SCA11_TUSCANY_NS, "http-headers"); + private static final QName HEADER_QNAME = new QName(Base.SCA11_TUSCANY_NS, "header"); + private static final String NAME = "name"; + private static final String VALUE = "value"; private static final String URI = "uri"; private RESTBindingFactory httpBindingFactory; @@ -71,49 +77,87 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S } public RESTBinding read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { - RESTBinding httpBinding = httpBindingFactory.createRESTBinding(); - + RESTBinding restBinding = httpBindingFactory.createRESTBinding(); + + /** + * <tuscany:binding.rest uri="http://localhost:8085/Customer"> + * <tuscany:wireFormat.xml /> + * <tuscany:operationSelector.jaxrs /> + * <tuscany:http-headers> + * <tuscany:header name="Cache-Control" value="no-cache"/> + * <tuscany:header name="Expires" value="-1"/> + * </tuscany:http-headers> + * </tuscany:binding.rest> + * + */ while(reader.hasNext()) { QName elementName = null; int event = reader.getEventType(); switch (event) { case START_ELEMENT: elementName = reader.getName(); - - if (RESTBinding.TYPE.equals(elementName)) { + + if(RESTBinding.TYPE.equals(elementName)) { + + // binding attributes String name = getString(reader, NAME); if(name != null) { - httpBinding.setName(name); + restBinding.setName(name); } String uri = getURIString(reader, URI); if (uri != null) { - httpBinding.setURI(uri); + restBinding.setURI(uri); + } + break; + + } else if (HEADERS_QNAME.equals(elementName)) { + + // ignore wrapper element + break; + + } else if (HEADER_QNAME.equals(elementName)) { + + // header name/value pair + String name = getString(reader, NAME); + String value = getURIString(reader, VALUE); + + if(name != null) { + restBinding.getHttpHeaders().add(new HTTPHeader(name, value)); } + break; + } else { // Read an extension element Object extension = extensionProcessor.read(reader, context); if (extension != null) { if (extension instanceof WireFormat) { - httpBinding.setRequestWireFormat((WireFormat)extension); + restBinding.setRequestWireFormat((WireFormat)extension); } else if(extension instanceof OperationSelector) { - httpBinding.setOperationSelector((OperationSelector)extension); + restBinding.setOperationSelector((OperationSelector)extension); } } + break; } - } + + case END_ELEMENT: + elementName = reader.getName(); - if (event == END_ELEMENT && RESTBinding.TYPE.equals(reader.getName())) { - break; + if(RESTBinding.TYPE.equals(elementName)) { + return restBinding; + } + break; } + + // Read the next element if (reader.hasNext()) { reader.next(); } } - return httpBinding; + return restBinding; } public void write(RESTBinding restBinding, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { diff --git a/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessorTestCase.java b/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessorTestCase.java new file mode 100644 index 0000000000..55a539044d --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest/src/test/java/org/apache/tuscany/sca/binding/rest/xml/RESTBindingProcessorTestCase.java @@ -0,0 +1,93 @@ +/* + * 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.xml; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + + +/** + * @version $Rev$ $Date$ + */ +public class RESTBindingProcessorTestCase { + + private static final String COMPOSITE = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:tuscany=\"http://tuscany.apache.org/xmlns/sca/1.1\" targetNamespace=\"http://binding-rest\" name=\"binding-rest\">" + + " <component name=\"CustomerService\">" + + " <implementation.java class=\"services.customer.CustomerServiceImpl\"/>" + + " <service name=\"CustomerService\">" + + " <tuscany:binding.rest uri=\"http://localhost:8085/Customer\">" + + " <tuscany:wireFormat.xml />" + + " <tuscany:operationSelector.jaxrs />" + + " <tuscany:http-headers>" + + " <tuscany:header name=\"Cache-Control\" value=\"no-cache\"/>" + + " <tuscany:header name=\"Expires\" value=\"-1\"/>" + + " </tuscany:http-headers>" + + " </tuscany:binding.rest>" + + " </service>" + + " </component>" + + "</composite>"; + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + inputFactory = XMLInputFactory.newInstance(); + + StAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(extensionPoints); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + } + + /** + * Test parsing valid composite definition. Valid composite populated with correct values expected. + * @throws Exception + */ + @Test + public void testLoadValidComposite() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE)); + + Composite composite = (Composite)staxProcessor.read(reader, context); + RESTBinding binding = (RESTBinding) composite.getComponents().get(0).getServices().get(0).getBindings().get(0); + + Assert.assertNotNull(binding); + Assert.assertEquals(2, binding.getHttpHeaders().size()); + Assert.assertEquals("Cache-Control", binding.getHttpHeaders().get(0).getName()); + Assert.assertEquals("no-cache", binding.getHttpHeaders().get(0).getValue()); + } +} |