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

io.stargate.auth.api.impl.AuthApiServer Maven / Gradle / Ivy

There is a newer version: 2.1.0-BETA-19
Show newest version
/*
 * Copyright The Stargate Authors
 *
 * 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 io.stargate.auth.api.impl;

import io.dropwizard.Application;
import io.dropwizard.cli.Cli;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.util.JarLocation;
import io.stargate.auth.AuthenticationService;
import io.stargate.auth.api.AuthApiActivator;
import io.stargate.auth.api.resources.AuthResource;
import io.stargate.auth.api.swagger.SwaggerUIResource;
import io.stargate.core.metrics.api.HttpMetricsTagProvider;
import io.stargate.core.metrics.api.Metrics;
import io.stargate.metrics.jersey.MetricsBinder;
import io.stargate.metrics.jersey.dwconfig.StargateV1ConfigurationSourceProvider;
import io.swagger.config.ScannerFactory;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.config.DefaultJaxrsScanner;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ServerProperties;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class AuthApiServer extends Application {

  AuthenticationService authenticationService;
  private final Metrics metrics;
  private final HttpMetricsTagProvider httpMetricsTagProvider;

  public AuthApiServer(
      AuthenticationService authenticationService,
      Metrics metrics,
      HttpMetricsTagProvider httpMetricsTagProvider) {
    this.authenticationService = authenticationService;
    this.metrics = metrics;
    this.httpMetricsTagProvider = httpMetricsTagProvider;

    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setSchemes(new String[] {"http"});
    beanConfig.setBasePath("/");
    ScannerFactory.setScanner(new DefaultJaxrsScanner());
  }

  /**
   * The only reason we override this is to remove the call to {@code bootstrap.registerMetrics()}.
   *
   * 

JVM metrics are registered once at the top level in the health-checker module. */ @Override public void run(String... arguments) { final Bootstrap bootstrap = new Bootstrap<>(this); addDefaultCommands(bootstrap); initialize(bootstrap); final Cli cli = new Cli(new JarLocation(getClass()), bootstrap, System.out, System.err); // only exit if there's an error running the command cli.run(arguments).ifPresent(this::onFatalError); } @Override public void run( final AuthApiServerConfiguration authApiServerConfiguration, final Environment environment) { environment .jersey() .register( new AbstractBinder() { @Override protected void configure() { bind(authenticationService).to(AuthenticationService.class); } }); environment.jersey().register(AuthResource.class); environment.jersey().register(ApiListingResource.class); environment.jersey().register(SwaggerSerializers.class); environment.jersey().register(SwaggerUIResource.class); environment .jersey() .register( new AbstractBinder() { @Override protected void configure() { bind(FrameworkUtil.getBundle(AuthApiActivator.class)).to(Bundle.class); } }); enableCors(environment); MetricsBinder metricsBinder = new MetricsBinder(metrics, httpMetricsTagProvider, AuthApiActivator.MODULE_NAME); metricsBinder.register(environment.jersey()); // no html content environment.jersey().property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true); } @Override public void initialize(final Bootstrap bootstrap) { super.initialize(bootstrap); bootstrap.setConfigurationSourceProvider( new StargateV1ConfigurationSourceProvider(AuthApiActivator.MODULE_NAME)); bootstrap.setMetricRegistry(metrics.getRegistry(AuthApiActivator.MODULE_NAME)); } private void enableCors(Environment environment) { FilterRegistration.Dynamic filter = environment.servlets().addFilter("cors", CrossOriginFilter.class); filter.setInitParameter( CrossOriginFilter.ALLOWED_METHODS_PARAM, "POST,GET,OPTIONS,PUT,DELETE,PATCH"); filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*"); filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); filter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "*"); filter.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, "true"); filter.setInitParameter(CrossOriginFilter.EXPOSED_HEADERS_PARAM, "Date"); filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); } @Override protected void bootstrapLogging() { // disable dropwizard logging, it will use external logback } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy