/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.startup;

import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import stroom.Config;

public class App extends Application<Config> {
    private final Logger LOGGER = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) throws Exception {
        // Hibernate requires JBoss Logging. The SLF4J API jar wasn't being detected so this sets it manually.
        System.setProperty("org.jboss.logging.provider", "slf4j");
        new App().run(args);
    }

    @Override
    public void initialize(Bootstrap<Config> bootstrap) {
        bootstrap.addBundle(new AssetsBundle("/ui", "/", "stroom.jsp", "ui"));
    }

    @Override
    public void run(Config configuration, io.dropwizard.setup.Environment environment) throws Exception {
        // The order in which the following are run is important.
        Environment.configure(environment);
        SpringContexts springContexts = new SpringContexts();
        Servlets servlets = new Servlets(environment.getApplicationContext());
        Filters filters = new Filters(environment.getApplicationContext());
        Listeners listeners = new Listeners(environment.servlets(), springContexts.rootContext);
        springContexts.start(environment, configuration);
        ServletMonitor servletMonitor = new ServletMonitor((servlets.upgradeDispatcherServletHolder));
        Resources resources = new Resources(environment.jersey(), servletMonitor);
        HealthChecks.registerHealthChecks(environment.healthChecks(), resources, servletMonitor);
        AdminTasks.registerAdminTasks(environment);
    }
}

@@ -731,16 +731,69 @@
 tstrap;%0A
+import org.eclipse.jetty.servlets.CrossOriginFilter;%0A
 import o
@@ -864,16 +864,121 @@
 onfig;%0A%0A
+import javax.servlet.DispatcherType;%0Aimport javax.servlet.FilterRegistration;%0Aimport java.util.EnumSet;%0A%0A
 public c
@@ -2455,20 +2455,761 @@
 vironment);%0A
+        configureCors(environment);%0A    %7D%0A%0A    private static final void configureCors(io.dropwizard.setup.Environment environment) %7B%0A        FilterRegistration.Dynamic cors = environment.servlets().addFilter(%22CORS%22, CrossOriginFilter.class);%0A        cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, new String%5B%5D%7B%22/*%22%7D);%0A        cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_METHODS_HEADER, %22GET,PUT,POST,DELETE,OPTIONS%22);%0A        cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, %22*%22);%0A        cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_HEADERS_HEADER, %22*%22);%0A        cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER, %22true%22);%0A
     %7D%0A%7D%0A

Enable CORS