Reworked echo sample to use maven for clients

This commit is contained in:
Arjen Poutsma
2010-02-26 09:11:12 +00:00
parent 9a608b4ba3
commit b931fc0b18
17 changed files with 41 additions and 98 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2006 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.echo.service;
/**
* Defines the "business logic" of the web service.
*
* @author Ingo Siebert
* @author Arjen Poutsma
*/
public interface EchoService {
/**
* Returns the given string.
*
* @return <code>s</code>
*/
String echo(String s);
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2006 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.echo.service.impl;
import org.springframework.ws.samples.echo.service.EchoService;
/**
* Default implementation of <code>EchoService</code>.
*
* @author Ingo Siebert
* @author Arjen Poutsma
*/
public class EchoServiceImpl implements EchoService {
public String echo(String s) {
return s;
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2006 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.echo.ws;
import org.springframework.util.Assert;
import org.springframework.ws.samples.echo.service.EchoService;
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
* Simple echoing Web service endpoint. Uses a <code>EchoService</code> to create a response string.
*
* @author Ingo Siebert
* @author Arjen Poutsma
*/
public class EchoEndpoint extends AbstractDomPayloadEndpoint {
/**
* Namespace of both request and response.
*/
public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/echo";
/**
* The local name of the expected request.
*/
public static final String ECHO_REQUEST_LOCAL_NAME = "echoRequest";
/**
* The local name of the created response.
*/
public static final String ECHO_RESPONSE_LOCAL_NAME = "echoResponse";
private EchoService echoService;
/**
* Sets the "business service" to delegate to.
*/
public void setEchoService(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 {
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++) {
if (children.item(i).getNodeType() == Node.TEXT_NODE) {
requestText = (Text) children.item(i);
break;
}
}
if (requestText == null) {
throw new IllegalArgumentException("Could not find request text node");
}
String echo = echoService.echo(requestText.getNodeValue());
Element responseElement = document.createElementNS(NAMESPACE_URI, ECHO_RESPONSE_LOCAL_NAME);
Text responseText = document.createTextNode(echo);
responseElement.appendChild(responseText);
return responseElement;
}
}

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,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified"
targetNamespace="http://www.springframework.org/spring-ws/samples/echo"
xmlns:tns="http://www.springframework.org/spring-ws/samples/echo">
<element name="echoRequest">
<simpleType>
<restriction base="string">
<pattern value="([A-Z]|[a-z])+"/>
</restriction>
</simpleType>
</element>
<element name="echoResponse" type="string"/>
</schema>

View File

@@ -0,0 +1,72 @@
<?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.0.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="echoEndpoint"/>
<property name="interceptors">
<list>
<ref local="validatingInterceptor"/>
<ref local="loggingInterceptor"/>
</list>
</property>
</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 'echo.xsd' XML
Schema file.
</description>
<property name="xsdSchema" ref="schema"/>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="true"/>
</bean>
<bean id="loggingInterceptor" class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
<description>
This interceptor logs the message payload.
</description>
</bean>
<bean id="echoEndpoint" class="org.springframework.ws.samples.echo.ws.EchoEndpoint">
<description>
This endpoint handles echo requests.
</description>
<property name="echoService" ref="echoService"/>
</bean>
<bean id="echo" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<description>
This bean definition represents a WSDL definition that is generated at runtime. It can be retrieved by
going to /echo/echo.wsdl (i.e. the bean name corresponds to the filename).
</description>
<property name="schema" ref="schema"/>
<property name="portTypeName" value="Echo"/>
<property name="locationUri" value="http://localhost:8080/echo/services"/>
</bean>
<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
<description>
This bean definition contains the XSD schema.
</description>
<property name="xsd" value="/WEB-INF/echo.xsd"/>
</bean>
<bean id="echoService" class="org.springframework.ws.samples.echo.service.impl.EchoServiceImpl">
<description>
This bean is our "business" service.
</description>
</bean>
</beans>

View File

@@ -0,0 +1,30 @@
<?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>
"Echo" WebService
</display-name>
<description>Returns a given string(only A-Z and a-z chars allowed). See echo.xsd file.</description>
<!-- Defines the Spring-WS MessageDispatcherServlet -->
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<!-- Transform the location attributes in WSDLs -->
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</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,76 @@
/*
* 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.samples.echo.ws;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.springframework.ws.samples.echo.service.EchoService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import static org.easymock.EasyMock.*;
public class EchoEndpointTest {
private EchoEndpoint endpoint;
private Document requestDocument;
private Document responseDocument;
private EchoService mock;
@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);
}
@Test
public void testInvokeInternal() throws Exception {
Element echoRequest =
requestDocument.createElementNS(EchoEndpoint.NAMESPACE_URI, EchoEndpoint.ECHO_REQUEST_LOCAL_NAME);
String content = "ABC";
Text requestText = requestDocument.createTextNode(content);
echoRequest.appendChild(requestText);
String result = "DEF";
expect(mock.echo(content)).andReturn(result);
replay(mock);
Element echoResponse = endpoint.invokeInternal(echoRequest, responseDocument);
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);
Assert.assertEquals("Invalid content", result, responseText.getNodeValue());
verify(mock);
}
}

View File

@@ -0,0 +1,5 @@
log4j.rootCategory=WARN, stdout
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