<%#
 Copyright 2013-2021 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.config;

<%_ if (reactive) { _%>
import com.fasterxml.jackson.databind.ObjectMapper;
<%_ } _%>
<%_ if (!skipClient || devDatabaseTypeH2Any) { _%>
import tech.jhipster.config.JHipsterConstants;
<%_ } _%>
import tech.jhipster.config.JHipsterProperties;
<%_ if (devDatabaseTypeH2Any) { _%>
import tech.jhipster.config.h2.H2ConfigurationHelper;
<%_ } _%>
<%_ if (!skipClient && reactive) { _%>
import tech.jhipster.web.filter.<% if (reactive) { %>reactive.<% } %>CachingHttpHeadersFilter;
<%_ } _%>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
<%_ if (!reactive) { _%>
import org.springframework.boot.web.server.*;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
<%_ } _%>
<%_ if (reactive && !skipClient) { _%>
import org.springframework.boot.autoconfigure.web.reactive.ResourceHandlerRegistrationCustomizer;
<%_ } _%>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
<%_ if (reactive) { _%>
    <%_ if (!skipClient) { _%>
import org.springframework.context.annotation.Profile;
    <%_ } _%>
import org.springframework.core.annotation.Order;
<%_ } _%>
<%_ if (reactive && !databaseTypeNo) { _%>
import org.springframework.data.web.ReactivePageableHandlerMethodArgumentResolver;
import org.springframework.data.web.ReactiveSortHandlerMethodArgumentResolver;
<%_ } _%>
<%_ if (!reactive || (devDatabaseTypeH2Any && reactive)) { _%>
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
<%_ } _%>
import org.springframework.util.CollectionUtils;
import org.springframework.web.cors.CorsConfiguration;
<%_ if (!reactive) { _%>
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
<%_ } _%>
<%_ if (reactive) { _%>
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
import org.springframework.web.server.WebExceptionHandler;
import org.zalando.problem.spring.webflux.advice.ProblemExceptionHandler;
import org.zalando.problem.spring.webflux.advice.ProblemHandling;
<%_ } _%>
<%_ if (!reactive) { _%>

import javax.servlet.*;
    <%_ if (!skipClient) { _%>
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;

import static java.net.URLDecoder.decode;
    <%_ } _%>
<%_ } _%>
<%_ if (reactive && !skipClient) { _%>

import java.util.concurrent.TimeUnit;
<%_ } _%>

/**
 * Configuration of web application with Servlet 3.0 APIs.
 */
@Configuration
public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitializer<% if (!skipClient) { %>, WebServerFactoryCustomizer<WebServerFactory><% } %><% } %><% if (reactive) { %>WebFluxConfigurer<% } %> {

    private final Logger log = LoggerFactory.getLogger(WebConfigurer.class);

<%_ if (!reactive) { _%>
    private final Environment env;

<%_ } _%>
    private final JHipsterProperties jHipsterProperties;

    public WebConfigurer(<% if (!reactive || (devDatabaseTypeH2Any && reactive)) { %>Environment env, <% } %>JHipsterProperties jHipsterProperties) {
<%_ if (!reactive) { _%>
        this.env = env;
<%_ } _%>
        this.jHipsterProperties = jHipsterProperties;
<%_ if (devDatabaseTypeH2Any && reactive) { _%>
        if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
            try {
                H2ConfigurationHelper.initH2Console();
            } catch (Exception e) {
                // Console may already be running on another app or after a refresh.
                e.printStackTrace();
            };
        }
<%_ } _%>
    }


