static String getFileName(FileAttachment fatt) { if (fatt.getFilename() == null || fatt.getFilename().isEmpty()) return fatt.getLongFilename(); return fatt.getFilename(); }
/** Import MSG file as MailNode. */ private String importMsg(String path, InputStream is) throws MessagingException, PathNotFoundException, ItemExistsException, VirusDetectedException, AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException, UnsupportedMimeTypeException, FileSizeExceededException, ExtensionException, AutomationException, IOException { log.debug("importMsg({}, {})", path, is); String errorMsg = null; try { // Convert file MsgParser msgp = new MsgParser(); Message msg = msgp.parseMsg(is); Mail mail = MailUtils.messageToMail(msg); // Create phantom path. In this case we don't have the IMAP message ID, son create a random // one. mail.setPath( path + "/" + UUID.randomUUID().toString() + "-" + PathUtils.escape(mail.getSubject())); // Import files OKMMail.getInstance().create(null, mail); for (Attachment att : msg.getAttachments()) { if (att instanceof FileAttachment) { FileAttachment fileAtt = (FileAttachment) att; log.debug("Importing attachment: {}", fileAtt.getFilename()); String fileName = fileAtt.getFilename(); String fileExtension = fileAtt.getExtension(); String testName = fileName + "." + fileExtension; // Test if already exists a document with the same name in the mail for (int j = 1; OKMRepository.getInstance().hasNode(null, mail.getPath() + "/" + testName); j++) { // log.debug("Trying with: {}", testName); testName = fileName + " (" + j + ")." + fileExtension; } Document attachment = new Document(); String mimeType = MimeTypeConfig.mimeTypes.getContentType(testName.toLowerCase()); attachment.setMimeType(mimeType); attachment.setPath(mail.getPath() + "/" + testName); ByteArrayInputStream bais = new ByteArrayInputStream(fileAtt.getData()); if (Config.REPOSITORY_NATIVE) { new DbDocumentModule() .create(null, attachment, bais, fileAtt.getSize(), PrincipalUtils.getUser()); } else { new JcrDocumentModule().create(null, attachment, bais, PrincipalUtils.getUser()); } IOUtils.closeQuietly(bais); } } } catch (IOException e) { log.error("Error importing msg", e); throw e; } finally { IOUtils.closeQuietly(is); } log.debug("importMsg: {}", errorMsg); return errorMsg; }