This commit is contained in:
Arjen Poutsma
2008-02-17 15:29:57 +00:00
parent a17d6fc6ce
commit e7b73d743d
17 changed files with 474 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2008 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.samples.pox.ws;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.springframework.ws.server.endpoint.AbstractSaxPayloadEndpoint;
public class ContactCountEndpoint extends AbstractSaxPayloadEndpoint {
private static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/pox";
private static final String CONTANT_NAME = "Contact";
private static final String CONTANT_COUNT_NAME = "ContactCount";
private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
public ContactCountEndpoint() {
documentBuilderFactory.setNamespaceAware(true);
}
protected ContentHandler createContentHandler() throws Exception {
return new ContactCounter();
}
protected Source getResponse(ContentHandler contentHandler) throws Exception {
ContactCounter counter = (ContactCounter) contentHandler;
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document response = documentBuilder.newDocument();
Element contactCountElement = response.createElementNS(NAMESPACE_URI, CONTANT_COUNT_NAME);
response.appendChild(contactCountElement);
contactCountElement.setTextContent(Integer.toString(counter.contactCount));
return new DOMSource(response);
}
private static class ContactCounter extends DefaultHandler {
private int contactCount = 0;
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (NAMESPACE_URI.equals(uri) && CONTANT_NAME.equals(localName)) {
contactCount++;
}
}
}
}

View File

@@ -0,0 +1,7 @@
log4j.rootLogger=WARN, stdout
log4j.logger.org.springframework.ws=DEBUG
log4j.logger.org.springframework.xml=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.springframework.org/spring-ws/samples/pox">
<xsd:element name="Contacts">
<xsd:annotation>
<xsd:documentation>
Defines a contact-list, with names and phone numbers.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Contact" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:all>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Phone" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ContactCount" type="xsd:integer"/>
</xsd:schema>

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<description>
This web application context contains Spring-WS beans. The beans defined in this context are automatically
detected by Spring-WS, similar to the way Controllers are picked up in Spring Web MVC.
</description>
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<description>
This endpoint mapping uses the qualified name of the payload (body contents) to determine the endpoint for
an incoming message. Every message is passed to the default endpoint. Additionally, messages are logged
using the logging interceptor.
</description>
<property name="defaultEndpoint" ref="contactCountEndpoint"/>
<property name="interceptors" ref="validatingInterceptor"/>
</bean>
<bean id="validatingInterceptor"
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<description>
This interceptor validates both incoming and outgoing message contents according to the 'contacts.xsd' XML
Schema file.
</description>
<property name="schema" value="/WEB-INF/contacts.xsd"/>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="true"/>
</bean>
<bean id="contactCountEndpoint" class="org.springframework.ws.samples.pox.ws.ContactCountEndpoint"/>
<bean id="messageFactory" class="org.springframework.ws.pox.dom.DomPoxMessageFactory"/>
</beans>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>
POX WebService
</display-name>
<description>A Spring-WS sample that uses Plain Old XML, rather than SOAP.</description>
<!-- Defines the Spring-WS MessageDispatcherServlet -->
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
</servlet>
<!-- Map all requests to this servlet -->
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

View File

@@ -0,0 +1,63 @@
/*
* Copyright ${YEAR} 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.samples.pox.ws;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import org.springframework.ws.samples.pox.ws.ContactCountEndpoint;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.core.io.ClassPathResource;
import org.custommonkey.xmlunit.XMLTestCase;
import org.w3c.dom.Document;
public class ContactCountEndpointTest extends XMLTestCase {
private ContactCountEndpoint endpoint;
private Transformer transformer;
private DocumentBuilder documentBuilder;
protected void setUp() throws Exception {
endpoint = new ContactCountEndpoint();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
}
public void testInvoke() throws Exception {
Document request = documentBuilder
.parse(SaxUtils.createInputSource(new ClassPathResource("contactsRequest.xml", getClass())));
Document expected = documentBuilder
.parse(SaxUtils.createInputSource(new ClassPathResource("contactCountResponse.xml", getClass())));
Source source = endpoint.invoke(new DOMSource(request));
DOMResult result = new DOMResult();
transformer.transform(source, result);
assertXMLEqual("Invalid resonse", expected, (Document) result.getNode());
}
}

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContactCount xmlns="http://www.springframework.org/spring-ws/samples/pox">2</ContactCount>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Contacts xmlns="http://www.springframework.org/spring-ws/samples/pox">
<Contact>
<Name>John Doe</Name>
<Phone>626-555-3456</Phone>
</Contact>
<Contact>
<Name>Jane Doe</Name>
<Phone>626-555-6543</Phone>
</Contact>
</Contacts>