Skip to content

Instantly share code, notes, and snippets.

@JamieKalloe
Created April 29, 2017 15:59
Show Gist options
  • Save JamieKalloe/481cc21a85838325f6378d152fd4e02a to your computer and use it in GitHub Desktop.
Save JamieKalloe/481cc21a85838325f6378d152fd4e02a to your computer and use it in GitHub Desktop.
Cors configuration for a dropwizard java api
@Override
public void run(ApiConfiguration configuration, Environment environment) throws Exception {
Database.getInstance(configuration);
//cors
final FilterRegistration.Dynamic cors =
environment.servlets().addFilter("CORS", CrossOriginFilter.class);
// Configure CORS parameters
//TODO: get hosts from configuration file!
cors.setInitParameter("allowedOrigins", configuration.getAllowedOrigins());
cors.setInitParameter("allowedHeaders", configuration.getAllowedHeaders());
cors.setInitParameter("allowedMethods", configuration.getAllowedMethods());
// Add URL mapping
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
logger.info(String.format("Set API name to %s", configuration.getApiName()));
//Create and register DAOs, Services and Resources.
IntroductionDAO introductionDAO = new IntroductionDAO();
AccountDAO accountDAO = new AccountDAO();
IntroductionService introductionService = new IntroductionService(introductionDAO);
AccountService accountService = new AccountService(accountDAO);
IntroductionResource introductionResource = new IntroductionResource(introductionService);
AccountResource accountResource = new AccountResource(accountService);
//Register
setupAuthentication(environment, accountDAO);
configureClientFilter(environment);
environment.jersey().register(introductionResource);
environment.jersey().register(accountResource);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment