001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018package org.nuxeo.ecm.platform.mail.adapter; 019 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.nuxeo.ecm.core.api.Blob; 024import org.nuxeo.ecm.core.api.Blobs; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder; 027import org.nuxeo.ecm.platform.mail.utils.MailCoreConstants; 028 029/** 030 * BlobHolder for MailMessage documents. The blob returned is a StringBlob with the mail body message as content. 031 * 032 * @author ldoguin 033 * @since 5.7.3 034 */ 035public class MailMessageBlobHolder extends DocumentBlobHolder { 036 037 protected Pattern isHtmlPattern = Pattern.compile("(.*)<(html|head|body)>(.*)", Pattern.CASE_INSENSITIVE 038 | Pattern.DOTALL); 039 040 public MailMessageBlobHolder(DocumentModel doc, String xPath, String xPathFilename) { 041 super(doc, xPath, xPathFilename); 042 } 043 044 @Override 045 public Blob getBlob() { 046 String htmlTextProperty = (String) doc.getPropertyValue(xPath); 047 Blob blob = null; 048 if (htmlTextProperty != null && xPathFilename != null && htmlTextProperty.length() != 0) { 049 blob = Blobs.createBlob(htmlTextProperty); 050 Matcher m = isHtmlPattern.matcher(htmlTextProperty); 051 if (m.matches()) { 052 blob.setMimeType("text/html"); 053 } // else default is text/plain 054 } else { 055 String txt = (String) doc.getPropertyValue(MailCoreConstants.TEXT_PROPERTY_NAME); 056 if (txt == null) { 057 txt = ""; 058 } 059 blob = Blobs.createBlob(txt); 060 } 061 if (blob != null) { 062 blob.setFilename(xPathFilename); 063 // set dummy digest to avoid comparison error 064 blob.setDigest("notInBinaryStore"); 065 } 066 return blob; 067 } 068}