Search in sources :

Example 1 with CachedContentFactory

use of org.eclipse.jetty.server.CachedContentFactory in project jetty.project by eclipse.

the class DefaultServlet method init.

/* ------------------------------------------------------------ */
@Override
public void init() throws UnavailableException {
    _servletContext = getServletContext();
    _contextHandler = initContextHandler(_servletContext);
    _mimeTypes = _contextHandler.getMimeTypes();
    _welcomes = _contextHandler.getWelcomeFiles();
    if (_welcomes == null)
        _welcomes = new String[] { "index.html", "index.jsp" };
    _resourceService.setAcceptRanges(getInitBoolean("acceptRanges", _resourceService.isAcceptRanges()));
    _resourceService.setDirAllowed(getInitBoolean("dirAllowed", _resourceService.isDirAllowed()));
    _resourceService.setRedirectWelcome(getInitBoolean("redirectWelcome", _resourceService.isRedirectWelcome()));
    _resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip", false)));
    _resourceService.setPathInfoOnly(getInitBoolean("pathInfoOnly", _resourceService.isPathInfoOnly()));
    _resourceService.setEtags(getInitBoolean("etags", _resourceService.isEtags()));
    if ("exact".equals(getInitParameter("welcomeServlets"))) {
        _welcomeExactServlets = true;
        _welcomeServlets = false;
    } else
        _welcomeServlets = getInitBoolean("welcomeServlets", _welcomeServlets);
    _useFileMappedBuffer = getInitBoolean("useFileMappedBuffer", _useFileMappedBuffer);
    _relativeResourceBase = getInitParameter("relativeResourceBase");
    String rb = getInitParameter("resourceBase");
    if (rb != null) {
        if (_relativeResourceBase != null)
            throw new UnavailableException("resourceBase & relativeResourceBase");
        try {
            _resourceBase = _contextHandler.newResource(rb);
        } catch (Exception e) {
            LOG.warn(Log.EXCEPTION, e);
            throw new UnavailableException(e.toString());
        }
    }
    String css = getInitParameter("stylesheet");
    try {
        if (css != null) {
            _stylesheet = Resource.newResource(css);
            if (!_stylesheet.exists()) {
                LOG.warn("!" + css);
                _stylesheet = null;
            }
        }
        if (_stylesheet == null) {
            _stylesheet = Resource.newResource(this.getClass().getResource("/jetty-dir.css"));
        }
    } catch (Exception e) {
        LOG.warn(e.toString());
        LOG.debug(e);
    }
    int encodingHeaderCacheSize = getInitInt("encodingHeaderCacheSize", -1);
    if (encodingHeaderCacheSize >= 0)
        _resourceService.setEncodingCacheSize(encodingHeaderCacheSize);
    String cc = getInitParameter("cacheControl");
    if (cc != null)
        _resourceService.setCacheControl(new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, cc));
    String resourceCache = getInitParameter("resourceCache");
    int max_cache_size = getInitInt("maxCacheSize", -2);
    int max_cached_file_size = getInitInt("maxCachedFileSize", -2);
    int max_cached_files = getInitInt("maxCachedFiles", -2);
    if (resourceCache != null) {
        if (max_cache_size != -1 || max_cached_file_size != -2 || max_cached_files != -2)
            LOG.debug("ignoring resource cache configuration, using resourceCache attribute");
        if (_relativeResourceBase != null || _resourceBase != null)
            throw new UnavailableException("resourceCache specified with resource bases");
        _cache = (CachedContentFactory) _servletContext.getAttribute(resourceCache);
    }
    try {
        if (_cache == null && (max_cached_files != -2 || max_cache_size != -2 || max_cached_file_size != -2)) {
            _cache = new CachedContentFactory(null, this, _mimeTypes, _useFileMappedBuffer, _resourceService.isEtags(), _resourceService.getPrecompressedFormats());
            if (max_cache_size >= 0)
                _cache.setMaxCacheSize(max_cache_size);
            if (max_cached_file_size >= -1)
                _cache.setMaxCachedFileSize(max_cached_file_size);
            if (max_cached_files >= -1)
                _cache.setMaxCachedFiles(max_cached_files);
            _servletContext.setAttribute(resourceCache == null ? "resourceCache" : resourceCache, _cache);
        }
    } catch (Exception e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new UnavailableException(e.toString());
    }
    HttpContent.ContentFactory contentFactory = _cache;
    if (contentFactory == null) {
        contentFactory = new ResourceContentFactory(this, _mimeTypes, _resourceService.getPrecompressedFormats());
        if (resourceCache != null)
            _servletContext.setAttribute(resourceCache, contentFactory);
    }
    _resourceService.setContentFactory(contentFactory);
    _resourceService.setWelcomeFactory(this);
    List<String> gzip_equivalent_file_extensions = new ArrayList<String>();
    String otherGzipExtensions = getInitParameter("otherGzipFileExtensions");
    if (otherGzipExtensions != null) {
        //comma separated list
        StringTokenizer tok = new StringTokenizer(otherGzipExtensions, ",", false);
        while (tok.hasMoreTokens()) {
            String s = tok.nextToken().trim();
            gzip_equivalent_file_extensions.add((s.charAt(0) == '.' ? s : "." + s));
        }
    } else {
        //.svgz files are gzipped svg files and must be served with Content-Encoding:gzip
        gzip_equivalent_file_extensions.add(".svgz");
    }
    _resourceService.setGzipEquivalentFileExtensions(gzip_equivalent_file_extensions);
    _servletHandler = _contextHandler.getChildHandlerByClass(ServletHandler.class);
    for (ServletHolder h : _servletHandler.getServlets()) if (h.getServletInstance() == this)
        _defaultHolder = h;
    if (LOG.isDebugEnabled())
        LOG.debug("resource base = " + _resourceBase);
}
Also used : CachedContentFactory(org.eclipse.jetty.server.CachedContentFactory) UnavailableException(javax.servlet.UnavailableException) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException) StringTokenizer(java.util.StringTokenizer) ResourceContentFactory(org.eclipse.jetty.server.ResourceContentFactory) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpContent(org.eclipse.jetty.http.HttpContent)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 HttpContent (org.eclipse.jetty.http.HttpContent)1 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)1 CachedContentFactory (org.eclipse.jetty.server.CachedContentFactory)1 ResourceContentFactory (org.eclipse.jetty.server.ResourceContentFactory)1