Search in sources :

Example 61 with Resource

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

the class AntWebInfConfiguration method preConfigure.

@Override
public void preConfigure(final WebAppContext context) throws Exception {
    //Make a temp directory for the webapp if one is not already set
    resolveTempDirectory(context);
    //Extract webapp if necessary
    unpack(context);
    //Apply an initial ordering to the jars which governs which will be scanned for META-INF
    //info and annotations. The ordering is based on inclusion patterns.       
    String tmp = (String) context.getAttribute(WEBINF_JAR_PATTERN);
    Pattern webInfPattern = (tmp == null ? null : Pattern.compile(tmp));
    tmp = (String) context.getAttribute(CONTAINER_JAR_PATTERN);
    Pattern containerPattern = (tmp == null ? null : Pattern.compile(tmp));
    //Apply ordering to container jars - if no pattern is specified, we won't
    //match any of the container jars
    PatternMatcher containerJarNameMatcher = new PatternMatcher() {

        public void matched(URI uri) throws Exception {
            context.getMetaData().addContainerResource(Resource.newResource(uri));
        }
    };
    ClassLoader loader = context.getClassLoader();
    if (loader != null) {
        loader = loader.getParent();
        if (loader != null) {
            URI[] containerUris = null;
            if (loader instanceof URLClassLoader) {
                URL[] urls = ((URLClassLoader) loader).getURLs();
                if (urls != null) {
                    containerUris = new URI[urls.length];
                    int i = 0;
                    for (URL u : urls) {
                        try {
                            containerUris[i] = u.toURI();
                        } catch (URISyntaxException e) {
                            containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
                        }
                        i++;
                    }
                }
            } else if (loader instanceof AntClassLoader) {
                AntClassLoader antLoader = (AntClassLoader) loader;
                String[] paths = antLoader.getClasspath().split(new String(new char[] { File.pathSeparatorChar }));
                if (paths != null) {
                    containerUris = new URI[paths.length];
                    int i = 0;
                    for (String p : paths) {
                        File f = new File(p);
                        containerUris[i] = f.toURI();
                        i++;
                    }
                }
            }
            containerJarNameMatcher.match(containerPattern, containerUris, false);
        }
    }
    //Apply ordering to WEB-INF/lib jars
    PatternMatcher webInfJarNameMatcher = new PatternMatcher() {

        @Override
        public void matched(URI uri) throws Exception {
            context.getMetaData().addWebInfJar(Resource.newResource(uri));
        }
    };
    List<Resource> jars = findJars(context);
    //Convert to uris for matching
    URI[] uris = null;
    if (jars != null) {
        uris = new URI[jars.size()];
        int i = 0;
        for (Resource r : jars) {
            uris[i++] = r.getURI();
        }
    }
    //null is inclusive, no pattern == all jars match 
    webInfJarNameMatcher.match(webInfPattern, uris, true);
    //No pattern to appy to classes, just add to metadata
    context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
}
Also used : Pattern(java.util.regex.Pattern) Resource(org.eclipse.jetty.util.resource.Resource) AntClassLoader(org.apache.tools.ant.AntClassLoader) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) PatternMatcher(org.eclipse.jetty.util.PatternMatcher) File(java.io.File)

Example 62 with Resource

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

the class TestAnnotationConfiguration method testGetFragmentFromJar.

@Test
public void testGetFragmentFromJar() throws Exception {
    String dir = MavenTestingUtils.getTargetTestingDir("getFragmentFromJar").getAbsolutePath();
    File file = new File(dir);
    file = new File(file.getCanonicalPath());
    URL url = file.toURL();
    Resource jar1 = Resource.newResource(url + "file.jar");
    AnnotationConfiguration config = new AnnotationConfiguration();
    WebAppContext wac = new WebAppContext();
    List<FragmentDescriptor> frags = new ArrayList<FragmentDescriptor>();
    frags.add(new FragmentDescriptor(Resource.newResource("jar:" + url + "file.jar!/fooa.props")));
    frags.add(new FragmentDescriptor(Resource.newResource("jar:" + url + "file2.jar!/foob.props")));
    assertNotNull(config.getFragmentFromJar(jar1, frags));
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Resource(org.eclipse.jetty.util.resource.Resource) ArrayList(java.util.ArrayList) File(java.io.File) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 63 with Resource

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

the class PreconfigureQuickStartWar method main.

public static void main(String... args) throws Exception {
    Resource war = null;
    Resource dir = null;
    Resource xml = null;
    switch(args.length) {
        case 0:
            error("No WAR file or directory given");
            break;
        case 1:
            dir = Resource.newResource(args[0]);
            break;
        case 2:
            war = Resource.newResource(args[0]);
            if (war.isDirectory()) {
                dir = war;
                war = null;
                xml = Resource.newResource(args[1]);
            } else {
                dir = Resource.newResource(args[1]);
            }
            break;
        case 3:
            war = Resource.newResource(args[0]);
            dir = Resource.newResource(args[1]);
            xml = Resource.newResource(args[2]);
            break;
        default:
            error("Too many args");
            break;
    }
    preconfigure(war, dir, xml);
}
Also used : JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource)

Example 64 with Resource

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

the class QuickStartConfiguration method preConfigure.

/**
     * @see org.eclipse.jetty.webapp.AbstractConfiguration#preConfigure(org.eclipse.jetty.webapp.WebAppContext)
     */
@Override
public void preConfigure(WebAppContext context) throws Exception {
    //check that webapp is suitable for quick start - it is not a packed war
    String war = context.getWar();
    if (war == null || war.length() <= 0)
        throw new IllegalStateException("No location for webapp");
    //Make a temp directory for the webapp if one is not already set
    resolveTempDirectory(context);
    Resource webApp = context.newResource(war);
    // Accept aliases for WAR files
    if (webApp.isAlias()) {
        LOG.debug(webApp + " anti-aliased to " + webApp.getAlias());
        webApp = context.newResource(webApp.getAlias());
    }
    // Is the WAR usable directly?
    if (!webApp.exists() || !webApp.isDirectory() || webApp.toString().startsWith("jar:"))
        throw new IllegalStateException("Webapp does not exist or is not unpacked");
    context.setBaseResource(webApp);
    LOG.debug("webapp={}", webApp);
    //look for quickstart-web.xml in WEB-INF of webapp
    Resource quickStartWebXml = getQuickStartWebXml(context);
    LOG.debug("quickStartWebXml={}", quickStartWebXml);
    context.getMetaData().setWebXml(quickStartWebXml);
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource)

Example 65 with Resource

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

the class ResourceCacheTest method testUncacheable.

@Test
public void testUncacheable() 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) {

        @Override
        public boolean isCacheable(Resource resource) {
            return super.isCacheable(resource) && resource.getName().indexOf("2.txt") < 0;
        }
    };
    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)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6