001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.imap.message; 017 018 import java.io.File; 019 import java.io.FileInputStream; 020 import java.io.FileOutputStream; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.OutputStream; 024 import java.util.Date; 025 026 import javax.mail.Flags; 027 028 import org.apache.commons.io.FileUtils; 029 import org.apache.commons.io.IOUtils; 030 import org.apache.james.mime4j.field.address.Mailbox; 031 032 import com.hs.mail.container.config.Config; 033 034 /** 035 * 036 * @author Won Chul Doh 037 * @since May 5, 2010 038 * 039 */ 040 public class MailMessage extends FetchData { 041 042 private MessageHeader header; 043 private File file; 044 045 private MailMessage(File file) { 046 super(); 047 this.file = file; 048 } 049 050 public MessageHeader getHeader() { 051 return header; 052 } 053 054 public void setHeader(MessageHeader header) { 055 this.header = header; 056 } 057 058 public String getSubject() { 059 return header.getSubject(); 060 } 061 062 public String getFrom() { 063 return header.getFrom().getAddress(); 064 } 065 066 public String getReplyTo() { 067 Mailbox m = header.getReplyTo(); 068 if (m == null) 069 return getFrom(); 070 else 071 return header.getReplyTo().getAddress(); 072 } 073 074 public InputStream getInputStream() throws IOException { 075 return new FileInputStream(file); 076 } 077 078 public void save(boolean deleteSrc) throws IOException { 079 File dest = Config.getDataFile(getInternalDate(), getPhysMessageID()); 080 if (deleteSrc) { 081 FileUtils.moveFile(file, dest); 082 } else { 083 FileUtils.copyFile(file, dest); 084 } 085 } 086 087 public void save(InputStream is) throws IOException { 088 File dest = Config.getDataFile(getInternalDate(), getPhysMessageID()); 089 OutputStream os = null; 090 try { 091 os = new FileOutputStream(dest); 092 IOUtils.copyLarge(is, os); 093 } finally { 094 IOUtils.closeQuietly(os); 095 IOUtils.closeQuietly(is); 096 } 097 } 098 099 public static MailMessage createMailMessage(File file) throws IOException { 100 return createMailMessage(file, new Date(), null); 101 } 102 103 public static MailMessage createMailMessage(File file, Date internalDate, 104 Flags flags) throws IOException { 105 MailMessage message = new MailMessage(file); 106 FileInputStream fis = null; 107 try { 108 fis = new FileInputStream(file); 109 MessageHeader header = new MessageHeader(fis); 110 message.setPhysMessageID(0); 111 message.setHeader(header); 112 message.setSize(file.length()); 113 message.setInternalDate(internalDate); 114 message.setFlags(flags); 115 return message; 116 } finally { 117 IOUtils.closeQuietly(fis); 118 } 119 } 120 121 }