package alien4cloud.paas.cloudify3;

import java.text.SimpleDateFormat;
import java.util.List;

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.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;

import alien4cloud.paas.cloudify3.error.CloudifyResponseErrorHandler;
import alien4cloud.paas.cloudify3.service.SchedulerServiceFactoryBean;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.google.common.collect.Lists;

@Configuration
@ComponentScan(basePackages = { "alien4cloud.paas.cloudify3" })
@ImportResource("classpath:cloudify3-plugin-properties-config.xml")
public class PluginContextConfiguration {

    @Bean(name = "cloudify-rest-template")
    public RestTemplate restTemplate() {
        // Object mapper configuration
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);

        // Configure message converters for rest template
        List<HttpMessageConverter<?>> messageConverters = Lists.newArrayList();
        messageConverters.add(new ByteArrayHttpMessageConverter());
        messageConverters.add(new ResourceHttpMessageConverter());
        messageConverters.add(jackson2HttpMessageConverter);

        // Sync rest template
        RestTemplate syncRestTemplate = new RestTemplate();
        syncRestTemplate.setErrorHandler(new CloudifyResponseErrorHandler());
        syncRestTemplate.setMessageConverters(messageConverters);
        return syncRestTemplate;
    }

    @Bean(name = "cloudify-async-rest-template")
    public AsyncRestTemplate asyncRestTemplate() {
        // Async rest template
        SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setTaskExecutor(simpleAsyncTaskExecutor);
        return new AsyncRestTemplate(simpleClientHttpRequestFactory, restTemplate());
    }

    @Bean(name = "cloudify-scheduler")
    public SchedulerServiceFactoryBean schedulerServiceFactoryBean() {
        return new SchedulerServiceFactoryBean();
    }
}

@@ -89,16 +89,98 @@
 .List;%0A%0A
+import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;%0A
 import o
@@ -219,24 +219,24 @@
 ation.Bean;%0A
-
 import org.s
@@ -407,16 +407,66 @@
 source;%0A
+import org.springframework.core.annotation.Order;%0A
 import o
@@ -875,32 +875,135 @@
 ssageConverter;%0A
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;%0A
 import org.sprin
@@ -1100,150 +1100,8 @@
 e;%0A%0A
-import alien4cloud.paas.cloudify3.error.CloudifyResponseErrorHandler;%0Aimport alien4cloud.paas.cloudify3.service.SchedulerServiceFactoryBean;%0A%0A
 impo
@@ -1251,16 +1251,266 @@
 Lists;%0A%0A
+import alien4cloud.paas.cloudify3.error.CloudifyResponseErrorHandler;%0Aimport alien4cloud.paas.cloudify3.service.SchedulerServiceFactoryBean;%0A%0A@EnableGlobalMethodSecurity(prePostEnabled = true)%0A@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)%0A
 @Configu

Add annotations to secure the rest controllers of plugins.