package org.mskcc.cbio.oncokb.config;

import com.monitorjbl.json.JsonViewSupportFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;

@Configuration
@ComponentScan(basePackages = "org.mskcc.cbio.oncokb")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
        registry.addResourceHandler("/app/**").addResourceLocations("/app/");
        registry.addResourceHandler("/components/**").addResourceLocations("/components/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
        registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
        registry.addResourceHandler("/views/**").addResourceLocations("/views/");
        registry.addResourceHandler("/data/**").addResourceLocations("/data/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui.html").setViewName("redirect:/api/v1/swagger-ui.html");
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("utf-8");
        multipartResolver.setMaxUploadSize(50000000);
        return multipartResolver;
    }

    @Bean
    public JsonViewSupportFactoryBean views() {
        return new JsonViewSupportFactoryBean();
    }

    protected ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
            "OncoKB APIs",
            "OncoKB, a comprehensive and curated precision oncology knowledge base, offers oncologists detailed, evidence-based information about individual somatic mutations and structural alterations present in patient tumors with the goal of supporting optimal treatment decisions.",
            "v1.0.0",
            "http://oncokb.org/#/terms",
            new Contact("OncoKB", "http://www.oncokb.org", "team@oncokb.org"),
            "Usage Terms",
            "http://oncokb.org/#/terms");
        return apiInfo;
    }

    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }
}

@@ -83,24 +83,73 @@
 actoryBean;%0A
+import io.sentry.spring.SentryExceptionResolver;%0A
 import org.s
@@ -442,32 +442,85 @@
 eptionResolver;%0A
+import org.springframework.web.servlet.ModelAndView;%0A
 import org.sprin
@@ -1043,16 +1043,110 @@
 ntact;%0A%0A
+import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 @Configu
@@ -3602,61 +3602,1132 @@
     
-return new io.sentry.spring.SentryExceptionResolver()
+// Exclude specific events https://stackoverflow.com/questions/48914391/avoid-reporting-broken-pipe-errors-to-sentry-in-a-spring-boot-application%0A        return new SentryExceptionResolver() %7B%0A            @Override%0A            public ModelAndView resolveException(HttpServletRequest request,%0A                                                 HttpServletResponse response,%0A                                                 Object handler,%0A                                                 Exception ex) %7B%0A                Throwable rootCause = ex;%0A%0A                while (rootCause.getCause() != null && rootCause.getCause() != rootCause) %7B%0A                    rootCause = ex.getCause();%0A                %7D%0A%0A                if (!rootCause.getMessage().contains(%22Broken pipe%22)%0A                    && !rootCause.getMessage().contains(%22Required request body content is missing%22)%0A                    && !rootCause.getMessage().contains(%22Could not read document%22)%0A                    ) %7B%0A                    super.resolveException(request, response, handler, ex);%0A                %7D%0A                return null;%0A            %7D%0A%0A        %7D
 ;%0A  

Implement resolveException to filter out certain events