<%_ if (!reactive) { _%>

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        if (env.getActiveProfiles().length != 0) {
            log.info("Web application configuration, using profiles: {}", (Object[]) env.getActiveProfiles());
        }

  <%_ if (devDatabaseTypeH2Any) { _%>
        if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
            initH2Console(servletContext);
        }
  <%_ } _%>
        log.info("Web application fully configured");
    }
  <%_ if (!skipClient) { _%>

    /**
     * Customize the Servlet engine: Mime types, the document root, the cache.
     */
    @Override
    public void customize(WebServerFactory server) {
        // When running in an IDE or with <% if (buildToolGradle) { %>./gradlew bootRun<% } else { %>./mvnw spring-boot:run<% } %>, set location of the static web assets.
        setLocationForStaticAssets(server);
    }

    private void setLocationForStaticAssets(WebServerFactory server) {
        if (server instanceof ConfigurableServletWebServerFactory) {
            ConfigurableServletWebServerFactory servletWebServer = (ConfigurableServletWebServerFactory) server;
            File root;
            String prefixPath = resolvePathPrefix();
            root = new File(prefixPath + "<%= CLIENT_DIST_DIR %>");
            if (root.exists() && root.isDirectory()) {
                servletWebServer.setDocumentRoot(root);
            }
        }
    }

    /**
     * Resolve path prefix to static resources.
     */
    private String resolvePathPrefix() {
        String fullExecutablePath = decode(this.getClass().getResource("").getPath(), StandardCharsets.UTF_8);
        String rootPath = Paths.get(".").toUri().normalize().getPath();
        String extractedPath = fullExecutablePath.replace(rootPath, "");
        int extractionEndIndex = extractedPath.indexOf("<%= BUILD_DIR %>");
        if (extractionEndIndex <= 0) {
            return "";
        }
        return extractedPath.substring(0, extractionEndIndex);
    }

  <%_ } _%>
<%_ } _%>

    @Bean
    public Cors<% if (reactive) { %>Web<% } %>Filter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = jHipsterProperties.getCors();
        if (!CollectionUtils.isEmpty(config.getAllowedOrigins()) || ! CollectionUtils.isEmpty(config.getAllowedOriginPatterns())) {
            log.debug("Registering CORS filter");
            source.registerCorsConfiguration("/api/**", config);
            source.registerCorsConfiguration("/management/**", config);
            source.registerCorsConfiguration("/v2/api-docs", config);
            source.registerCorsConfiguration("/v3/api-docs", config);
            source.registerCorsConfiguration("/swagger-resources", config);
            source.registerCorsConfiguration("/swagger-ui/**", config);
<%_ if (applicationTypeGateway) { _%>
            source.registerCorsConfiguration("/*/api/**", config);
            source.registerCorsConfiguration("/services/*/api/**", config);
            source.registerCorsConfiguration("/*/management/**", config);
<%_ } _%>
        }
        return new Cors<% if (reactive) { %>Web<% } %>Filter(source);
    }
<%_ if (reactive) { _%>

  <%_ if (!databaseTypeNo) { _%>
    // TODO: remove when this is supported in spring-boot
    @Bean
    HandlerMethodArgumentResolver reactivePageableHandlerMethodArgumentResolver() {
        return new ReactivePageableHandlerMethodArgumentResolver();
    }

    // TODO: remove when this is supported in spring-boot
    @Bean
    HandlerMethodArgumentResolver reactiveSortHandlerMethodArgumentResolver() {
        return new ReactiveSortHandlerMethodArgumentResolver();
    }
  <%_ } _%>

    @Bean
    @Order(-2) // The handler must have precedence over WebFluxResponseStatusExceptionHandler and Spring Boot's ErrorWebExceptionHandler
    public WebExceptionHandler problemExceptionHandler(ObjectMapper mapper, ProblemHandling problemHandling) {
        return new ProblemExceptionHandler(mapper, problemHandling);
    }
  <%_ if (!skipClient) { _%>

    @Bean
    ResourceHandlerRegistrationCustomizer registrationCustomizer() {
        // Disable built-in cache control to use our custom filter instead
        return registration -> registration.setCacheControl(null);
    }

    @Bean
    @Profile(JHipsterConstants.SPRING_PROFILE_PRODUCTION)
    public CachingHttpHeadersFilter cachingHttpHeadersFilter() {
        // Use a cache filter that only match selected paths
        return new CachingHttpHeadersFilter(TimeUnit.DAYS.toMillis(jHipsterProperties.getHttp().getCache().getTimeToLiveInDays()));
    }
  <%_ } _%>
<%_ } _%>
<%_ if (devDatabaseTypeH2Any && !reactive) { _%>

    /**
     * Initializes H2 console.
     */
    private void initH2Console(ServletContext servletContext) {
        log.debug("Initialize H2 console");
        H2ConfigurationHelper.initH2Console(servletContext);
    }
<%_ } _%>

}
