mirror of
https://github.com/sismics/docs.git
synced 2025-12-14 02:06:25 +00:00
#177: import document from EML file (wip)
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
130
docs-core/src/main/java/com/sismics/util/HtmlToPlainText.java
Normal file
130
docs-core/src/main/java/com/sismics/util/HtmlToPlainText.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user