package fr.cnp.jug.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@SpringBootApplication
@ComponentScan("fr.cnp.jug")
@EnableWebMvc
public class Application {

	  @Autowired
	  @Qualifier("_halObjectMapper")
	  private ObjectMapper springHateoasObjectMapper;

	  @Bean(name = "objectMapper")
	  ObjectMapper objectMapper() {
	    springHateoasObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
	    return springHateoasObjectMapper;
	  }

    /**
     * http://stackoverflow.com/a/31748398/122441 until https://jira.spring.io/browse/DATAREST-573
     * @return
     */
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        // on fait le bourrin, tout est autorisé
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }

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

@@ -25,134 +25,8 @@
 r;%0A%0A
-import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Qualifier;%0A
 impo
@@ -577,576 +577,100 @@
 c;%0A%0A
-import com.fasterxml.jackson.databind.DeserializationFeature;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.SerializationFeature;%0A%0A@SpringBootApplication%0A@ComponentScan(%22fr.cnp.jug%22)%0A@EnableWebMvc%0Apublic class Application %7B%0A%0A%09  @Autowired%0A%09  @Qualifier(%22_halObjectMapper%22)%0A%09  private ObjectMapper springHateoasObjectMapper;%0A%0A%09  @Bean(name = %22objectMapper%22)%0A%09  ObjectMapper objectMapper() %7B%0A%09    springHateoasObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);%0A%09    return springHateoasObjectMapper;%0A%09  %7D
+@SpringBootApplication%0A@ComponentScan(%22fr.cnp.jug%22)%0A@EnableWebMvc%0Apublic class Application %7B
 %0A%0A  

version OK avec CORS