Search in sources :

Example 16 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jetty.project by eclipse.

the class ResourceCacheTest method testMutlipleSources1.

@Test
public void testMutlipleSources1() throws Exception {
    ResourceCollection rc = new ResourceCollection(new String[] { "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/one/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/two/", "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/three/" });
    Resource[] r = rc.getResources();
    MimeTypes mime = new MimeTypes();
    CachedContentFactory rc3 = new CachedContentFactory(null, r[2], mime, false, false, CompressedContentFormat.NONE);
    CachedContentFactory rc2 = new CachedContentFactory(rc3, r[1], mime, false, false, CompressedContentFormat.NONE);
    CachedContentFactory rc1 = new CachedContentFactory(rc2, r[0], mime, false, false, CompressedContentFormat.NONE);
    assertEquals("1 - one", getContent(rc1, "1.txt"));
    assertEquals("2 - two", getContent(rc1, "2.txt"));
    assertEquals("3 - three", getContent(rc1, "3.txt"));
    assertEquals("1 - two", getContent(rc2, "1.txt"));
    assertEquals("2 - two", getContent(rc2, "2.txt"));
    assertEquals("3 - three", getContent(rc2, "3.txt"));
    assertEquals(null, getContent(rc3, "1.txt"));
    assertEquals("2 - three", getContent(rc3, "2.txt"));
    assertEquals("3 - three", getContent(rc3, "3.txt"));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) MimeTypes(org.eclipse.jetty.http.MimeTypes) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 17 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jetty.project by eclipse.

the class ResourceCacheTest method testNoextension.

@Test
public void testNoextension() throws Exception {
    ResourceCollection rc = new ResourceCollection(new String[] { "../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/four/" });
    Resource[] resources = rc.getResources();
    MimeTypes mime = new MimeTypes();
    CachedContentFactory cache = new CachedContentFactory(null, resources[0], mime, false, false, CompressedContentFormat.NONE);
    assertEquals("4 - four", getContent(cache, "four.txt"));
    assertEquals("4 - four (no extension)", getContent(cache, "four"));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) MimeTypes(org.eclipse.jetty.http.MimeTypes) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 18 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jetty.project by eclipse.

the class WebAppContextTest method testAlias.

@Test
public void testAlias() throws Exception {
    File dir = File.createTempFile("dir", null);
    dir.delete();
    dir.mkdir();
    dir.deleteOnExit();
    File webinf = new File(dir, "WEB-INF");
    webinf.mkdir();
    File classes = new File(dir, "classes");
    classes.mkdir();
    File someclass = new File(classes, "SomeClass.class");
    someclass.createNewFile();
    WebAppContext context = new WebAppContext();
    context.setBaseResource(new ResourceCollection(dir.getAbsolutePath()));
    context.setResourceAlias("/WEB-INF/classes/", "/classes/");
    assertTrue(Resource.newResource(context.getServletContext().getResource("/WEB-INF/classes/SomeClass.class")).exists());
    assertTrue(Resource.newResource(context.getServletContext().getResource("/classes/SomeClass.class")).exists());
}
Also used : File(java.io.File) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 19 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jetty.project by eclipse.

the class WebInfConfiguration method configure.

@Override
public void configure(WebAppContext context) throws Exception {
    //cannot configure if the context is already started
    if (context.isStarted()) {
        if (LOG.isDebugEnabled())
            LOG.debug("Cannot configure webapp " + context + " after it is started");
        return;
    }
    Resource web_inf = context.getWebInf();
    // Add WEB-INF classes and lib classpaths
    if (web_inf != null && web_inf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader) {
        // Look for classes directory
        Resource classes = web_inf.addPath("classes/");
        if (classes.exists())
            ((WebAppClassLoader) context.getClassLoader()).addClassPath(classes);
        // Look for jars
        Resource lib = web_inf.addPath("lib/");
        if (lib.exists() || lib.isDirectory())
            ((WebAppClassLoader) context.getClassLoader()).addJars(lib);
    }
    // Look for extra resource
    @SuppressWarnings("unchecked") Set<Resource> resources = (Set<Resource>) context.getAttribute(RESOURCE_DIRS);
    if (resources != null && !resources.isEmpty()) {
        Resource[] collection = new Resource[resources.size() + 1];
        int i = 0;
        collection[i++] = context.getBaseResource();
        for (Resource resource : resources) collection[i++] = resource;
        context.setBaseResource(new ResourceCollection(collection));
    }
}
Also used : Set(java.util.Set) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 20 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project jetty.project by eclipse.

the class WebInfConfiguration method unpack.

public void unpack(WebAppContext context) throws IOException {
    Resource web_app = context.getBaseResource();
    _preUnpackBaseResource = context.getBaseResource();
    if (web_app == null) {
        String war = context.getWar();
        if (war != null && war.length() > 0)
            web_app = context.newResource(war);
        else
            web_app = context.getBaseResource();
        if (web_app == null)
            throw new IllegalStateException("No resourceBase or war set for context");
        // Accept aliases for WAR files
        if (web_app.isAlias()) {
            LOG.debug(web_app + " anti-aliased to " + web_app.getAlias());
            web_app = context.newResource(web_app.getAlias());
        }
        if (LOG.isDebugEnabled())
            LOG.debug("Try webapp=" + web_app + ", exists=" + web_app.exists() + ", directory=" + web_app.isDirectory() + " file=" + (web_app.getFile()));
        // Is the WAR usable directly?
        if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:")) {
            // No - then lets see if it can be turned into a jar URL.
            Resource jarWebApp = JarResource.newJarResource(web_app);
            if (jarWebApp.exists() && jarWebApp.isDirectory())
                web_app = jarWebApp;
        }
        // If we should extract or the URL is still not usable
        if (web_app.exists() && ((context.isCopyWebDir() && web_app.getFile() != null && web_app.getFile().isDirectory()) || (context.isExtractWAR() && web_app.getFile() != null && !web_app.getFile().isDirectory()) || (context.isExtractWAR() && web_app.getFile() == null) || !web_app.isDirectory())) {
            // Look for sibling directory.
            File extractedWebAppDir = null;
            if (war != null) {
                // look for a sibling like "foo/" to a "foo.war"
                File warfile = Resource.newResource(war).getFile();
                if (warfile != null && warfile.getName().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                    File sibling = new File(warfile.getParent(), warfile.getName().substring(0, warfile.getName().length() - 4));
                    if (sibling.exists() && sibling.isDirectory() && sibling.canWrite())
                        extractedWebAppDir = sibling;
                }
            }
            if (extractedWebAppDir == null)
                // Then extract it if necessary to the temporary location
                extractedWebAppDir = new File(context.getTempDirectory(), "webapp");
            if (web_app.getFile() != null && web_app.getFile().isDirectory()) {
                // Copy directory
                LOG.debug("Copy " + web_app + " to " + extractedWebAppDir);
                web_app.copyTo(extractedWebAppDir);
            } else {
                //Use a sentinel file that will exist only whilst the extraction is taking place.
                //This will help us detect interrupted extractions.
                File extractionLock = new File(context.getTempDirectory(), ".extract_lock");
                if (!extractedWebAppDir.exists()) {
                    //it hasn't been extracted before so extract it
                    extractionLock.createNewFile();
                    extractedWebAppDir.mkdir();
                    LOG.debug("Extract " + web_app + " to " + extractedWebAppDir);
                    Resource jar_web_app = JarResource.newJarResource(web_app);
                    jar_web_app.copyTo(extractedWebAppDir);
                    extractionLock.delete();
                } else {
                    //only extract if the war file is newer, or a .extract_lock file is left behind meaning a possible partial extraction
                    if (web_app.lastModified() > extractedWebAppDir.lastModified() || extractionLock.exists()) {
                        extractionLock.createNewFile();
                        IO.delete(extractedWebAppDir);
                        extractedWebAppDir.mkdir();
                        LOG.debug("Extract " + web_app + " to " + extractedWebAppDir);
                        Resource jar_web_app = JarResource.newJarResource(web_app);
                        jar_web_app.copyTo(extractedWebAppDir);
                        extractionLock.delete();
                    }
                }
            }
            web_app = Resource.newResource(extractedWebAppDir.getCanonicalPath());
        }
        // Now do we have something usable?
        if (!web_app.exists() || !web_app.isDirectory()) {
            LOG.warn("Web application not found " + war);
            throw new java.io.FileNotFoundException(war);
        }
        context.setBaseResource(web_app);
        if (LOG.isDebugEnabled())
            LOG.debug("webapp=" + web_app);
    }
    // Do we need to extract WEB-INF/lib?
    if (context.isCopyWebInf() && !context.isCopyWebDir()) {
        Resource web_inf = web_app.addPath("WEB-INF/");
        File extractedWebInfDir = new File(context.getTempDirectory(), "webinf");
        if (extractedWebInfDir.exists())
            IO.delete(extractedWebInfDir);
        extractedWebInfDir.mkdir();
        Resource web_inf_lib = web_inf.addPath("lib/");
        File webInfDir = new File(extractedWebInfDir, "WEB-INF");
        webInfDir.mkdir();
        if (web_inf_lib.exists()) {
            File webInfLibDir = new File(webInfDir, "lib");
            if (webInfLibDir.exists())
                IO.delete(webInfLibDir);
            webInfLibDir.mkdir();
            LOG.debug("Copying WEB-INF/lib " + web_inf_lib + " to " + webInfLibDir);
            web_inf_lib.copyTo(webInfLibDir);
        }
        Resource web_inf_classes = web_inf.addPath("classes/");
        if (web_inf_classes.exists()) {
            File webInfClassesDir = new File(webInfDir, "classes");
            if (webInfClassesDir.exists())
                IO.delete(webInfClassesDir);
            webInfClassesDir.mkdir();
            LOG.debug("Copying WEB-INF/classes from " + web_inf_classes + " to " + webInfClassesDir.getAbsolutePath());
            web_inf_classes.copyTo(webInfClassesDir);
        }
        web_inf = Resource.newResource(extractedWebInfDir.getCanonicalPath());
        ResourceCollection rc = new ResourceCollection(web_inf, web_app);
        if (LOG.isDebugEnabled())
            LOG.debug("context.resourcebase = " + rc);
        context.setBaseResource(rc);
    }
}
Also used : JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) File(java.io.File) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Aggregations

ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)30 Resource (org.eclipse.jetty.util.resource.Resource)22 File (java.io.File)13 ArrayList (java.util.ArrayList)8 URL (java.net.URL)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 JarResource (org.eclipse.jetty.util.resource.JarResource)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 MimeTypes (org.eclipse.jetty.http.MimeTypes)3 Server (org.eclipse.jetty.server.Server)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2