예제 #1
0
  /* ------------------------------------------------------------ */
  private HttpContent load(String pathInContext, Resource resource) throws IOException {
    Content content = null;

    if (resource == null || !resource.exists()) return null;

    // Will it fit in the cache?
    if (!resource.isDirectory() && isCacheable(resource)) {
      // Create the Content (to increment the cache sizes before adding the content
      content = new Content(pathInContext, resource);

      // reduce the cache to an acceptable size.
      shrinkCache();

      // Add it to the cache.
      Content added = _cache.putIfAbsent(pathInContext, content);
      if (added != null) {
        content.invalidate();
        content = added;
      }

      return content;
    }

    return new HttpContent.ResourceAsHttpContent(
        resource,
        _mimeTypes.getMimeByExtension(resource.toString()),
        getMaxCachedFileSize(),
        _etags);
  }
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String fileName = request.getServletPath();
    byte[] dataBytes = loadContentFileBytes(fileName);

    response.setContentLength(dataBytes.length);
    if (fileName.endsWith(".js")) {
      // intentionally long-form content type to test ";" splitting in code
      response.setContentType("text/javascript; charset=utf-8");
    } else {
      String mime = mimeTypes.getMimeByExtension(fileName);
      if (mime != null) response.setContentType(mime);
    }
    ServletOutputStream out = response.getOutputStream();
    out.write(dataBytes);
  }
  /* ------------------------------------------------------------ */
  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 a gzip resource
      if (_gzip) {
        String pathInContextGz = pathInContext + ".gz";
        CachedHttpContent contentGz = _cache.get(pathInContextGz);
        if (contentGz == null || !contentGz.isValid()) {
          contentGz = null;
          Resource resourceGz = _factory.getResource(pathInContextGz);
          if (resourceGz.exists()
              && resourceGz.lastModified() >= resource.lastModified()
              && resourceGz.length() < resource.length()) {
            contentGz = new CachedHttpContent(pathInContextGz, resourceGz, null);
            CachedHttpContent added = _cache.putIfAbsent(pathInContextGz, contentGz);
            if (added != null) {
              contentGz.invalidate();
              contentGz = added;
            }
          }
        }
        content = new CachedHttpContent(pathInContext, resource, contentGz);
      } 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 gzip resource or content
    String mt = _mimeTypes.getMimeByExtension(pathInContext);
    if (_gzip) {
      // Is the gzip content cached?
      String pathInContextGz = pathInContext + ".gz";
      CachedHttpContent contentGz = _cache.get(pathInContextGz);
      if (contentGz != null
          && contentGz.isValid()
          && contentGz.getResource().lastModified() >= resource.lastModified())
        return new ResourceHttpContent(resource, mt, maxBufferSize, contentGz);

      // Is there a gzip resource?
      Resource resourceGz = _factory.getResource(pathInContextGz);
      if (resourceGz.exists()
          && resourceGz.lastModified() >= resource.lastModified()
          && resourceGz.length() < resource.length())
        return new ResourceHttpContent(
            resource,
            mt,
            maxBufferSize,
            new ResourceHttpContent(
                resourceGz, _mimeTypes.getMimeByExtension(pathInContextGz), maxBufferSize));
    }

    return new ResourceHttpContent(resource, mt, maxBufferSize);
  }