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);
    }
  }
Пример #2
0
 public ServletContextImpl(final ServletContainer servletContainer, final Deployment deployment) {
   this.servletContainer = servletContainer;
   this.deployment = deployment;
   this.deploymentInfo = deployment.getDeploymentInfo();
   sessionCookieConfig = new SessionCookieConfigImpl(this);
   ServletSessionConfig sc = deploymentInfo.getServletSessionConfig();
   if (sc != null) {
     sessionCookieConfig.setName(sc.getName());
     sessionCookieConfig.setComment(sc.getComment());
     sessionCookieConfig.setDomain(sc.getDomain());
     sessionCookieConfig.setHttpOnly(sc.isHttpOnly());
     sessionCookieConfig.setMaxAge(sc.getMaxAge());
     sessionCookieConfig.setPath(sc.getPath());
     sessionCookieConfig.setSecure(sc.isSecure());
     if (sc.getSessionTrackingModes() != null) {
       defaultSessionTrackingModes =
           sessionTrackingModes = new HashSet<SessionTrackingMode>(sc.getSessionTrackingModes());
     }
   }
   if (deploymentInfo.getServletContextAttributeBackingMap() == null) {
     this.attributes = new ConcurrentHashMap<String, Object>();
   } else {
     this.attributes = deploymentInfo.getServletContextAttributeBackingMap();
   }
   attributes.putAll(deployment.getDeploymentInfo().getServletContextAttributes());
 }
Пример #3
0
 @Override
 public boolean setInitParameter(final String name, final String value) {
   if (deploymentInfo.getInitParameters().containsKey(name)) {
     return false;
   }
   deploymentInfo.addInitParameter(name, value);
   return true;
 }
 private void createServletsAndFilters(
     final DeploymentImpl deployment, final DeploymentInfo deploymentInfo) {
   for (Map.Entry<String, ServletInfo> servlet : deploymentInfo.getServlets().entrySet()) {
     deployment.getServlets().addServlet(servlet.getValue());
   }
   for (Map.Entry<String, FilterInfo> filter : deploymentInfo.getFilters().entrySet()) {
     deployment.getFilters().addFilter(filter.getValue());
   }
 }
 private void initializeTempDir(
     final ServletContextImpl servletContext, final DeploymentInfo deploymentInfo) {
   if (deploymentInfo.getTempDir() != null) {
     servletContext.setAttribute(ServletContext.TEMPDIR, deploymentInfo.getTempDir());
   } else {
     servletContext.setAttribute(
         ServletContext.TEMPDIR, new File(SecurityActions.getSystemProperty("java.io.tmpdir")));
   }
 }
Пример #6
0
  public void deployServlet(
      String name, String contextPath, Class<? extends Servlet> servletClass) {
    DeploymentInfo deploymentInfo = new DeploymentInfo();
    deploymentInfo.setClassLoader(getClass().getClassLoader());
    deploymentInfo.setDeploymentName(name);
    deploymentInfo.setContextPath(contextPath);

    ServletInfo servlet = new ServletInfo(servletClass.getSimpleName(), servletClass);
    servlet.addMapping("/*");

    deploymentInfo.addServlet(servlet);
    server.getServer().deploy(deploymentInfo);
  }
Пример #7
0
 @Override
 public FilterRegistration.Dynamic addFilter(
     final String filterName, final Class<? extends Filter> filterClass) {
   ensureNotProgramaticListener();
   ensureNotInitialized();
   if (deploymentInfo.getFilters().containsKey(filterName)) {
     return null;
   }
   FilterInfo filter = new FilterInfo(filterName, filterClass);
   deploymentInfo.addFilter(filter);
   deployment.getFilters().addFilter(filter);
   return new FilterRegistrationImpl(filter, deployment);
 }
Пример #8
0
 @Override
 public ServletRegistration.Dynamic addServlet(
     final String servletName, final Class<? extends Servlet> servletClass) {
   ensureNotProgramaticListener();
   ensureNotInitialized();
   if (deploymentInfo.getServlets().containsKey(servletName)) {
     return null;
   }
   ServletInfo servlet = new ServletInfo(servletName, servletClass);
   deploymentInfo.addServlet(servlet);
   deployment.getServlets().addServlet(servlet);
   return new ServletRegistrationImpl(servlet, deployment);
 }
Пример #9
0
  @Override
  public FilterRegistration.Dynamic addFilter(final String filterName, final Filter filter) {
    ensureNotProgramaticListener();
    ensureNotInitialized();

    if (deploymentInfo.getFilters().containsKey(filterName)) {
      return null;
    }
    FilterInfo f =
        new FilterInfo(filterName, filter.getClass(), new ImmediateInstanceFactory<Filter>(filter));
    deploymentInfo.addFilter(f);
    deployment.getFilters().addFilter(f);
    return new FilterRegistrationImpl(f, deployment);
  }
Пример #10
0
 @Override
 public ServletRegistration.Dynamic addServlet(final String servletName, final Servlet servlet) {
   ensureNotProgramaticListener();
   ensureNotInitialized();
   if (deploymentInfo.getServlets().containsKey(servletName)) {
     return null;
   }
   ServletInfo s =
       new ServletInfo(
           servletName, servlet.getClass(), new ImmediateInstanceFactory<Servlet>(servlet));
   deploymentInfo.addServlet(s);
   deployment.getServlets().addServlet(s);
   return new ServletRegistrationImpl(s, deployment);
 }
Пример #11
0
 @Override
 public Set<String> getResourcePaths(final String path) {
   final Resource resource;
   try {
     resource = deploymentInfo.getResourceManager().getResource(path);
   } catch (IOException e) {
     return null;
   }
   if (resource == null || !resource.isDirectory()) {
     return null;
   }
   final Set<String> resources = new HashSet<String>();
   for (Resource res : resource.list()) {
     File file = res.getFile();
     if (file != null) {
       File base = res.getResourceManagerRoot();
       String filePath = file.getAbsolutePath().substring(base.getAbsolutePath().length());
       filePath = filePath.replace('\\', '/'); // for windows systems
       if (file.isDirectory()) {
         filePath = filePath + "/";
       }
       resources.add(filePath);
     }
   }
   return resources;
 }
  private HttpHandler handleDevelopmentModePersistentSessions(
      HttpHandler next,
      final DeploymentInfo deploymentInfo,
      final SessionManager sessionManager,
      final ServletContext servletContext) {
    final SessionPersistenceManager sessionPersistenceManager =
        deploymentInfo.getSessionPersistenceManager();
    if (sessionPersistenceManager != null) {
      SessionRestoringHandler handler =
          new ConvergedSessionRestoringHandler(
              super.getDeployment().getDeploymentInfo().getDeploymentName(),
              sessionManager,
              servletContext,
              next,
              sessionPersistenceManager);

      // ((DeploymentImpl)super.getDeployment()).addLifecycleObjects(handler);
      this.invokeDeploymentMethod(
          ((DeploymentImpl) super.getDeployment()),
          "addLifecycleObjects",
          new Class[] {SessionRestoringHandler.class},
          new Object[] {handler});

      return handler;
    }
    return next;
  }
Пример #13
0
 @Override
 public String getInitParameter(final String name) {
   if (name == null) {
     throw UndertowServletMessages.MESSAGES.nullName();
   }
   return deploymentInfo.getInitParameters().get(name);
 }
Пример #14
0
 private ServletChain createHandler(
     final DeploymentInfo deploymentInfo,
     final ServletHandler targetServlet,
     final Map<DispatcherType, List<ManagedFilter>> noExtension,
     final String servletPath,
     final boolean defaultServlet) {
   final ServletChain initialHandler;
   if (noExtension.isEmpty()) {
     initialHandler =
         servletChain(
             targetServlet,
             targetServlet.getManagedServlet(),
             servletPath,
             deploymentInfo,
             defaultServlet);
   } else {
     FilterHandler handler =
         new FilterHandler(
             noExtension, deploymentInfo.isAllowNonStandardWrappers(), targetServlet);
     initialHandler =
         servletChain(
             handler,
             targetServlet.getManagedServlet(),
             servletPath,
             deploymentInfo,
             defaultServlet);
   }
   return initialHandler;
 }
 private void initializeMimeMappings(
     final DeploymentImpl deployment, final DeploymentInfo deploymentInfo) {
   final Map<String, String> mappings = new HashMap<>(MimeMappings.DEFAULT_MIME_MAPPINGS);
   for (MimeMapping mapping : deploymentInfo.getMimeMappings()) {
     mappings.put(mapping.getExtension(), mapping.getMimeType());
   }
   deployment.setMimeExtensionMappings(mappings);
 }
Пример #16
0
 @Override
 public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
   ensureNotProgramaticListener();
   final Map<String, FilterRegistration> ret = new HashMap<String, FilterRegistration>();
   for (Map.Entry<String, FilterInfo> entry : deploymentInfo.getFilters().entrySet()) {
     ret.put(entry.getKey(), new FilterRegistrationImpl(entry.getValue(), deployment));
   }
   return ret;
 }
Пример #17
0
 @Override
 public FilterRegistration getFilterRegistration(final String filterName) {
   ensureNotProgramaticListener();
   final FilterInfo filterInfo = deploymentInfo.getFilters().get(filterName);
   if (filterInfo == null) {
     return null;
   }
   return new FilterRegistrationImpl(filterInfo, deployment);
 }
Пример #18
0
 @Override
 public ServletRegistration getServletRegistration(final String servletName) {
   ensureNotProgramaticListener();
   final ServletInfo servlet = deploymentInfo.getServlets().get(servletName);
   if (servlet == null) {
     return null;
   }
   return new ServletRegistrationImpl(servlet, deployment);
 }
Пример #19
0
 @Override
 public void addListener(final Class<? extends EventListener> listenerClass) {
   ensureNotInitialized();
   ensureNotProgramaticListener();
   if (ApplicationListeners.listenerState() != NO_LISTENER
       && ServletContextListener.class.isAssignableFrom(listenerClass)) {
     throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
   }
   InstanceFactory<? extends EventListener> factory = null;
   try {
     factory = deploymentInfo.getClassIntrospecter().createInstanceFactory(listenerClass);
   } catch (Exception e) {
     throw new IllegalArgumentException(e);
   }
   final ListenerInfo listener = new ListenerInfo(listenerClass, factory);
   deploymentInfo.addListener(listener);
   deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
 }
Пример #20
0
 @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);
   }
 }
Пример #21
0
 @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);
   }
 }
Пример #22
0
 @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);
   }
 }
  protected PathHandler mountServerEndpoints(
      final PathHandler pathHandler, List<Class<?>> serverEndpoints) throws ServletException {
    if (!serverEndpoints.isEmpty()) {
      DeploymentInfo builder =
          new DeploymentInfo().setClassLoader(this.getClass().getClassLoader()).setContextPath("/");
      WebSocketDeploymentInfo wsDeployInfo = new WebSocketDeploymentInfo();
      wsDeployInfo.setBuffers(new ByteBufferSlicePool(100, 1000));
      for (Class<?> serverEndpoint : serverEndpoints) {
        wsDeployInfo.addEndpoint(serverEndpoint);
      }
      builder.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, wsDeployInfo);
      builder.setDeploymentName("websocketDeploy.war");

      final ServletContainer container = ServletContainer.Factory.newInstance();
      DeploymentManager manager = container.addDeployment(builder);
      manager.deploy();
      pathHandler.addPrefixPath("/", manager.start());
    }
    return pathHandler;
  }
Пример #24
0
 // todo when this DeploymentInfo method of the same name is fixed.
 public boolean isAuthenticationMechanismPresent(
     DeploymentInfo deploymentInfo, final String mechanismName) {
   LoginConfig loginConfig = deploymentInfo.getLoginConfig();
   if (loginConfig != null) {
     for (AuthMethodConfig method : loginConfig.getAuthMethods()) {
       if (method.getName().equalsIgnoreCase(mechanismName)) {
         return true;
       }
     }
   }
   return false;
 }
Пример #25
0
 private static ServletChain servletChain(
     HttpHandler next,
     final ManagedServlet managedServlet,
     final String servletPath,
     final DeploymentInfo deploymentInfo,
     boolean defaultServlet) {
   HttpHandler servletHandler =
       new ServletSecurityRoleHandler(next, deploymentInfo.getAuthorizationManager());
   servletHandler =
       wrapHandlers(servletHandler, managedServlet.getServletInfo().getHandlerChainWrappers());
   return new ServletChain(servletHandler, managedServlet, servletPath, defaultServlet);
 }
Пример #26
0
 public void handleDeploymentSessionConfig(
     DeploymentInfo deploymentInfo, ServletContextImpl servletContext) {
   SessionCookieConfigImpl sessionCookieConfig = servletContext.getSessionCookieConfig();
   ServletSessionConfig sc = deploymentInfo.getServletSessionConfig();
   if (sc != null) {
     sessionCookieConfig.setName(sc.getName());
     sessionCookieConfig.setComment(sc.getComment());
     sessionCookieConfig.setDomain(sc.getDomain());
     sessionCookieConfig.setHttpOnly(sc.isHttpOnly());
     sessionCookieConfig.setMaxAge(sc.getMaxAge());
     if (sc.getPath() != null) {
       sessionCookieConfig.setPath(sc.getPath());
     } else {
       sessionCookieConfig.setPath(deploymentInfo.getContextPath());
     }
     sessionCookieConfig.setSecure(sc.isSecure());
     if (sc.getSessionTrackingModes() != null) {
       servletContext.setDefaultSessionTrackingModes(new HashSet<>(sc.getSessionTrackingModes()));
     }
   }
 }
  @BeforeClass
  public static void setup() throws Exception {
    js =
        UndertowJS.builder()
            .addResources(
                new ClassPathResourceManager(
                    JavascriptSecurityTestCase.class.getClassLoader(),
                    JavascriptSecurityTestCase.class.getPackage()),
                "security.js")
            .build();
    js.start();

    final ServletContainer container = ServletContainer.Factory.newInstance();

    ServletIdentityManager identityManager = new ServletIdentityManager();
    identityManager.addUser("user1", "password1", "admin");
    identityManager.addUser("user2", "password2", "user");

    DeploymentInfo builder =
        new DeploymentInfo()
            .setClassLoader(JavascriptSecurityTestCase.class.getClassLoader())
            .setContextPath("/servletContext")
            .setDeploymentName("servletContext.war")
            .setIdentityManager(identityManager)
            .setLoginConfig(new LoginConfig("BASIC", "Test Realm"))
            .addInnerHandlerChainWrapper(
                new HandlerWrapper() {
                  @Override
                  public HttpHandler wrap(HttpHandler handler) {
                    return js.getHandler(handler);
                  }
                });

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    PathHandler root = new PathHandler();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
  }
  @Test
  public void testExtensionMatchServletWithGlobalFilter() throws IOException, ServletException {

    DeploymentInfo builder = new DeploymentInfo();

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    builder.addServlet(new ServletInfo("*.jsp", PathMappingServlet.class).addMapping("*.jsp"));

    builder.addFilter(new FilterInfo("/*", PathFilter.class));
    builder.addFilterUrlMapping("/*", "/*", DispatcherType.REQUEST);

    builder
        .setClassIntrospecter(TestClassIntrospector.INSTANCE)
        .setClassLoader(FilterPathMappingTestCase.class.getClassLoader())
        .setContextPath("/servletContext")
        .setDeploymentName("servletContext.war");

    final DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);

    TestHttpClient client = new TestHttpClient();
    try {
      runTest(client, "aa.jsp", "*.jsp - /aa.jsp - null", "/*");

    } finally {
      client.getConnectionManager().shutdown();
    }
  }
  private void deployServlet(final ServerWebSocketContainer deployment) throws ServletException {

    final DeploymentInfo builder;
    builder =
        new DeploymentInfo()
            .setClassLoader(getClass().getClassLoader())
            .setContextPath("/")
            .setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("websocket.war")
            .addFilter(new FilterInfo("filter", JsrWebSocketFilter.class))
            .addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST)
            .addServletContextAttribute(
                javax.websocket.server.ServerContainer.class.getName(), deployment);

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
  }
Пример #30
0
 @Override
 public <T extends EventListener> void addListener(final T t) {
   ensureNotInitialized();
   ensureNotProgramaticListener();
   if (ApplicationListeners.listenerState() != NO_LISTENER
       && ServletContextListener.class.isAssignableFrom(t.getClass())) {
     throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
   }
   ListenerInfo listener =
       new ListenerInfo(t.getClass(), new ImmediateInstanceFactory<EventListener>(t));
   deploymentInfo.addListener(listener);
   deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
 }