/*
 * Copyright 2017 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-authorization
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.epam.reportportal.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.PathProvider;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletContext;
import java.security.Principal;

import static com.google.common.base.Predicates.not;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.epam.reportportal.auth")
public class Swagger2Configuration {

    @Autowired
    private ServletContext servletContext;

    @Autowired
    @Value("${info.build.version}")
    private String buildVersion;

    @Autowired
    @Value("${spring.application.name}")
    private String appName;

    @Bean
    public Docket docket(){
        ApiInfo rpInfo = new ApiInfo("Report Portal", "Report Portal UAT documentation", buildVersion, "urn:tos",
                new Contact("EPAM Systems", "https://www.epam.com", ""),
                "GPLv3", "https://www.gnu.org/licenses/licenses.html#GPL");

        Docket rpDocket = new Docket(DocumentationType.SWAGGER_2)
                .ignoredParameterTypes(Principal.class)
                .useDefaultResponseMessages(false)
                .pathProvider(rpPathProvider())
				/* remove default endpoints from listing */
                .select().apis(not(or(
                        basePackage("org.springframework.boot"),
                        basePackage("org.springframework.cloud"),
                        basePackage("org.springframework.security.oauth2.provider.endpoint"))))
                .build();
        //@formatter:on

        rpDocket.apiInfo(rpInfo);
        return rpDocket;
    }

    @Bean
    public PathProvider rpPathProvider() {
        return new RPPathProvider(servletContext, appName);
    }

    @Bean
    public UiConfiguration uiConfig() {
        return new UiConfiguration(null);
    }

    private static class RPPathProvider extends RelativePathProvider {

        private String gatewayPath;

        RPPathProvider(ServletContext servletContext, String gatewayPath) {
            super(servletContext);
            this.gatewayPath = gatewayPath;
        }

        @Override
        protected String applicationPath() {
            return "/" + gatewayPath + super.applicationPath();
        }
    }

}

@@ -2119,93 +2119,8 @@
 n;%0A%0A
-    @Autowired%0A    @Value(%22$%7Bspring.application.name%7D%22)%0A    private String appName;%0A%0A
     
@@ -3142,17 +3142,23 @@
 rn new R
-P
+elative
 PathProv
@@ -3180,17 +3180,8 @@
 text
-, appName
 );%0A 
@@ -3232,24 +3232,24 @@
 iConfig() %7B%0A
+
         retu
@@ -3289,425 +3289,6 @@
  %7D%0A%0A
-    private static class RPPathProvider extends RelativePathProvider %7B%0A%0A        private String gatewayPath;%0A%0A        RPPathProvider(ServletContext servletContext, String gatewayPath) %7B%0A            super(servletContext);%0A            this.gatewayPath = gatewayPath;%0A        %7D%0A%0A        @Override%0A        protected String applicationPath() %7B%0A            return %22/%22 + gatewayPath + super.applicationPath();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

revert back relative path provider