private void handleExtensions(
      final DeploymentInfo deploymentInfo, final ServletContext servletContext) {
    Set<Class<?>> loadedExtensions = new HashSet<>();

    for (ServletExtension extension :
        ServiceLoader.load(ServletExtension.class, deploymentInfo.getClassLoader())) {
      loadedExtensions.add(extension.getClass());
      extension.handleDeployment(deploymentInfo, servletContext);
    }

    if (!ServletExtension.class.getClassLoader().equals(deploymentInfo.getClassLoader())) {
      for (ServletExtension extension : ServiceLoader.load(ServletExtension.class)) {

        // Note: If the CLs are different, but can the see the same extensions and extension might
        // get loaded
        // and thus instantiated twice, but the handleDeployment() is executed only once.

        if (!loadedExtensions.contains(extension.getClass())) {
          extension.handleDeployment(deploymentInfo, servletContext);
        }
      }
    }
    for (ServletExtension extension : deploymentInfo.getServletExtensions()) {
      extension.handleDeployment(deploymentInfo, servletContext);
    }
  }
 @Override
 public void addListener(final String className) {
   try {
     Class<? extends EventListener> clazz =
         (Class<? extends EventListener>) deploymentInfo.getClassLoader().loadClass(className);
     addListener(clazz);
   } catch (ClassNotFoundException e) {
     throw new IllegalArgumentException(e);
   }
 }
