Search in sources :

Example 1 with CompressedContentFormat

use of org.eclipse.jetty.http.CompressedContentFormat in project jetty.project by eclipse.

the class DefaultServlet method parsePrecompressedFormats.

private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, boolean gzip) {
    List<CompressedContentFormat> ret = new ArrayList<>();
    if (precompressed != null && precompressed.indexOf('=') > 0) {
        for (String pair : precompressed.split(",")) {
            String[] setting = pair.split("=");
            String encoding = setting[0].trim();
            String extension = setting[1].trim();
            ret.add(new CompressedContentFormat(encoding, extension));
            if (gzip && !ret.contains(CompressedContentFormat.GZIP))
                ret.add(CompressedContentFormat.GZIP);
        }
    } else if (precompressed != null) {
        if (Boolean.parseBoolean(precompressed)) {
            ret.add(CompressedContentFormat.BR);
            ret.add(CompressedContentFormat.GZIP);
        }
    } else if (gzip) {
        // gzip handling is for backwards compatibility with older Jetty
        ret.add(CompressedContentFormat.GZIP);
    }
    return ret.toArray(new CompressedContentFormat[ret.size()]);
}
Also used : ArrayList(java.util.ArrayList) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat)

Example 2 with CompressedContentFormat

use of org.eclipse.jetty.http.CompressedContentFormat in project jetty.project by eclipse.

the class CachedContentFactory 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()), getMaxCachedFileSize());
    // Will it fit in the cache?
    if (isCacheable(resource)) {
        CachedHttpContent content = null;
        // Look for precompressed resources
        if (_precompressedFormats.length > 0) {
            Map<CompressedContentFormat, CachedHttpContent> precompresssedContents = new HashMap<>(_precompressedFormats.length);
            for (CompressedContentFormat format : _precompressedFormats) {
                String compressedPathInContext = pathInContext + format._extension;
                CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
                if (compressedContent == null || compressedContent.isValid()) {
                    compressedContent = null;
                    Resource compressedResource = _factory.getResource(compressedPathInContext);
                    if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified() && compressedResource.length() < resource.length()) {
                        compressedContent = new CachedHttpContent(compressedPathInContext, compressedResource, null);
                        CachedHttpContent added = _cache.putIfAbsent(compressedPathInContext, compressedContent);
                        if (added != null) {
                            compressedContent.invalidate();
                            compressedContent = added;
                        }
                    }
                }
                if (compressedContent != null)
                    precompresssedContents.put(format, compressedContent);
            }
            content = new CachedHttpContent(pathInContext, resource, precompresssedContents);
        } else
            content = new CachedHttpContent(pathInContext, resource, null);
        // Add it to the cache.
        CachedHttpContent added = _cache.putIfAbsent(pathInContext, content);
        if (added != null) {
            content.invalidate();
            content = added;
        }
        return content;
    }
    // Look for non Cacheable precompressed resource or content
    String mt = _mimeTypes.getMimeByExtension(pathInContext);
    if (_precompressedFormats.length > 0) {
        // Is the precompressed content cached?
        Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>();
        for (CompressedContentFormat format : _precompressedFormats) {
            String compressedPathInContext = pathInContext + format._extension;
            CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
            if (compressedContent != null && compressedContent.isValid() && compressedContent.getResource().lastModified() >= resource.lastModified())
                compressedContents.put(format, compressedContent);
            // Is there a precompressed resource?
            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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent) Resource(org.eclipse.jetty.util.resource.Resource) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat) PrecompressedHttpContent(org.eclipse.jetty.http.PrecompressedHttpContent) HttpContent(org.eclipse.jetty.http.HttpContent) ResourceHttpContent(org.eclipse.jetty.http.ResourceHttpContent)

Example 3 with CompressedContentFormat

use of org.eclipse.jetty.http.CompressedContentFormat 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 4 with CompressedContentFormat

use of org.eclipse.jetty.http.CompressedContentFormat in project jetty.project by eclipse.

the class ResourceService method doGet.

