|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.webservices;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
19 | 22 | import java.util.Map;
|
20 | 23 |
|
| 24 | +import org.springframework.beans.BeansException; |
| 25 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 26 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 27 | +import org.springframework.beans.factory.config.ConstructorArgumentValues; |
| 28 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 29 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 30 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
21 | 31 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
22 | 32 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
23 | 33 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
24 | 34 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
25 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
26 | 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
27 | 38 | import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
|
28 | 39 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
| 40 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 41 | +import org.springframework.boot.context.properties.bind.Binder; |
29 | 42 | import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
30 | 43 | import org.springframework.context.ApplicationContext;
|
| 44 | +import org.springframework.context.ApplicationContextAware; |
31 | 45 | import org.springframework.context.annotation.Bean;
|
32 | 46 | import org.springframework.context.annotation.Configuration;
|
| 47 | +import org.springframework.core.io.Resource; |
| 48 | +import org.springframework.util.StringUtils; |
33 | 49 | import org.springframework.ws.config.annotation.EnableWs;
|
34 | 50 | import org.springframework.ws.config.annotation.WsConfigurationSupport;
|
35 | 51 | import org.springframework.ws.transport.http.MessageDispatcherServlet;
|
| 52 | +import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition; |
| 53 | +import org.springframework.xml.xsd.SimpleXsdSchema; |
36 | 54 |
|
37 | 55 | /**
|
38 | 56 | * {@link EnableAutoConfiguration Auto-configuration} for Spring Web Services.
|
@@ -72,10 +90,123 @@ public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServle
|
72 | 90 | return registration;
|
73 | 91 | }
|
74 | 92 |
|
| 93 | + @Bean |
| 94 | + @ConditionalOnProperty(prefix = "spring.webservices", name = "wsdl-locations") |
| 95 | + public static WsdlDefinitionBeanFactoryPostProcessor wsdlDefinitionBeanFactoryPostProcessor() { |
| 96 | + return new WsdlDefinitionBeanFactoryPostProcessor(); |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + @ConditionalOnProperty(prefix = "spring.webservices", name = "xsd-locations") |
| 101 | + public static XsdSchemaBeanFactoryPostProcessor xsdSchemaBeanFactoryPostProcessor() { |
| 102 | + return new XsdSchemaBeanFactoryPostProcessor(); |
| 103 | + } |
| 104 | + |
| 105 | + private static String ensureTrailingSlash(String path) { |
| 106 | + if (!path.endsWith("/")) { |
| 107 | + return path + "/"; |
| 108 | + } |
| 109 | + return path; |
| 110 | + } |
| 111 | + |
| 112 | + private static BeanDefinition createBeanDefinition(Resource resource, Class type) { |
| 113 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(type); |
| 114 | + ConstructorArgumentValues constructorArguments = new ConstructorArgumentValues(); |
| 115 | + constructorArguments.addIndexedArgumentValue(0, resource); |
| 116 | + beanDefinition.setConstructorArgumentValues(constructorArguments); |
| 117 | + return beanDefinition; |
| 118 | + } |
| 119 | + |
75 | 120 | @Configuration
|
76 | 121 | @EnableWs
|
77 | 122 | protected static class WsConfiguration {
|
78 | 123 |
|
79 | 124 | }
|
80 | 125 |
|
| 126 | + private static class WsdlDefinitionBeanFactoryPostProcessor |
| 127 | + implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { |
| 128 | + |
| 129 | + private ApplicationContext applicationContext; |
| 130 | + |
| 131 | + @Override |
| 132 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) |
| 133 | + throws BeansException { |
| 134 | + Binder binder = Binder.get(this.applicationContext.getEnvironment()); |
| 135 | + List<String> wsdlLocations = binder |
| 136 | + .bind("spring.webservices.wsdl-locations", |
| 137 | + Bindable.listOf(String.class)) |
| 138 | + .orElse(Collections.emptyList()); |
| 139 | + for (String wsdlLocation : wsdlLocations) { |
| 140 | + Resource[] wsdlResources = new Resource[] {}; |
| 141 | + try { |
| 142 | + wsdlResources = this.applicationContext |
| 143 | + .getResources(ensureTrailingSlash(wsdlLocation) + "*.wsdl"); |
| 144 | + } |
| 145 | + catch (IOException ignored) { |
| 146 | + } |
| 147 | + for (Resource wsdlResource : wsdlResources) { |
| 148 | + registry.registerBeanDefinition( |
| 149 | + StringUtils |
| 150 | + .stripFilenameExtension(wsdlResource.getFilename()), |
| 151 | + createBeanDefinition(wsdlResource, |
| 152 | + SimpleWsdl11Definition.class)); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) |
| 159 | + throws BeansException { |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void setApplicationContext(ApplicationContext applicationContext) |
| 164 | + throws BeansException { |
| 165 | + this.applicationContext = applicationContext; |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + private static class XsdSchemaBeanFactoryPostProcessor |
| 171 | + implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { |
| 172 | + |
| 173 | + private ApplicationContext applicationContext; |
| 174 | + |
| 175 | + @Override |
| 176 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) |
| 177 | + throws BeansException { |
| 178 | + Binder binder = Binder.get(this.applicationContext.getEnvironment()); |
| 179 | + List<String> xsdLocations = binder |
| 180 | + .bind("spring.webservices.xsd-locations", |
| 181 | + Bindable.listOf(String.class)) |
| 182 | + .orElse(Collections.emptyList()); |
| 183 | + for (String xsdLocation : xsdLocations) { |
| 184 | + Resource[] xsdResources = new Resource[] {}; |
| 185 | + try { |
| 186 | + xsdResources = this.applicationContext |
| 187 | + .getResources(ensureTrailingSlash(xsdLocation) + "*.xsd"); |
| 188 | + } |
| 189 | + catch (IOException ignored) { |
| 190 | + } |
| 191 | + for (Resource xsdResource : xsdResources) { |
| 192 | + registry.registerBeanDefinition( |
| 193 | + StringUtils.stripFilenameExtension(xsdResource.getFilename()), |
| 194 | + createBeanDefinition(xsdResource, SimpleXsdSchema.class)); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) |
| 201 | + throws BeansException { |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void setApplicationContext(ApplicationContext applicationContext) |
| 206 | + throws BeansException { |
| 207 | + this.applicationContext = applicationContext; |
| 208 | + } |
| 209 | + |
| 210 | + } |
| 211 | + |
81 | 212 | }
|
0 commit comments