protected Handler createAdminServlet(Server server, MutableServletContextHandler handler, MetricRegistry metrics, HealthCheckRegistry healthChecks) { configureSessionsAndSecurity(handler, server); handler.setServer(server); handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics); handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks); handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*"); final String allowedMethodsParam = allowedMethods.stream() .collect(Collectors.joining(",")); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam); return handler; }
/** * Add one or more servlet event listeners. * * @param listeners one or more listener instances that implement {@link * javax.servlet.ServletContextListener}, {@link javax.servlet.ServletContextAttributeListener}, * {@link javax.servlet.ServletRequestListener} or {@link * javax.servlet.ServletRequestAttributeListener} */ public void addServletListeners(EventListener... listeners) { for (EventListener listener : listeners) { handler.addEventListener(listener); } }
this.validator = validator; this.servletContext = new MutableServletContextHandler(); servletContext.setClassLoader(classLoader); this.servletEnvironment = new ServletEnvironment(servletContext); this.adminContext = new MutableServletContextHandler(); adminContext.setClassLoader(classLoader); jerseyConfig.setContextPath(servletContext.getContextPath());
/** * Set the security handler. * * @param securityHandler The securityHandler to set. */ public void setSecurityHandler(SecurityHandler securityHandler) { handler.setSecurityEnabled(securityHandler != null); handler.setSecurityHandler(securityHandler); }
private void configureSessionsAndSecurity(MutableServletContextHandler handler, Server server) { handler.setServer(server); if (handler.isSecurityEnabled()) { handler.getSecurityHandler().setServer(server); } if (handler.isSessionsEnabled()) { handler.getSessionHandler().setServer(server); } }
jersey.getResourceConfig().packages(getApplicationPackagesToLoad().toArray(new String[] {})); applicationContext.addFilter(LogTraceIdFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); applicationContext.addBean(bean); applicationContext.addServlet(entry.getValue(), entry.getKey());
protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) { configureSessionsAndSecurity(handler, server); final String allowedMethodsParam = allowedMethods.stream() .collect(Collectors.joining(",")); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam); if (enableThreadNameFilter) { handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); } serverPush.addFilter(handler); if (jerseyContainer != null) { jerseyRootPath.ifPresent(jersey::setUrlPattern); jersey.register(new JacksonFeature(objectMapper)); jersey.register(new HibernateValidationBinder(validator)); if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) { jersey.register(new ExceptionMapperBinder(detailedJsonProcessingExceptionMapper)); } handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern()); } final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry); instrumented.setServer(server); instrumented.setHandler(handler); return instrumented; }
private void registerServlets(Environment environment) { environment.getApplicationContext().addServlet(ClusterInfoServlet.class, "/servlet/clusterinfo"); environment.getApplicationContext().addServlet(KafkaTopicStatsServlet.class, "/servlet/topicstats"); environment.getApplicationContext().addServlet(DoctorKafkaActionsServlet.class, "/servlet/actions"); environment.getApplicationContext().addServlet(DoctorKafkaInfoServlet.class, "/servlet/info"); environment.getApplicationContext().addServlet(DoctorKafkaBrokerStatsServlet.class, "/servlet/brokerstats"); environment.getApplicationContext().addServlet(UnderReplicatedPartitionsServlet.class, "/servlet/urp"); }
public static ServerContainer configureContext(final MutableServletContextHandler context, final MetricRegistry metrics) throws ServletException { WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context); NativeWebSocketConfiguration wsConfig = filter.getConfiguration(); ServerContainer wsContainer = new ServerContainer(wsConfig, context.getServer().getThreadPool()); EventDriverFactory edf = wsConfig.getFactory().getEventDriverFactory(); edf.clearImplementations(); edf.addImplementation(new InstJsrServerEndpointImpl(metrics)); edf.addImplementation(new InstJsrServerExtendsEndpointImpl(metrics)); context.addBean(wsContainer); context.setAttribute(javax.websocket.server.ServerContainer.class.getName(), wsContainer); context.setAttribute(WebSocketUpgradeFilter.class.getName(), filter); return wsContainer; } }
@Override public void start() throws Exception { for (MutableServletContextHandler environment : listeners.keySet()) { final SessionHandler sessionHandler = environment.getSessionHandler(); if (sessionHandler == null) { final String msg = String.format( "Can't register session listeners for %s because sessions support is not enabled: %s", environment.getDisplayName().toLowerCase(), Joiner.on(',').join(listeners.get(environment).stream() .map(it -> FeatureUtils.getInstanceClass(it).getSimpleName()) .collect(Collectors.toList()))); if (failWithoutSession) { throw new IllegalStateException(msg); } else { logger.warn(msg); } } else { listeners.get(environment).forEach(sessionHandler::addEventListener); } } }
keycloak.setAdapterConfig(getKeycloakConfiguration(configuration)); ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); environment.getApplicationContext().setSecurityHandler(securityHandler); environment.getApplicationContext().getSecurityHandler().setAuthenticator(keycloak);
@Override public void configure(Environment environment) { LOGGER.info("Registering jersey handler with root path prefix: {}", applicationContextPath); environment.getApplicationContext().setContextPath(applicationContextPath); LOGGER.info("Registering admin handler with root path prefix: {}", adminContextPath); environment.getAdminContext().setContextPath(adminContextPath); } }
private void initializeShiro(final ShiroConfiguration config, Environment environment) { if (config.isEnabled()) { LOG.debug("Shiro is enabled"); if (config.isDropwizardSessionHandler() && environment.getApplicationContext().getSessionHandler() == null) { LOG.debug("Adding DropWizard SessionHandler to environment."); environment.getApplicationContext().setSessionHandler(new SessionHandler()); } // This line ensure Shiro is configured and its .ini file found in the designated location. // e.g., via the shiroConfigLocations ContextParameter with fall-backs to default locations if that parameter isn't specified. environment.servlets().addServletListeners( new EnvironmentLoaderListener() ); final String filterUrlPattern = config.getSecuredUrlPattern(); LOG.debug("ShiroFilter will check URLs matching '{}'.", filterUrlPattern); environment.servlets().addFilter("shiro-filter", new ShiroFilter()).addMappingForUrlPatterns( EnumSet.allOf(DispatcherType.class), true, filterUrlPattern ); } else { LOG.debug("Shiro is not enabled"); } }
public static ComposedConfigurationAccessor getAccessor(Configuration configuration, Environment environment) { ComposedConfigurationAccessor accessor = (ComposedConfigurationAccessor)environment.getApplicationContext().getAttribute(ComposedConfigurationAccessor.class.getName()); if ( accessor == null ) { accessor = new ComposedConfigurationAccessor(configuration); environment.getApplicationContext().setAttribute(ComposedConfigurationAccessor.class.getName(), accessor); } return accessor; }
private void evaluateBlackList() throws Exception { if (!runPerServiceMode(blackList)) { return; } MapStore<Long> blackListIpValueStore = _injector.getInstance(Key.get(new TypeLiteral<MapStore<Long>>() { }, BlackListIpValueStore.class)); // Allow manually specifying IPs to be throttled or blacklisted. _environment.getApplicationContext().addFilter(new FilterHolder(new BlackListedIpFilter(blackListIpValueStore)), "/*", EnumSet.of(DispatcherType.REQUEST)); }
protected String getHealthCheckUrl() { final UriBuilder builder = UriBuilder.fromPath(environment.getAdminContext().getContextPath()); builder.path("healthcheck"); builder.scheme("http"); if (serviceAddress.get() == null) { builder.host("127.0.0.1"); } else { builder.host(serviceAddress.get()); } builder.port(serviceAdminPort.get()); return builder.build().toString(); } }
public static AdminResourceEnvironment getOrCreate(@NonNull @Nonnull final Environment environment) { final AdminResourceEnvironment currentEnvironment = environment.getAdminContext().getBean(AdminResourceEnvironment.class); if(currentEnvironment != null) { return currentEnvironment; } final AdminResourceEnvironment adminResourceEnvironment = new AdminResourceEnvironment(environment); environment.getAdminContext().addBean(adminResourceEnvironment); return adminResourceEnvironment; } }
@Override public void run(Configuration configuration, Environment environment) throws Exception { SoaConfiguration soaConfiguration = ComposedConfigurationAccessor.access(configuration, environment, SoaConfiguration.class); updateInstanceName(soaConfiguration); List<String> scopes = Lists.newArrayList(); scopes.add(soaConfiguration.getInstanceName()); scopes.add(soaConfiguration.getServiceName()); scopes.addAll(soaConfiguration.getScopes()); environment.getApplicationContext().setAttribute(DynamicAttributesBundle.Scopes.class.getName(), new Scopes(scopes)); // attributes must be allocated first - Discovery et al might need them DynamicAttributes attributes = StandardAttributesContainer.wrapAttributes(SoaBundle.checkManaged(environment, soaConfiguration.getAttributesFactory().build(configuration, environment, scopes)), SoaBundle.hasAdminKey); environment.getApplicationContext().setAttribute(DynamicAttributes.class.getName(), attributes); }
private void configureSessionsAndSecurity(MutableServletContextHandler handler, Server server) { handler.setServer(server); if (handler.isSecurityEnabled()) { handler.getSecurityHandler().setServer(server); } if (handler.isSessionsEnabled()) { handler.getSessionHandler().setServer(server); } }
protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) { configureSessionsAndSecurity(handler, server); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, Joiner.on(',').join(allowedMethods)); if (enableThreadNameFilter) { handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); } serverPush.addFilter(handler); if (jerseyContainer != null) { jerseyRootPath.ifPresent(jersey::setUrlPattern); jersey.register(new JacksonBinder(objectMapper)); jersey.register(new HibernateValidationFeature(validator)); if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) { jersey.register(new ExceptionMapperBinder(detailedJsonProcessingExceptionMapper)); } handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern()); } final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry); instrumented.setServer(server); instrumented.setHandler(handler); return instrumented; }