#177: import document from EML file (wip)

This commit is contained in:
Benjamin Gamard 2018-02-21 21:47:57 +01:00
parent 5d335049a2
commit d3a40ebca8
11 changed files with 9805 additions and 314 deletions

View File

@ -67,6 +67,11 @@
<artifactId>javax.json</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>

View File

@ -0,0 +1,39 @@
package com.sismics.docs.core.util;
import com.sismics.docs.core.constant.AclType;
import com.sismics.docs.core.constant.PermType;
import com.sismics.docs.core.dao.jpa.AclDao;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.model.jpa.Acl;
import com.sismics.docs.core.model.jpa.Document;
/**
* Document utilities.
*
* @author bgamard
*/
public class DocumentUtil {
public static Document createDocument(Document document, String userId) {
DocumentDao documentDao = new DocumentDao();
String documentId = documentDao.create(document, userId);
// Create read ACL
AclDao aclDao = new AclDao();
Acl acl = new Acl();
acl.setPerm(PermType.READ);
acl.setType(AclType.USER);
acl.setSourceId(documentId);
acl.setTargetId(userId);
aclDao.create(acl, userId);
// Create write ACL
acl = new Acl();
acl.setPerm(PermType.WRITE);
acl.setType(AclType.USER);
acl.setSourceId(documentId);
acl.setTargetId(userId);
aclDao.create(acl, userId);
return document;
}
}

View File

