io.dropwizard.jetty.MutableServletContextHandler Java Examples

The following examples show how to use io.dropwizard.jetty.MutableServletContextHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KaramelServiceApplication.java    From karamel with Apache License 2.0 6 votes vote down vote up
public int getPort(Environment environment) {
  int defaultPort = 9090;
  MutableServletContextHandler h = environment.getApplicationContext();
  if (h == null) {
    return defaultPort;
  }
  Server s = h.getServer();
  if (s == null) {
    return defaultPort;
  }
  Connector[] c = s.getConnectors();
  if (c != null && c.length > 0) {
    AbstractNetworkConnector anc = (AbstractNetworkConnector) c[0];
    if (anc != null) {
      return anc.getLocalPort();
    }
  }
  return defaultPort;
}
 
Example #2
Source File: SessionListenersSupport.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@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);
        }
    }
}
 
Example #3
Source File: InstWebSocketServerContainerInitializer.java    From dropwizard-websockets with MIT License 5 votes vote down vote up
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;
}
 
Example #4
Source File: WebMappingsRenderer.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
private void renderContext(final MappingsConfig config,
                           final MutableServletContextHandler handler,
                           final String name,
                           final StringBuilder res) {
    final TreeNode root = new TreeNode("%s %s", name, handler.getContextPath());
    try {
        final Multimap<String, FilterReference> servletFilters = renderContextFilters(config, handler, root);

        // may be null if server not started and no servlets were registered (Guice Filter is always registered)
        if (handler.getServletHandler().getServletMappings() != null) {
            for (ServletMapping mapping : handler.getServletHandler().getServletMappings()) {
                final ServletHolder servlet = handler.getServletHandler().getServlet(mapping.getServletName());
                if (isAllowed(servlet.getClassName(), config)) {
                    renderServlet(mapping,
                            servlet,
                            servletFilters,
                            root);
                }
            }
        }
    } catch (Exception ex) {
        Throwables.throwIfUnchecked(ex);
        throw new IllegalStateException(ex);
    }
    if (root.hasChildren()) {
        res.append(NEWLINE).append(NEWLINE);
        root.render(res);
    }
}
 
Example #5
Source File: WebMappingsRenderer.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private Multimap<String, FilterReference> renderContextFilters(final MappingsConfig config,
                                                               final MutableServletContextHandler handler,
                                                               final TreeNode root) throws Exception {
    final Multimap<String, FilterReference> servletFilters = LinkedHashMultimap.create();
    for (FilterMapping mapping : handler.getServletHandler().getFilterMappings()) {
        final FilterHolder holder = handler.getServletHandler().getFilter(mapping.getFilterName());
        // single filter instance used for both contexts and so the name is also the same
        final boolean isGuiceFilter = mapping.getFilterName().equals(GuiceWebModule.GUICE_FILTER);
        if ((isGuiceFilter && !config.isGuiceMappings())
                || !isAllowed(holder.getClassName(), config)) {
            continue;
        }
        if (mapping.getServletNames() != null && mapping.getServletNames().length > 0) {
            // filters targeting exact servlet are only remembered to be shown below target servlet
            for (String servlet : mapping.getServletNames()) {
                servletFilters.put(servlet, new FilterReference(mapping, holder));
            }
        } else {
            final TreeNode filter = renderFilter(mapping, holder, root);
            if (isGuiceFilter) {
                renderGuiceWeb(filter);
            }
        }
    }
    return servletFilters;
}
 
Example #6
Source File: WebListenerInstaller.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
private void configure(final MutableServletContextHandler environment, final EventListener listener,
                       final boolean context, final boolean session) {
    if (session) {
        support.add(environment, listener);
    }
    if (context) {
        environment.addEventListener(listener);
    }
}
 
Example #7
Source File: SessionListenersSupport.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
public void add(final MutableServletContextHandler environment, final EventListener listener) {
    listeners.put(environment, listener);
}
 
Example #8
Source File: WebInstallersBundle.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
private void nameContext(final MutableServletContextHandler context, final String name) {
    if (context.getDisplayName() == null) {
        context.setDisplayName(name);
    }
}
 
Example #9
Source File: Pac4jBundle.java    From dropwizard-pac4j with Apache License 2.0 3 votes vote down vote up
/**
 * Override if needed, but prefer to exploit
 * {@link Pac4jFactory#setSessionEnabled(boolean)} first.
 * 
 * @param environment
 *            the dropwizard {@link Environment}
 * @since 1.1.0
 */
protected void setupJettySession(Environment environment) {
    MutableServletContextHandler contextHandler = environment
            .getApplicationContext();
    if (contextHandler.getSessionHandler() == null) {
        contextHandler.setSessionHandler(new SessionHandler());
    }
}