Search in sources :

Example 16 with Resource

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

the class ContextHandler method getResource.

/* ------------------------------------------------------------ */
/*
     */
public Resource getResource(String path) throws MalformedURLException {
    if (path == null || !path.startsWith(URIUtil.SLASH))
        throw new MalformedURLException(path);
    if (_baseResource == null)
        return null;
    try {
        path = URIUtil.canonicalPath(path);
        Resource resource = _baseResource.addPath(path);
        if (checkAlias(path, resource))
            return resource;
        return null;
    } catch (Exception e) {
        LOG.ignore(e);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) Resource(org.eclipse.jetty.util.resource.Resource) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 17 with Resource

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

the class ResourceContentFactory method load.

/* ------------------------------------------------------------ */
private HttpContent load(String pathInContext, Resource resource, int maxBufferSize) throws IOException {
    if (resource == null || !resource.exists())
        return null;
    if (resource.isDirectory())
        return new ResourceHttpContent(resource, _mimeTypes.getMimeByExtension(resource.toString()), maxBufferSize);
    // Look for a precompressed resource or content
    String mt = _mimeTypes.getMimeByExtension(pathInContext);
    if (_precompressedFormats.length > 0) {
        // Is there a compressed resource?
        Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>(_precompressedFormats.length);
        for (CompressedContentFormat format : _precompressedFormats) {
            String compressedPathInContext = pathInContext + format._extension;
            Resource compressedResource = _factory.getResource(compressedPathInContext);
            if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified() && compressedResource.length() < resource.length())
                compressedContents.put(format, new ResourceHttpContent(compressedResource, _mimeTypes.getMimeByExtension(compressedPathInContext), maxBufferSize));
        }
        if (!compressedContents.isEmpty())
            return new ResourceHttpContent(resource, mt, maxBufferSize, compressedContents);
    }
    return new ResourceHttpContent(resource, mt, maxBufferSize);
}
Also used : HashMap(java.util.HashMap) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) Resource(org.eclipse.jetty.util.resource.Resource) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 18 with Resource

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

the class ResourceContentFactory method getContent.

/* ------------------------------------------------------------ */
@Override
public HttpContent getContent(String pathInContext, int maxBufferSize) throws IOException {
    // try loading the content from our factory.
    Resource resource = _factory.getResource(pathInContext);
    HttpContent loaded = load(pathInContext, resource, maxBufferSize);
    return loaded;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 19 with Resource

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

the class MavenQuickStartConfiguration method deconfigure.

@Override
public void deconfigure(WebAppContext context) throws Exception {
    //if we're not persisting the temp dir, get rid of any overlays
    if (!context.isPersistTempDirectory()) {
        Resource originalBases = (Resource) context.getAttribute("org.eclipse.jetty.resources.originalBases");
        String originalBaseStr = originalBases.toString();
        //Iterate over all of the resource bases and ignore any that were original bases, just
        //deleting the overlays
        Resource res = context.getBaseResource();
        if (res instanceof ResourceCollection) {
            for (Resource r : ((ResourceCollection) res).getResources()) {
                if (originalBaseStr.contains(r.toString()))
                    continue;
                IO.delete(r.getFile());
            }
        }
    }
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 20 with Resource

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

the class MavenWebInfConfiguration method unpackOverlay.

protected Resource unpackOverlay(WebAppContext context, Overlay overlay) throws IOException {
    LOG.debug("Unpacking overlay: " + overlay);
    if (overlay.getResource() == null)
        //nothing to unpack
        return null;
    //Get the name of the overlayed war and unpack it to a dir of the
    //same name in the temporary directory
    String name = overlay.getResource().getName();
    if (name.endsWith("!/"))
        name = name.substring(0, name.length() - 2);
    int i = name.lastIndexOf('/');
    if (i > 0)
        name = name.substring(i + 1, name.length());
    name = name.replace('.', '_');
    //add some digits to ensure uniqueness
    name = name + (++COUNTER);
    File dir = new File(context.getTempDirectory(), name);
    //if specified targetPath, unpack to that subdir instead
    File unpackDir = dir;
    if (overlay.getConfig() != null && overlay.getConfig().getTargetPath() != null)
        unpackDir = new File(dir, overlay.getConfig().getTargetPath());
    overlay.getResource().copyTo(unpackDir);
    //use top level of unpacked content
    Resource unpackedOverlay = Resource.newResource(dir.getCanonicalPath());
    LOG.debug("Unpacked overlay: " + overlay + " to " + unpackedOverlay);
    return unpackedOverlay;
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) File(java.io.File)

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