private static int getPort(Config config) { int port = 0; DefaultServerFactory serverFactory = (DefaultServerFactory) config.getServerFactory(); List<ConnectorFactory> connectorFactories = serverFactory.getApplicationConnectors(); if (Preconditions.checkNotNull(connectorFactories).size() != 1) { throw new RuntimeException( String.format("Unexpected number of connectorFactories %s, check 'applicationConnectors' in the YAML config", connectorFactories.size())); } HttpConnectorFactory connector = (HttpConnectorFactory) connectorFactories.get(0); if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) { port = connector.getPort(); } return port; }
@Override public void run(SimpleFormConfiguration configuration, Environment environment) throws Exception { environment.jersey().setUrlPattern("/api/*"); environment.jersey().register(new SimpleFormRest()); /* very ugly code, but does not seem that Dropwizard gives you any alternative :( */ HttpConnectorFactory applicationConnector = ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()) .getApplicationConnectors().get(0)); applicationConnector.setPort(port); ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()) .getAdminConnectors().get(0)).setPort(0); environment.lifecycle().addServerLifecycleListener(server -> jettyServer = server); BeanConfig beanConfig = new BeanConfig(); beanConfig.setVersion("0.0.1"); beanConfig.setSchemes(new String[]{"http"}); //beanConfig.setHost("localhost:8080"); beanConfig.setBasePath("/api"); beanConfig.setResourcePackage("com.foo.rest.examples.dw.simpleform"); beanConfig.setScan(true); environment.jersey().register(new io.swagger.jaxrs.listing.ApiListingResource()); environment.jersey().register(new io.swagger.jaxrs.listing.SwaggerSerializers()); }
@Override public void run(PIConfiguration configuration, Environment environment) throws Exception { environment.jersey().setUrlPattern("/api/*"); environment.jersey().register(new PositiveIntegerRest()); /* very ugly code, but does not seem that Dropwizard gives you any alternative :( */ HttpConnectorFactory applicationConnector = ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()) .getApplicationConnectors().get(0)); applicationConnector.setPort(port); ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()) .getAdminConnectors().get(0)).setPort(0); environment.lifecycle().addServerLifecycleListener(server -> jettyServer = server); BeanConfig beanConfig = new BeanConfig(); beanConfig.setVersion("0.0.1"); beanConfig.setSchemes(new String[]{"http"}); //beanConfig.setHost("localhost:8080"); beanConfig.setBasePath("/api"); beanConfig.setResourcePackage("com.foo.rest.examples.dw.positiveinteger"); beanConfig.setScan(true); environment.jersey().register(new io.swagger.jaxrs.listing.ApiListingResource()); environment.jersey().register(new io.swagger.jaxrs.listing.SwaggerSerializers()); }
private static void updatePortsToAvoidCollision(ServerFactory serverFactory) { if (serverFactory instanceof DefaultServerFactory) { DefaultServerFactory defaultServerFactory = (DefaultServerFactory)serverFactory; updatePortsToAvoidCollision(defaultServerFactory.getApplicationConnectors()); updatePortsToAvoidCollision(defaultServerFactory.getAdminConnectors()); } else if (serverFactory instanceof SimpleServerFactory) { SimpleServerFactory simpleServerFactory = (SimpleServerFactory)serverFactory; updatePortsToAvoidCollision(Collections.singleton(simpleServerFactory.getConnector())); } else { throw new IllegalStateException("Encountered an unexpected ServerFactory type"); } }
protected String getAdminBaseURI () { int httpPort = 0; for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getAdminConnectors()) { if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) { httpPort = ((HttpConnectorFactory) connector).getPort(); break; } } return format("http://localhost:%d", httpPort); }
protected String getServiceBaseURI () { int port = 0; for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getApplicationConnectors()) { if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) { port = ((HttpConnectorFactory) connector).getPort(); break; } } return format("http://localhost:%d", port); }
protected int getServiceBasePort () { int port = 0; for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getApplicationConnectors()) { if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) { port = ((HttpConnectorFactory) connector).getPort(); break; } } return port; }
@Provides @Singleton @SelfHostAndPort public HostAndPort provideSelfHostAndPort(ServerFactory serverFactory) { // Our method for obtaining connector factories from the server factory varies depending on the latter's type List<ConnectorFactory> appConnectorFactories; if (serverFactory instanceof DefaultServerFactory) { appConnectorFactories = ((DefaultServerFactory) serverFactory).getApplicationConnectors(); } else if (serverFactory instanceof SimpleServerFactory) { appConnectorFactories = Collections.singletonList(((SimpleServerFactory) serverFactory).getConnector()); } else { throw new IllegalStateException("Encountered an unexpected ServerFactory type"); } return getHostAndPortFromConnectorFactories(appConnectorFactories); }
@Provides @Singleton @SelfAdminHostAndPort public HostAndPort provideSelfAdminHostAndPort(ServerFactory serverFactory) { // Our method for obtaining connector factories from the server factory varies depending on the latter's type List<ConnectorFactory> adminConnectorFactories; if (serverFactory instanceof DefaultServerFactory) { adminConnectorFactories = ((DefaultServerFactory) serverFactory).getAdminConnectors(); } else if (serverFactory instanceof SimpleServerFactory) { adminConnectorFactories = Collections.singletonList(((SimpleServerFactory) serverFactory).getConnector()); } else { throw new IllegalStateException("Encountered an unexpected ServerFactory type"); } return getHostAndPortFromConnectorFactories(adminConnectorFactories); }
private static int getPort(Configuration config) { DefaultServerFactory serverFactory = (DefaultServerFactory) config.getServerFactory(); ConnectorFactory connectorFactory = serverFactory.getApplicationConnectors().get(0); if (connectorFactory instanceof HttpsConnectorFactory) { return ((HttpsConnectorFactory) connectorFactory).getPort(); } else if (connectorFactory instanceof HttpConnectorFactory) { return ((HttpConnectorFactory) connectorFactory).getPort(); } throw new IllegalArgumentException("Could not extract main application port from configuration"); }
private void setupAuthentication(ApiServerConfig cfg, Environment env) throws Exception { final Client client = new RestClientBuilder(env, cfg).build(getName()); // Health check for oauth2 server presence final OAuth2HealthCheck healthCheck = new OAuth2HealthCheck(cfg.getOauth2Config(), client); env.healthChecks().register("Oauth2 server", healthCheck); // Setting up the oauth2 authenticator CookieEncrypter cookieEncrypter = new CookieEncrypter(cfg.getOauth2Config().getCookieSecretKey()); boolean https = ((DefaultServerFactory)cfg.getServerFactory()).getApplicationConnectors().get(0) instanceof HttpsConnectorFactory; cookieEncrypter.setSecureFlag(https); OAuth2Authenticator authenticator = new OAuth2Authenticator(cfg.getOauth2Config(), client); // Using cache authenticator CachingAuthenticator<OAuth2Credentials, User> cachingAuthenticator = new CachingAuthenticator<OAuth2Credentials, User>(env.metrics(), authenticator, cfg.getCacheSpec()); final OAuth2AuthFilter<User> oAuth2AuthFilter = new OAuth2AuthFilter.Builder<OAuth2Credentials, User, OAuth2AuthFilter<User>, CachingAuthenticator<OAuth2Credentials, User>>() .setAuthenticator(cachingAuthenticator) .setCookieEncrypter(cookieEncrypter) .build(); env.jersey().register(new AuthDynamicFeature(oAuth2AuthFilter)); env.jersey().register(RolesAllowedDynamicFeature.class); env.jersey().register(new AuthValueFactoryProvider.Binder<User>(User.class)); // Register the oauth2 resource that handles client authentication final OAuth2Resource or = new OAuth2Resource(client, cfg.getOauth2Config(), cookieEncrypter); env.jersey().register(or); }
public static Integer getPort(ServerFactory serverFactory) { if(serverFactory instanceof SimpleServerFactory) { return getPort(((SimpleServerFactory)serverFactory).getConnector()); } else if(serverFactory instanceof DefaultServerFactory) { return getPort(((DefaultServerFactory)serverFactory).getApplicationConnectors().get(0)); } throw new RuntimeException("Unable to infer Port of " + serverFactory); }
public static Integer getAdminPort(ServerFactory serverFactory) { if(serverFactory instanceof SimpleServerFactory) { return getPort(((SimpleServerFactory)serverFactory).getConnector()); } else if(serverFactory instanceof DefaultServerFactory) { return getPort(((DefaultServerFactory)serverFactory).getAdminConnectors().get(0)); } throw new RuntimeException("Unable to infer AdminPort of " + serverFactory); }
private @Nonnull HttpConnectorFactory getConnectorFactoy(ServerFactory serverFactory) { if(serverFactory instanceof DefaultServerFactory) { return getDefaultServerFactory(serverFactory); } else if(serverFactory instanceof SimpleServerFactory) { return getSimpleServerFactory(serverFactory); } throw new IllegalArgumentException( String.format("Unknonw ServerFactory instance '%s'", serverFactory.getClass().getName())); }
private @Nonnull HttpConnectorFactory getDefaultServerFactory(ServerFactory serverFactory) { for (ConnectorFactory connector : ((DefaultServerFactory)serverFactory).getApplicationConnectors()) { if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) { return (HttpConnectorFactory) connector; } } throw new IllegalArgumentException(String.format("Failed to find any server ConnectorFactory in serverFactory '%s'", serverFactory.getClass().getName())); }
@Override public void run(DropAngularConfiguration config, Environment environment) throws ClassNotFoundException { final PingDAO pingDAO = new PingDAO(hibernateBundle.getSessionFactory()); environment.jersey().register(new PingResource(pingDAO)); // If you're using a version previous to v.0.8.x // you need this line to avoid the error: // Multiple servlets map to path: /*: // environment.jersey().setUrlPattern("/api/*"); // there's an option to configure your rootpath in your configuration.yml file ((DefaultServerFactory) config.getServerFactory()).setJerseyRootPath("/api/*"); environment.healthChecks().register("Ping", new PingHealthCheck()); // CORS configureCors(environment); }
@Provides @Named(SingularityServiceUIModule.SINGULARITY_URI_BASE) String getSingularityUriBase(final SingularityConfiguration configuration) { final String singularityUiPrefix; if (configuration.getServerFactory() instanceof SimpleServerFactory) { singularityUiPrefix = configuration.getUiConfiguration().getBaseUrl().or(((SimpleServerFactory) configuration.getServerFactory()).getApplicationContextPath()); } else { singularityUiPrefix = configuration.getUiConfiguration().getBaseUrl().or(((DefaultServerFactory) configuration.getServerFactory()).getApplicationContextPath()); } return (singularityUiPrefix.endsWith("/")) ? singularityUiPrefix.substring(0, singularityUiPrefix.length() - 1) : singularityUiPrefix; }
private static void setPort(Configuration config, int port) { DefaultServerFactory serverFactory = (DefaultServerFactory) config.getServerFactory(); HttpConnectorFactory connectorFactory = (HttpConnectorFactory) serverFactory.getApplicationConnectors().get(0); connectorFactory.setPort(port); }
private static void setAdminPort(Configuration config, int port) { DefaultServerFactory serverFactory = (DefaultServerFactory) config.getServerFactory(); HttpConnectorFactory connectorFactory = (HttpConnectorFactory) serverFactory.getAdminConnectors().get(0); connectorFactory.setPort(port); }
void useRandomPorts() { DefaultServerFactory serverFactory = (DefaultServerFactory) getServerFactory(); ((HttpConnectorFactory) serverFactory.getApplicationConnectors().get(0)).setPort(0); ((HttpConnectorFactory) serverFactory.getAdminConnectors().get(0)).setPort(0); }
@Override @JsonProperty("server") @Value.Default public ServerFactory getServerFactory() { return new DefaultServerFactory(); }
@Override public void run(CommaFeedConfiguration config, Environment environment) throws Exception { // guice init Injector injector = Guice.createInjector(new CommaFeedModule(hibernateBundle.getSessionFactory(), config, environment.metrics())); // session management environment.servlets().setSessionHandler(new SessionHandler(config.getSessionManagerFactory().build())); // support for "@SecurityCheck User user" injection environment.jersey().register(new SecurityCheckFactoryProvider.Binder(injector.getInstance(UserService.class))); // support for "@Context SessionHelper sessionHelper" injection environment.jersey().register(new SessionHelperFactoryProvider.Binder()); // REST resources environment.jersey().setUrlPattern("/rest/*"); ((DefaultServerFactory) config.getServerFactory()).setJerseyRootPath("/rest/*"); environment.jersey().register(injector.getInstance(AdminREST.class)); environment.jersey().register(injector.getInstance(CategoryREST.class)); environment.jersey().register(injector.getInstance(EntryREST.class)); environment.jersey().register(injector.getInstance(FeedREST.class)); environment.jersey().register(injector.getInstance(PubSubHubbubCallbackREST.class)); environment.jersey().register(injector.getInstance(ServerREST.class)); environment.jersey().register(injector.getInstance(UserREST.class)); // Servlets environment.servlets().addServlet("next", injector.getInstance(NextUnreadServlet.class)).addMapping("/next"); environment.servlets().addServlet("logout", injector.getInstance(LogoutServlet.class)).addMapping("/logout"); environment.servlets().addServlet("customCss", injector.getInstance(CustomCssServlet.class)).addMapping("/custom_css.css"); environment.servlets().addServlet("analytics.js", injector.getInstance(AnalyticsServlet.class)).addMapping("/analytics.js"); // Scheduled tasks Set<ScheduledTask> tasks = injector.getInstance(Key.get(new TypeLiteral<Set<ScheduledTask>>() { })); ScheduledExecutorService executor = environment.lifecycle().scheduledExecutorService("task-scheduler", true).threads(tasks.size()) .build(); for (ScheduledTask task : tasks) { task.register(executor); } // database init/changelogs environment.lifecycle().manage(injector.getInstance(StartupService.class)); // background feed fetching environment.lifecycle().manage(injector.getInstance(FeedRefreshTaskGiver.class)); environment.lifecycle().manage(injector.getInstance(FeedRefreshWorker.class)); environment.lifecycle().manage(injector.getInstance(FeedRefreshUpdater.class)); // cache configuration // prevent caching on REST resources, except for favicons environment.servlets().addFilter("cache-filter", new CacheBustingFilter() { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String path = ((HttpServletRequest) request).getRequestURI(); if (path.contains("/feed/favicon")) { chain.doFilter(request, response); } else { super.doFilter(request, response, chain); } } }).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/rest/*"); }