package com.konkerlabs.platform.registry.api.config;

import java.util.Arrays;
import java.util.Map;

import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.konkerlabs.platform.registry.api.web.interceptor.RequestResponseInterceptor;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean(name = "messageSource")
    public MessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.addBasenames("classpath:/messages/alert-triggers");
        messageSource.addBasenames("classpath:/messages/applications");
        messageSource.addBasenames("classpath:/messages/device-model");
        messageSource.addBasenames("classpath:/messages/devices");
        messageSource.addBasenames("classpath:/messages/devices-config");
        messageSource.addBasenames("classpath:/messages/gateways");
        messageSource.addBasenames("classpath:/messages/locations");
        messageSource.addBasenames("classpath:/messages/rest-destination");
        messageSource.addBasenames("classpath:/messages/routes");
        messageSource.addBasenames("classpath:/messages/transformations");
        messageSource.addBasenames("classpath:/messages/users");
        messageSource.addBasenames("classpath:/messages/tenants");
        messageSource.addBasenames("classpath:/messages/health-alert");
        messageSource.addBasenames("classpath:/mail/MailMessages");
        messageSource.addBasenames("classpath:/messages/private-storage");
        messageSource.addBasenames("classpath:/messages/global");
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    }

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {

            @Override
            public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
                    boolean includeStackTrace) {
                Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
                errorAttributes.remove("path");
                errorAttributes.remove("exceptions");
                errorAttributes.remove("error");
                errorAttributes.put("code", errorAttributes.get("status"));
                errorAttributes.put("status", "error");

                Object message = errorAttributes.get("message");
                errorAttributes.put("messages", Arrays.asList(message));
                errorAttributes.remove("message");

                return errorAttributes;
            }

        };
    }
        
    @Bean
    public RequestResponseInterceptor requestResponseInterceptor() {
    	return new RequestResponseInterceptor();
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(requestResponseInterceptor()).addPathPatterns("/applications/*", "/users/*");
    }

}

@@ -512,45 +512,175 @@
 ork.
-web.context.request.RequestAttributes
+http.MediaType;%0Aimport org.springframework.web.context.request.RequestAttributes;%0Aimport org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
 ;%0Aim
@@ -3676,10 +3676,439 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) %7B%0A        configurer.favorPathExtension(true)%0A            .favorParameter(false)%0A            .ignoreAcceptHeader(false)%0A            .useJaf(false)%0A            .defaultContentType(MediaType.APPLICATION_JSON)%0A            .mediaType(%22xml%22, MediaType.APPLICATION_XML)%0A            .mediaType(%22json%22, MediaType.APPLICATION_JSON);%0A    %7D%0A
 %7D%0A

Add ContentNegotiationManager to fix problem in get method with email in path