Updated echo sample to use new APIs

This commit is contained in:
Arjen Poutsma
2010-05-20 08:43:57 +00:00
parent fb2663ad3d
commit 3b8661d3ae
4 changed files with 33 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006 the original author or authors.
* 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.
@@ -16,9 +16,18 @@
package org.springframework.ws.samples.echo.ws;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.ws.samples.echo.service.EchoService;
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -31,7 +40,8 @@ import org.w3c.dom.Text;
* @author Ingo Siebert
* @author Arjen Poutsma
*/
public class EchoEndpoint extends AbstractDomPayloadEndpoint {
@Endpoint
public class EchoEndpoint {
/**
* Namespace of both request and response.
@@ -48,26 +58,28 @@ public class EchoEndpoint extends AbstractDomPayloadEndpoint {
*/
public static final String ECHO_RESPONSE_LOCAL_NAME = "echoResponse";
private EchoService echoService;
private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
/**
* Sets the "business service" to delegate to.
*/
public void setEchoService(EchoService echoService) {
private final EchoService echoService;
@Autowired
public EchoEndpoint(EchoService echoService) {
this.echoService = echoService;
}
/**
* Reads the given <code>requestElement</code>, and sends a the response back.
*
* @param requestElement the contents of the SOAP message as DOM elements
* @param document a DOM document to be used for constructing <code>Node</code>s
* @return the response element
*/
@Override
protected Element invokeInternal(Element requestElement, Document document) throws Exception {
@PayloadRoot(localPart = ECHO_REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
@ResponsePayload
public Element handleEchoRequest(@RequestPayload Element requestElement) throws ParserConfigurationException {
Assert.isTrue(NAMESPACE_URI.equals(requestElement.getNamespaceURI()), "Invalid namespace");
Assert.isTrue(ECHO_REQUEST_LOCAL_NAME.equals(requestElement.getLocalName()), "Invalid local name");
NodeList children = requestElement.getChildNodes();
Text requestText = null;
for (int i = 0; i < children.getLength(); i++) {
@@ -81,6 +93,9 @@ public class EchoEndpoint extends AbstractDomPayloadEndpoint {
}
String echo = echoService.echo(requestText.getNodeValue());
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element responseElement = document.createElementNS(NAMESPACE_URI, ECHO_RESPONSE_LOCAL_NAME);
Text responseText = document.createTextNode(echo);
responseElement.appendChild(responseText);

View File

@@ -7,13 +7,12 @@
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">
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<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="echoEndpoint"/>
<property name="interceptors">
<list>
<ref local="validatingInterceptor"/>
@@ -33,6 +32,8 @@
<property name="validateResponse" value="true"/>
</bean>
<bean class="org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter"/>
<bean id="loggingInterceptor" class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
<description>
This interceptor logs the message payload.
@@ -43,7 +44,7 @@
<description>
This endpoint handles echo requests.
</description>
<property name="echoService" ref="echoService"/>
<constructor-arg ref="echoService"/>
</bean>
<bean id="echo" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">

View File

@@ -42,14 +42,13 @@ public class EchoEndpointTest {
@Before
public void setUp() throws Exception {
endpoint = new EchoEndpoint();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
requestDocument = documentBuilder.newDocument();
responseDocument = documentBuilder.newDocument();
mock = createMock(EchoService.class);
endpoint.setEchoService(mock);
endpoint = new EchoEndpoint(mock);
}
@Test
@@ -64,7 +63,7 @@ public class EchoEndpointTest {
replay(mock);
Element echoResponse = endpoint.invokeInternal(echoRequest, responseDocument);
Element echoResponse = endpoint.handleEchoRequest(echoRequest);
Assert.assertEquals("Invalid namespace", EchoEndpoint.NAMESPACE_URI, echoResponse.getNamespaceURI());
Assert.assertEquals("Invalid namespace", EchoEndpoint.ECHO_RESPONSE_LOCAL_NAME, echoResponse.getLocalName());
Text responseText = (Text) echoResponse.getChildNodes().item(0);