Project

General

Profile

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

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

    
16
import javax.servlet.ServletContext;
17

    
18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.bgbm.utis.jackson.ClassificationMixIn;
21
import org.cybertaxonomy.utis.tnr.msg.Classification;
22
import org.cybertaxonomy.utis.tnr.msg.TnrMsg;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.context.annotation.Bean;
25
import org.springframework.context.annotation.ComponentScan;
26
import org.springframework.context.annotation.Configuration;
27
import org.springframework.http.MediaType;
28
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
29
import org.springframework.web.accept.ContentNegotiationManager;
30
import org.springframework.web.context.support.ServletContextResource;
31
import org.springframework.web.servlet.View;
32
import org.springframework.web.servlet.ViewResolver;
33
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
34
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
35
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
36
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
37
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
38
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
39
import org.springframework.web.servlet.view.InternalResourceViewResolver;
40
import org.springframework.web.servlet.view.XmlViewResolver;
41
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
42
import org.springframework.web.servlet.view.xml.MarshallingView;
43

    
44
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
45
import com.mangofactory.swagger.plugin.EnableSwagger;
46
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
47
import com.wordnik.swagger.model.ApiInfo;
48

    
49
/**
50
 * @author a.kohlbecker
51
 * @date Jul 1, 2014
52
 *
53
 */
54
@Configuration
55
@EnableWebMvc
56
@ComponentScan(basePackages = {
57
        "com.mangofactory.swagger.configuration.SpringSwaggerConfig",
58
        "com.mangofactory.swagger.controllers",
59
        "org.bgbm.utis.controller"
60
        })
61
@EnableSwagger
62
public class SpringMVCConfig extends WebMvcConfigurerAdapter {
63

    
64
    public static final String[] WEB_JAR_RESOURCE_PATTERNS = {"css/", "images/", "lib/", "swagger-ui.js"};
65
    public static final String WEB_JAR_RESOURCE_LOCATION = "classpath:META-INF/resources/";
66
    public static final String WEB_JAR_VIEW_RESOLVER_PREFIX = "classpath:/resources/";
67
    public static final String WEB_JAR_VIEW_RESOLVER_SUFFIX = ".jsp";
68

    
69
    private static final Logger log = LogManager.getLogger(SpringMVCConfig.class.getName());
70

    
71
    @Autowired
72
    protected ServletContext servletContext;
73

    
74
    private SpringSwaggerConfig springSwaggerConfig;
75

    
76
    @Override
77
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
78
      registry.addResourceHandler(WEB_JAR_RESOURCE_PATTERNS)
79
              .addResourceLocations("/")
80
              .addResourceLocations(WEB_JAR_RESOURCE_LOCATION).setCachePeriod(0);
81
    }
82

    
83
    @Bean
84
    public InternalResourceViewResolver getInternalResourceViewResolver() {
85
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
86
      resolver.setPrefix(WEB_JAR_VIEW_RESOLVER_PREFIX);
87
      resolver.setSuffix(WEB_JAR_VIEW_RESOLVER_SUFFIX);
88
      return resolver;
89
    }
90

    
91
    @Override
92
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
93
      configurer.enable();
94
    }
95

    
96
    @Override
97
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
98
        configurer.favorPathExtension(true).favorParameter(false)
99
        .defaultContentType(MediaType.APPLICATION_JSON)
100
        .mediaType("xml", MediaType.APPLICATION_XML)
101
        .mediaType("json", MediaType.APPLICATION_JSON);
102
    }
103

    
104
    /**
105
     * Create the CNVR.  Specify the view resolvers to use explicitly.  Get Spring to inject
106
     * the ContentNegotiationManager created by the configurer (see previous method).
107
     */
108
   @Bean
109
   public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
110

    
111
       log.info("CNVR");
112

    
113
     // Define the view resolvers
114
       List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
115

    
116
//       resolvers.add(getXmlViewResolver());
117
       resolvers.add(getJsonViewResolver());
118
       resolvers.add(getMarshallingXmlViewResolver());
119

    
120
       ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
121
       resolver.setContentNegotiationManager(manager);
122
       resolver.setViewResolvers(resolvers);
123
       return resolver;
124
   }
125

    
126

    
127
    private ViewResolver getXmlViewResolver() {
128
        XmlViewResolver resolver = new XmlViewResolver();
129
        resolver.setLocation(new ServletContextResource(servletContext, "/WEB-INF/views.xml"));
130
        return resolver;
131
    }
132

    
133
    private ViewResolver getJsonViewResolver() {
134
        return new ViewResolver() {
135

    
136
            /**
137
             * Get the view to use.
138
             *
139
             * @return Always returns an instance of
140
             *         {@link MappingJacksonJsonView}.
141
             */
142
            @Override
143
            public View resolveViewName(String viewName, Locale locale) throws Exception {
144
                MappingJacksonJsonView view = new MappingJacksonJsonView();
145
                view.setPrettyPrint(true);
146
                view.getObjectMapper().getSerializationConfig()
147
                        .addMixInAnnotations(Classification.class, ClassificationMixIn.class);
148

    
149
                return view;
150
            }
151
        };
152
    }
153

    
154
   /**
155
    * @return
156
    */
157
   private ViewResolver getMarshallingXmlViewResolver() {
158

    
159
       final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
160

    
161

    
162
       marshaller.setPackagesToScan(new String[]{TnrMsg.class.getPackage().getName()});
163

    
164
       return new ViewResolver() {
165

    
166
        @Override
167
        public View resolveViewName(String viewName, Locale locale) throws Exception {
168
            MarshallingView view = new MarshallingView();
169
            view.setMarshaller(marshaller);
170
            return view;
171
        }
172

    
173
       };
174
   }
175

    
176

    
177

    
178
    // -------- Swagger configuration ------------ //
179

    
180
   /**
181
    * Required to autowire SpringSwaggerConfig
182
    */
183
   @Autowired
184
   public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
185
      this.springSwaggerConfig = springSwaggerConfig;
186
   }
187

    
188
   /**
189
    * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc framework - allowing for multiple
190
    * swagger groups i.e. same code base multiple swagger resource listings.
191
    */
192
   @Bean
193
   public SwaggerSpringMvcPlugin customImplementation(){
194
       // includePatterns: If not supplied a single pattern ".*?" is used by SwaggerSpringMvcPlugin
195
       // which matches anything and hence all RequestMappings. Here we define it explicitly
196
      return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
197
              .apiInfo(apiInfo()).
198
              includePatterns(".*?");
199
   }
200

    
201
   private ApiInfo apiInfo() {
202
     ApiInfo apiInfo = new ApiInfo("EU BON UTIS",
203
            "The Unified Taxonomic Information Service (UTIS) is the taxonomic backbone for the EU-BON project",
204
            "https://www.biodiversitycatalogue.org/services/79", "EditSupport@bgbm.org",
205
            "Mozilla Public License 2.0", "http://www.mozilla.org/MPL/2.0/");
206
     return apiInfo;
207
   }
208

    
209

    
210
}
    (1-1/1)