| 21165 |
ashik.ali |
1 |
package com.spice.profitmandi.web.config;
|
|
|
2 |
|
| 35387 |
amit |
3 |
import java.util.EnumSet;
|
|
|
4 |
|
|
|
5 |
import javax.servlet.DispatcherType;
|
| 35385 |
amit |
6 |
import javax.servlet.FilterRegistration;
|
| 21165 |
ashik.ali |
7 |
import javax.servlet.ServletContext;
|
|
|
8 |
|
|
|
9 |
import javax.servlet.ServletException;
|
|
|
10 |
import javax.servlet.ServletRegistration;
|
|
|
11 |
|
| 23568 |
govind |
12 |
import org.apache.logging.log4j.Logger;
|
|
|
13 |
import org.apache.logging.log4j.LogManager;
|
| 21165 |
ashik.ali |
14 |
import org.springframework.web.WebApplicationInitializer;
|
|
|
15 |
import org.springframework.web.context.ContextLoaderListener;
|
|
|
16 |
import org.springframework.web.context.WebApplicationContext;
|
|
|
17 |
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
|
18 |
import org.springframework.web.servlet.DispatcherServlet;
|
|
|
19 |
|
| 35385 |
amit |
20 |
import com.spice.profitmandi.common.web.filter.CorsFilter;
|
| 37195 |
ranu |
21 |
import com.spice.profitmandi.web.filter.BrandParamNormalizingFilter;
|
| 36510 |
amit |
22 |
import com.spice.profitmandi.web.filter.RequestCachingFilter;
|
| 35385 |
amit |
23 |
|
| 21165 |
ashik.ali |
24 |
public class SpringWebAppInitializer implements WebApplicationInitializer{
|
|
|
25 |
|
| 23568 |
govind |
26 |
private static final Logger LOGGER=LogManager.getLogger(SpringWebAppInitializer.class);
|
| 21165 |
ashik.ali |
27 |
|
|
|
28 |
private static final String DISPATCHER_SERVLET_NAME="DispatcherServlet";
|
| 21220 |
ashik.ali |
29 |
private static final String SPRING_CONFIG_PACKAGE_LOCATION="com.spice.profitmandi.web.config";
|
| 21165 |
ashik.ali |
30 |
|
|
|
31 |
public void onStartup(ServletContext servletContext) throws ServletException {
|
|
|
32 |
WebApplicationContext context = getContext();
|
|
|
33 |
servletContext.addListener(new ContextLoaderListener(context));
|
|
|
34 |
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(context));
|
|
|
35 |
dispatcher.setLoadOnStartup(1);
|
|
|
36 |
dispatcher.addMapping("/");
|
| 35385 |
amit |
37 |
|
| 36510 |
amit |
38 |
FilterRegistration.Dynamic cachingFilter = servletContext.addFilter("requestCachingFilter", new RequestCachingFilter());
|
|
|
39 |
cachingFilter.addMappingForServletNames(null, false, DISPATCHER_SERVLET_NAME);
|
|
|
40 |
|
| 37195 |
ranu |
41 |
// Safety-net for URL '+' decode issue on brand query params. Only touches GET
|
|
|
42 |
// requests with a brand/brands param present; falls back only when the raw value
|
|
|
43 |
// doesn't exist in catalog.brand but "<value>+" does.
|
|
|
44 |
FilterRegistration.Dynamic brandFilter = servletContext.addFilter("brandParamNormalizingFilter", new BrandParamNormalizingFilter());
|
|
|
45 |
brandFilter.addMappingForServletNames(null, false, DISPATCHER_SERVLET_NAME);
|
|
|
46 |
|
| 35385 |
amit |
47 |
// Register CORS filter to handle CORS for all requests including error responses
|
|
|
48 |
FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", new CorsFilter());
|
| 35387 |
amit |
49 |
corsFilter.addMappingForUrlPatterns(
|
|
|
50 |
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR),
|
|
|
51 |
false,
|
|
|
52 |
"/*"
|
|
|
53 |
);
|
| 35385 |
amit |
54 |
|
| 21165 |
ashik.ali |
55 |
LOGGER.info("Context is loaded");
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private AnnotationConfigWebApplicationContext getContext() {
|
|
|
59 |
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
|
|
60 |
context.setConfigLocation(SPRING_CONFIG_PACKAGE_LOCATION);
|
|
|
61 |
return context;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
}
|