|
| 1 | +package com.readme.starter.config; |
| 2 | + |
| 3 | +import com.readme.dataextraction.RequestDataCollector; |
| 4 | +import com.readme.dataextraction.UserDataCollector; |
| 5 | +import com.readme.starter.datacollection.DataCollectionFilter; |
| 6 | +import com.readme.starter.datacollection.ServletDataPayloadAdapter; |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 9 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.ComponentScan; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.core.Ordered; |
| 14 | + |
| 15 | +/** |
| 16 | + * Configuration class for registering and initializing the JakartaDataCollectionFilter |
| 17 | + * along with its dependencies in a Spring Boot application. |
| 18 | + * <p> |
| 19 | + * This configuration provides the following: |
| 20 | + * <ul> |
| 21 | + * <li>Instantiates the {@link DataCollectionFilter} with required collectors.</li> |
| 22 | + * <li>Registers the filter using {@link FilterRegistrationBean} for servlet-based applications.</li> |
| 23 | + * <li>Sets up default implementations for collecting request and user data.</li> |
| 24 | + * </ul> |
| 25 | + */ |
| 26 | +@Configuration |
| 27 | +@ConditionalOnClass({UserDataProperties.class}) |
| 28 | +@ComponentScan(basePackages = {"com.readme.starter"}) |
| 29 | +@AllArgsConstructor |
| 30 | +public class DataCollectionAutoConfiguration { |
| 31 | + |
| 32 | + @Bean |
| 33 | + public FilterRegistrationBean<DataCollectionFilter> metricsFilter( |
| 34 | + RequestDataCollector<ServletDataPayloadAdapter> requestDataCollector, |
| 35 | + UserDataCollector<ServletDataPayloadAdapter> userDataCollector) { |
| 36 | + FilterRegistrationBean<DataCollectionFilter> registrationBean = new FilterRegistrationBean<>(); |
| 37 | + registrationBean.setFilter(new DataCollectionFilter(requestDataCollector, userDataCollector)); |
| 38 | + registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); |
| 39 | + registrationBean.addUrlPatterns("/*"); |
| 40 | + return registrationBean; |
| 41 | + } |
| 42 | + |
| 43 | +} |
0 commit comments