View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.wsdl.wsdl11;
18  
19  import java.util.Properties;
20  import javax.xml.transform.Source;
21  
22  import org.springframework.beans.factory.InitializingBean;
23  import org.springframework.util.StringUtils;
24  import org.springframework.ws.wsdl.wsdl11.provider.DefaultMessagesProvider;
25  import org.springframework.ws.wsdl.wsdl11.provider.InliningXsdSchemaTypesProvider;
26  import org.springframework.ws.wsdl.wsdl11.provider.SoapProvider;
27  import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedMessagesProvider;
28  import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedPortTypesProvider;
29  import org.springframework.xml.xsd.XsdSchema;
30  import org.springframework.xml.xsd.XsdSchemaCollection;
31  
32  /**
33   * Convenient implementation of {@link Wsdl11Definition} that creates a SOAP 1.1 or 1.2 binding based on naming
34   * conventions in one or more inlined XSD schemas. Delegates to {@link InliningXsdSchemaTypesProvider}, {@link
35   * DefaultMessagesProvider}, {@link SuffixBasedPortTypesProvider}, {@link SoapProvider} underneath; effectively
36   * equivalent to using a {@link ProviderBasedWsdl4jDefinition} with all these providers.
37   * <p/>
38   * Example configuration:
39   * <pre>
40   * &lt;bean id=&quot;airline&quot; class=&quot;org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition&quot;&gt;
41   *   &lt;property name=&quot;schema&quot;&gt;
42   *     &lt;bean class=&quot;org.springframework.xml.xsd.SimpleXsdSchema&quot;&gt;
43   *       &lt;property name=&quot;xsd&quot; value=&quot;/WEB-INF/airline.xsd&quot;/&gt;
44   *     &lt;/bean&gt;
45   *   &lt;/property&gt;
46   *   &lt;property name=&quot;portTypeName&quot; value=&quot;Airline&quot;/&gt;
47   *   &lt;property name=&quot;locationUri&quot; value=&quot;http://localhost:8080/airline/services&quot;/&gt;
48   * &lt;/bean&gt;
49   * </pre>
50   *
51   * @author Arjen Poutsma
52   * @since 1.5.0
53   */
54  public class DefaultWsdl11Definition implements Wsdl11Definition, InitializingBean {
55  
56      private final InliningXsdSchemaTypesProvider typesProvider = new InliningXsdSchemaTypesProvider();
57  
58      private final SuffixBasedMessagesProvider messagesProvider = new SuffixBasedMessagesProvider();
59  
60      private final SuffixBasedPortTypesProvider portTypesProvider = new SuffixBasedPortTypesProvider();
61  
62      private final SoapProvider soapProvider = new SoapProvider();
63  
64      private final ProviderBasedWsdl4jDefinition delegate = new ProviderBasedWsdl4jDefinition();
65  
66      private String serviceName;
67  
68      /** Creates a new instance of the {@link DefaultWsdl11Definition}. */
69      public DefaultWsdl11Definition() {
70          delegate.setTypesProvider(typesProvider);
71          delegate.setMessagesProvider(messagesProvider);
72          delegate.setPortTypesProvider(portTypesProvider);
73          delegate.setBindingsProvider(soapProvider);
74          delegate.setServicesProvider(soapProvider);
75      }
76  
77      /**
78       * Sets the target namespace used for this definition.
79       * <p/>
80       * Defaults to the target namespace of the defined schema.
81       */
82      public void setTargetNamespace(String targetNamespace) {
83          delegate.setTargetNamespace(targetNamespace);
84      }
85  
86      /**
87       * Sets the single XSD schema to inline. Either this property, or {@link #setSchemaCollection(XsdSchemaCollection)
88       * schemaCollection} must be set.
89       */
90      public void setSchema(final XsdSchema schema) {
91          typesProvider.setSchema(schema);
92      }
93  
94      /**
95       * Sets the XSD schema collection to inline. Either this property, or {@link #setSchema(XsdSchema) schema} must be
96       * set.
97       */
98      public void setSchemaCollection(XsdSchemaCollection schemaCollection) {
99          typesProvider.setSchemaCollection(schemaCollection);
100     }
101 
102     /** Sets the port type name used for this definition. Required. */
103     public void setPortTypeName(String portTypeName) {
104         portTypesProvider.setPortTypeName(portTypeName);
105     }
106 
107     /** Sets the suffix used to detect request elements in the schema. */
108     public void setRequestSuffix(String requestSuffix) {
109         portTypesProvider.setRequestSuffix(requestSuffix);
110         messagesProvider.setRequestSuffix(requestSuffix);
111     }
112 
113     /** Sets the suffix used to detect response elements in the schema. */
114     public void setResponseSuffix(String responseSuffix) {
115         portTypesProvider.setResponseSuffix(responseSuffix);
116         messagesProvider.setResponseSuffix(responseSuffix);
117     }
118 
119     /** Sets the suffix used to detect fault elements in the schema. */
120     public void setFaultSuffix(String faultSuffix) {
121         portTypesProvider.setFaultSuffix(faultSuffix);
122         messagesProvider.setFaultSuffix(faultSuffix);
123     }
124 
125     /** Indicates whether a SOAP 1.1 binding should be created. */
126     public void setCreateSoap11Binding(boolean createSoap11Binding) {
127         soapProvider.setCreateSoap11Binding(createSoap11Binding);
128     }
129 
130     /** Indicates whether a SOAP 1.2 binding should be created. */
131     public void setCreateSoap12Binding(boolean createSoap12Binding) {
132         soapProvider.setCreateSoap12Binding(createSoap12Binding);
133     }
134 
135     /**
136      * Sets the SOAP Actions for this binding. Keys are {@link javax.wsdl.BindingOperation#getName() binding operation
137      * names}; values are {@link javax.wsdl.extensions.soap.SOAPOperation#getSoapActionURI() SOAP Action URIs}.
138      *
139      * @param soapActions the soap
140      */
141     public void setSoapActions(Properties soapActions) {
142         soapProvider.setSoapActions(soapActions);
143     }
144 
145     /** Sets the value used for the binding transport attribute value. Defaults to HTTP. */
146     public void setTransportUri(String transportUri) {
147         soapProvider.setTransportUri(transportUri);
148     }
149 
150     /** Sets the value used for the SOAP Address location attribute value. */
151     public void setLocationUri(String locationUri) {
152         soapProvider.setLocationUri(locationUri);
153     }
154 
155     /** Sets the service name. */
156     public void setServiceName(String serviceName) {
157         soapProvider.setServiceName(serviceName);
158         this.serviceName = serviceName;
159     }
160 
161     public void afterPropertiesSet() throws Exception {
162         if (!StringUtils.hasText(delegate.getTargetNamespace()) && typesProvider.getSchemaCollection() != null &&
163                 typesProvider.getSchemaCollection().getXsdSchemas().length > 0) {
164             XsdSchema schema = typesProvider.getSchemaCollection().getXsdSchemas()[0];
165             setTargetNamespace(schema.getTargetNamespace());
166         }
167         if (!StringUtils.hasText(serviceName) && StringUtils.hasText(portTypesProvider.getPortTypeName())) {
168             soapProvider.setServiceName(portTypesProvider.getPortTypeName() + "Service");
169         }
170         delegate.afterPropertiesSet();
171     }
172 
173     public Source getSource() {
174         return delegate.getSource();
175     }
176 }