This commit is contained in:
Arjen Poutsma
2008-04-27 12:51:27 +00:00
parent 708203f2b0
commit be5f18784c
5 changed files with 216 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ import org.springframework.util.StringUtils;
import org.springframework.ws.wsdl.wsdl11.provider.DefaultMessagesProvider;
import org.springframework.ws.wsdl.wsdl11.provider.InliningXsdSchemaTypesProvider;
import org.springframework.ws.wsdl.wsdl11.provider.SoapProvider;
import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedMessagesProvider;
import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedPortTypesProvider;
import org.springframework.xml.xsd.XsdSchema;
import org.springframework.xml.xsd.XsdSchemaCollection;
@@ -54,7 +55,7 @@ public class DefaultWsdl11Definition implements Wsdl11Definition, InitializingBe
private final InliningXsdSchemaTypesProvider typesProvider = new InliningXsdSchemaTypesProvider();
private final DefaultMessagesProvider messagesProvider = new DefaultMessagesProvider();
private final SuffixBasedMessagesProvider messagesProvider = new SuffixBasedMessagesProvider();
private final SuffixBasedPortTypesProvider portTypesProvider = new SuffixBasedPortTypesProvider();
@@ -106,16 +107,19 @@ public class DefaultWsdl11Definition implements Wsdl11Definition, InitializingBe
/** Sets the suffix used to detect request elements in the schema. */
public void setRequestSuffix(String requestSuffix) {
portTypesProvider.setRequestSuffix(requestSuffix);
messagesProvider.setRequestSuffix(requestSuffix);
}
/** Sets the suffix used to detect response elements in the schema. */
public void setResponseSuffix(String responseSuffix) {
portTypesProvider.setResponseSuffix(responseSuffix);
messagesProvider.setResponseSuffix(responseSuffix);
}
/** Sets the suffix used to detect fault elements in the schema. */
public void setFaultSuffix(String faultSuffix) {
portTypesProvider.setFaultSuffix(faultSuffix);
messagesProvider.setResponseSuffix(faultSuffix);
}
/** Indicates whether a SOAP 1.1 binding should be created. */

View File

