Project

General

Profile

Download (6.85 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.http.MediaType;
24
import org.springframework.web.accept.ContentNegotiationManager;
25
import org.springframework.web.context.support.ServletContextResource;
26
import org.springframework.web.servlet.ViewResolver;
27
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
28
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
29
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
30
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
31
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
32
import org.springframework.web.servlet.view.XmlViewResolver;
33

    
34
import eu.etaxonomy.cdm.remote.controller.interceptor.LocaleContextHandlerInterceptor;
35
import eu.etaxonomy.cdm.remote.view.PatternViewResolver;
36

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

    
56
    /**
57
     * turn caching off FOR DEBUGING ONLY !!!!
58
     */
59
    private static final boolean XML_VIEW_CACHING = true;
60

    
61
    public static final Logger logger = Logger.getLogger(CdmSpringMVCConfig.class);
62

    
63

    
64
    @Autowired
65
    protected ServletContext servletContext;
66

    
67
    @Autowired // is initialized in PreloadedBeans.class
68
    private LocaleContextHandlerInterceptor localeContextHandlerInterceptor;
69

    
70
    Collection<Class<? extends Object>> allCdmTypes = null;
71

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

    
85
//  @Bean
86
//  public InternalResourceViewResolver getInternalResourceViewResolverJsp() {
87
//    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
88
//    resolver.setOrder(0);
89
//    resolver.setPrefix(WEB_JAR_VIEW_RESOLVER_PREFIX);
90
//    resolver.setSuffix(WEB_JAR_VIEW_RESOLVER_SUFFIX);
91
//    // view names (or name patterns) that can be handled
92
//    resolver.setViewNames(new String[]{...});
93
//    return resolver;
94
//  }
95
//  ======================================================================
96

    
97
    public CdmSpringMVCConfig() {
98
        super();
99
        logger.debug("contructor");
100

    
101
    }
102

    
103
    @Bean
104
    @DependsOn({"requestMappingHandlerAdapter"})
105
    public XmlViewResolver getOaiXmlViewResolver() {
106
        XmlViewResolver resolver = new XmlViewResolver();
107
      resolver.setOrder(1);
108
      resolver.setLocation(new ServletContextResource(servletContext,"/WEB-INF/oai-views.xml"));
109
      resolver.setCache(XML_VIEW_CACHING);
110
      return resolver;
111
    }
112

    
113

    
114
    /* (non-Javadoc)
115
     * @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
116
     */
117
    @Override
118
    protected void addInterceptors(InterceptorRegistry registry) {
119
        // TODO does it work?
120
        registry.addInterceptor(localeContextHandlerInterceptor);
121
        logger.debug("addInterceptors");
122
    }
123

    
124
    @Override
125
    protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
126
      // DefaultServletHandlerConfigurer: delegates unhandled requests by forwarding to
127
      // the Servlet container's "default" servlet, since the DispatcherServlet is mapped to "/"
128
      // so static content ad welcome files are handled by the default servlet
129
      configurer.enable();
130
      logger.debug("configureDefaultServletHandling");
131
    }
132

    
133

    
134
    @Override
135
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
136
        configurer
137
        .favorPathExtension(true)
138
        .favorParameter(false)
139
        .defaultContentType(MediaType.APPLICATION_JSON)
140
        .mediaType("xml", MediaType.APPLICATION_XML)
141
        .mediaType("dc", MediaType.APPLICATION_XML)
142
        .mediaType("rdf", MediaType.APPLICATION_XML)
143
        .mediaType("rdfxml", MediaType.APPLICATION_XML)
144
        .mediaType("json", MediaType.APPLICATION_JSON);
145

    
146
        logger.debug("configureContentNegotiation");
147
    }
148

    
149
    /**
150
     * Create the CNVR.  Specify the view resolvers to use explicitly.  Get Spring to inject
151
     * the ContentNegotiationManager created by the configurer (see previous method).
152
     */
153
   @Bean
154
   public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
155

    
156
       List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
157

    
158
       resolvers.add(getPatternViewResolver("xml"));
159
       resolvers.add(getPatternViewResolver("json"));
160
       resolvers.add(getPatternViewResolver("rdf"));
161

    
162
       ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
163
       resolver.setOrder(2);
164
       resolver.setContentNegotiationManager(manager);
165
       resolver.setViewResolvers(resolvers);
166
       logger.debug("contentNegotiatingViewResolver");
167
       return resolver;
168
       }
169

    
170

    
171
   private ViewResolver getPatternViewResolver(String type) {
172
       PatternViewResolver resolver = new PatternViewResolver();
173
       resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/"+  type + "-views.xml"));
174
       resolver.setCache(XML_VIEW_CACHING);
175
       return resolver;
176
   }
177

    
178
}
(1-1/5)