View Javadoc

1   /*
2    * Copyright 2005-2010 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(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     /**
126      * Indicates whether a SOAP 1.1 binding should be created.
127      * <p/>
128      * Defaults to <code>true</code>.
129      */
130     public void setCreateSoap11Binding(boolean createSoap11Binding) {
131         soapProvider.setCreateSoap11Binding(createSoap11Binding);
132     }
133 
134     /**
135      * Indicates whether a SOAP 1.2 binding should be created.
136      * <p/>
137      * Defaults to <code>false</code>.
138      */
139     public void setCreateSoap12Binding(boolean createSoap12Binding) {
140         soapProvider.setCreateSoap12Binding(createSoap12Binding);
141     }
142 
143     /**
144      * Sets the SOAP Actions for this binding. Keys are {@link javax.wsdl.BindingOperation#getName() binding operation
145      * names}; values are {@link javax.wsdl.extensions.soap.SOAPOperation#getSoapActionURI() SOAP Action URIs}.
146      *
147      * @param soapActions the soap
148      */
149     public void setSoapActions(Properties soapActions) {
150         soapProvider.setSoapActions(soapActions);
151     }
152 
153     /** Sets the value used for the binding transport attribute value. Defaults to HTTP. */
154     public void setTransportUri(String transportUri) {
155         soapProvider.setTransportUri(transportUri);
156     }
157 
158     /** Sets the value used for the SOAP Address location attribute value. */
159     public void setLocationUri(String locationUri) {
160         soapProvider.setLocationUri(locationUri);
161     }
162 
163     /**
164      * Sets the service name.
165      * <p/>
166      * Defaults to the port type name, with the suffix {@code Service} appended to it.
167      */
168     public void setServiceName(String serviceName) {
169         soapProvider.setServiceName(serviceName);
170         this.serviceName = serviceName;
171     }
172 
173     public void afterPropertiesSet() throws Exception {
174         if (!StringUtils.hasText(delegate.getTargetNamespace()) && typesProvider.getSchemaCollection() != null &&
175                 typesProvider.getSchemaCollection().getXsdSchemas().length > 0) {
176             XsdSchema schema = typesProvider.getSchemaCollection().getXsdSchemas()[0];
177             setTargetNamespace(schema.getTargetNamespace());
178         }
179         if (!StringUtils.hasText(serviceName) && StringUtils.hasText(portTypesProvider.getPortTypeName())) {
180             soapProvider.setServiceName(portTypesProvider.getPortTypeName() + "Service");
181         }
182         delegate.afterPropertiesSet();
183     }
184 
185     public Source getSource() {
186         return delegate.getSource();
187     }
188 }