示例#1
0
  public static URL getIconByFilename(String filename) {

    String fileExtension = FileUtils.getFileExtension(filename);

    if (fileExtension == null) {

      return getDefaultIcon();
    }

    return getIcon(fileExtension);
  }
  private void sendFile(
      HttpServletRequest req,
      HttpServletResponse res,
      URIParser uriParser,
      URL url,
      InputStream fileStream,
      User user,
      String staticContentPackage,
      String filePath,
      ModuleDescriptor moduleDescriptor,
      String moduletype) {

    OutputStream outstream = null;

    try {
      Date lastModified = this.getLastModified(url, uriParser);

      if (lastModified != null) {

        String lastModifiedString = RFC1123_DATE_FORMATTER.format(lastModified);

        String modifiedSinceString = req.getHeader("If-Modified-Since");

        if (modifiedSinceString != null
            && lastModifiedString.equalsIgnoreCase(modifiedSinceString)) {

          res.setStatus(304);
          res.flushBuffer();

          return;
        }

        res.setHeader("Last-Modified", lastModifiedString);
      }

      // Set content type, filename
      String filename = FileUtils.toValidHttpFilename(uriParser.get(uriParser.size() - 1));

      res.setStatus(200);
      res.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");

      String contentType = MimeUtils.getMimeType(filename);

      if (contentType != null) {
        res.setContentType(contentType);
      } else {
        res.setContentType("application/x-unknown-mime-type");
      }

      outstream = res.getOutputStream();

      StreamUtils.transfer(fileStream, outstream);

      if (log.isDebugEnabled()) {

        if (moduleDescriptor != null) {

          log.debug(
              "Sent file "
                  + staticContentPackage
                  + filePath
                  + " from "
                  + moduletype
                  + " "
                  + moduleDescriptor
                  + " to user "
                  + user);

        } else {

          log.debug(
              "Sent file "
                  + staticContentPackage
                  + filePath
                  + " from global content links to user "
                  + user);
        }
      }

    } catch (IOException e) {

      if (log.isDebugEnabled()) {

        if (moduleDescriptor != null) {

          log.info(
              "Error sending file "
                  + staticContentPackage
                  + filePath
                  + " from "
                  + moduletype
                  + " "
                  + moduleDescriptor
                  + " to user "
                  + user
                  + ", "
                  + e);

        } else {

          log.info(
              "Error sending file "
                  + staticContentPackage
                  + filePath
                  + " from global content links to user "
                  + user
                  + ", "
                  + e);
        }
      }

    } finally {
      try {
        StreamUtils.closeStream(fileStream);
      } catch (NullPointerException e) {
        // Workaround for JVM bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5041014
        // (Windows only)
      }
      StreamUtils.closeStream(outstream);
    }
  }