/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.service.dto; import java.util.Date; import java.util.regex.Pattern; import javax.xml.bind.annotation.XmlRootElement; import serpro.mailarchiver.domain.metaarchive.AddressListField; import serpro.mailarchiver.domain.metaarchive.ContentTypeField; import serpro.mailarchiver.domain.metaarchive.DateTimeField; import serpro.mailarchiver.domain.metaarchive.Folder; import serpro.mailarchiver.domain.metaarchive.MailboxField; import serpro.mailarchiver.domain.metaarchive.MailboxListField; import serpro.mailarchiver.domain.metaarchive.Message; import serpro.mailarchiver.domain.metaarchive.TextBody; import serpro.mailarchiver.domain.metaarchive.UnstructuredField; import serpro.mailarchiver.util.BodyVisitor; import serpro.mailarchiver.util.Logger; @XmlRootElement(name="Message") public class TMessage implements Identifiable { private static final Logger log = Logger.getLocalLogger(); private String id; @Override public String getId() { return id; } @Override public void setId(String id) { this.id = id; } //-------------------------------------------------------------------------- private String subject; public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } //-------------------------------------------------------------------------- private String preview; public String getPreview() { return preview; } public void setPreview(String preview) { this.preview = preview; } //-------------------------------------------------------------------------- private long size; public long getSize() { return size; } public void setSize(long size) { this.size = size; } //-------------------------------------------------------------------------- private String sender; public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } //-------------------------------------------------------------------------- private String from; public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } //-------------------------------------------------------------------------- private String to; public String getTo() { return to; } public void setTo(String to) { this.to = to; } //-------------------------------------------------------------------------- private String cc; public String getCc() { return cc; } public void setCc(String cc) { this.cc = cc; } //-------------------------------------------------------------------------- private String bcc; public String getBcc() { return bcc; } public void setBcc(String bcc) { this.bcc = bcc; } //-------------------------------------------------------------------------- private String replyTo; public String getReplyTo() { return replyTo; } public void setReplyTo(String replyTo) { this.replyTo = replyTo; } //-------------------------------------------------------------------------- private String dispositionNotificationTo; public String getDispositionNotificationTo() { return dispositionNotificationTo; } public void setDispositionNotificationTo(String dispositionNotificationTo) { this.dispositionNotificationTo = dispositionNotificationTo; } //-------------------------------------------------------------------------- private long date; public long getDate() { return date; } public void setDate(long date) { this.date = date; } //-------------------------------------------------------------------------- private long receivedDate; public long getReceivedDate() { return receivedDate; } public void setReceivedDate(long receivedDate) { this.receivedDate = receivedDate; } //-------------------------------------------------------------------------- private String folderId; public String getFolderId() { return folderId; } public void setFolderId(String folderId) { this.folderId = folderId; } //-------------------------------------------------------------------------- private String folderPath; public String getFolderPath() { return folderPath; } public void setFolderPath(String folderPath) { this.folderPath = folderPath; } //-------------------------------------------------------------------------- private String tags; public String getTags() { return tags; } public void setTags(String tags) { this.tags = tags; } //-------------------------------------------------------------------------- private String contentType; public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } //-------------------------------------------------------------------------- private String attachments; public String getAttachments() { return attachments; } public void setAttachments(String attachments) { this.attachments = attachments; } //========================================================================== private static final Pattern controlCharactersPattern = Pattern.compile("[\\x00-\\x1F]"); public TMessage() {} public TMessage(Message message) { id = message.getOid(); Folder folder = message.getFolder(); folderId = folder.getExternalOid(); folderPath = folder.getRelativePath().toString().replace('\\', '/'); UnstructuredField subjectField = message.getSubjectField(); subject = (subjectField != null) ? subjectField.getText() : ""; message.visitBodies(new BodyVisitor() { @Override public void visitTextBody(TextBody textBody) { preview = controlCharactersPattern.matcher(textBody.getPreview()).replaceAll(""); quit(); } }); DateTimeField dateField = message.getDateField(); date = (dateField != null && dateField.getDate() != null) ? dateField.getDate().getTime() : 0; Date recDate = message.getLastReceivedDate(); receivedDate = (recDate != null) ? recDate.getTime() : 0; size = message.getSize(); tags = message.getTagsJSONString(); ContentTypeField contentTypeField = message.getContentTypeField(); contentType = (contentTypeField != null) ? contentTypeField.getMediaType() + "/" + contentTypeField.getSubType() : ""; MailboxField senderField = message.getSenderField(); sender = (senderField != null) ? senderField.toJSONString() : ""; MailboxListField fromField = message.getFromField(); from = (fromField != null) ? fromField.toJSONString() : ""; AddressListField toField = message.getToField(); to = (toField != null) ? toField.toJSONString() : ""; AddressListField ccField = message.getCcField(); cc = (ccField != null) ? ccField.toJSONString() : ""; AddressListField bccField = message.getBccField(); bcc = (bccField != null) ? bccField.toJSONString() : ""; AddressListField replyToField = message.getReplyToField(); replyTo = (replyToField != null) ? replyToField.toJSONString() : ""; MailboxListField dispositionNotificationToField = message.getDispositionNotificationToField(); dispositionNotificationTo = (dispositionNotificationToField != null) ? dispositionNotificationToField.toJSONString() : ""; attachments = message.getAttachmentsJSONString(); //DEBUG //System.out.println(toString()); } @Override public String toString() { return String.format( "TMessage%n" + "%1$sid: %2$s%n" + "%1$ssubject: %3$s%n" + "%1$spreview: %4$s%n" + "%1$ssize: %5$d%n" + "%1$ssender: %6$s%n" + "%1$sfrom: %7$s%n" + "%1$sto: %8$s%n" + "%1$scc: %9$s%n" + "%1$sbcc: %10$s%n" + "%1$sreplyTo: %11$s%n" + "%1$sdispositionNotificationTo: %12$s%n" + "%1$sdate: %13$d%n" + "%1$sreceivedDate: %14$d%n" + "%1$sfolderId: %15$s%n" + "%1$sfolderPath: %16$s%n" + "%1$stags: %17$s%n" + "%1$scontentType: %18$s%n" + "%1$sattachments: %19$s" , " " , id , subject , preview , size , sender , from , to , cc , bcc , replyTo , dispositionNotificationTo , date , receivedDate , folderId , folderPath , tags , contentType , attachments); } }