@@ -65,7 +65,7 @@ public class DefaultMessagesProvider implements MessagesProvider {
private void createMessages(Definition definition, Element schemaElement) throws WSDLException {
String schemaTargetNamespace = schemaElement.getAttribute("targetNamespace");
Assert.hasText("No targetNamespace defined on schema");
Assert.hasText(schemaTargetNamespace, "No targetNamespace defined on schema");
if (logger.isDebugEnabled()) {
logger.debug("Looking for elements in schema with target namespace [" + schemaTargetNamespace + "]");
}
@@ -75,7 +75,7 @@ public class DefaultMessagesProvider implements MessagesProvider {
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element childElement = (Element) child;
if (isMessageElement(childElement)) {
QName elementName = new QName(schemaTargetNamespace, childElement.getAttribute("name"));
QName elementName = new QName(schemaTargetNamespace, getElementName(childElement));
Message message = definition.createMessage();
populateMessage(definition, message, elementName);
Part part = definition.createPart();
@@ -88,6 +88,16 @@ public class DefaultMessagesProvider implements MessagesProvider {
}
}
/**
* Returns the name attribute of the given element.
*
* @param element the element whose name to return
* @return the name of the element
*/
protected String getElementName(Element element) {
return element.getAttribute("name");
}
/**
* Indicates whether the given element should be includes as {@link Message} in the definition.
* <p/>

View File

@@ -0,0 +1,111 @@
/*
* 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.wsdl.wsdl11.provider;
import org.w3c.dom.Element;
import org.springframework.util.Assert;
/**
* Implementation of the {@link MessagesProvider} interface that is based on suffixes.
*
* @author Arjen Poutsma
* @since 1.5.1
*/
public class SuffixBasedMessagesProvider extends DefaultMessagesProvider {
/** The default suffix used to detect request elements in the schema. */
public static final String DEFAULT_REQUEST_SUFFIX = "Request";
/** The default suffix used to detect response elements in the schema. */
public static final String DEFAULT_RESPONSE_SUFFIX = "Response";
/** The default suffix used to detect fault elements in the schema. */
public static final String DEFAULT_FAULT_SUFFIX = "Fault";
private String requestSuffix = DEFAULT_REQUEST_SUFFIX;
private String responseSuffix = DEFAULT_RESPONSE_SUFFIX;
private String faultSuffix = DEFAULT_FAULT_SUFFIX;
/**
* Returns the suffix used to detect request elements in the schema.
*
* @see #DEFAULT_REQUEST_SUFFIX
*/
public String getRequestSuffix() {
return requestSuffix;
}
/**
* Sets the suffix used to detect request elements in the schema.
*
* @see #DEFAULT_REQUEST_SUFFIX
*/
public void setRequestSuffix(String requestSuffix) {
this.requestSuffix = requestSuffix;
}
/**
* Returns the suffix used to detect response elements in the schema.
*
* @see #DEFAULT_RESPONSE_SUFFIX
*/
public String getResponseSuffix() {
return responseSuffix;
}
/**
* Sets the suffix used to detect response elements in the schema.
*
* @see #DEFAULT_RESPONSE_SUFFIX
*/
public void setResponseSuffix(String responseSuffix) {
this.responseSuffix = responseSuffix;
}
/**
* Returns the suffix used to detect fault elements in the schema.
*
* @see #DEFAULT_FAULT_SUFFIX
*/
public String getFaultSuffix() {
return faultSuffix;
}
/**
* Sets the suffix used to detect fault elements in the schema.
*
* @see #DEFAULT_FAULT_SUFFIX
*/
public void setFaultSuffix(String faultSuffix) {
this.faultSuffix = faultSuffix;
}
protected boolean isMessageElement(Element element) {
if (super.isMessageElement(element)) {
String elementName = getElementName(element);
Assert.hasText(elementName, "Element has no name");
return elementName.endsWith(getRequestSuffix()) || elementName.endsWith(getResponseSuffix()) ||
elementName.endsWith(getFaultSuffix());
}
else {
return false;
}
}
}

View File

@@ -68,6 +68,8 @@ public class DefaultMessagesProviderTest extends TestCase {
provider.addMessages(definition);
assertEquals("Invalid amount of messages created", 3, definition.getMessages().size());
Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
assertNotNull("Message not created", message);
Part part = message.getPart("GetOrderRequest");

View File

@@ -0,0 +1,86 @@
/*
* 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.wsdl.wsdl11.provider;
import javax.wsdl.Definition;
import javax.wsdl.Message;
import javax.wsdl.Part;
import javax.wsdl.Types;
import javax.wsdl.extensions.schema.Schema;
import javax.wsdl.factory.WSDLFactory;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.xml.sax.SaxUtils;
public class SuffixBasedMessagesProviderTest extends TestCase {
private SuffixBasedMessagesProvider provider;
private Definition definition;
private DocumentBuilder documentBuilder;
protected void setUp() throws Exception {
provider = new SuffixBasedMessagesProvider();
provider.setFaultSuffix("Foo");
WSDLFactory factory = WSDLFactory.newInstance();
definition = factory.newDefinition();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
}
public void testAddMessages() throws Exception {
String definitionNamespace = "http://springframework.org/spring-ws";
definition.addNamespace("tns", definitionNamespace);
definition.setTargetNamespace(definitionNamespace);
String schemaNamespace = "http://www.springframework.org/spring-ws/schema";
definition.addNamespace("schema", schemaNamespace);
Resource resource = new ClassPathResource("schema.xsd", getClass());
Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(resource));
Types types = definition.createTypes();
definition.setTypes(types);
Schema schema = (Schema) definition.getExtensionRegistry()
.createExtension(Types.class, new QName("http://www.w3.org/2001/XMLSchema", "schema"));
types.addExtensibilityElement(schema);
schema.setElement(schemaDocument.getDocumentElement());
provider.addMessages(definition);
assertEquals("Invalid amount of messages created", 2, definition.getMessages().size());
Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
assertNotNull("Message not created", message);
Part part = message.getPart("GetOrderRequest");
assertNotNull("Part not created", part);
assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderRequest"), part.getElementName());
message = definition.getMessage(new QName(definitionNamespace, "GetOrderResponse"));
assertNotNull("Message not created", message);
part = message.getPart("GetOrderResponse");
assertNotNull("Part not created", part);
assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderResponse"), part.getElementName());
}
}