From b3243c64346cda94f30f9c7ab6a25b32a666e09c Mon Sep 17 00:00:00 2001 From: lresende Date: Fri, 9 Jul 2010 02:22:14 +0000 Subject: TUSCANY-3617 - Adding support for configuring wireFormat only for response to allow RPC over GET services to define the wireFormat to use for generating the response payload. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@962390 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/binding/rest/impl/RESTBindingImpl.java | 33 +++++---- .../sca/binding/rest/xml/RESTBindingProcessor.java | 86 +++++++++++++++------- 2 files changed, 77 insertions(+), 42 deletions(-) (limited to 'sca-java-2.x/trunk/modules/binding-rest/src/main/java') 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 f6acc6934e..93b59de5a4 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 @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.binding.rest.impl; @@ -32,18 +32,19 @@ import org.apache.tuscany.sca.common.http.HTTPHeader; /** * Implementation of the HTTP binding model. - * + * * @version $Rev$ $Date$ */ class RESTBindingImpl implements RESTBinding { private String name; private String uri; - - private List httpHeaders = new ArrayList(); - private WireFormat wireFormat; - private OperationSelector operationSelector; + private List httpHeaders = new ArrayList(); + + private WireFormat requestWireFormat; + private WireFormat responseWireFormat; + private OperationSelector operationSelector; public QName getType() { return TYPE; @@ -68,7 +69,7 @@ class RESTBindingImpl implements RESTBinding { public List getHttpHeaders() { return this.httpHeaders; } - + public boolean isUnresolved() { return false; } @@ -80,20 +81,20 @@ class RESTBindingImpl implements RESTBinding { // Wireformat and Operation selection public WireFormat getRequestWireFormat() { - return wireFormat; + return requestWireFormat; } public void setRequestWireFormat(WireFormat wireFormat) { - this.wireFormat = wireFormat; + this.requestWireFormat = wireFormat; } public WireFormat getResponseWireFormat() { - return wireFormat; + return responseWireFormat; } public void setResponseWireFormat(WireFormat wireFormat) { - this.wireFormat = wireFormat; - } + this.responseWireFormat = wireFormat; + } public OperationSelector getOperationSelector() { return operationSelector; @@ -101,10 +102,10 @@ class RESTBindingImpl implements RESTBinding { public void setOperationSelector(OperationSelector operationSelector) { this.operationSelector = operationSelector; - } + } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); - } + } } 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 bd6b1dd7a2..81463b2889 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 @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.binding.rest.xml; @@ -46,12 +46,13 @@ import org.apache.tuscany.sca.core.FactoryExtensionPoint; /** * REST Binding Artifact Processor - * + * * @version $Rev$ $Date$ */ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { 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 QName RESPONSE_QNAME = new QName(Base.SCA11_TUSCANY_NS, "response"); private static final String NAME = "name"; private static final String VALUE = "value"; @@ -60,7 +61,7 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S private RESTBindingFactory httpBindingFactory; private StAXArtifactProcessor extensionProcessor; - public RESTBindingProcessor(ExtensionPointRegistry extensionPoints, + public RESTBindingProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor extensionProcessor, StAXAttributeProcessor extensionAttributeProcessor) { FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); @@ -81,12 +82,15 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S /** * - * - * - * - * - * - * + * + * + * + * + * + * + * + * + * * * */ @@ -96,9 +100,9 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S switch (event) { case START_ELEMENT: elementName = reader.getName(); - + if(RESTBinding.TYPE.equals(elementName)) { - + // binding attributes String name = getString(reader, NAME); if(name != null) { @@ -110,36 +114,61 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S 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 if (RESPONSE_QNAME.equals(elementName)) { + + // skip response + reader.next(); + // and position to the next start_element event + while (reader.hasNext()) { + int sub_event = reader.getEventType(); + switch (sub_event) { + case START_ELEMENT: + elementName = reader.getName(); + break; + default: reader.next(); + } + break; + } + + // dispatch to read wire format for the response + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + if (extension instanceof WireFormat) { + restBinding.setResponseWireFormat((WireFormat)extension); + } + } + break; } else { // Read an extension element Object extension = extensionProcessor.read(reader, context); if (extension != null) { if (extension instanceof WireFormat) { restBinding.setRequestWireFormat((WireFormat)extension); + restBinding.setResponseWireFormat((WireFormat)extension); } else if(extension instanceof OperationSelector) { restBinding.setOperationSelector((OperationSelector)extension); } } break; } - + case END_ELEMENT: elementName = reader.getName(); @@ -149,7 +178,7 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S break; } - + // Read the next element if (reader.hasNext()) { @@ -161,7 +190,6 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S } public void write(RESTBinding restBinding, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { - //writer.writeStartElement(Constants.SCA10_NS, BINDING_HTTP); writeStart(writer, RESTBinding.TYPE.getNamespaceURI(), RESTBinding.TYPE.getLocalPart()); @@ -174,24 +202,30 @@ public class RESTBindingProcessor extends BaseStAXArtifactProcessor implements S if (restBinding.getURI() != null) { writer.writeAttribute(URI, restBinding.getURI()); } - + // Write operation selectors if ( restBinding.getOperationSelector() != null ) { extensionProcessor.write(restBinding.getOperationSelector(), writer, context); } - + // Write wire formats if ( restBinding.getRequestWireFormat() != null ) { extensionProcessor.write(restBinding.getRequestWireFormat(), writer, context); } + if ( restBinding.getResponseWireFormat() != null && restBinding.getRequestWireFormat() != restBinding.getResponseWireFormat()) { + writeStart(writer, RESPONSE_QNAME.getNamespaceURI(), RESPONSE_QNAME.getLocalPart()); + extensionProcessor.write(restBinding.getResponseWireFormat(), writer, context); + writeEnd(writer); + } + + writeEnd(writer); - //writer.writeEndElement(); } public void resolve(RESTBinding model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { - // Should not need to do anything here for now... + // Should not need to do anything here for now... } -- cgit v1.2.3