diff --git a/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodProcessor.java b/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodProcessor.java new file mode 100644 index 00000000..94e68159 --- /dev/null +++ b/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodProcessor.java @@ -0,0 +1,77 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed 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.springframework.ws.server.endpoint.adapter.method; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.core.MethodParameter; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.DOMReader; +import org.dom4j.io.DocumentResult; +import org.dom4j.io.DocumentSource; + +/** + * Implementation of {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} that supports dom4j + * {@linkplain Element elements}. + * + * @author Arjen Poutsma + * @since 2.0 + */ +public class Dom4jPayloadMethodProcessor extends AbstractPayloadMethodProcessor { + + @Override + protected boolean supportsRequestPayloadParameter(MethodParameter parameter) { + return supports(parameter); + } + + @Override + protected Object resolveRequestPayloadArgument(Source requestPayload, MethodParameter parameter) + throws TransformerException { + if (requestPayload instanceof DOMSource) { + org.w3c.dom.Node node = ((DOMSource) requestPayload).getNode(); + if (node.getNodeType() == org.w3c.dom.Node.DOCUMENT_NODE) { + DOMReader domReader = new DOMReader(); + Document document = domReader.read((org.w3c.dom.Document) node); + return document.getRootElement(); + } + } + // we have no other option than to transform + DocumentResult dom4jResult = new DocumentResult(); + transform(requestPayload, dom4jResult); + return dom4jResult.getDocument().getRootElement(); + } + + @Override + protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) { + return supports(returnType); + } + + @Override + protected Source createResponsePayload(MethodParameter returnType, Object returnValue) { + Element returnedNode = (Element) returnValue; + return new DocumentSource(returnedNode); + } + + private boolean supports(MethodParameter parameter) { + return Element.class.equals(parameter.getParameterType()); + } + +} \ No newline at end of file diff --git a/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/DomPayloadMethodProcessor.java b/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/DomPayloadMethodProcessor.java index e35df050..d8069363 100644 --- a/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/DomPayloadMethodProcessor.java +++ b/core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/DomPayloadMethodProcessor.java @@ -78,7 +78,7 @@ public class DomPayloadMethodProcessor extends AbstractPayloadMethodProcessor { @Override protected DOMSource createResponsePayload(MethodParameter returnType, Object returnValue) { - Node returnedNode = (Node) returnValue; + Element returnedNode = (Element) returnValue; return new DOMSource(returnedNode); } diff --git a/core/src/test/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodArgumentResolverTest.java b/core/src/test/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodArgumentResolverTest.java new file mode 100644 index 00000000..cc72a4bd --- /dev/null +++ b/core/src/test/java/org/springframework/ws/server/endpoint/adapter/method/Dom4jPayloadMethodArgumentResolverTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed 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.springframework.ws.server.endpoint.adapter.method; + +import org.springframework.core.MethodParameter; +import org.springframework.ws.server.endpoint.annotation.RequestPayload; +import org.springframework.ws.server.endpoint.annotation.ResponsePayload; + +import org.dom4j.Document; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class Dom4jPayloadMethodArgumentResolverTest extends AbstractPayloadMethodProcessorTest { + + @Override + protected AbstractPayloadMethodProcessor createProcessor() { + return new Dom4jPayloadMethodProcessor(); + } + + @Override + protected MethodParameter[] createSupportedParameters() throws NoSuchMethodException { + return new MethodParameter[]{ + new MethodParameter(getClass().getMethod("element", Element.class), 0)}; + } + + @Override + protected MethodParameter[] createSupportedReturnTypes() throws NoSuchMethodException { + return new MethodParameter[]{new MethodParameter(getClass().getMethod("element", Element.class), -1)}; + } + + @Override + protected void testArgument(Object argument, MethodParameter parameter) { + assertTrue("argument not a node", argument instanceof Element); + Element element = (Element) argument; + assertEquals("Invalid namespace", NAMESPACE_URI, element.getNamespaceURI()); + assertEquals("Invalid local name", LOCAL_NAME, element.getName()); + } + + @Override + protected Element getReturnValue(MethodParameter returnType) { + Document document = DocumentHelper.createDocument(); + return document.addElement(LOCAL_NAME, NAMESPACE_URI); + } + + @ResponsePayload + public Element element(@RequestPayload Element element) { + return element; + } + +} \ No newline at end of file