List of usage examples for org.apache.commons.mail.util MimeMessageParser getAttachmentList
public List<DataSource> getAttachmentList()
From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java
final public void readMessage(IndexDocument crawlIndexDocument, IndexDocument parserIndexDocument, Folder folder, Message message, String id) throws Exception { crawlIndexDocument.addString(MailboxFieldEnum.message_id.name(), id); crawlIndexDocument.addString(MailboxFieldEnum.message_number.name(), Integer.toString(message.getMessageNumber())); if (message instanceof MimeMessage) crawlIndexDocument.addString(MailboxFieldEnum.content_id.name(), ((MimeMessage) message).getContentID()); crawlIndexDocument.addString(MailboxFieldEnum.subject.name(), message.getSubject()); putAddresses(crawlIndexDocument, message.getFrom(), MailboxFieldEnum.from_address.name(), MailboxFieldEnum.from_personal.name()); putAddresses(crawlIndexDocument, message.getReplyTo(), MailboxFieldEnum.reply_to_address.name(), MailboxFieldEnum.reply_to_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.TO), MailboxFieldEnum.recipient_to_address.name(), MailboxFieldEnum.recipient_to_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.CC), MailboxFieldEnum.recipient_cc_address.name(), MailboxFieldEnum.recipient_cc_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.BCC), MailboxFieldEnum.recipient_bcc_address.name(), MailboxFieldEnum.recipient_bcc_personal.name()); Date dt = message.getSentDate(); if (dt != null) crawlIndexDocument.addString(MailboxFieldEnum.send_date.name(), dt.toString()); dt = message.getReceivedDate();/* www . j a v a 2 s.co m*/ if (dt != null) crawlIndexDocument.addString(MailboxFieldEnum.received_date.name(), dt.toString()); if (message.isSet(Flag.ANSWERED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "ANSWERED"); if (message.isSet(Flag.DELETED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DELETED"); if (message.isSet(Flag.DRAFT)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DRAFT"); if (message.isSet(Flag.FLAGGED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "FLAGGED"); if (message.isSet(Flag.SEEN)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "SEEN"); if (message instanceof MimeMessage) { MimeMessageParser mimeMessageParser = new MimeMessageParser((MimeMessage) message).parse(); crawlIndexDocument.addString(MailboxFieldEnum.html_content.name(), mimeMessageParser.getHtmlContent()); crawlIndexDocument.addString(MailboxFieldEnum.plain_content.name(), mimeMessageParser.getPlainContent()); for (DataSource dataSource : mimeMessageParser.getAttachmentList()) { crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_name.name(), dataSource.getName()); crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_type.name(), dataSource.getContentType()); if (parserSelector == null) continue; Parser attachParser = parserSelector.parseStream(null, dataSource.getName(), dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null); if (attachParser == null) continue; List<ParserResultItem> parserResults = attachParser.getParserResults(); if (parserResults != null) for (ParserResultItem parserResult : parserResults) crawlIndexDocument.addFieldIndexDocument(MailboxFieldEnum.email_attachment_content.name(), parserResult.getParserDocument()); } } }
From source file:com.opensearchserver.extractor.parser.Eml.java
@Override protected void parseContent(InputStream inputStream, String extension, String mimeType) throws Exception { Session session = Session.getDefaultInstance(JAVAMAIL_PROPS); MimeMessage mimeMessage = new MimeMessage(session, inputStream); MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse(); ParserDocument document = getNewParserDocument(); String from = mimeMessageParser.getFrom(); if (from != null) document.add(FROM, from.toString()); for (Address address : mimeMessageParser.getTo()) document.add(RECIPIENT_TO, address.toString()); for (Address address : mimeMessageParser.getCc()) document.add(RECIPIENT_CC, address.toString()); for (Address address : mimeMessageParser.getBcc()) document.add(RECIPIENT_BCC, address.toString()); document.add(SUBJECT, mimeMessageParser.getSubject()); document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent()); document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent()); document.add(SENT_DATE, mimeMessage.getSentDate()); document.add(RECEIVED_DATE, mimeMessage.getReceivedDate()); for (DataSource dataSource : mimeMessageParser.getAttachmentList()) { document.add(ATTACHMENT_NAME, dataSource.getName()); document.add(ATTACHMENT_TYPE, dataSource.getContentType()); // TODO Extract content from attachmend // if (parserSelector != null) { // Parser attachParser = parserSelector.parseStream( // getSourceDocument(), dataSource.getName(), // dataSource.getContentType(), null, // dataSource.getInputStream(), null, null, null); // if (attachParser != null) { // List<ParserResultItem> parserResults = attachParser // .getParserResults(); // if (parserResults != null) // for (ParserResultItem parserResult : parserResults) // result.addField( // ParserFieldEnum.email_attachment_content, // parserResult); // }/*from w w w .java 2s . c o m*/ // } } if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent())) document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000)); else document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000)); }
From source file:com.qwazr.library.email.EmlParser.java
@Override public void parseContent(final MultivaluedMap<String, String> parameters, final InputStream inputStream, final String extension, final String mimeType, final ParserResultBuilder resultBuilder) throws Exception { final Session session = Session.getDefaultInstance(JAVAMAIL_PROPS); resultBuilder.metas().set(MIME_TYPE, findMimeType(extension, mimeType, this::findMimeTypeUsingDefault)); final MimeMessage mimeMessage = new MimeMessage(session, inputStream); final MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse(); ParserFieldsBuilder document = resultBuilder.newDocument(); final String from = mimeMessageParser.getFrom(); if (from != null) document.add(FROM, from);//from w w w . ja v a2 s . c om for (Address address : mimeMessageParser.getTo()) document.add(RECIPIENT_TO, address.toString()); for (Address address : mimeMessageParser.getCc()) document.add(RECIPIENT_CC, address.toString()); for (Address address : mimeMessageParser.getBcc()) document.add(RECIPIENT_BCC, address.toString()); document.add(SUBJECT, mimeMessageParser.getSubject()); document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent()); document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent()); document.add(SENT_DATE, mimeMessage.getSentDate()); document.add(RECEIVED_DATE, mimeMessage.getReceivedDate()); for (DataSource dataSource : mimeMessageParser.getAttachmentList()) { document.add(ATTACHMENT_NAME, dataSource.getName()); document.add(ATTACHMENT_TYPE, dataSource.getContentType()); // TODO Extract content from attachmend // if (parserSelector != null) { // Parser attachParser = parserSelector.parseStream( // getSourceDocument(), dataSource.getName(), // dataSource.getContentType(), null, // dataSource.getInputStream(), null, null, null); // if (attachParser != null) { // List<ParserResultItem> parserResults = attachParser // .getParserResults(); // if (parserResults != null) // for (ParserResultItem parserResult : parserResults) // result.addField( // ParserFieldEnum.email_attachment_content, // parserResult); // } // } } if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent())) document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000)); else document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000)); }
From source file:com.jaeksoft.searchlib.parser.EmlParser.java
@Override protected void parseContent(StreamLimiter streamLimiter, LanguageEnum lang) throws IOException, SearchLibException { Session session = Session.getDefaultInstance(JAVAMAIL_PROPS); try {//from ww w . j a v a 2s .c o m MimeMessage mimeMessage = new MimeMessage(session, streamLimiter.getNewInputStream()); MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse(); ParserResultItem result = getNewParserResultItem(); String from = mimeMessageParser.getFrom(); if (from != null) result.addField(ParserFieldEnum.email_display_from, from.toString()); for (Address address : mimeMessageParser.getTo()) result.addField(ParserFieldEnum.email_display_to, address.toString()); for (Address address : mimeMessageParser.getCc()) result.addField(ParserFieldEnum.email_display_cc, address.toString()); for (Address address : mimeMessageParser.getBcc()) result.addField(ParserFieldEnum.email_display_bcc, address.toString()); result.addField(ParserFieldEnum.subject, mimeMessageParser.getSubject()); result.addField(ParserFieldEnum.htmlSource, mimeMessageParser.getHtmlContent()); result.addField(ParserFieldEnum.content, mimeMessageParser.getPlainContent()); result.addField(ParserFieldEnum.email_sent_date, mimeMessage.getSentDate()); result.addField(ParserFieldEnum.email_received_date, mimeMessage.getReceivedDate()); for (DataSource dataSource : mimeMessageParser.getAttachmentList()) { result.addField(ParserFieldEnum.email_attachment_name, dataSource.getName()); result.addField(ParserFieldEnum.email_attachment_type, dataSource.getContentType()); if (parserSelector == null) continue; Parser attachParser = parserSelector.parseStream(getSourceDocument(), dataSource.getName(), dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null); if (attachParser == null) continue; List<ParserResultItem> parserResults = attachParser.getParserResults(); if (parserResults != null) for (ParserResultItem parserResult : parserResults) result.addField(ParserFieldEnum.email_attachment_content, parserResult); } if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent())) result.langDetection(10000, ParserFieldEnum.content); else result.langDetection(10000, ParserFieldEnum.htmlSource); } catch (Exception e) { throw new IOException(e); } }
From source file:edu.hawaii.soest.hioos.storx.StorXDispatcher.java
/** * A method that executes the reading of data from the email account to the * RBNB server after all configuration of settings, connections to hosts, * and thread initiatizing occurs. This method contains the detailed code * for reading the data and interpreting the data files. *///w w w. ja v a2 s.co m protected boolean execute() { logger.debug("StorXDispatcher.execute() called."); boolean failed = true; // indicates overall success of execute() boolean messageProcessed = false; // indicates per message success // declare the account properties that will be pulled from the // email.account.properties.xml file String accountName = ""; String server = ""; String username = ""; String password = ""; String protocol = ""; String dataMailbox = ""; String processedMailbox = ""; String prefetch = ""; // fetch data from each sensor in the account list List accountList = this.xmlConfiguration.getList("account.accountName"); for (Iterator aIterator = accountList.iterator(); aIterator.hasNext();) { int aIndex = accountList.indexOf(aIterator.next()); // populate the email connection variables from the xml properties // file accountName = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").accountName"); server = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").server"); username = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").username"); password = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").password"); protocol = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").protocol"); dataMailbox = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").dataMailbox"); processedMailbox = (String) this.xmlConfiguration .getProperty("account(" + aIndex + ").processedMailbox"); prefetch = (String) this.xmlConfiguration.getProperty("account(" + aIndex + ").prefetch"); logger.debug("\n\nACCOUNT DETAILS: \n" + "accountName : " + accountName + "\n" + "server : " + server + "\n" + "username : " + username + "\n" + "password : " + password + "\n" + "protocol : " + protocol + "\n" + "dataMailbox : " + dataMailbox + "\n" + "processedMailbox: " + processedMailbox + "\n" + "prefetch : " + prefetch + "\n"); // get a connection to the mail server Properties props = System.getProperties(); props.setProperty("mail.store.protocol", protocol); props.setProperty("mail.imaps.partialfetch", prefetch); try { // create the imaps mail session this.mailSession = Session.getDefaultInstance(props, null); this.mailStore = mailSession.getStore(protocol); } catch (NoSuchProviderException nspe) { try { // pause for 10 seconds logger.debug( "There was a problem connecting to the IMAP server. " + "Waiting 10 seconds to retry."); Thread.sleep(10000L); this.mailStore = mailSession.getStore(protocol); } catch (NoSuchProviderException nspe2) { logger.debug("There was an error connecting to the mail server. The " + "message was: " + nspe2.getMessage()); nspe2.printStackTrace(); failed = true; return !failed; } catch (InterruptedException ie) { logger.debug("The thread was interrupted: " + ie.getMessage()); failed = true; return !failed; } } try { this.mailStore.connect(server, username, password); // get folder references for the inbox and processed data box Folder inbox = mailStore.getFolder(dataMailbox); inbox.open(Folder.READ_WRITE); Folder processed = this.mailStore.getFolder(processedMailbox); processed.open(Folder.READ_WRITE); Message[] msgs; while (!inbox.isOpen()) { inbox.open(Folder.READ_WRITE); } msgs = inbox.getMessages(); List<Message> messages = new ArrayList<Message>(); Collections.addAll(messages, msgs); // sort the messages found in the inbox by date sent Collections.sort(messages, new Comparator<Message>() { public int compare(Message message1, Message message2) { int value = 0; try { value = message1.getSentDate().compareTo(message2.getSentDate()); } catch (MessagingException e) { e.printStackTrace(); } return value; } }); logger.debug("Number of messages: " + messages.size()); for (Message message : messages) { // Copy the message to ensure we have the full attachment MimeMessage mimeMessage = (MimeMessage) message; MimeMessage copiedMessage = new MimeMessage(mimeMessage); // determine the sensor serial number for this message String messageSubject = copiedMessage.getSubject(); Date sentDate = copiedMessage.getSentDate(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); // The subfolder of the processed mail folder (e.g. 2016-12); String destinationFolder = formatter.format(sentDate); logger.debug("Message date: " + sentDate + "\tNumber: " + copiedMessage.getMessageNumber()); String[] subjectParts = messageSubject.split("\\s"); String loggerSerialNumber = "SerialNumber"; if (subjectParts.length > 1) { loggerSerialNumber = subjectParts[2]; } // Do we have a data attachment? If not, there's no data to // process if (copiedMessage.isMimeType("multipart/mixed")) { logger.debug("Message size: " + copiedMessage.getSize()); MimeMessageParser parser = new MimeMessageParser(copiedMessage); try { parser.parse(); } catch (Exception e) { logger.error("Failed to parse the MIME message: " + e.getMessage()); continue; } ByteBuffer messageAttachment = ByteBuffer.allocate(256); // init only logger.debug("Has attachments: " + parser.hasAttachments()); for (DataSource dataSource : parser.getAttachmentList()) { if (StringUtils.isNotBlank(dataSource.getName())) { logger.debug( "Attachment: " + dataSource.getName() + ", " + dataSource.getContentType()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(dataSource.getInputStream(), outputStream); messageAttachment = ByteBuffer.wrap(outputStream.toByteArray()); } } // We now have the attachment and serial number. Parse the attachment // for the data components, look up the storXSource based on the serial // number, and push the data to the DataTurbine // parse the binary attachment StorXParser storXParser = new StorXParser(messageAttachment); // iterate through the parsed framesMap and handle each // frame // based on its instrument type BasicHierarchicalMap framesMap = (BasicHierarchicalMap) storXParser.getFramesMap(); Collection frameCollection = framesMap.getAll("/frames/frame"); Iterator framesIterator = frameCollection.iterator(); while (framesIterator.hasNext()) { BasicHierarchicalMap frameMap = (BasicHierarchicalMap) framesIterator.next(); // logger.debug(frameMap.toXMLString(1000)); String frameType = (String) frameMap.get("type"); String sensorSerialNumber = (String) frameMap.get("serialNumber"); // handle each instrument type if (frameType.equals("HDR")) { logger.debug("This is a header frame. Skipping it."); } else if (frameType.equals("STX")) { try { // handle StorXSource StorXSource source = (StorXSource) sourceMap.get(sensorSerialNumber); // process the data using the StorXSource // driver messageProcessed = source.process(this.xmlConfiguration, frameMap); } catch (ClassCastException cce) { } } else if (frameType.equals("SBE")) { try { // handle CTDSource CTDSource source = (CTDSource) sourceMap.get(sensorSerialNumber); // process the data using the CTDSource // driver messageProcessed = source.process(this.xmlConfiguration, frameMap); } catch (ClassCastException cce) { } } else if (frameType.equals("NLB")) { try { // handle ISUSSource ISUSSource source = (ISUSSource) sourceMap.get(sensorSerialNumber); // process the data using the ISUSSource // driver messageProcessed = source.process(this.xmlConfiguration, frameMap); } catch (ClassCastException cce) { } } else if (frameType.equals("NDB")) { try { // handle ISUSSource ISUSSource source = (ISUSSource) sourceMap.get(sensorSerialNumber); // process the data using the ISUSSource // driver messageProcessed = source.process(this.xmlConfiguration, frameMap); } catch (ClassCastException cce) { } } else { logger.debug("The frame type " + frameType + " is not recognized. Skipping it."); } } // end while() if (this.sourceMap.get(loggerSerialNumber) != null) { // Note: Use message (not copiedMessage) when setting flags if (!messageProcessed) { logger.info("Failed to process message: " + "Message Number: " + message.getMessageNumber() + " " + "Logger Serial:" + loggerSerialNumber); // leave it in the inbox, flagged as seen (read) message.setFlag(Flags.Flag.SEEN, true); logger.debug("Saw message " + message.getMessageNumber()); } else { // message processed successfully. Create a by-month sub folder if it doesn't exist // Copy the message and flag it deleted Folder destination = processed.getFolder(destinationFolder); boolean created = destination.create(Folder.HOLDS_MESSAGES); inbox.copyMessages(new Message[] { message }, destination); message.setFlag(Flags.Flag.DELETED, true); logger.debug("Deleted message " + message.getMessageNumber()); } // end if() } else { logger.debug("There is no configuration information for " + "the logger serial number " + loggerSerialNumber + ". Please add the configuration to the " + "email.account.properties.xml configuration file."); } // end if() } else { logger.debug("This is not a data email since there is no " + "attachment. Skipping it. Subject: " + messageSubject); } // end if() } // end for() // expunge messages and close the mail server store once we're // done inbox.expunge(); this.mailStore.close(); } catch (MessagingException me) { try { this.mailStore.close(); } catch (MessagingException me2) { failed = true; return !failed; } logger.info( "There was an error reading the mail message. The " + "message was: " + me.getMessage()); me.printStackTrace(); failed = true; return !failed; } catch (IOException me) { try { this.mailStore.close(); } catch (MessagingException me3) { failed = true; return !failed; } logger.info("There was an I/O error reading the message part. The " + "message was: " + me.getMessage()); me.printStackTrace(); failed = true; return !failed; } catch (IllegalStateException ese) { try { this.mailStore.close(); } catch (MessagingException me4) { failed = true; return !failed; } logger.info("There was an error reading messages from the folder. The " + "message was: " + ese.getMessage()); failed = true; return !failed; } finally { try { this.mailStore.close(); } catch (MessagingException me2) { logger.debug("Couldn't close the mail store: " + me2.getMessage()); } } } return !failed; }
From source file:org.apache.juddi.v3.tck.UDDI_090_Smtp_ExternalTest.java
/** * gets all current messages from the mail server and returns the number * of messages containing the string, messages containing the string are * deleted from the mail server String is the body of each message * * @return number of matching and deleted messages * @param contains a string to search for *///from ww w. j av a2s.co m private static int fetchMail(String contains) { //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; /* Set the mail properties */ Properties properties = TckPublisher.getProperties(); // Set manual Properties int found = 0; Session session = Session.getDefaultInstance(properties, null); Store store = null; try { store = session.getStore("pop3"); store.connect(properties.getProperty("mail.pop3.host"), Integer.parseInt(properties.getProperty("mail.pop3.port", "110")), properties.getProperty("mail.pop3.username"), properties.getProperty("mail.pop3.password")); /* Mention the folder name which you want to read. */ // inbox = store.getDefaultFolder(); // inbox = inbox.getFolder("INBOX"); Folder inbox = store.getFolder("INBOX"); /* Open the inbox using store. */ inbox.open(Folder.READ_WRITE); Message messages[] = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { MimeMessageParser p = new MimeMessageParser(new MimeMessage(session, messages[i].getInputStream())); Enumeration allHeaders = p.getMimeMessage().getAllHeaders(); while (allHeaders.hasMoreElements()) { Object j = allHeaders.nextElement(); if (j instanceof javax.mail.Header) { javax.mail.Header msg = (javax.mail.Header) j; logger.info("XML as message header is " + msg.getValue()); if (msg.getValue().contains(contains)) { //found it messages[i].setFlag(Flags.Flag.DELETED, true); found++; } } } for (int k = 0; k < p.getAttachmentList().size(); k++) { InputStream is = p.getAttachmentList().get((k)).getInputStream(); QuotedPrintableCodec qp = new QuotedPrintableCodec(); // If "is" is not already buffered, wrap a BufferedInputStream // around it. if (!(is instanceof BufferedInputStream)) { is = new BufferedInputStream(is); } int c; StringBuilder sb = new StringBuilder(); System.out.println("Message : "); while ((c = is.read()) != -1) { sb.append(c); System.out.write(c); } is.close(); String decoded = qp.decode(sb.toString()); logger.info("decode message is " + decoded); if (decoded.contains(contains)) { //found it messages[i].setFlag(Flags.Flag.DELETED, true); found++; } } } inbox.close(true); } catch (Exception ex) { ex.printStackTrace(); } finally { if (store != null) { try { store.close(); } catch (Exception ex) { } } } return found; }
From source file:org.apache.nifi.processors.email.ExtractEmailAttachments.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final ComponentLog logger = getLogger(); final FlowFile originalFlowFile = session.get(); if (originalFlowFile == null) { return;/* w w w. ja va2s .c o m*/ } final List<FlowFile> attachmentsList = new ArrayList<>(); final List<FlowFile> invalidFlowFilesList = new ArrayList<>(); final List<FlowFile> originalFlowFilesList = new ArrayList<>(); session.read(originalFlowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { try (final InputStream in = new BufferedInputStream(rawIn)) { Properties props = new Properties(); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage originalMessage = new MimeMessage(mailSession, in); MimeMessageParser parser = new MimeMessageParser(originalMessage).parse(); // RFC-2822 determines that a message must have a "From:" header // if a message lacks the field, it is flagged as invalid Address[] from = originalMessage.getFrom(); Date sentDate = originalMessage.getSentDate(); if (from == null || sentDate == null) { // Throws MessageException due to lack of minimum required headers throw new MessagingException("Message failed RFC2822 validation"); } originalFlowFilesList.add(originalFlowFile); if (parser.hasAttachments()) { final String originalFlowFileName = originalFlowFile .getAttribute(CoreAttributes.FILENAME.key()); try { for (final DataSource data : parser.getAttachmentList()) { FlowFile split = session.create(originalFlowFile); final Map<String, String> attributes = new HashMap<>(); if (StringUtils.isNotBlank(data.getName())) { attributes.put(CoreAttributes.FILENAME.key(), data.getName()); } if (StringUtils.isNotBlank(data.getContentType())) { attributes.put(CoreAttributes.MIME_TYPE.key(), data.getContentType()); } String parentUuid = originalFlowFile.getAttribute(CoreAttributes.UUID.key()); attributes.put(ATTACHMENT_ORIGINAL_UUID, parentUuid); attributes.put(ATTACHMENT_ORIGINAL_FILENAME, originalFlowFileName); split = session.append(split, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { IOUtils.copy(data.getInputStream(), out); } }); split = session.putAllAttributes(split, attributes); attachmentsList.add(split); } } catch (FlowFileHandlingException e) { // Something went wrong // Removing splits that may have been created session.remove(attachmentsList); // Removing the original flow from its list originalFlowFilesList.remove(originalFlowFile); logger.error( "Flowfile {} triggered error {} while processing message removing generated FlowFiles from sessions", new Object[] { originalFlowFile, e }); invalidFlowFilesList.add(originalFlowFile); } } } catch (Exception e) { // Another error hit... // Removing the original flow from its list originalFlowFilesList.remove(originalFlowFile); logger.error("Could not parse the flowfile {} as an email, treating as failure", new Object[] { originalFlowFile, e }); // Message is invalid or triggered an error during parsing invalidFlowFilesList.add(originalFlowFile); } } }); session.transfer(attachmentsList, REL_ATTACHMENTS); // As per above code, originalFlowfile may be routed to invalid or // original depending on RFC2822 compliance. session.transfer(invalidFlowFilesList, REL_FAILURE); session.transfer(originalFlowFilesList, REL_ORIGINAL); if (attachmentsList.size() > 10) { logger.info("Split {} into {} files", new Object[] { originalFlowFile, attachmentsList.size() }); } else if (attachmentsList.size() > 1) { logger.info("Split {} into {} files: {}", new Object[] { originalFlowFile, attachmentsList.size(), attachmentsList }); } }
From source file:org.apache.nifi.processors.email.ExtractEmailHeaders.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final ComponentLog logger = getLogger(); final List<FlowFile> invalidFlowFilesList = new ArrayList<>(); final List<FlowFile> processedFlowFilesList = new ArrayList<>(); final FlowFile originalFlowFile = session.get(); if (originalFlowFile == null) { return;//ww w . j a va2 s . c om } final List<String> capturedHeadersList = Arrays .asList(context.getProperty(CAPTURED_HEADERS).getValue().toLowerCase().split(":")); final Map<String, String> attributes = new HashMap<>(); session.read(originalFlowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { try (final InputStream in = new BufferedInputStream(rawIn)) { Properties props = new Properties(); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage originalMessage = new MimeMessage(mailSession, in); MimeMessageParser parser = new MimeMessageParser(originalMessage).parse(); // RFC-2822 determines that a message must have a "From:" header // if a message lacks the field, it is flagged as invalid Address[] from = originalMessage.getFrom(); Date sentDate = originalMessage.getSentDate(); if (from == null || sentDate == null) { // Throws MessageException due to lack of minimum required headers throw new MessagingException("Message failed RFC2822 validation"); } else if (capturedHeadersList.size() > 0) { Enumeration headers = originalMessage.getAllHeaders(); while (headers.hasMoreElements()) { Header header = (Header) headers.nextElement(); if (StringUtils.isNotEmpty(header.getValue()) && capturedHeadersList.contains(header.getName().toLowerCase())) { attributes.put("email.headers." + header.getName().toLowerCase(), header.getValue()); } } } if (Array.getLength(originalMessage.getAllRecipients()) > 0) { for (int toCount = 0; toCount < ArrayUtils .getLength(originalMessage.getRecipients(Message.RecipientType.TO)); toCount++) { attributes.put(EMAIL_HEADER_TO + "." + toCount, originalMessage.getRecipients(Message.RecipientType.TO)[toCount].toString()); } for (int toCount = 0; toCount < ArrayUtils .getLength(originalMessage.getRecipients(Message.RecipientType.BCC)); toCount++) { attributes.put(EMAIL_HEADER_BCC + "." + toCount, originalMessage.getRecipients(Message.RecipientType.BCC)[toCount].toString()); } for (int toCount = 0; toCount < ArrayUtils .getLength(originalMessage.getRecipients(Message.RecipientType.CC)); toCount++) { attributes.put(EMAIL_HEADER_CC + "." + toCount, originalMessage.getRecipients(Message.RecipientType.CC)[toCount].toString()); } } // Incredibly enough RFC-2822 specified From as a "mailbox-list" so an array I returned by getFrom for (int toCount = 0; toCount < ArrayUtils.getLength(originalMessage.getFrom()); toCount++) { attributes.put(EMAIL_HEADER_FROM + "." + toCount, originalMessage.getFrom()[toCount].toString()); } if (StringUtils.isNotEmpty(originalMessage.getMessageID())) { attributes.put(EMAIL_HEADER_MESSAGE_ID, originalMessage.getMessageID()); } if (originalMessage.getReceivedDate() != null) { attributes.put(EMAIL_HEADER_RECV_DATE, originalMessage.getReceivedDate().toString()); } if (originalMessage.getSentDate() != null) { attributes.put(EMAIL_HEADER_SENT_DATE, originalMessage.getSentDate().toString()); } if (StringUtils.isNotEmpty(originalMessage.getSubject())) { attributes.put(EMAIL_HEADER_SUBJECT, originalMessage.getSubject()); } // Zeroes EMAIL_ATTACHMENT_COUNT attributes.put(EMAIL_ATTACHMENT_COUNT, "0"); // But insert correct value if attachments are present if (parser.hasAttachments()) { attributes.put(EMAIL_ATTACHMENT_COUNT, String.valueOf(parser.getAttachmentList().size())); } } catch (Exception e) { // Message is invalid or triggered an error during parsing attributes.clear(); logger.error("Could not parse the flowfile {} as an email, treating as failure", new Object[] { originalFlowFile, e }); invalidFlowFilesList.add(originalFlowFile); } } }); if (attributes.size() > 0) { FlowFile updatedFlowFile = session.putAllAttributes(originalFlowFile, attributes); logger.info("Extracted {} headers into {} file", new Object[] { attributes.size(), updatedFlowFile }); processedFlowFilesList.add(updatedFlowFile); } session.transfer(processedFlowFilesList, REL_SUCCESS); session.transfer(invalidFlowFilesList, REL_FAILURE); }
From source file:org.mangelp.fakeSmtpWeb.httpServer.mailBrowser.MailFile.java
/** * Parse the file on disk using a MimeMessageParser and set all the instance * properties we will be using./*from www .ja va 2 s.c o m*/ * * @throws FileNotFoundException * @throws MessagingException * @throws ParseException * @throws IOException */ protected void parseEmail() throws FileNotFoundException, MessagingException, ParseException, IOException { InputStream inputStream = new BufferedInputStream(new FileInputStream(this.getFile())); try { final Session session = Session.getDefaultInstance(new Properties()); MimeMessage message = new MimeMessage(session, inputStream); MimeMessageParser mimeParser = new MimeMessageParser(message); mimeParser.parse(); this.setSubject(mimeParser.getSubject()); this.setFrom(mimeParser.getFrom()); this.setReplyTo(mimeParser.getReplyTo()); ArrayList<String> toList = new ArrayList<String>(); for (Address emailAddress : mimeParser.getTo()) { toList.add(emailAddress.toString()); } this.setTo(toList.toArray(this.getTo())); ArrayList<String> ccList = new ArrayList<String>(); for (Address emailAddress : mimeParser.getCc()) { ccList.add(emailAddress.toString()); } this.setCc(ccList.toArray(this.getCc())); ArrayList<String> bccList = new ArrayList<String>(); for (Address emailAddress : mimeParser.getBcc()) { bccList.add(emailAddress.toString()); } this.setBcc(bccList.toArray(this.getBcc())); if (mimeParser.hasAttachments()) { attachments = new ArrayList<MailAttachment>(mimeParser.getAttachmentList().size()); int index = 0; for (DataSource ds : mimeParser.getAttachmentList()) { attachments.add(new MailAttachment(++index, ds)); } } if (mimeParser.hasHtmlContent()) { this.setHtmlContent(mimeParser.getHtmlContent()); } if (mimeParser.hasPlainContent()) { this.setPlainContent(mimeParser.getPlainContent()); } } catch (Exception e) { throw new ParseException("Failed to parse file " + this.getFile().toString() + ": " + e.getMessage()); } this.setId(DigestUtils.sha1Hex(inputStream)); inputStream.close(); }