View Javadoc
1   package org.imageconverter.config;
2   
3   import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
4   import static com.fasterxml.jackson.databind.MapperFeature.ALLOW_COERCION_OF_SCALARS;
5   
6   import java.time.format.DateTimeFormatter;
7   
8   import org.springframework.boot.context.properties.EnableConfigurationProperties;
9   import org.springframework.context.MessageSource;
10  import org.springframework.context.annotation.Bean;
11  import org.springframework.context.annotation.Configuration;
12  import org.springframework.context.annotation.EnableAspectJAutoProxy;
13  import org.springframework.context.annotation.Primary;
14  import org.springframework.context.support.ResourceBundleMessageSource;
15  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16  import org.springframework.transaction.annotation.EnableTransactionManagement;
17  import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
18  import org.springframework.web.multipart.commons.CommonsMultipartResolver;
19  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
20  import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
21  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
22  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
23  import org.springframework.web.util.UrlPathHelper;
24  
25  import com.fasterxml.jackson.databind.ObjectMapper;
26  import com.fasterxml.jackson.databind.json.JsonMapper;
27  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
28  import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
29  import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
30  import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
31  
32  import brave.sampler.Sampler;
33  
34  /**
35   * Web configuration bean.
36   * 
37   * @author Fernando Romulo da Silva
38   */
39  @Configuration
40  @EnableWebSecurity
41  @EnableWebMvc
42  @EnableAspectJAutoProxy
43  @EnableConfigurationProperties
44  @EnableTransactionManagement
45  public class WebConfig implements WebMvcConfigurer {
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public void configurePathMatch(final PathMatchConfigurer configurer) {
52  
53  	final var urlPathHelper = new UrlPathHelper();
54  	urlPathHelper.setRemoveSemicolonContent(false);
55  	configurer.setUrlPathHelper(urlPathHelper);
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public void addResourceHandlers(final ResourceHandlerRegistry registry) {
63  	registry.addResourceHandler("/swagger-ui/**")//
64  			.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.1.3/");
65      }
66  
67      @Bean
68      Sampler defaultSampler() {
69  	return Sampler.ALWAYS_SAMPLE;
70      }
71  
72      /**
73       * Create a valid {@link JsonMapper} object configured.
74       * 
75       * @return A {@link JsonMapper} object
76       */
77      @Bean
78      @Primary
79      ObjectMapper objectMapper() {
80  	final var module = new JavaTimeModule();
81  	module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ISO_DATE));
82  	module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
83  	module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ISO_TIME));
84  
85  	return JsonMapper.builder() //
86  			.configure(ALLOW_COERCION_OF_SCALARS, false) //
87  			.serializationInclusion(NON_NULL) //
88  			.addModule(module) //
89  			.build(); //
90      }
91  
92  //    @Bean
93  //    MessageSource messageSource() {
94  //	final var messageSource = new ReloadableResourceBundleMessageSource();
95  //	messageSource.setBasename("messages");
96  //	messageSource.setDefaultEncoding("UTF-8");
97  //	return messageSource;
98  //    }
99      
100     @Bean
101     MessageSource messageSource() {
102 	final var messageSource = new ResourceBundleMessageSource();
103         messageSource.setBasename("messages");
104         messageSource.setDefaultEncoding("UTF-8");
105         messageSource.setUseCodeAsDefaultMessage(true);
106         return messageSource;
107     }    
108     
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Bean
114     @Primary
115     @Override
116     public LocalValidatorFactoryBean getValidator() {
117 	final var bean = new LocalValidatorFactoryBean();
118 	bean.setValidationMessageSource(messageSource());
119 	return bean;
120     }
121 
122    
123     /**
124      * Create a valid {@link CommonsMultipartResolver} object configured (max upload and default encoding).
125      * 
126      * @return A {@link CommonsMultipartResolver} object
127      */
128     @Bean
129     CommonsMultipartResolver multipartResolver() {
130 	final var resolver = new CommonsMultipartResolver();
131 	resolver.setDefaultEncoding("utf-8");
132 //        resolver.setMaxInMemorySize();
133 //        resolver.setMaxUploadSize(ofMegabytes(20));
134 	return resolver;
135     }
136 
137 //    @Bean
138 //    public InternalResourceViewResolver viewResolver() {
139 //	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
140 //	viewResolver.setViewClass(JstlView.class);
141 //	viewResolver.setPrefix("/WEB-INF/views/");
142 //	viewResolver.setSuffix(".jsp");
143 //	return viewResolver;
144 //    }
145 //
146 //
147 //    @Override
148 //    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
149 //	registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
150 //    }
151 }