@ -1,227 +0,0 @@
package com.sismics.util;
import com.google.common.collect.ImmutableMap;
import java.util.Map.Entry;
/**
* Date utilities.
*
* @author jtremeaux
*/
public class DateUtil {
private final static ImmutableMap<String, String> TIMEZONE_CODE_MAP = new ImmutableMap.Builder<String, String>()
.put(" ACDT", " +10:30")
.put(" ACST", " +09:30")
.put(" ACT", " +08")
.put(" ADT", " 03")
.put(" AEDT", " +11")
.put(" AEST", " +10")
.put(" AFT", " +04:30")
.put(" AKDT", " 08")
.put(" AKST", " 09")
.put(" AMST", " +05")
.put(" AMT", " +04")
.put(" ART", " 03")
// .put(" AST", " +03")
.put(" AST", " 04")
.put(" AWDT", " +09")
.put(" AWST", " +08")
.put(" AZOST", " 01")
.put(" AZT", " +04")
.put(" BDT", " +08")
.put(" BIOT", " +06")
.put(" BIT", " 12")
.put(" BOT", " 04")
.put(" BRT", " 03")
// .put(" BST", " +06")
.put(" BST", " +01")
.put(" BTT", " +06")
.put(" CAT", " +02")
.put(" CCT", " +06:30")
.put(" CDT", " 05")
// .put(" CDT", " 04")
.put(" CEDT", " +02")
.put(" CEST", " +02")
.put(" CET", " +01")
.put(" CHADT", " +13:45")
.put(" CHAST", " +12:45")
.put(" CHOT", " 08")
.put(" ChST", " +10")
.put(" CHUT", " +10")
.put(" CIST", " 08")
.put(" CIT", " +08")
.put(" CKT", " 10")
.put(" CLST", " 03")
.put(" CLT", " 04")
.put(" COST", " 04")
.put(" COT", " 05")
.put(" CST", " 06")
// .put(" CST", " +08")
// .put(" CST", " +09:30")
// .put(" CST", " +10:30")
// .put(" CST", " 05")
.put(" CT", " +08")
.put(" CVT", " 01")
.put(" CWST", " +08:45")
.put(" CXT", " +07")
.put(" DAVT", " +07")
.put(" DDUT", " +10")
.put(" DFT", " +01")
.put(" EASST", " 05")
.put(" EAST", " 06")
.put(" EAT", " +03")
// .put(" ECT", " 04")
.put(" ECT", " 05")
.put(" EDT", " 04")
.put(" EEDT", " +03")
.put(" EEST", " +03")
.put(" EET", " +02")
.put(" EGST", " +00")
.put(" EGT", " 01")
.put(" EIT", " +09")
.put(" EST", " 05")
// .put(" EST", " +10")
.put(" FET", " +03")
.put(" FJT", " +12")
.put(" FKST", " 03")
.put(" FKT", " 04")
.put(" FNT", " 02")
.put(" GALT", " 06")
.put(" GAMT", " 09")
.put(" GET", " +04")
.put(" GFT", " 03")
.put(" GILT", " +12")
.put(" GIT", " 09")
.put(" GMT", " ")
// .put(" GST", " 02")
.put(" GST", " +04")
.put(" GYT", " 04")
.put(" HADT", " 09")
.put(" HAEC", " +02")
.put(" HAST", " 10")
.put(" HKT", " +08")
.put(" HMT", " +05")
.put(" HOVT", " +07")
.put(" HST", " 10")
.put(" ICT", " +07")
.put(" IDT", " +03")
.put(" IOT", " +03")
.put(" IRDT", " +08")
.put(" IRKT", " +09")
.put(" IRST", " +03:30")
.put(" IST", " +05:30")
// .put(" IST", " +01")
// .put(" IST", " +02")
// .put(" JST", " +09")
.put(" KGT", " +06")
.put(" KOST", " +11")
.put(" KRAT", " +07")
.put(" KST", " +09")
.put(" LHST", " +10:30")
// .put(" LHST", " +11")
.put(" LINT", " +14")
.put(" MAGT", " +12")
.put(" MART", " 09:30")
.put(" MAWT", " +05")
.put(" MDT", " 06")
.put(" MET", " +01")
.put(" MEST", " +02")
.put(" MHT", " +12")
.put(" MIST", " +11")
.put(" MIT", " 09:30")
.put(" MMT", " +06:30")
.put(" MSK", " +04")
// .put(" MST", " +08")
.put(" MST", " 07")
// .put(" MST", " +06:30")
.put(" MUT", " +04")
.put(" MVT", " +05")
.put(" MYT", " +08")
.put(" NCT", " +11")
.put(" NDT", " 02:30")
.put(" NFT", " +11:30")
.put(" NPT", " +05:45")
.put(" NST", " 03:30")
.put(" NT", " 03:30")
.put(" NUT", " 11:30")
.put(" NZDT", " +13")
.put(" NZST", " +12")
.put(" OMST", " +06")
.put(" ORAT", " +05")
.put(" PDT", " 07")
.put(" PET", " 05")
.put(" PETT", " +12")
.put(" PGT", " +10")
.put(" PHOT", " +13")
.put(" PHT", " +08")
.put(" PKT", " +05")
.put(" PMDT", " 02")
.put(" PMST", " 03")
.put(" PONT", " +11")
.put(" PST", " 08")
// .put(" PST", " +08")
.put(" RET", " +04")
.put(" ROTT", " 03")
.put(" SAKT", " +11")
.put(" SAMT", " +04")
.put(" SAST", " +02")
.put(" SBT", " +11")
.put(" SCT", " +04")
.put(" SGT", " +08")
.put(" SLT", " +05:30")
.put(" SRT", " 03")
.put(" SST", " 11")
// .put(" SST", " +08")
.put(" SYOT", " +03")
.put(" TAHT", " 10")
.put(" THA", " +07")
.put(" TFT", " +05")
.put(" TJT", " +05")
.put(" TKT", " +14")
.put(" TLT", " +09")
.put(" TMT", " +05")
.put(" TOT", " +13")
.put(" TVT", " +12")
.put(" UCT", " ")
.put(" ULAT", " +08")
.put(" UTC", " ")
.put(" UYST", " 02")
.put(" UYT", " 03")
.put(" UZT", " +05")
.put(" VET", " 04:30")
.put(" VLAT", " +10")
.put(" VOLT", " +04")
.put(" VOST", " +06")
.put(" VUT", " +11")
.put(" WAKT", " +12")
.put(" WAST", " +02")
.put(" WAT", " +01")
.put(" WEDT", " +01")
.put(" WEST", " +01")
.put(" WET", " ")
.put(" WST", " +08")
.put(" YAKT", " +09")
.put(" YEKT", " +05")
.build();
/**
* Try to guess the timezone code, and replace it with a timezone offset.
* Note: JodaTime can guess a few codes already, they didn't include all codes since they are not standardized.
* This method should only be used in last resort.
*
* @param date Formated date, supposedly ending with a timezone code
* @return Date with the code replaced by its offset if there is a match
*/
public static String guessTimezoneOffset(String date) {
for (Entry<String, String> entry : TIMEZONE_CODE_MAP.entrySet()) {
String code = entry.getKey();
String offset = entry.getValue();
if (date.endsWith(code)) {
return date.substring(0, date.length() - code.length()) + offset;
}
}
return date;
}
}

View File

