/** Find extended mime.types from the spring-context-support module. */
 private static FileTypeMap initFileTypeMap() {
   Resource resource = new ClassPathResource("org/springframework/mail/javamail/mime.types");
   if (resource.exists()) {
     if (logger.isTraceEnabled()) {
       logger.trace("Loading JAF FileTypeMap from " + resource);
     }
     InputStream inputStream = null;
     try {
       inputStream = resource.getInputStream();
       return new MimetypesFileTypeMap(inputStream);
     } catch (IOException ex) {
       // ignore
     } finally {
       if (inputStream != null) {
         try {
           inputStream.close();
         } catch (IOException ex) {
           // ignore
         }
       }
     }
   }
   if (logger.isTraceEnabled()) {
     logger.trace("Loading default Java Activation Framework FileTypeMap");
   }
   return FileTypeMap.getDefaultFileTypeMap();
 }
 private static FileTypeMap loadFileTypeMapFromContextSupportModule() {
   // see if we can find the extended mime.types from the context-support module
   Resource mappingLocation =
       new ClassPathResource("org/springframework/mail/javamail/mime.types");
   if (mappingLocation.exists()) {
     if (logger.isTraceEnabled()) {
       logger.trace("Loading Java Activation Framework FileTypeMap from " + mappingLocation);
     }
     InputStream inputStream = null;
     try {
       inputStream = mappingLocation.getInputStream();
       return new MimetypesFileTypeMap(inputStream);
     } catch (IOException ex) {
       // ignore
     } finally {
       if (inputStream != null) {
         try {
           inputStream.close();
         } catch (IOException ex) {
           // ignore
         }
       }
     }
   }
   if (logger.isTraceEnabled()) {
     logger.trace("Loading default Java Activation Framework FileTypeMap");
   }
   return FileTypeMap.getDefaultFileTypeMap();
 }
Exemple #3
0
  /** @param args */
  public static void main(String[] args) {
    FileTypeMap fileTypeMap = FileTypeMap.getDefaultFileTypeMap();

    String path = ".";
    File dir = new File(path);
    File[] listFiles = dir.listFiles();
    for (File file : listFiles) {
      System.out.println(file.getName() + ":" + fileTypeMap.getContentType(file));
    }
  }
 /**
  * Use the specified file as the content for this part.
  *
  * @param file the file
  * @since JavaMail 1.4
  */
 public void attachFile(File file) throws IOException, MessagingException {
   FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
   String contentType = map.getContentType(file);
   if (contentType == null)
     throw new MessagingException("Unable to determine MIME type of " + file);
   /*GPL->GNU@03/15/2011 - Fix for adding attachments into MimeBodyPart start*/
   // setContent(new FileInputStream(file), contentType);
   setContent(file);
   /*GPL->GNU@03/15/2011 - Fix for adding attachments into MimeBodyPart end*/
   setFileName(file.getName());
 }
Exemple #5
0
    /**
     * 添付ファイルのDataSourceインスタンスを生成して返します。
     *
     * @return 添付ファイルのDataSourceインスタンス
     */
    public DataSource getDataSource() {
      if (file != null) {
        return new FileDataSource(file);
      }

      if (url != null) {
        return new URLDataSource(url);
      }

      // InputStreamからDataSourceを生成
      String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(name);
      if (is != null) {
        // InputStreamからDataSourceを生成
        return new ByteArrayDataSource(is, contentType);
      } else {
        // byte配列からDataSourceを生成
        return new ByteArrayDataSource(bytes, contentType);
      }
    }
 public static String getMimeType(String filePath) {
   return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
 }
/**
 * A RequestHandler that serves static content (files) from a predefined directory.
 *
 * <p>"Cache-Control: public" indicates that the response MAY be cached by any cache, even if it
 * would normally be non-cacheable or cacheable only within a non- shared cache.
 */
public class StaticContentHandler extends RequestHandler {

  private static final Logger logger = LoggerFactory.getLogger(StaticContentHandler.class);

  private static final StaticContentHandler instance = new StaticContentHandler();

  private final FileTypeMap mimeTypeMap = FileTypeMap.getDefaultFileTypeMap();

  public static StaticContentHandler getInstance() {
    return instance;
  }

  /** {inheritDoc} */
  @Override
  public void get(HttpRequest request, HttpResponse response) {
    perform(request, response, true);
  }

  /** {inheritDoc} */
  @Override
  public void head(final HttpRequest request, final HttpResponse response) {
    perform(request, response, false);
  }

  /**
   * @param request the <code>HttpRequest</code>
   * @param response the <code>HttpResponse</code>
   * @param hasBody <code>true</code> to write the message body; <code>false</code> otherwise.
   */
  private void perform(final HttpRequest request, final HttpResponse response, boolean hasBody) {

    final String path = request.getRequestedPath();
    final File file = new File(path.substring(1)); // remove the leading '/'
    if (!file.exists()) {
      throw new HttpException(HttpStatus.CLIENT_ERROR_NOT_FOUND);
    } else if (!file.isFile()) {
      throw new HttpException(HttpStatus.CLIENT_ERROR_FORBIDDEN, path + "is not a file");
    }

    final long lastModified = file.lastModified();
    response.setHeader("Last-Modified", DateUtil.parseToRFC1123(lastModified));
    response.setHeader("Cache-Control", "public");
    String mimeType = mimeTypeMap.getContentType(file);
    if ("text/plain".equals(mimeType)) {
      mimeType += "; charset=utf-8";
    }
    response.setHeader("Content-Type", mimeType);
    final String ifModifiedSince = request.getHeader("If-Modified-Since");
    if (ifModifiedSince != null) {
      final long ims = DateUtil.parseToMilliseconds(ifModifiedSince);
      if (lastModified <= ims) {
        response.setStatus(HttpStatus.REDIRECTION_NOT_MODIFIED);
        logger.debug("not modified");
        return;
      }
    }

    if (hasBody) {
      response.write(file);
    }
  }
}