Project

General

Profile

Download (7.56 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

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

    
16
import javax.servlet.ServletContext;
17

    
18
import org.apache.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 http://dev.e-taxonomy.eu/trac/wiki/cdmlib-remote-webappConfigurationAndBootstrapping}
49
 *
50
 *
51
 * @author a.kohlbecker
52
 * @since Jul 1, 2014
53
 *
54
 */
55
// @EnableWebMvc // do not add this since we are overriding WebMvcConfigurationSupport directly
56
@Import(value={PreloadedBeans.class}) // can not be replaced by @DependsOn("...") ?
57
//@DependsOn("objectMapperConfigurer")
58
public abstract class CdmSpringMVCConfig extends WebMvcConfigurationSupport  {
59

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

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

    
67

    
68
    @Autowired
69
    protected ServletContext servletContext;
70

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

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

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

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

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

    
104
    public CdmSpringMVCConfig() {
105
        super();
106
        logger.debug("contructor");
107

    
108
    }
109

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

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

    
130
    /* (non-Javadoc)
131
     * @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
132
     */
133
    @Override
134
    protected void addInterceptors(InterceptorRegistry registry) {
135
        // TODO does it work?
136
        registry.addInterceptor(localeContextHandlerInterceptor);
137
        logger.debug("addInterceptors");
138
    }
139

    
140
    @Override
141
    public void addFormatters(FormatterRegistry registry) {
142
        registry.addConverter(new RestrictionConverter(jsonObjectMapper));
143
    }
144

    
145

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

    
155

    
156
    @Override
157
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
158
        configurer
159
        .favorPathExtension(true)
160
        .favorParameter(false)
161
        .defaultContentType(MediaType.APPLICATION_JSON)
162
        .mediaType("xml", MediaType.APPLICATION_XML)
163
        .mediaType("dc", MediaType.APPLICATION_XML)
164
        .mediaType("rdf", MediaType.APPLICATION_XML)
165
        .mediaType("rdfxml", MediaType.APPLICATION_XML)
166
        .mediaType("json", MediaType.APPLICATION_JSON);
167

    
168
        logger.debug("configureContentNegotiation");
169
    }
170

    
171
    /**
172
     * Create the CNVR.  Specify the view resolvers to use explicitly.  Get Spring to inject
173
     * the ContentNegotiationManager created by the configurer (see previous method).
174
     */
175
   @Bean
176
   public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
177

    
178
       List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
179

    
180
       resolvers.add(getPatternViewResolver("xml"));
181
       resolvers.add(getPatternViewResolver("json"));
182
       resolvers.add(getPatternViewResolver("rdf"));
183

    
184
       ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
185
       resolver.setOrder(2);
186
       resolver.setContentNegotiationManager(manager);
187
       resolver.setViewResolvers(resolvers);
188
       logger.debug("contentNegotiatingViewResolver");
189
       return resolver;
190
       }
191

    
192

    
193
   private ViewResolver getPatternViewResolver(String type) {
194
       PatternViewResolver resolver = new PatternViewResolver();
195
       resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/"+  type + "-views.xml"));
196
       resolver.setCache(XML_VIEW_CACHING);
197
       return resolver;
198
   }
199

    
200
}
(1-1/5)