Created
April 29, 2017 15:59
-
-
Save JamieKalloe/481cc21a85838325f6378d152fd4e02a to your computer and use it in GitHub Desktop.
Cors configuration for a dropwizard java api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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