package com.netflix.spinnaker.fiat.config;

import com.google.common.collect.ImmutableList;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.fiat.model.resources.Role;
import com.netflix.spinnaker.fiat.roles.UserRolesProvider;
import com.netflix.spinnaker.kork.web.interceptors.MetricsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Import(RetrofitConfig.class)
@EnableConfigurationProperties(FiatServerConfigurationProperties.class)
public class FiatConfig extends WebMvcConfigurerAdapter {

  @Autowired
  private Registry registry;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    List<String> pathVarsToTag = ImmutableList.of("accountName", "applicationName", "resourceName");
    List<String> exclude = ImmutableList.of("BasicErrorController");
    MetricsInterceptor interceptor = new MetricsInterceptor(this.registry,
                                                            "controller.invocations",
                                                            pathVarsToTag,
                                                            exclude);
    registry.addInterceptor(interceptor);
  }

  @Bean
  @ConditionalOnMissingBean(UserRolesProvider.class)
  UserRolesProvider defaultUserRolesProvider() {
    return new UserRolesProvider() {
      @Override
      public Map<String, Collection<Role>> multiLoadRoles(Collection<String> userIds) {
        return new HashMap<>();
      }

      @Override
      public List<Role> loadRoles(String userId) {
        return new ArrayList<>();
      }
    };
  }
}

@@ -708,16 +708,146 @@
 Import;%0A
+import org.springframework.http.MediaType;%0Aimport org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;%0A
 import o
@@ -1944,16 +1944,258 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) %7B%0A    super.configureContentNegotiation(configurer);%0A    configurer.favorPathExtension(false).defaultContentType(MediaType.APPLICATION_JSON);%0A  %7D%0A%0A
   @Bean%0A

disable content negotiation via URI path extension