Project

General

Profile

Download (7.45 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.config;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14

    
15
import javax.servlet.ServletContext;
16

    
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.context.annotation.Bean;
21
import org.springframework.context.annotation.DependsOn;
22
import org.springframework.context.annotation.Import;
23
import org.springframework.format.FormatterRegistry;
24
import org.springframework.http.MediaType;
25
import org.springframework.web.accept.ContentNegotiationManager;
26
import org.springframework.web.context.support.ServletContextResource;
27
import org.springframework.web.servlet.ViewResolver;
28
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
29
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
30
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
31
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
32
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
33
import org.springframework.web.servlet.view.XmlViewResolver;
34

    
35
import com.fasterxml.jackson.databind.ObjectMapper;
36

    
37
import eu.etaxonomy.cdm.remote.controller.interceptor.LocaleContextHandlerInterceptor;
38
import eu.etaxonomy.cdm.remote.converter.RestrictionConverter;
39
import eu.etaxonomy.cdm.remote.view.PatternViewResolver;
40

    
41
/**
42
 * This is the main configuration for spring MVC application context.
43
 *
44
 *
45
 * <h3>NOTE:</h3>
46
 *  For a detailed overview on the spring MVC and application context configuration and
47
 *  bootstrapping of this web application see:
48
 *  {@link https://dev.e-taxonomy.eu/redmine/projects/edit/wiki/cdmlib-remote-webappConfigurationAndBootstrapping}
49
 *
50
 *
51
 * @author a.kohlbecker
52
 * @since Jul 1, 2014
53
 */
54
// @EnableWebMvc // do not add this since we are overriding WebMvcConfigurationSupport directly
55
@Import(value={PreloadedBeans.class}) // can not be replaced by @DependsOn("...") ?
56
//@DependsOn("objectMapperConfigurer")
57
public abstract class CdmSpringMVCConfig extends WebMvcConfigurationSupport  {
58

    
59
    /**
60
     * turn caching off FOR DEBUGING ONLY !!!!
61
     */
62
    private static final boolean XML_VIEW_CACHING = true;
63

    
64
    public static final Logger logger = LogManager.getLogger(CdmSpringMVCConfig.class);
65

    
66

    
67
    @Autowired
68
    protected ServletContext servletContext;
69

    
70
    @Autowired // is initialized in PreloadedBeans.class
71
    private ObjectMapper jsonObjectMapper;
72

    
73
    @Autowired // is initialized in PreloadedBeans.class
74
    private LocaleContextHandlerInterceptor localeContextHandlerInterceptor;
75

    
76
    Collection<Class<? extends Object>> allCdmTypes = null;
77

    
78
//    ========================== JSP =================================
79
//    public static final String[] WEB_JAR_RESOURCE_PATTERNS = {"css/", "images/", "lib/", "swagger-ui.js"};
80
//    public static final String WEB_JAR_RESOURCE_LOCATION = "classpath:META-INF/resources/";
81
//
82
//    public static final String WEB_JAR_VIEW_RESOLVER_PREFIX = "/WEB-INF/jsp/";
83
//    public static final String WEB_JAR_VIEW_RESOLVER_SUFFIX = ".jsp";
84
//    @Override
85
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
86
//      registry.addResourceHandler(WEB_JAR_RESOURCE_PATTERNS)
87
//              .addResourceLocations("/")
88
//              .addResourceLocations(WEB_JAR_RESOURCE_LOCATION).setCachePeriod(0);
89
//    }
90

    
91
//  @Bean
92
//  public InternalResourceViewResolver getInternalResourceViewResolverJsp() {
93
//    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
94
//    resolver.setOrder(0);
95
//    resolver.setPrefix(WEB_JAR_VIEW_RESOLVER_PREFIX);
96
//    resolver.setSuffix(WEB_JAR_VIEW_RESOLVER_SUFFIX);
97
//    // view names (or name patterns) that can be handled
98
//    resolver.setViewNames(new String[]{...});
99
//    return resolver;
100
//  }
101
//  ======================================================================
102

    
103
    public CdmSpringMVCConfig() {
104
        super();
105
        logger.debug("constructor");
106
    }
107

    
108
	@Bean
109
	@DependsOn({ "requestMappingHandlerAdapter" })
110
	public XmlViewResolver getOaiXmlViewResolver() {
111
		XmlViewResolver resolver = new XmlViewResolver();
112
		resolver.setOrder(1);
113
		resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/oai-views.xml"));
114
		resolver.setCache(XML_VIEW_CACHING);
115
		return resolver;
116
	}
117

    
118
	@Bean
119
	@DependsOn({ "requestMappingHandlerAdapter" })
120
	public XmlViewResolver kmlXmlViewResolver() {
121
		XmlViewResolver resolver = new PatternViewResolver();
122
		resolver.setOrder(1);
123
		resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/kml-views.xml"));
124
		resolver.setCache(XML_VIEW_CACHING);
125
		return resolver;
126
	}
127

    
128
    @Override
129
    protected void addInterceptors(InterceptorRegistry registry) {
130
        // TODO does it work?
131
        registry.addInterceptor(localeContextHandlerInterceptor);
132
        logger.debug("addInterceptors");
133
    }
134

    
135
    @Override
136
    public void addFormatters(FormatterRegistry registry) {
137
        registry.addConverter(new RestrictionConverter(jsonObjectMapper));
138
    }
139

    
140
    @Override
141
    protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
142
        // DefaultServletHandlerConfigurer: delegates unhandled requests by forwarding to
143
        // the Servlet container's "default" servlet, since the DispatcherServlet is mapped to "/"
144
        // so static content ad welcome files are handled by the default servlet
145
        configurer.enable();
146
        logger.debug("configureDefaultServletHandling");
147
    }
148

    
149

    
150
    @Override
151
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
152
        configurer
153
            .favorPathExtension(true)
154
            .favorParameter(false)
155
            .defaultContentType(MediaType.APPLICATION_JSON)
156
            .mediaType("xml", MediaType.APPLICATION_XML)
157
            .mediaType("dc", MediaType.APPLICATION_XML)
158
            .mediaType("rdf", MediaType.APPLICATION_XML)
159
            .mediaType("rdfxml", MediaType.APPLICATION_XML)
160
            .mediaType("json", MediaType.APPLICATION_JSON);
161

    
162
        logger.debug("configureContentNegotiation");
163
    }
164

    
165
    /**
166
     * Create the CNVR.  Specify the view resolvers to use explicitly.  Get Spring to inject
167
     * the ContentNegotiationManager created by the configurer (see previous method).
168
     */
169
   @Bean
170
   public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
171

    
172
       List<ViewResolver> resolvers = new ArrayList<>();
173

    
174
       resolvers.add(getPatternViewResolver("xml"));
175
       resolvers.add(getPatternViewResolver("json"));
176
       resolvers.add(getPatternViewResolver("rdf"));
177

    
178
       ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
179
       resolver.setOrder(2);
180
       resolver.setContentNegotiationManager(manager);
181
       resolver.setViewResolvers(resolvers);
182
       logger.debug("contentNegotiatingViewResolver");
183
       return resolver;
184
       }
185

    
186

    
187
   private ViewResolver getPatternViewResolver(String type) {
188
       PatternViewResolver resolver = new PatternViewResolver();
189
       resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/"+  type + "-views.xml"));
190
       resolver.setCache(XML_VIEW_CACHING);
191
       return resolver;
192
   }
193

    
194
}
(1-1/5)