SWS-351 - Arbitrary parameter injection for @Endpoints

This commit is contained in:
Arjen Poutsma
2010-04-28 08:31:15 +00:00
parent e8685ce247
commit 51bd828dc2
6 changed files with 151 additions and 11 deletions

View File

@@ -66,8 +66,8 @@ public class Dom4jPayloadMethodProcessor extends AbstractPayloadMethodProcessor
@Override
protected Source createResponsePayload(MethodParameter returnType, Object returnValue) {
Element returnedNode = (Element) returnValue;
return new DocumentSource(returnedNode);
Element returnedElement = (Element) returnValue;
return new DocumentSource(returnedElement);
}
private boolean supports(MethodParameter parameter) {

View File

@@ -78,8 +78,8 @@ public class DomPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
@Override
protected DOMSource createResponsePayload(MethodParameter returnType, Object returnValue) {
Element returnedNode = (Element) returnValue;
return new DOMSource(returnedNode);
Element returnedElement = (Element) returnValue;
return new DOMSource(returnedElement);
}
private boolean supports(MethodParameter parameter) {

View File

@@ -0,0 +1,79 @@
/*
* 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.dom.DOMSource;
import org.springframework.core.MethodParameter;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;
import org.w3c.dom.Node;
/**
* Implementation of {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} that supports JDOM
* {@linkplain Element elements}.
*
* @author Arjen Poutsma
* @since 2.0
*/
public class JDomPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
@Override
protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
return supports(parameter);
}
@Override
protected Element resolveRequestPayloadArgument(Source requestPayload, MethodParameter parameter) throws Exception {
if (requestPayload instanceof DOMSource) {
Node node = ((DOMSource) requestPayload).getNode();
DOMBuilder domBuilder = new DOMBuilder();
if (node.getNodeType() == Node.ELEMENT_NODE) {
return domBuilder.build((org.w3c.dom.Element) node);
}
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
Document document = domBuilder.build((org.w3c.dom.Document) node);
return document.getRootElement();
}
}
// we have no other option than to transform
JDOMResult jdomResult = new JDOMResult();
transform(requestPayload, jdomResult);
return jdomResult.getDocument().getRootElement();
}
@Override
protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
return supports(returnType);
}
@Override
protected Source createResponsePayload(MethodParameter returnType, Object returnValue) {
Element returnedElement = (Element) returnValue;
return new JDOMSource(returnedElement);
}
private boolean supports(MethodParameter parameter) {
return Element.class.equals(parameter.getParameterType());
}
}

View File

@@ -27,7 +27,7 @@ import org.dom4j.Element;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class Dom4jPayloadMethodArgumentResolverTest extends AbstractPayloadMethodProcessorTest {
public class Dom4jPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
@@ -47,7 +47,7 @@ public class Dom4jPayloadMethodArgumentResolverTest extends AbstractPayloadMetho
@Override
protected void testArgument(Object argument, MethodParameter parameter) {
assertTrue("argument not a node", argument instanceof Element);
assertTrue("argument not a element", argument instanceof Element);
Element element = (Element) argument;
assertEquals("Invalid namespace", NAMESPACE_URI, element.getNamespaceURI());
assertEquals("Invalid local name", LOCAL_NAME, element.getName());

View File

@@ -26,7 +26,6 @@ import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -51,10 +50,10 @@ public class DomPayloadMethodProcessorTest extends AbstractPayloadMethodProcesso
@Override
protected void testArgument(Object argument, MethodParameter parameter) {
assertTrue("argument not a node", argument instanceof Node);
Node node = (Node) argument;
assertEquals("Invalid namespace", NAMESPACE_URI, node.getNamespaceURI());
assertEquals("Invalid local name", LOCAL_NAME, node.getLocalName());
assertTrue("argument not a element", argument instanceof Element);
Element element = (Element) argument;
assertEquals("Invalid namespace", NAMESPACE_URI, element.getNamespaceURI());
assertEquals("Invalid local name", LOCAL_NAME, element.getLocalName());
}
@Override

View File

@@ -0,0 +1,62 @@
/*
* 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.jdom.Element;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class JDomPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
return new JDomPayloadMethodProcessor();
}
@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 element", argument instanceof Element);
Element node = (Element) argument;
assertEquals("Invalid namespace", NAMESPACE_URI, node.getNamespaceURI());
assertEquals("Invalid local name", LOCAL_NAME, node.getName());
}
@Override
protected Element getReturnValue(MethodParameter returnType) {
return new Element(LOCAL_NAME, NAMESPACE_URI);
}
@ResponsePayload
public Element element(@RequestPayload Element element) {
return element;
}
}