Example #3
0
  @Override
  public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
    WebSocketDeploymentInfo info =
        (WebSocketDeploymentInfo)
            deploymentInfo
                .getServletContextAttributes()
                .get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);

    if (info == null) {
      return;
    }
    if (info.getWorker() == null) {
      JsrWebSocketLogger.ROOT_LOGGER.xnioWorkerWasNull();
    }
    final List<ThreadSetupAction> setup = new ArrayList<ThreadSetupAction>();
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup);
    ServerWebSocketContainer container =
        new ServerWebSocketContainer(
            deploymentInfo.getClassIntrospecter(),
            servletContext.getClassLoader(),
            info.getWorker(),
            info.getBuffers(),
            threadSetupAction,
            info.isDispatchToWorkerThread());
    try {
      for (Class<?> annotation : info.getAnnotatedEndpoints()) {
        container.addEndpoint(annotation);
      }
      for (ServerEndpointConfig programatic : info.getProgramaticEndpoints()) {
        container.addEndpoint(programatic);
      }
    } catch (DeploymentException e) {
      throw new RuntimeException(e);
    }
    servletContext.setAttribute(ServerContainer.class.getName(), container);
    info.containerReady(container);
    SecurityActions.addContainer(deploymentInfo.getClassLoader(), container);

    deploymentInfo.addListener(Servlets.listener(WebSocketListener.class));
  }
 @Override
 public FilterRegistration.Dynamic addFilter(final String filterName, final String className) {
   ensureNotProgramaticListener();
   ensureNotInitialized();
   if (deploymentInfo.getFilters().containsKey(filterName)) {
     return null;
   }
   try {
     FilterInfo filter =
         new FilterInfo(
             filterName,
             (Class<? extends Filter>) deploymentInfo.getClassLoader().loadClass(className));
     deploymentInfo.addFilter(filter);
     deployment.getFilters().addFilter(filter);
     return new FilterRegistrationImpl(filter, deployment);
   } catch (ClassNotFoundException e) {
     throw UndertowServletMessages.MESSAGES.cannotLoadClass(className, e);
   }
 }
 @Override
 public ServletRegistration.Dynamic addServlet(final String servletName, final String className) {
   ensureNotProgramaticListener();
   ensureNotInitialized();
   try {
     if (deploymentInfo.getServlets().containsKey(servletName)) {
       return null;
     }
     ServletInfo servlet =
         new ServletInfo(
             servletName,
             (Class<? extends Servlet>) deploymentInfo.getClassLoader().loadClass(className));
     deploymentInfo.addServlet(servlet);
     deployment.getServlets().addServlet(servlet);
     return new ServletRegistrationImpl(servlet, deployment);
   } catch (ClassNotFoundException e) {
     throw UndertowServletMessages.MESSAGES.cannotLoadClass(className, e);
   }
 }
  @Override
  public void deploy() {
    DeploymentInfo deploymentInfo = originalDeployment.clone();

    deploymentInfo.validate();
    final DeploymentImpl deployment = new DeploymentImpl(deploymentInfo);
    this.deployment = deployment;

    final ServletContextImpl servletContext = new ServletContextImpl(servletContainer, deployment);
    deployment.setServletContext(servletContext);

    final List<ThreadSetupAction> setup = new ArrayList<ThreadSetupAction>();
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup);
    deployment.setThreadSetupAction(threadSetupAction);

    ThreadSetupAction.Handle handle = threadSetupAction.setup(null);
    try {

      final ApplicationListeners listeners = createListeners();
      deployment.setApplicationListeners(listeners);
      // first run the SCI's
      for (final ServletContainerInitializerInfo sci :
          deploymentInfo.getServletContainerInitializers()) {
        final InstanceHandle<? extends ServletContainerInitializer> instance =
            sci.getInstanceFactory().createInstance();
        try {
          instance.getInstance().onStartup(sci.getHandlesTypes(), servletContext);
        } finally {
          instance.release();
        }
      }

      listeners.contextInitialized();
      initializeErrorPages(deployment, deploymentInfo);
      initializeMimeMappings(deployment, deploymentInfo);
      initializeTempDir(servletContext, deploymentInfo);
      // run

      ServletPathMatches matches = setupServletChains(servletContext, threadSetupAction, listeners);
      deployment.setServletPaths(matches);

      HttpHandler wrappedHandlers = ServletDispatchingHandler.INSTANCE;
      wrappedHandlers =
          wrapHandlers(wrappedHandlers, deploymentInfo.getInnerHandlerChainWrappers());
      HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
      wrappedHandlers =
          new PredicateHandler(DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);
      wrappedHandlers =
          wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
      final ServletInitialHandler servletInitialHandler =
          new ServletInitialHandler(
              matches, wrappedHandlers, deployment.getThreadSetupAction(), servletContext);
      deployment.setServletHandler(servletInitialHandler);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      handle.tearDown();
    }
    state = State.DEPLOYED;
  }
  @Override
  public void deploy() {
    DeploymentInfo deploymentInfo = originalDeployment.clone();

    if (deploymentInfo.getServletStackTraces() == ServletStackTraces.ALL) {
      UndertowServletLogger.REQUEST_LOGGER.servletStackTracesAll(
          deploymentInfo.getDeploymentName());
    }

    deploymentInfo.validate();
    final ConvergedDeploymentImpl deployment =
        new ConvergedDeploymentImpl(this, deploymentInfo, servletContainer);

    // set deployment to parent using reflection
    try {
      Field deploymentField = DeploymentManagerImpl.class.getDeclaredField("deployment");
      deploymentField.setAccessible(true);
      deploymentField.set(this, deployment);
      deploymentField.setAccessible(false);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      throw new RuntimeException(e);
    }

    final ServletContextImpl servletContexImpl =
        new ServletContextImpl(servletContainer, deployment);
    final ServletContext servletContext = new ConvergedServletContextImpl(servletContexImpl);

    // deployment.setServletContext(((ConvergedServletContextImpl)servletContext).getDelegatedContext());
    this.invokeDeploymentMethod(
        deployment,
        "setServletContext",
        new Class[] {ServletContextImpl.class},
        new Object[] {((ConvergedServletContextImpl) servletContext).getDelegatedContext()});
    deployment.setConvergedServletContext((ConvergedServletContextImpl) servletContext);

    handleExtensions(deploymentInfo, servletContext);

    deployment.setDefaultCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));

    handleDeploymentSessionConfig(
        deploymentInfo, ((ConvergedServletContextImpl) servletContext).getDelegatedContext());

    // deployment.setSessionManager(deploymentInfo.getSessionManagerFactory().createSessionManager(deployment));
    this.invokeDeploymentMethod(
        deployment,
        "setSessionManager",
        new Class[] {SessionManager.class},
        new Object[] {deploymentInfo.getSessionManagerFactory().createSessionManager(deployment)});

    deployment
        .getSessionManager()
        .setDefaultSessionTimeout(deploymentInfo.getDefaultSessionTimeout());
    for (SessionListener listener : deploymentInfo.getSessionListeners()) {
      deployment.getSessionManager().registerSessionListener(listener);
    }

    final List<ThreadSetupAction> setup = new ArrayList<>();
    setup.add(ServletRequestContextThreadSetupAction.INSTANCE);
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup);
    deployment.setThreadSetupAction(threadSetupAction);

    ThreadSetupAction.Handle handle = threadSetupAction.setup(null);
    try {

      final ApplicationListeners listeners = createListeners();
      listeners.start();

      // deployment.setApplicationListeners(listeners);
      this.invokeDeploymentMethod(
          deployment,
          "setApplicationListeners",
          new Class[] {ApplicationListeners.class},
          new Object[] {listeners});

      // now create the servlets and filters that we know about. We can still get more later
      createServletsAndFilters(deployment, deploymentInfo);

      // first run the SCI's
      for (final ServletContainerInitializerInfo sci :
          deploymentInfo.getServletContainerInitializers()) {
        final InstanceHandle<? extends ServletContainerInitializer> instance =
            sci.getInstanceFactory().createInstance();
        try {
          instance.getInstance().onStartup(sci.getHandlesTypes(), servletContext);
        } finally {
          instance.release();
        }
      }

      deployment
          .getSessionManager()
          .registerSessionListener(
              new ConvergedSessionListenerBridge(
                  threadSetupAction, listeners, servletContext, deployment.getSessionManager()));

      initializeErrorPages(deployment, deploymentInfo);
      initializeMimeMappings(deployment, deploymentInfo);
      initializeTempDir(servletContext, deploymentInfo);
      listeners.contextInitialized();
      // run

      HttpHandler wrappedHandlers = ServletDispatchingHandler.INSTANCE;
      wrappedHandlers =
          wrapHandlers(wrappedHandlers, deploymentInfo.getInnerHandlerChainWrappers());
      HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
      wrappedHandlers =
          new PredicateHandler(DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);

      HttpHandler outerHandlers =
          wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
      wrappedHandlers =
          new PredicateHandler(DispatcherTypePredicate.REQUEST, outerHandlers, wrappedHandlers);
      wrappedHandlers =
          handleDevelopmentModePersistentSessions(
              wrappedHandlers, deploymentInfo, deployment.getSessionManager(), servletContext);

      MetricsCollector metrics = deploymentInfo.getMetricsCollector();
      if (metrics != null) {
        wrappedHandlers = new MetricsChainHandler(wrappedHandlers, metrics, deployment);
      }

      final ServletInitialHandler servletInitialHandler =
          SecurityActions.createServletInitialHandler(
              deployment.getServletPaths(),
              wrappedHandlers,
              deployment.getThreadSetupAction(),
              servletContext);

      HttpHandler initialHandler =
          wrapHandlers(
              servletInitialHandler,
              deployment.getDeploymentInfo().getInitialHandlerChainWrappers());
      initialHandler = new HttpContinueReadHandler(initialHandler);
      if (deploymentInfo.getUrlEncoding() != null) {
        initialHandler =
            Handlers.urlDecodingHandler(deploymentInfo.getUrlEncoding(), initialHandler);
      }
      deployment.setInitialHandler(initialHandler);

      // deployment.setServletHandler(servletInitialHandler);
      this.invokeDeploymentMethod(
          deployment,
          "setServletHandler",
          new Class[] {ServletInitialHandler.class},
          new Object[] {servletInitialHandler});

      deployment.getServletPaths().invalidate(); // make sure we have a fresh set of servlet paths
      ((ConvergedServletContextImpl) servletContext).initDone();
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      handle.tearDown();
    }

    // set state using reflection:
    try {
      Field stateField = DeploymentManagerImpl.class.getDeclaredField("state");
      stateField.setAccessible(true);
      stateField.set(this, State.DEPLOYED);
      stateField.setAccessible(false);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }
  @Override
  public void deploy() {
    final DeploymentInfo deploymentInfo = originalDeployment.clone();

    if (deploymentInfo.getServletStackTraces() == ServletStackTraces.ALL) {
      UndertowServletLogger.REQUEST_LOGGER.servletStackTracesAll(
          deploymentInfo.getDeploymentName());
    }

    deploymentInfo.validate();
    final DeploymentImpl deployment = new DeploymentImpl(this, deploymentInfo, servletContainer);
    this.deployment = deployment;

    final List<ThreadSetupHandler> setup = new ArrayList<>();
    setup.add(ServletRequestContextThreadSetupAction.INSTANCE);
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    deployment.setThreadSetupActions(setup);

    final ServletContextImpl servletContext = new ServletContextImpl(servletContainer, deployment);
    deployment.setServletContext(servletContext);
    handleExtensions(deploymentInfo, servletContext);
    deployment.getServletPaths().setWelcomePages(deploymentInfo.getWelcomePages());

    deployment.setDefaultCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));

    handleDeploymentSessionConfig(deploymentInfo, servletContext);

    deployment.setSessionManager(
        deploymentInfo.getSessionManagerFactory().createSessionManager(deployment));
    deployment
        .getSessionManager()
        .setDefaultSessionTimeout(deploymentInfo.getDefaultSessionTimeout());

    try {
      deployment
          .createThreadSetupAction(
              new ThreadSetupHandler.Action<Void, Object>() {
                @Override
                public Void call(HttpServerExchange exchange, Object ignore) throws Exception {
                  final ApplicationListeners listeners = createListeners();
                  listeners.start();

                  deployment.setApplicationListeners(listeners);

                  // now create the servlets and filters that we know about. We can still get more
                  // later
                  createServletsAndFilters(deployment, deploymentInfo);

                  // first run the SCI's
                  for (final ServletContainerInitializerInfo sci :
                      deploymentInfo.getServletContainerInitializers()) {
                    final InstanceHandle<? extends ServletContainerInitializer> instance =
                        sci.getInstanceFactory().createInstance();
                    try {
                      instance.getInstance().onStartup(sci.getHandlesTypes(), servletContext);
                    } finally {
                      instance.release();
                    }
                  }

                  deployment
                      .getSessionManager()
                      .registerSessionListener(
                          new SessionListenerBridge(deployment, listeners, servletContext));
                  for (SessionListener listener : deploymentInfo.getSessionListeners()) {
                    deployment.getSessionManager().registerSessionListener(listener);
                  }

                  initializeErrorPages(deployment, deploymentInfo);
                  initializeMimeMappings(deployment, deploymentInfo);
                  initializeTempDir(servletContext, deploymentInfo);
                  listeners.contextInitialized();
                  // run

                  HttpHandler wrappedHandlers = ServletDispatchingHandler.INSTANCE;
                  wrappedHandlers =
                      wrapHandlers(wrappedHandlers, deploymentInfo.getInnerHandlerChainWrappers());
                  if (!deploymentInfo.isSecurityDisabled()) {
                    HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
                    wrappedHandlers =
                        new PredicateHandler(
                            DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);
                  }
                  HttpHandler outerHandlers =
                      wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
                  wrappedHandlers =
                      new PredicateHandler(
                          DispatcherTypePredicate.REQUEST, outerHandlers, wrappedHandlers);
                  wrappedHandlers =
                      handleDevelopmentModePersistentSessions(
                          wrappedHandlers,
                          deploymentInfo,
                          deployment.getSessionManager(),
                          servletContext);

                  MetricsCollector metrics = deploymentInfo.getMetricsCollector();
                  if (metrics != null) {
                    wrappedHandlers = new MetricsChainHandler(wrappedHandlers, metrics, deployment);
                  }
                  if (deploymentInfo.getCrawlerSessionManagerConfig() != null) {
                    wrappedHandlers =
                        new CrawlerSessionManagerHandler(
                            deploymentInfo.getCrawlerSessionManagerConfig(), wrappedHandlers);
                  }

                  final ServletInitialHandler servletInitialHandler =
                      SecurityActions.createServletInitialHandler(
                          deployment.getServletPaths(),
                          wrappedHandlers,
                          deployment,
                          servletContext);

                  HttpHandler initialHandler =
                      wrapHandlers(
                          servletInitialHandler,
                          deployment.getDeploymentInfo().getInitialHandlerChainWrappers());
                  initialHandler = new HttpContinueReadHandler(initialHandler);
                  if (deploymentInfo.getUrlEncoding() != null) {
                    initialHandler =
                        Handlers.urlDecodingHandler(
                            deploymentInfo.getUrlEncoding(), initialHandler);
                  }
                  deployment.setInitialHandler(initialHandler);
                  deployment.setServletHandler(servletInitialHandler);
                  deployment
                      .getServletPaths()
                      .invalidate(); // make sure we have a fresh set of servlet paths
                  servletContext.initDone();
                  return null;
                }
              })
          .call(null, null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    state = State.DEPLOYED;
  }
 @Override
 public ClassLoader getClassLoader() {
   return deploymentInfo.getClassLoader();
 }