All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.bundles.CorsBundle Maven / Gradle / Ivy

package com.hubspot.singularity.bundles;

import com.google.common.collect.Iterators;
import com.hubspot.singularity.config.CorsConfiguration;
import com.hubspot.singularity.config.SingularityConfiguration;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Enumeration;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import org.eclipse.jetty.servlets.CrossOriginFilter;

/**
 * Adds a CORS filter.
 */
public class CorsBundle implements ConfiguredBundle {
  private static final String FILTER_NAME = "Cross Origin Request Filter";

  @Override
  public void initialize(final Bootstrap bootstrap) {}

  @Override
  public void run(final SingularityConfiguration config, final Environment environment) {
    CorsConfiguration corsConfiguration = config.getCors();
    if (!config.isEnableCorsFilter() && !corsConfiguration.isEnabled()) {
      return;
    }

    final Filter corsFilter = new CrossOriginFilter();
    final FilterConfig corsFilterConfig = new FilterConfig() {

      @Override
      public String getFilterName() {
        return FILTER_NAME;
      }

      @Override
      public ServletContext getServletContext() {
        return null;
      }

      @Override
      public String getInitParameter(final String name) {
        return null;
      }

      @Override
      public Enumeration getInitParameterNames() {
        return Iterators.asEnumeration(Collections.emptyIterator());
      }
    };

    try {
      corsFilter.init(corsFilterConfig);
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }

    FilterRegistration.Dynamic filter = environment
      .servlets()
      .addFilter(FILTER_NAME, corsFilter);

    filter.setInitParameter(
      CrossOriginFilter.ALLOWED_ORIGINS_PARAM,
      corsConfiguration.getAllowedOrigins()
    );
    filter.setInitParameter(
      CrossOriginFilter.ALLOWED_HEADERS_PARAM,
      corsConfiguration.getAllowedHeaders()
    );
    filter.setInitParameter(
      CrossOriginFilter.ALLOWED_METHODS_PARAM,
      corsConfiguration.getAllowedMethods()
    );
    filter.setInitParameter(
      CrossOriginFilter.ALLOW_CREDENTIALS_PARAM,
      corsConfiguration.isAllowCredentials() ? "true" : "false"
    );

    filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy