Search in sources :

Example 26 with Resource

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

the class OSGiWebInfConfiguration method configure.

/* ------------------------------------------------------------ */
/** 
     * Allow fragments to supply some resources that are added to the baseResource of the webapp.
     * 
     * The resources can be either prepended or appended to the baseResource.
     * 
     * @see org.eclipse.jetty.webapp.WebInfConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
     */
@Override
public void configure(WebAppContext context) throws Exception {
    TreeMap<String, Resource> prependedResourcesPath = new TreeMap<String, Resource>();
    TreeMap<String, Resource> appendedResourcesPath = new TreeMap<String, Resource>();
    Bundle bundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
    if (bundle != null) {
        Set<Bundle> fragments = (Set<Bundle>) context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
        if (fragments != null && !fragments.isEmpty()) {
            // looked up.
            for (Bundle frag : fragments) {
                String path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
                convertFragmentPathToResource(path, frag, appendedResourcesPath);
                path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_PREPEND_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
                convertFragmentPathToResource(path, frag, prependedResourcesPath);
            }
            if (!appendedResourcesPath.isEmpty()) {
                LinkedHashSet<Resource> resources = new LinkedHashSet<Resource>();
                //Add in any existing setting of extra resource dirs
                Set<Resource> resourceDirs = (Set<Resource>) context.getAttribute(WebInfConfiguration.RESOURCE_DIRS);
                if (resourceDirs != null && !resourceDirs.isEmpty())
                    resources.addAll(resourceDirs);
                //Then append the values from JETTY_WAR_FRAGMENT_FOLDER_PATH
                resources.addAll(appendedResourcesPath.values());
                context.setAttribute(WebInfConfiguration.RESOURCE_DIRS, resources);
            }
        }
    }
    super.configure(context);
    // place the prepended resources at the beginning of the contexts's resource base
    if (!prependedResourcesPath.isEmpty()) {
        Resource[] resources = new Resource[1 + prependedResourcesPath.size()];
        System.arraycopy(prependedResourcesPath.values().toArray(new Resource[prependedResourcesPath.size()]), 0, resources, 0, prependedResourcesPath.size());
        resources[resources.length - 1] = context.getBaseResource();
        context.setBaseResource(new ResourceCollection(resources));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Bundle(org.osgi.framework.Bundle) Resource(org.eclipse.jetty.util.resource.Resource) TreeMap(java.util.TreeMap) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 27 with Resource

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

the class OSGiWebInfConfiguration method getBundleAsResource.

/* ------------------------------------------------------------ */
/**
    * Resolves the bundle. Usually that would be a single URL per bundle. But we do some more work if there are jars
    * embedded in the bundle.
    */
private List<Resource> getBundleAsResource(Bundle bundle) throws Exception {
    List<Resource> resources = new ArrayList<Resource>();
    File file = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bundle);
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            if (f.getName().endsWith(".jar") && f.isFile()) {
                resources.add(Resource.newResource(f));
            } else if (f.isDirectory() && f.getName().equals("lib")) {
                for (File f2 : file.listFiles()) {
                    if (f2.getName().endsWith(".jar") && f2.isFile()) {
                        resources.add(Resource.newResource(f));
                    }
                }
            }
        }
        //TODO really???
        resources.add(Resource.newResource(file));
    } else {
        resources.add(Resource.newResource(file));
    }
    return resources;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.eclipse.jetty.util.resource.Resource) File(java.io.File)

Example 28 with Resource

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

the class JettyRunMojo method configureWebApplication.

/** 
     * @see org.eclipse.jetty.maven.plugin.AbstractJettyMojo#configureWebApplication()
     */
public void configureWebApplication() throws Exception {
    super.configureWebApplication();
    //Set up the location of the webapp.
    //There are 2 parts to this: setWar() and setBaseResource(). On standalone jetty,
    //the former could be the location of a packed war, while the latter is the location
    //after any unpacking. With this mojo, you are running an unpacked, unassembled webapp,
    //so the two locations should be equal.
    Resource webAppSourceDirectoryResource = Resource.newResource(webAppSourceDirectory.getCanonicalPath());
    if (webApp.getWar() == null)
        webApp.setWar(webAppSourceDirectoryResource.toString());
    if (webApp.getBaseResource() == null)
        webApp.setBaseResource(webAppSourceDirectoryResource);
    if (classesDirectory != null)
        webApp.setClasses(classesDirectory);
    if (useTestScope && (testClassesDirectory != null))
        webApp.setTestClasses(testClassesDirectory);
    webApp.setWebInfLib(getDependencyFiles());
    //get copy of a list of war artifacts
    Set<Artifact> matchedWarArtifacts = new HashSet<Artifact>();
    //process any overlays and the war type artifacts
    List<Overlay> overlays = new ArrayList<Overlay>();
    for (OverlayConfig config : warPluginInfo.getMavenWarOverlayConfigs()) {
        //overlays can be individually skipped
        if (config.isSkip())
            continue;
        //an empty overlay refers to the current project - important for ordering
        if (config.isCurrentProject()) {
            Overlay overlay = new Overlay(config, null);
            overlays.add(overlay);
            continue;
        }
        //if a war matches an overlay config
        Artifact a = getArtifactForOverlay(config, getWarArtifacts());
        if (a != null) {
            matchedWarArtifacts.add(a);
            SelectiveJarResource r = new SelectiveJarResource(new URL("jar:" + Resource.toURL(a.getFile()).toString() + "!/"));
            r.setIncludes(config.getIncludes());
            r.setExcludes(config.getExcludes());
            Overlay overlay = new Overlay(config, r);
            overlays.add(overlay);
        }
    }
    //iterate over the left over war artifacts and unpack them (without include/exclude processing) as necessary
    for (Artifact a : getWarArtifacts()) {
        if (!matchedWarArtifacts.contains(a)) {
            Overlay overlay = new Overlay(null, Resource.newResource(new URL("jar:" + Resource.toURL(a.getFile()).toString() + "!/")));
            overlays.add(overlay);
        }
    }
    webApp.setOverlays(overlays);
    //if we have not already set web.xml location, need to set one up
    if (webApp.getDescriptor() == null) {
        //Has an explicit web.xml file been configured to use?
        if (webXml != null) {
            Resource r = Resource.newResource(webXml);
            if (r.exists() && !r.isDirectory()) {
                webApp.setDescriptor(r.toString());
            }
        }
        //Still don't have a web.xml file: try the resourceBase of the webapp, if it is set
        if (webApp.getDescriptor() == null && webApp.getBaseResource() != null) {
            Resource r = webApp.getBaseResource().addPath("WEB-INF/web.xml");
            if (r.exists() && !r.isDirectory()) {
                webApp.setDescriptor(r.toString());
            }
        }
        //Still don't have a web.xml file: finally try the configured static resource directory if there is one
        if (webApp.getDescriptor() == null && (webAppSourceDirectory != null)) {
            File f = new File(new File(webAppSourceDirectory, "WEB-INF"), "web.xml");
            if (f.exists() && f.isFile()) {
                webApp.setDescriptor(f.getCanonicalPath());
            }
        }
    }
    getLog().info("web.xml file = " + webApp.getDescriptor());
    getLog().info("Webapp directory = " + webAppSourceDirectory.getCanonicalPath());
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ArrayList(java.util.ArrayList) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) URL(java.net.URL) HashSet(java.util.HashSet)

Example 29 with Resource

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

the class JettyRunMojo method gatherScannables.

public void gatherScannables() throws Exception {
    if (webApp.getDescriptor() != null) {
        Resource r = Resource.newResource(webApp.getDescriptor());
        scanner.watch(r.getFile().toPath());
    }
    if (webApp.getJettyEnvXml() != null)
        scanner.watch(new File(webApp.getJettyEnvXml()).toPath());
    if (webApp.getDefaultsDescriptor() != null) {
        if (!WebAppContext.WEB_DEFAULTS_XML.equals(webApp.getDefaultsDescriptor()))
            scanner.watch(new File(webApp.getDefaultsDescriptor()).toPath());
    }
    if (webApp.getOverrideDescriptor() != null) {
        scanner.watch(new File(webApp.getOverrideDescriptor()).toPath());
    }
    File jettyWebXmlFile = findJettyWebXmlFile(new File(webAppSourceDirectory, "WEB-INF"));
    if (jettyWebXmlFile != null) {
        scanner.watch(jettyWebXmlFile.toPath());
    }
    //make sure each of the war artifacts is added to the scanner
    for (Artifact a : getWarArtifacts()) {
        scanner.watch(a.getFile().toPath());
    }
    //handle the explicit extra scan targets
    if (scanTargets != null) {
        for (File f : scanTargets) {
            if (f.isDirectory()) {
                PathWatcher.Config config = new PathWatcher.Config(f.toPath());
                config.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
                scanner.watch(config);
            } else
                scanner.watch(f.toPath());
        }
    }
    //handle the extra scan patterns
    if (scanTargetPatterns != null) {
        for (ScanTargetPattern p : scanTargetPatterns) {
            PathWatcher.Config config = new PathWatcher.Config(p.getDirectory().toPath());
            config.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
            for (String pattern : p.getExcludes()) config.addExcludeGlobRelative(pattern);
            for (String pattern : p.getIncludes()) config.addIncludeGlobRelative(pattern);
            scanner.watch(config);
        }
    }
    scanner.watch(project.getFile().toPath());
    if (webApp.getTestClasses() != null && webApp.getTestClasses().exists()) {
        PathWatcher.Config config = new PathWatcher.Config(webApp.getTestClasses().toPath());
        config.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
        if (scanTestClassesPattern != null) {
            for (String p : scanTestClassesPattern.getExcludes()) config.addExcludeGlobRelative(p);
            for (String p : scanTestClassesPattern.getIncludes()) config.addIncludeGlobRelative(p);
        }
        scanner.watch(config);
    }
    if (webApp.getClasses() != null && webApp.getClasses().exists()) {
        PathWatcher.Config config = new PathWatcher.Config(webApp.getClasses().toPath());
        config.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
        if (scanClassesPattern != null) {
            for (String p : scanClassesPattern.getExcludes()) config.addExcludeGlobRelative(p);
            for (String p : scanClassesPattern.getIncludes()) config.addIncludeGlobRelative(p);
        }
        scanner.watch(config);
    }
    if (webApp.getWebInfLib() != null) {
        for (File f : webApp.getWebInfLib()) {
            PathWatcher.Config config = new PathWatcher.Config(f.toPath());
            config.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
            scanner.watch(config);
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) PathWatcher(org.eclipse.jetty.util.PathWatcher) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 30 with Resource

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

the class HttpOutputTest method testWriteSmall.

@Test
public void testWriteSmall() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/big.txt");
    _handler._writeLengthIfKnown = false;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[8];
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, Matchers.not(containsString("Content-Length")));
    assertThat(response, endsWith(toUTF8String(big)));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) 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