/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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
 *
 * http://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 org.jboss.pnc.rest.configuration;

import io.swagger.jaxrs.config.BeanConfig;
import org.jboss.pnc.rest.debug.TestEndpoint;
import org.jboss.pnc.rest.endpoint.BpmEndpoint;
import org.jboss.pnc.rest.endpoint.BuildConfigSetRecordEndpoint;
import org.jboss.pnc.rest.endpoint.BuildConfigurationEndpoint;
import org.jboss.pnc.rest.endpoint.BuildConfigurationSetEndpoint;
import org.jboss.pnc.rest.endpoint.BuildEndpoint;
import org.jboss.pnc.rest.endpoint.BuildEnvironmentEndpoint;
import org.jboss.pnc.rest.endpoint.BuildRecordEndpoint;
import org.jboss.pnc.rest.endpoint.BuildRecordPushEndpoint;
import org.jboss.pnc.rest.endpoint.BuildTaskEndpoint;
import org.jboss.pnc.rest.endpoint.DebugEndpoint;
import org.jboss.pnc.rest.endpoint.LicenseEndpoint;
import org.jboss.pnc.rest.endpoint.ProductEndpoint;
import org.jboss.pnc.rest.endpoint.ProductMilestoneEndpoint;
import org.jboss.pnc.rest.endpoint.ProductReleaseEndpoint;
import org.jboss.pnc.rest.endpoint.ProductVersionEndpoint;
import org.jboss.pnc.rest.endpoint.ProjectEndpoint;
import org.jboss.pnc.rest.endpoint.RepositoryConfigurationEndpoint;
import org.jboss.pnc.rest.endpoint.RunningBuildRecordEndpoint;
import org.jboss.pnc.rest.endpoint.UserEndpoint;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {

    public JaxRsActivator() throws IOException {
        configureSwagger();
    }

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        addSwaggerResources(resources);
        addProjectResources(resources);
        return resources;
    }

    private final void configureSwagger() throws IOException {
        BeanConfig swaggerConfig = new BeanConfig();
        swaggerConfig.setVersion("1.0.0");
        swaggerConfig.setBasePath(getRestBasePath());
        swaggerConfig.setPrettyPrint(true);
        swaggerConfig.setResourcePackage("org.jboss.pnc.rest");
        swaggerConfig.setScan(true);
    }

    private void addProjectResources(Set<Class<?>> resources) {
        addEndpoints(resources);
        addExceptionMappers(resources);
    }

    private void addEndpoints(Set<Class<?>> resources) {
        resources.add(ProductEndpoint.class);
        resources.add(ProductVersionEndpoint.class);
        resources.add(ProductMilestoneEndpoint.class);
        resources.add(ProductReleaseEndpoint.class);
        resources.add(ProjectEndpoint.class);
        resources.add(RepositoryConfigurationEndpoint.class);
        resources.add(BuildConfigurationEndpoint.class);
        resources.add(BuildConfigurationSetEndpoint.class);
        resources.add(BuildConfigSetRecordEndpoint.class);
        resources.add(BuildRecordEndpoint.class);
        resources.add(BuildRecordPushEndpoint.class);
        resources.add(RunningBuildRecordEndpoint.class);
        resources.add(UserEndpoint.class);
        resources.add(LicenseEndpoint.class);
        resources.add(BuildEnvironmentEndpoint.class);
        resources.add(TestEndpoint.class);
        resources.add(BuildTaskEndpoint.class);
        resources.add(BuildEndpoint.class);
        resources.add(BpmEndpoint.class);
        resources.add(DebugEndpoint.class);
    }

    private void addExceptionMappers(Set<Class<?>> resources) {
        resources.add(ValidationExceptionExceptionMapper.class);
        resources.add(BuildConflictExceptionMapper.class);
        resources.add(AllOtherExceptionsMapper.class);
    }


    private void addSwaggerResources(Set<Class<?>> resources) {
        resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
        resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
    }

    private String getRestBasePath() throws IOException {
        URL resource = Thread.currentThread().getContextClassLoader().getResource("/swagger.properties");
        Properties properties = new Properties();
        try (InputStream inStream = resource.openStream()) {
            properties.load(inStream);
        }
        return properties.getProperty("baseUrl");
    }

}

@@ -1916,16 +1916,75 @@
 ndpoint;
+%0Aimport org.jboss.resteasy.plugins.interceptors.CorsFilter;
 %0A%0Aimport
@@ -2282,16 +2282,77 @@
 tion %7B%0A%0A
+    private Set%3CObject%3E singletons = new HashSet%3CObject%3E();%0A%0A
     publ
@@ -2416,24 +2416,138 @@
 eSwagger();%0A
+        configureCors();%0A    %7D%0A%0A    @Override%0A    public Set%3CObject%3E getSingletons() %7B%0A        return singletons;%0A
     %7D%0A%0A    @
@@ -2718,32 +2718,32 @@
 ces(resources);%0A
-
         return r
@@ -2751,32 +2751,290 @@
 sources;%0A    %7D%0A%0A
+    private void configureCors () %7B%0A        CorsFilter corsFilter = new CorsFilter();%0A        corsFilter.getAllowedOrigins().add(%22*%22);%0A        corsFilter.setAllowedMethods(%22OPTIONS, GET, POST, DELETE, PUT, PATCH%22);%0A        singletons.add(corsFilter);%0A    %7D%0A%0A
     private fina

Configure CORS filter in RestEasy.