001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.webservices;
018
019import java.io.IOException;
020import java.util.Collections;
021import java.util.List;
022import java.util.function.Function;
023
024import org.springframework.beans.BeansException;
025import org.springframework.beans.factory.config.BeanDefinition;
026import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
027import org.springframework.beans.factory.support.BeanDefinitionBuilder;
028import org.springframework.beans.factory.support.BeanDefinitionRegistry;
029import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
030import org.springframework.boot.autoconfigure.AutoConfigureAfter;
031import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
032import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
033import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
034import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
035import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
036import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
037import org.springframework.boot.context.properties.EnableConfigurationProperties;
038import org.springframework.boot.context.properties.bind.Bindable;
039import org.springframework.boot.context.properties.bind.Binder;
040import org.springframework.boot.web.servlet.ServletRegistrationBean;
041import org.springframework.context.ApplicationContext;
042import org.springframework.context.ApplicationContextAware;
043import org.springframework.context.annotation.Bean;
044import org.springframework.context.annotation.Conditional;
045import org.springframework.context.annotation.Configuration;
046import org.springframework.core.io.Resource;
047import org.springframework.util.StringUtils;
048import org.springframework.ws.config.annotation.EnableWs;
049import org.springframework.ws.config.annotation.WsConfigurationSupport;
050import org.springframework.ws.transport.http.MessageDispatcherServlet;
051import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
052import org.springframework.xml.xsd.SimpleXsdSchema;
053
054/**
055 * {@link EnableAutoConfiguration Auto-configuration} for Spring Web Services.
056 *
057 * @author Vedran Pavic
058 * @author Stephane Nicoll
059 * @since 1.4.0
060 */
061@Configuration
062@ConditionalOnWebApplication(type = Type.SERVLET)
063@ConditionalOnClass(MessageDispatcherServlet.class)
064@ConditionalOnMissingBean(WsConfigurationSupport.class)
065@EnableConfigurationProperties(WebServicesProperties.class)
066@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
067public class WebServicesAutoConfiguration {
068
069        private final WebServicesProperties properties;
070
071        public WebServicesAutoConfiguration(WebServicesProperties properties) {
072                this.properties = properties;
073        }
074
075        @Bean
076        public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
077                        ApplicationContext applicationContext) {
078                MessageDispatcherServlet servlet = new MessageDispatcherServlet();
079                servlet.setApplicationContext(applicationContext);
080                String path = this.properties.getPath();
081                String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
082                ServletRegistrationBean<MessageDispatcherServlet> registration = new ServletRegistrationBean<>(
083                                servlet, urlMapping);
084                WebServicesProperties.Servlet servletProperties = this.properties.getServlet();
085                registration.setLoadOnStartup(servletProperties.getLoadOnStartup());
086                servletProperties.getInit().forEach(registration::addInitParameter);
087                return registration;
088        }
089
090        @Bean
091        @Conditional(OnWsdlLocationsCondition.class)
092        public static WsdlDefinitionBeanFactoryPostProcessor wsdlDefinitionBeanFactoryPostProcessor() {
093                return new WsdlDefinitionBeanFactoryPostProcessor();
094        }
095
096        @Configuration
097        @EnableWs
098        protected static class WsConfiguration {
099
100        }
101
102        private static class WsdlDefinitionBeanFactoryPostProcessor
103                        implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
104
105                private ApplicationContext applicationContext;
106
107                @Override
108                public void setApplicationContext(ApplicationContext applicationContext) {
109                        this.applicationContext = applicationContext;
110                }
111
112                @Override
113                public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
114                                throws BeansException {
115                        Binder binder = Binder.get(this.applicationContext.getEnvironment());
116                        List<String> wsdlLocations = binder
117                                        .bind("spring.webservices.wsdl-locations",
118                                                        Bindable.listOf(String.class))
119                                        .orElse(Collections.emptyList());
120                        for (String wsdlLocation : wsdlLocations) {
121                                registerBeans(wsdlLocation, "*.wsdl", SimpleWsdl11Definition.class,
122                                                SimpleWsdl11Definition::new, registry);
123                                registerBeans(wsdlLocation, "*.xsd", SimpleXsdSchema.class,
124                                                SimpleXsdSchema::new, registry);
125                        }
126                }
127
128                @Override
129                public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
130                                throws BeansException {
131                }
132
133                private <T> void registerBeans(String location, String pattern, Class<T> type,
134                                Function<Resource, T> beanSupplier, BeanDefinitionRegistry registry) {
135                        for (Resource resource : getResources(location, pattern)) {
136                                BeanDefinition beanDefinition = BeanDefinitionBuilder
137                                                .genericBeanDefinition(type, () -> beanSupplier.apply(resource))
138                                                .getBeanDefinition();
139                                registry.registerBeanDefinition(
140                                                StringUtils.stripFilenameExtension(resource.getFilename()),
141                                                beanDefinition);
142                        }
143                }
144
145                private Resource[] getResources(String location, String pattern) {
146                        try {
147                                return this.applicationContext
148                                                .getResources(ensureTrailingSlash(location) + pattern);
149                        }
150                        catch (IOException ex) {
151                                return new Resource[0];
152                        }
153                }
154
155                private String ensureTrailingSlash(String path) {
156                        return path.endsWith("/") ? path : path + "/";
157                }
158
159        }
160
161}