Refactored QNameUtils.getQNameForSource() into separate helper class to fix code tangle.
This commit is contained in:
@@ -24,7 +24,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.server.EndpointMapping;
|
||||
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.springframework.ws.server.endpoint.mapping.support.PayloadRootUtils;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link EndpointMapping} interface that uses the {@link PayloadRoot} annotation to map methods
|
||||
@@ -53,7 +53,8 @@ public class PayloadRootAnnotationMethodEndpointMapping extends AbstractAnnotati
|
||||
}
|
||||
|
||||
protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
|
||||
QName qName = QNameUtils.getQNameForSource(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
QName qName = PayloadRootUtils
|
||||
.getPayloadRootQName(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
return qName != null ? qName.toString() : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.ws.soap.server.endpoint.mapping;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
@@ -43,21 +43,18 @@ public class SoapActionAnnotationMethodEndpointMappingTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testRegistration() throws Exception {
|
||||
MockControl messageControl = MockControl.createControl(SoapMessage.class);
|
||||
SoapMessage requestMock = (SoapMessage) messageControl.getMock();
|
||||
messageControl.expectAndReturn(requestMock.getSoapAction(), "http://springframework.org/spring-ws/SoapAction");
|
||||
messageControl.replay();
|
||||
MockControl factoryControl = MockControl.createControl(WebServiceMessageFactory.class);
|
||||
WebServiceMessageFactory factoryMock = (WebServiceMessageFactory) factoryControl.getMock();
|
||||
factoryControl.replay();
|
||||
MessageContext context = new DefaultMessageContext(requestMock, factoryMock);
|
||||
SoapMessage requestMock = createMock(SoapMessage.class);
|
||||
expect(requestMock.getSoapAction()).andReturn("http://springframework.org/spring-ws/SoapAction");
|
||||
WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
|
||||
replay(requestMock, factoryMock);
|
||||
|
||||
MessageContext context = new DefaultMessageContext(requestMock, factoryMock);
|
||||
EndpointInvocationChain chain = mapping.getEndpoint(context);
|
||||
assertNotNull("MethodEndpoint not registered", chain);
|
||||
MethodEndpoint expected = new MethodEndpoint(applicationContext.getBean("endpoint"), "doIt", new Class[0]);
|
||||
assertEquals("Invalid endpoint registered", expected, chain.getEndpoint());
|
||||
messageControl.verify();
|
||||
factoryControl.verify();
|
||||
|
||||
verify(requestMock,factoryMock);
|
||||
}
|
||||
|
||||
@Endpoint
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Represents a bean method that will be invoked as part of an incoming Web service message.
|
||||
@@ -80,7 +81,7 @@ public final class MethodEndpoint {
|
||||
* @throws InvocationTargetException when the method invocation results in an exception
|
||||
*/
|
||||
public Object invoke(Object[] args) throws IllegalAccessException, InvocationTargetException {
|
||||
return method.invoke(bean, args);
|
||||
return ReflectionUtils.invokeMethod(method, bean, args);
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.springframework.ws.server.endpoint.mapping.support.PayloadRootUtils;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>EndpointMapping</code> interface to map from the qualified name of the request payload
|
||||
@@ -52,8 +52,7 @@ public class PayloadRootQNameEndpointMapping extends AbstractQNameEndpointMappin
|
||||
}
|
||||
|
||||
protected QName resolveQName(MessageContext messageContext) throws TransformerException {
|
||||
return QNameUtils.getQNameForSource(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
return PayloadRootUtils.getPayloadRootQName(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2007 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.mapping.support;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Helper class for determining the root qualified name of a Web Service payload.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class PayloadRootUtils {
|
||||
|
||||
/**
|
||||
* Returns the root qualified name of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static QName getPayloadRootQName(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return QNameUtils.getQNameForNode(node);
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return QNameUtils.getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
XMLStreamReader streamReader = staxSource.getXMLStreamReader();
|
||||
if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
|
||||
streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
|
||||
return streamReader.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
// we have no other option than to transform
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return QNameUtils.getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides helper classes for <code>EndpointMapping</code> implementations.
|
||||
</body>
|
||||
</html>
|
||||
@@ -17,19 +17,9 @@
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
@@ -128,44 +118,6 @@ public abstract class QNameUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root qualified name of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static QName getQNameForSource(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return getQNameForNode(node);
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
XMLStreamReader streamReader = staxSource.getXMLStreamReader();
|
||||
if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
|
||||
streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
|
||||
return streamReader.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
// we have no other option than to transform
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
|
||||
* <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
|
||||
|
||||
@@ -16,25 +16,14 @@
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class QNameUtilsTest extends TestCase {
|
||||
|
||||
@@ -113,53 +102,5 @@ public class QNameUtilsTest extends TestCase {
|
||||
assertEquals("invalid localname", "localName", result.getLocalPart());
|
||||
}
|
||||
|
||||
public void testGetQNameForDomSource() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElementNS("namespace", "prefix:localname");
|
||||
document.appendChild(element);
|
||||
Source source = new DOMSource(document);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
|
||||
while (streamReader.getEventType() != XMLStreamConstants.START_ELEMENT) {
|
||||
streamReader.next();
|
||||
}
|
||||
Source source = new StaxSource(streamReader);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStreamSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new StreamSource(new StringReader(contents));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForSaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new SAXSource(new InputSource(new StringReader(contents)));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user