22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .fasterxml .jackson .databind .SerializationFeature ;
5+ import com .fasterxml .jackson .databind .ser .FilterProvider ;
6+ import com .fasterxml .jackson .databind .ser .impl .SimpleFilterProvider ;
57import com .fasterxml .jackson .datatype .joda .JodaModule ;
68import com .sdl .webapp .common .api .contextengine .ContextEngine ;
9+ import com .sdl .webapp .common .api .serialization .json .DxaViewModelJsonChainFilter ;
710import com .sdl .webapp .common .util .ApplicationContextHolder ;
811import com .sdl .webapp .common .util .InitializationUtils ;
912import com .sdl .webapp .common .views .AtomView ;
1720import org .springframework .context .annotation .ImportResource ;
1821import org .springframework .context .support .PropertySourcesPlaceholderConfigurer ;
1922import org .springframework .core .Ordered ;
23+ import org .springframework .core .env .MutablePropertySources ;
24+ import org .springframework .core .env .PropertiesPropertySource ;
2025import org .springframework .core .io .ClassPathResource ;
2126import org .springframework .web .servlet .View ;
2227import org .springframework .web .servlet .ViewResolver ;
2732
2833import java .util .Locale ;
2934
35+ import static com .sdl .webapp .common .api .serialization .json .DxaViewModelJsonChainFilter .FILTER_NAME ;
3036import static com .sdl .webapp .common .util .InitializationUtils .loadDxaProperties ;
3137import static com .sdl .webapp .common .util .InitializationUtils .traceBeanInitialization ;
3238
@@ -56,7 +62,10 @@ public class DxaSpringInitialization {
5662 @ Bean
5763 public static PropertySourcesPlaceholderConfigurer placeholderConfigurer () {
5864 PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer ();
59- configurer .setProperties (InitializationUtils .loadDxaProperties ());
65+ MutablePropertySources propertySources = new MutablePropertySources ();
66+ propertySources .addLast (new PropertiesPropertySource ("dxa.properties.merged" , InitializationUtils .loadDxaProperties ()));
67+ configurer .setPropertySources (propertySources );
68+ configurer .setNullValue ("null" );
6069 traceBeanInitialization (configurer );
6170 return configurer ;
6271 }
@@ -73,31 +82,38 @@ public ViewResolver fallbackViewResolver() {
7382
7483 @ Bean
7584 public ViewResolver dxaViewResolver () {
76- UrlBasedViewResolver viewResolver = new UrlBasedViewResolver () {
85+ final UrlBasedViewResolver viewResolver = new UrlBasedViewResolver () {
7786
7887 @ Override
7988 public View resolveViewName (String viewName , Locale locale ) throws Exception {
80- viewName = processDeviceFamily (viewName );
81-
82- View overriddenView = super .resolveViewName (viewResolverOverride + "/" + viewName , locale );
83- if (null != overriddenView ) {
84- log .debug ("Found overridden view for {}, using it" , viewName );
85- return overriddenView ;
89+ if (viewName .startsWith (FORWARD_URL_PREFIX ) || viewName .startsWith (REDIRECT_URL_PREFIX )) {
90+ return super .resolveViewName (viewName , locale );
8691 }
8792
88- return super .resolveViewName (viewName , locale );
93+ View view = tryViewNames (locale , processDeviceFamily (viewName ), viewResolverOverride + "/" + viewName );
94+
95+ return view != null ? view : super .resolveViewName (viewName , locale );
96+ }
97+
98+ private View tryViewNames (Locale locale , String ... viewNames ) throws Exception {
99+ for (String viewName : viewNames ) {
100+ View view = super .resolveViewName (viewName , locale );
101+ if (null != view ) {
102+ log .debug ("Found view name {}, using it" , viewName );
103+ return view ;
104+ }
105+ }
106+ return null ;
89107 }
90108
91109 private String processDeviceFamily (String viewName ) {
92110 ContextEngine contextEngine = ApplicationContextHolder .getContext ().getBean (ContextEngine .class );
93111 String deviceFamily = contextEngine .getDeviceFamily ();
94- if (!"desktop" .equals (deviceFamily )) {
95- viewName = viewName + "." + deviceFamily ;
96- log .debug ("ViewName is changed to {} and current device family is {}" , viewName , deviceFamily );
97- }
98- return viewName ;
112+ log .debug ("Current device family is {}" , deviceFamily );
113+ return "desktop" .equals (deviceFamily ) ? viewName : (viewName + "." + deviceFamily );
99114 }
100115 };
116+
101117 viewResolver .setViewClass (OptionalJstlView .class );
102118 viewResolver .setOrder (1 );
103119 viewResolver .setPrefix (viewResolverPrefix );
@@ -140,11 +156,23 @@ public ObjectMapper objectMapper() {
140156 ObjectMapper objectMapper = new ObjectMapper ();
141157 objectMapper .configure (SerializationFeature .INDENT_OUTPUT , true );
142158 objectMapper .registerModule (new JodaModule ());
159+ objectMapper .setFilters (jsonFilterProvider ());
143160 objectMapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
144161 traceBeanInitialization (objectMapper );
145162 return objectMapper ;
146163 }
147164
165+ @ Bean
166+ public FilterProvider jsonFilterProvider () {
167+ SimpleFilterProvider provider = new SimpleFilterProvider ();
168+ return provider .addFilter (FILTER_NAME , dxaViewModelJsonChainFilter ());
169+ }
170+
171+ @ Bean
172+ public DxaViewModelJsonChainFilter dxaViewModelJsonChainFilter () {
173+ return new DxaViewModelJsonChainFilter ();
174+ }
175+
148176 private static class OptionalJstlView extends JstlView {
149177
150178 @ Override
0 commit comments