/* ------------------------------------------------------------ */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String servletPath = null;
    String pathInfo = null;
    Enumeration<String> reqRanges = null;
    boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null;
    if (included) {
        servletPath = _pathInfoOnly ? "/" : (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
        pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
        if (servletPath == null) {
            servletPath = request.getServletPath();
            pathInfo = request.getPathInfo();
        }
    } else {
        servletPath = _pathInfoOnly ? "/" : request.getServletPath();
        pathInfo = request.getPathInfo();
        // Is this a Range request?
        reqRanges = request.getHeaders(HttpHeader.RANGE.asString());
        if (!hasDefinedRange(reqRanges))
            reqRanges = null;
    }
    String pathInContext = URIUtil.addPaths(servletPath, pathInfo);
    boolean endsWithSlash = (pathInfo == null ? request.getServletPath() : pathInfo).endsWith(URIUtil.SLASH);
    boolean checkPrecompressedVariants = _precompressedFormats.length > 0 && !endsWithSlash && !included && reqRanges == null;
    HttpContent content = null;
    boolean release_content = true;
    try {
        // Find the content
        content = _contentFactory.getContent(pathInContext, response.getBufferSize());
        if (LOG.isDebugEnabled())
            LOG.info("content={}", content);
        // Not found?
        if (content == null || !content.getResource().exists()) {
            if (included)
                throw new FileNotFoundException("!" + pathInContext);
            notFound(request, response);
            return;
        }
        // Directory?
        if (content.getResource().isDirectory()) {
            sendWelcome(content, pathInContext, endsWithSlash, included, request, response);
            return;
        }
        // Strip slash?
        if (endsWithSlash && pathInContext.length() > 1) {
            String q = request.getQueryString();
            pathInContext = pathInContext.substring(0, pathInContext.length() - 1);
            if (q != null && q.length() != 0)
                pathInContext += "?" + q;
            response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(), pathInContext)));
            return;
        }
        // Conditional response?
        if (!included && !passConditionalHeaders(request, response, content))
            return;
        // Precompressed variant available?
        Map<CompressedContentFormat, ? extends HttpContent> precompressedContents = checkPrecompressedVariants ? content.getPrecompressedContents() : null;
        if (precompressedContents != null && precompressedContents.size() > 0) {
            // Tell caches that response may vary by accept-encoding
            response.addHeader(HttpHeader.VARY.asString(), HttpHeader.ACCEPT_ENCODING.asString());
            List<String> preferredEncodings = getPreferredEncodingOrder(request);
            CompressedContentFormat precompressedContentEncoding = getBestPrecompressedContent(preferredEncodings, precompressedContents.keySet());
            if (precompressedContentEncoding != null) {
                HttpContent precompressedContent = precompressedContents.get(precompressedContentEncoding);
                if (LOG.isDebugEnabled())
                    LOG.debug("precompressed={}", precompressedContent);
                content = precompressedContent;
                response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), precompressedContentEncoding._encoding);
            }
        }
        // TODO this should be done by HttpContent#getContentEncoding
        if (isGzippedContent(pathInContext))
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
        // Send the data
        release_content = sendData(request, response, included, content, reqRanges);
    } catch (IllegalArgumentException e) {
        LOG.warn(Log.EXCEPTION, e);
        if (!response.isCommitted())
            response.sendError(500, e.getMessage());
    } finally {
        if (release_content) {
            if (content != null)
                content.release();
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) CompressedContentFormat(org.eclipse.jetty.http.CompressedContentFormat) HttpContent(org.eclipse.jetty.http.HttpContent)

Aggregations

CompressedContentFormat (org.eclipse.jetty.http.CompressedContentFormat)4 HttpContent (org.eclipse.jetty.http.HttpContent)3 HashMap (java.util.HashMap)2 ResourceHttpContent (org.eclipse.jetty.http.ResourceHttpContent)2 Resource (org.eclipse.jetty.util.resource.Resource)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PrecompressedHttpContent (org.eclipse.jetty.http.PrecompressedHttpContent)1