@ -1,24 +1,39 @@
package com.sismics.util;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.sismics.docs.core.constant.ConfigType;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.jpa.ConfigDao;
import com.sismics.docs.core.dao.jpa.dto.UserDto;
import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.core.util.ConfigUtil;
import com.sismics.util.context.ThreadLocalContext;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.Template;
import org.apache.commons.mail.HtmlEmail;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -159,4 +174,92 @@ public class EmailUtil {
String subject = MessageUtil.getMessage(userLocale, "email.template." + templateName + ".subject");
sendEmail(templateName, recipientUser, subject, paramMap);
}
public static void parseMailContent(Part part, MailContent mailContent) throws MessagingException, IOException {
Object content = part.getContent();
if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
int partCount = multiPart.getCount();
for (int partIndex = 0; partIndex < partCount; partIndex++) {
MimeBodyPart subPart = (MimeBodyPart) multiPart.getBodyPart(partIndex);
String disposition = subPart.getDisposition();
if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
FileContent fileContent = new FileContent();
fileContent.name = subPart.getFileName();
fileContent.file = ThreadLocalContext.get().createTemporaryFile();
Files.copy(subPart.getInputStream(), fileContent.file, StandardCopyOption.REPLACE_EXISTING);
fileContent.size = Files.size(fileContent.file);
mailContent.fileContentList.add(fileContent);
} else {
parseMailContent(subPart, mailContent);
}
}
} else if (content instanceof Message) {
// An email attached to an email, traverse its content
parseMailContent((Message) content, mailContent);
} else if (content instanceof String) {
if (mailContent.message == null) {
// Do not overwrite the content
if (part.isMimeType("text/plain")) {
mailContent.message = (String) content;
} else if (part.isMimeType("text/html")) {
// Convert HTML to plain text
mailContent.message = new HtmlToPlainText().getPlainText(Jsoup.parse((String) content));
}
}
} else if (content instanceof InputStream) {
FileContent fileContent = new FileContent();
fileContent.file = ThreadLocalContext.get().createTemporaryFile();
Files.copy((InputStream) content, fileContent.file, StandardCopyOption.REPLACE_EXISTING);
fileContent.size = Files.size(fileContent.file);
mailContent.fileContentList.add(fileContent);
}
}
/**
* Structure defining a parsed email to be imported.
*/
public static class MailContent {
private String subject;
private String message;
private Date date;
List<FileContent> fileContentList = Lists.newArrayList();
public String getSubject() {
return subject;
}
public String getMessage() {
return message;
}
public List<FileContent> getFileContentList() {
return fileContentList;
}
public MailContent setSubject(String subject) {
this.subject = subject;
return this;
}
public Date getDate() {
return date;
}
public MailContent setDate(Date date) {
this.date = date;
return this;
}
}
/**
* Structure defining a file from an email to be imported.
*/
public static class FileContent {
private String name;
private Path file;
private long size;
}
}

View File

