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); } }
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()); }
@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"))); } }
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); }
@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); }
@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); }
@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); }
@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); }
@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; }
@Override public String getInitParameter(final String name) { if (name == null) { throw UndertowServletMessages.MESSAGES.nullName(); } return deploymentInfo.getInitParameters().get(name); }
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); }
@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; }
@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); }
@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); }
@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)); }
@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 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; }
// 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; }
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); }
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); }
@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)); }