Search in sources :

Example 1 with ResourceContentFactory

use of org.eclipse.jetty.server.ResourceContentFactory 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)

Example 2 with ResourceContentFactory

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

the class ResourceHandler method doStart.

/* ------------------------------------------------------------ */
@Override
public void doStart() throws Exception {
    Context scontext = ContextHandler.getCurrentContext();
    _context = (scontext == null ? null : scontext.getContextHandler());
    _mimeTypes = _context == null ? new MimeTypes() : _context.getMimeTypes();
    _resourceService.setContentFactory(new ResourceContentFactory(this, _mimeTypes, _resourceService.getPrecompressedFormats()));
    _resourceService.setWelcomeFactory(this);
    super.doStart();
}
Also used : Context(org.eclipse.jetty.server.handler.ContextHandler.Context) ResourceContentFactory(org.eclipse.jetty.server.ResourceContentFactory) MimeTypes(org.eclipse.jetty.http.MimeTypes)

Example 3 with ResourceContentFactory

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

the class DefaultServletTest method testDirectFromResourceHttpContent.

@Test
public void testDirectFromResourceHttpContent() throws Exception {
    if (!OS.IS_LINUX)
        return;
    testdir.ensureEmpty();
    File resBase = testdir.getPathFile("docroot").toFile();
    FS.ensureDirExists(resBase);
    context.setBaseResource(Resource.newResource(resBase));
    File index = new File(resBase, "index.html");
    createFile(index, "<h1>Hello World</h1>");
    ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
    defholder.setInitParameter("dirAllowed", "true");
    defholder.setInitParameter("redirectWelcome", "false");
    defholder.setInitParameter("useFileMappedBuffer", "true");
    defholder.setInitParameter("welcomeServlets", "exact");
    defholder.setInitParameter("gzip", "false");
    defholder.setInitParameter("resourceCache", "resourceCache");
    String response;
    response = connector.getResponse("GET /context/index.html HTTP/1.0\r\n\r\n");
    assertResponseContains("<h1>Hello World</h1>", response);
    ResourceContentFactory factory = (ResourceContentFactory) context.getServletContext().getAttribute("resourceCache");
    HttpContent content = factory.getContent("/index.html", 200);
    ByteBuffer buffer = content.getDirectBuffer();
    Assert.assertTrue(buffer.isDirect());
    content = factory.getContent("/index.html", 5);
    buffer = content.getDirectBuffer();
    Assert.assertTrue(buffer == null);
}
Also used : ResourceContentFactory(org.eclipse.jetty.server.ResourceContentFactory) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) HttpContent(org.eclipse.jetty.http.HttpContent) Test(org.junit.Test)

Aggregations

ResourceContentFactory (org.eclipse.jetty.server.ResourceContentFactory)3 HttpContent (org.eclipse.jetty.http.HttpContent)2 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 MimeTypes (org.eclipse.jetty.http.MimeTypes)1 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)1 CachedContentFactory (org.eclipse.jetty.server.CachedContentFactory)1 Context (org.eclipse.jetty.server.handler.ContextHandler.Context)1 Test (org.junit.Test)1