net.gbmb.collector.ApplicationRunner.java Source code

Java tutorial

Introduction

Here is the source code for net.gbmb.collector.ApplicationRunner.java

Source

/*
 * Copyright 2014 Guillaume Bailleul
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.gbmb.collector;

import net.gbmb.collector.dao.Application;
import net.gbmb.collector.dao.ApplicationRepository;
import net.gbmb.collector.example.WebdavFinalStorage;
import net.gbmb.collector.example.WebdavTempStorage;
import net.gbmb.collector.rest.HttpRecordMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;

@Configuration
@ImportResource({ "collector-context.xml" })
@ComponentScan
@EnableAutoConfiguration
public class ApplicationRunner extends WebMvcConfigurerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRunner.class);

    @Resource
    private ApplicationRepository applicationRepository;

    @Autowired
    private ConfigurableApplicationContext context;

    @Bean
    public HttpMessageConverters customConverters() {
        return new HttpMessageConverters(new HttpRecordMessageConverter());
    }

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("128KB");
        factory.setMaxRequestSize("128KB");
        return factory.createMultipartConfig();
    }

    @Bean
    public FlowFilter flowFilter() {
        return new FlowFilter();
    }

    @Resource
    private FlowFilter ff;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // TODO initialisation pas clean (besoin de faire un bean puis resource ?)
        registry.addInterceptor(ff);

    }

    @Bean
    public TemporaryStorage temporaryStorage() {
        return new WebdavTempStorage();
    }

    @Bean
    public FinalStorage finalStorage() {
        return new WebdavFinalStorage();
    }

    @PostConstruct
    public void ensureDefaultExists() {
        LOGGER.debug("Check default application exists");
        if (applicationRepository.findByShortName("defapp") == null) {
            LOGGER.info("Creating default application");
            Application app = new Application();
            app.setShortName("defapp");
            app.setDescription("Default Application");
            app.setName("Default Application");
            app.setCancelableCollection(true);
            applicationRepository.save(app);
        } else {
            LOGGER.debug("Default application already existing");
        }

    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationRunner.class, args);
    }

    public void stop() {
        context.close();
    }

}