Search in sources :

Example 1 with SecurityConstaintBuilder

use of org.apache.tomee.embedded.SecurityConstaintBuilder in project tomee by apache.

the class StandardContextCustomizer method customize.

public void customize(@Observes final LifecycleEvent event) {
    final Object data = event.getSource();
    if (!StandardContext.class.isInstance(data)) {
        return;
    }
    final StandardContext context = StandardContext.class.cast(data);
    final String contextRoot = module.getContextRoot();
    final String path = context.getPath();
    final boolean rightPath = (path.isEmpty() && contextRoot.equals(path)) || (contextRoot.startsWith("/") ? contextRoot : '/' + contextRoot).equals(path);
    if (!rightPath) {
        return;
    }
    switch(event.getType()) {
        case Lifecycle.BEFORE_START_EVENT:
            final StandardRoot resources = new StandardRoot(context);
            resources.setCachingAllowed(config.areWebResourcesCached());
            context.setResources(resources);
            if (!module.getProperties().containsKey("fakeJarLocation")) {
                context.setDocBase(module.getJarLocation());
            }
            // move last fake folder, tomcat is broken without it so we can't remove it
            final List allResources = List.class.cast(Reflections.get(resources, "allResources"));
            final Object mainResources = allResources.remove(1);
            allResources.add(mainResources);
            for (final URL url : module.getScannableUrls()) {
                final File file = URLs.toFile(url);
                final String absolutePath = file.getAbsolutePath();
                if (file.isDirectory()) {
                    resources.createWebResourceSet(WebResourceRoot.ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", absolutePath, "", "/");
                    if (new File(file, "META-INF/resources").exists()) {
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", absolutePath, "", "/META-INF/resources");
                    }
                } else {
                    if (absolutePath.endsWith(".jar") || Boolean.getBoolean("tomee.embedded.resources.add-war-as-jar")) {
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.CLASSES_JAR, "/WEB-INF/lib", absolutePath, null, "/");
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources");
                    }
                // else endsWith .war => ignore
                }
            }
            if (config.getCustomWebResources() != null) {
                for (final String web : config.getCustomWebResources()) {
                    final File file = new File(web);
                    if (file.isDirectory()) {
                        try {
                            resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", file.toURI().toURL(), "/");
                        } catch (final MalformedURLException e) {
                            throw new IllegalArgumentException(e);
                        }
                    } else {
                        Logger.getLogger(StandardContextCustomizer.class.getName()).warning("'" + web + "' is not a directory, ignoring");
                    }
                }
            }
            if (config.getLoginConfig() != null) {
                context.setLoginConfig(config.getLoginConfig().build());
            }
            for (final SecurityConstaintBuilder sc : config.getSecurityConstraints()) {
                context.addConstraint(sc.build());
            }
            if (config.getWebXml() != null) {
                context.getServletContext().setAttribute(Globals.ALT_DD_ATTR, config.getWebXml());
            }
            if (loader != null) {
                context.setLoader(new ProvidedLoader(loader));
            }
            break;
        case Lifecycle.CONFIGURE_START_EVENT:
            SystemInstance.get().getComponent(TomcatWebAppBuilder.class).setFinderOnContextConfig(context, module.appModule());
            break;
        default:
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) TomcatWebAppBuilder(org.apache.tomee.catalina.TomcatWebAppBuilder) StandardRoot(org.apache.catalina.webresources.StandardRoot) URL(java.net.URL) StandardContext(org.apache.catalina.core.StandardContext) List(java.util.List) File(java.io.File) SecurityConstaintBuilder(org.apache.tomee.embedded.SecurityConstaintBuilder)

Aggregations

File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 List (java.util.List)1 StandardContext (org.apache.catalina.core.StandardContext)1 StandardRoot (org.apache.catalina.webresources.StandardRoot)1 TomcatWebAppBuilder (org.apache.tomee.catalina.TomcatWebAppBuilder)1 SecurityConstaintBuilder (org.apache.tomee.embedded.SecurityConstaintBuilder)1