@ -0,0 +1,130 @@
package com.sismics.util;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;
import java.io.IOException;
/**
* HTML to plain-text. This example program demonstrates the use of jsoup to convert HTML input to lightly-formatted
* plain-text. That is divergent from the general goal of jsoup's .text() methods, which is to get clean data from a
* scrape.
* <p>
* Note that this is a fairly simplistic formatter -- for real world use you'll want to embrace and extend.
* </p>
* <p>
* To invoke from the command line, assuming you've downloaded the jsoup jar to your current directory:</p>
* <p><code>java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]</code></p>
* where <i>url</i> is the URL to fetch, and <i>selector</i> is an optional CSS selector.
*
* @author Jonathan Hedley, jonathan@hedley.net
*/
public class HtmlToPlainText {
private static final String userAgent = "Mozilla/5.0 (jsoup)";
private static final int timeout = 5 * 1000;
public static void main(String... args) throws IOException {
Validate.isTrue(args.length == 1 || args.length == 2, "usage: java -cp jsoup.jar org.jsoup.examples.HtmlToPlainText url [selector]");
final String url = args[0];
final String selector = args.length == 2 ? args[1] : null;
// fetch the specified URL and parse to a HTML DOM
Document doc = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).get();
HtmlToPlainText formatter = new HtmlToPlainText();
if (selector != null) {
Elements elements = doc.select(selector); // get each element that matches the CSS selector
for (Element element : elements) {
String plainText = formatter.getPlainText(element); // format that element to plain text
System.out.println(plainText);
}
} else { // format the whole doc
String plainText = formatter.getPlainText(doc);
System.out.println(plainText);
}
}
/**
* Format an Element to plain-text
* @param element the root element to format
* @return formatted text
*/
public String getPlainText(Element element) {
FormattingVisitor formatter = new FormattingVisitor();
NodeTraversor.traverse(formatter, element); // walk the DOM, and call .head() and .tail() for each node
return formatter.toString();
}
// the formatting rules, implemented in a breadth-first DOM traverse
private class FormattingVisitor implements NodeVisitor {
private static final int maxWidth = 80;
private int width = 0;
private StringBuilder accum = new StringBuilder(); // holds the accumulated text
// hit when the node is first seen
public void head(Node node, int depth) {
String name = node.nodeName();
if (node instanceof TextNode)
append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
else if (name.equals("li"))
append("\n * ");
else if (name.equals("dt"))
append(" ");
else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr"))
append("\n");
}
// hit when all of the node's children (if any) have been visited
public void tail(Node node, int depth) {
String name = node.nodeName();
if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5"))
append("\n");
else if (name.equals("a"))
append(String.format(" <%s>", node.absUrl("href")));
}
// appends text to the string builder with a simple word wrap method
private void append(String text) {
if (text.startsWith("\n"))
width = 0; // reset counter if starts with a newline. only from formats above, not in natural text
if (text.equals(" ") &&
(accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ", "\n")))
return; // don't accumulate long runs of empty spaces
if (text.length() + width > maxWidth) { // won't fit, needs to wrap
String words[] = text.split("\\s+");
for (int i = 0; i < words.length; i++) {
String word = words[i];
boolean last = i == words.length - 1;
if (!last) // insert a space if not the last word
word = word + " ";
if (word.length() + width > maxWidth) { // wrap and reset counter
accum.append("\n").append(word);
width = word.length();
} else {
accum.append(word);
width += word.length();
}
}
} else { // fits as is, without need to wrap text
accum.append(text);
width += text.length();
}
}
@Override
public String toString() {
return accum.toString();
}
}
}

View File

@ -1,17 +0,0 @@
package com.sismics.util;
import org.junit.Assert;
import org.junit.Test;
/**
* Test of the date utilities.
*
* @author jtremeaux
*/
public class TestDateUtil {
@Test
public void guessTimezoneCodeTest() throws Exception {
Assert.assertEquals("Thu, 04 APR 2013 20:37:27 +10", DateUtil.guessTimezoneOffset("Thu, 04 APR 2013 20:37:27 AEST"));
}
}

View File

@ -13,10 +13,10 @@ import com.sismics.docs.core.event.DocumentCreatedAsyncEvent;
import com.sismics.docs.core.event.DocumentDeletedAsyncEvent;
import com.sismics.docs.core.event.DocumentUpdatedAsyncEvent;
import com.sismics.docs.core.event.FileDeletedAsyncEvent;
import com.sismics.docs.core.model.jpa.Acl;
import com.sismics.docs.core.model.jpa.Document;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.util.DocumentUtil;
import com.sismics.docs.core.util.PdfUtil;
import com.sismics.docs.core.util.jpa.PaginatedList;
import com.sismics.docs.core.util.jpa.PaginatedLists;
@ -26,9 +26,12 @@ import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.AclUtil;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.EmailUtil;
import com.sismics.util.JsonUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.mime.MimeType;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
@ -38,11 +41,18 @@ import org.joda.time.format.DateTimeParser;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.MessageFormat;
import java.util.*;
@ -595,7 +605,6 @@ public class DocumentResource extends BaseResource {
}
// Create the document
DocumentDao documentDao = new DocumentDao();
Document document = new Document();
document.setUserId(principal.getId());
document.setTitle(title);
@ -614,30 +623,15 @@ public class DocumentResource extends BaseResource {
} else {
document.setCreateDate(createDate);
}
String documentId = documentDao.create(document, principal.getId());
// Create read ACL
AclDao aclDao = new AclDao();
Acl acl = new Acl();
acl.setPerm(PermType.READ);
acl.setType(AclType.USER);
acl.setSourceId(documentId);
acl.setTargetId(principal.getId());
aclDao.create(acl, principal.getId());
// Create write ACL
acl = new Acl();
acl.setPerm(PermType.WRITE);
acl.setType(AclType.USER);
acl.setSourceId(documentId);
acl.setTargetId(principal.getId());
aclDao.create(acl, principal.getId());
// Save the document, create the base ACLs
document = DocumentUtil.createDocument(document, principal.getId());
// Update tags
updateTagList(documentId, tagList);
updateTagList(document.getId(), tagList);
// Update relations
updateRelationList(documentId, relationList);
updateRelationList(document.getId(), relationList);
// Raise a document created event
DocumentCreatedAsyncEvent documentCreatedAsyncEvent = new DocumentCreatedAsyncEvent();
@ -646,7 +640,7 @@ public class DocumentResource extends BaseResource {
ThreadLocalContext.get().addAsyncEvent(documentCreatedAsyncEvent);
JsonObjectBuilder response = Json.createObjectBuilder()
.add("id", documentId);
.add("id", document.getId());
return Response.ok().entity(response.build()).build();
}
@ -772,50 +766,90 @@ public class DocumentResource extends BaseResource {
}
/**
* Update tags list on a document.
* Import a new document from an EML file.
*
* @param documentId Document ID
* @param tagList Tag ID list
* @api {put} /document/eml Import a new document from an EML file
* @apiName PutDocumentEml
* @apiGroup Document
* @apiParam {String} file File data
* @apiError (client) ForbiddenError Access denied
* @apiError (client) ValidationError Validation error
* @apiError (server) StreamError Error reading the input file
* @apiError (server) ErrorGuessMime Error guessing mime type
* @apiError (client) QuotaReached Quota limit reached
* @apiError (server) FileError Error adding a file
* @apiPermission user
* @apiVersion 1.5.0
*
* @param fileBodyPart File to import
* @return Response
*/
private void updateTagList(String documentId, List<String> tagList) {
if (tagList != null) {
TagDao tagDao = new TagDao();
Set<String> tagSet = new HashSet<>();
Set<String> tagIdSet = new HashSet<>();
List<TagDto> tagDtoList = tagDao.findByCriteria(new TagCriteria().setTargetIdList(getTargetIdList(null)), null);
for (TagDto tagDto : tagDtoList) {
tagIdSet.add(tagDto.getId());
}
for (String tagId : tagList) {
if (!tagIdSet.contains(tagId)) {
throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", tagId));
}
tagSet.add(tagId);
}
tagDao.updateTagList(documentId, tagSet);
}
@PUT
@Path("eml")
@Consumes("multipart/form-data")
public Response importEml(@FormDataParam("file") FormDataBodyPart fileBodyPart) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
/**
* Update relations list on a document.
*
* @param documentId Document ID
* @param relationList Relation ID list
*/
private void updateRelationList(String documentId, List<String> relationList) {
if (relationList != null) {
DocumentDao documentDao = new DocumentDao();
RelationDao relationDao = new RelationDao();
Set<String> documentIdSet = new HashSet<>();
for (String targetDocId : relationList) {
// ACL are not checked, because the editing user is not forced to view the target document
Document document = documentDao.getById(targetDocId);
if (document != null && !documentId.equals(targetDocId)) {
documentIdSet.add(targetDocId);
// Validate input data
ValidationUtil.validateRequired(fileBodyPart, "file");
// Save the file to a temporary file
java.nio.file.Path unencryptedFile;
try {
unencryptedFile = ThreadLocalContext.get().createTemporaryFile();
Files.copy(fileBodyPart.getValueAs(InputStream.class), unencryptedFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new ServerException("StreamError", "Error reading the input file", e);
}
// Read the EML file
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
EmailUtil.MailContent mailContent = new EmailUtil.MailContent();
try (InputStream inputStream = Files.newInputStream(unencryptedFile)) {
Message message = new MimeMessage(mailSession, inputStream);
mailContent.setSubject(message.getSubject());
mailContent.setDate(message.getSentDate());
EmailUtil.parseMailContent(message, mailContent);
} catch (IOException | MessagingException e) {
throw new ServerException("StreamError", "Error reading the temporary file", e);
}
relationDao.updateRelationList(documentId, documentIdSet);
// Create the document
Document document = new Document();
document.setUserId(principal.getId());
if (mailContent.getSubject() == null) {
document.setTitle("Imported email from EML file");
} else {
document.setTitle(mailContent.getSubject());
}
document.setDescription(mailContent.getMessage());
document.setSubject(mailContent.getSubject());
document.setFormat("EML");
document.setSource("Email");
document.setLanguage("eng");
if (mailContent.getDate() == null) {
document.setCreateDate(new Date());
} else {
document.setCreateDate(mailContent.getDate());
}
// Save the document, create the base ACLs
document = DocumentUtil.createDocument(document, principal.getId());
// Raise a document created event
DocumentCreatedAsyncEvent documentCreatedAsyncEvent = new DocumentCreatedAsyncEvent();
documentCreatedAsyncEvent.setUserId(principal.getId());
documentCreatedAsyncEvent.setDocument(document);
ThreadLocalContext.get().addAsyncEvent(documentCreatedAsyncEvent);
// TODO Add files to the document
JsonObjectBuilder response = Json.createObjectBuilder()
.add("id", document.getId());
return Response.ok().entity(response.build()).build();
}
/**
@ -873,4 +907,51 @@ public class DocumentResource extends BaseResource {
.add("status", "ok");
return Response.ok().entity(response.build()).build();
}
/**
* Update tags list on a document.
*
* @param documentId Document ID
* @param tagList Tag ID list
*/
private void updateTagList(String documentId, List<String> tagList) {
if (tagList != null) {
TagDao tagDao = new TagDao();
Set<String> tagSet = new HashSet<>();
Set<String> tagIdSet = new HashSet<>();
List<TagDto> tagDtoList = tagDao.findByCriteria(new TagCriteria().setTargetIdList(getTargetIdList(null)), null);
for (TagDto tagDto : tagDtoList) {
tagIdSet.add(tagDto.getId());
}
for (String tagId : tagList) {
if (!tagIdSet.contains(tagId)) {
throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", tagId));
}
tagSet.add(tagId);
}
tagDao.updateTagList(documentId, tagSet);
}
}
/**
* Update relations list on a document.
*
* @param documentId Document ID
* @param relationList Relation ID list
*/
private void updateRelationList(String documentId, List<String> relationList) {
if (relationList != null) {
DocumentDao documentDao = new DocumentDao();
RelationDao relationDao = new RelationDao();
Set<String> documentIdSet = new HashSet<>();
for (String targetDocId : relationList) {
// ACL are not checked, because the editing user is not forced to view the target document
Document document = documentDao.getById(targetDocId);
if (document != null && !documentId.equals(targetDocId)) {
documentIdSet.add(targetDocId);
}
}
relationDao.updateRelationList(documentId, documentIdSet);
}
}
}

View File

@ -77,7 +77,6 @@ public class FileResource extends BaseResource {
* @apiError (client) NotFound Document not found
* @apiError (server) StreamError Error reading the input file
* @apiError (server) ErrorGuessMime Error guessing mime type
* @apiError (client) InvalidFileType File type not recognized
* @apiError (client) QuotaReached Quota limit reached
* @apiError (server) FileError Error adding a file
* @apiPermission user

View File

@ -602,4 +602,29 @@ public class TestDocumentResource extends BaseJerseyTest {
Assert.assertTrue(fileBytes.length > 0); // Images rendered from PDF differ in size from OS to OS due to font issues
Assert.assertEquals(MimeType.IMAGE_JPEG, MimeTypeUtil.guessMimeType(fileBytes, null));
}
/**
* Test EML import.
*
* @throws Exception e
*/
@Test
public void testEmlImport() throws Exception {
// Login document_eml
clientUtil.createUser("document_eml");
String documentEmlToken = clientUtil.login("document_eml");
// Import a document as EML
try (InputStream is = Resources.getResource("file/test_mail.eml").openStream()) {
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "test_mail.eml");
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
target()
.register(MultiPartFeature.class)
.path("/document/eml").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentEmlToken)
.put(Entity.entity(multiPart.bodyPart(streamDataBodyPart),
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -44,6 +44,7 @@
<com.levigo.jbig2.levigo-jbig2-imageio.version>1.6.5</com.levigo.jbig2.levigo-jbig2-imageio.version>
<com.github.jai-imageio.jai-imageio-core.version>1.3.1</com.github.jai-imageio.jai-imageio-core.version>
<org.subethamail.subethasmtp-wiser.version>1.2</org.subethamail.subethasmtp-wiser.version>
<org.jsoup.jsoup.version>1.11.2</org.jsoup.jsoup.version>
<org.eclipse.jetty.jetty-server.version>9.2.13.v20150730</org.eclipse.jetty.jetty-server.version>
<org.eclipse.jetty.jetty-webapp.version>9.2.13.v20150730</org.eclipse.jetty.jetty-webapp.version>
@ -214,6 +215,12 @@
<version>${com.google.guava.guava.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${org.jsoup.jsoup.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>