📄 messageprocessor.java
字号:
messageBuffer.append(recipient); messageBuffer.append('.'); logStatusInfo(messageBuffer.toString()); return; } /** * Method rejectBlacklistedRecipient. * @param recipient * @throws MessagingException */ protected void rejectBlacklistedRecipient(MailAddress recipient) throws MessagingException { // Update the flags of the received message if (!isLeaveBlacklisted()) setMessageDeleted(); if (isMarkBlacklistedSeen()) setMessageSeen(); StringBuffer messageBuffer = new StringBuffer("Rejected mail intended for blacklisted recipient: "); messageBuffer.append(recipient); messageBuffer.append('.'); logStatusInfo(messageBuffer.toString()); return; } /** * Method rejectRecipientNotFound. * @throws MessagingException */ protected void rejectRecipientNotFound() throws MessagingException { // Update the flags of the received message if (!isLeaveRecipientNotFound()) setMessageDeleted(); if (isMarkRecipientNotFoundSeen()) setMessageSeen(); StringBuffer messageBuffer = new StringBuffer("Rejected mail for which a sole intended recipient could not be found."); messageBuffer.append(" Recipients: "); Address[] allRecipients = getMessageIn().getAllRecipients(); for (int i = 0; i < allRecipients.length; i++) { messageBuffer.append(allRecipients[i]); messageBuffer.append(' '); } messageBuffer.append('.'); logStatusInfo(messageBuffer.toString()); return; } /** * Method rejectUserUndefined. * @param recipient * @throws MessagingException */ protected void rejectUserUndefined(MailAddress recipient) throws MessagingException { // Update the flags of the received message if (!isLeaveUserUndefined()) setMessageDeleted(); if (isMarkUserUndefinedSeen()) setMessageSeen(); StringBuffer messageBuffer = new StringBuffer("Rejected mail intended for undefined user: "); messageBuffer.append(recipient); messageBuffer.append('.'); logStatusInfo(messageBuffer.toString()); return; } /** * Method rejectMaxMessageSizeExceeded. * @param message size * @throws MessagingException */ protected void rejectMaxMessageSizeExceeded(int messageSize) throws MessagingException { // Update the flags of the received message if (!isLeaveMaxMessageSizeExceeded()) setMessageDeleted(); if (isMarkMaxMessageSizeExceededSeen()) setMessageSeen(); StringBuffer messageBuffer = new StringBuffer("Rejected mail exceeding message size limit. Message size: "); messageBuffer.append(messageSize/1024); messageBuffer.append("KB."); logStatusInfo(messageBuffer.toString()); return; } /** * Method rejectRemoteReceivedHeaderInvalid. * @throws MessagingException */ protected void rejectRemoteReceivedHeaderInvalid() throws MessagingException { // Update the flags of the received message if (!isLeaveRemoteReceivedHeaderInvalid()) setMessageDeleted(); if (isMarkRemoteReceivedHeaderInvalidSeen()) setMessageSeen(); StringBuffer messageBuffer = new StringBuffer("Rejected mail with an invalid Received: header at index "); messageBuffer.append(getRemoteReceivedHeaderIndex()); messageBuffer.append("."); logStatusInfo(messageBuffer.toString()); return; } /** * <p>Method createMessage answers a new <code>MimeMessage</code> from the * fetched message.</p> * * <p>If the maximum message size is exceeded, an empty message is created, * else the new message is a copy of the received message.</p> * * @return MimeMessage * @throws MessagingException */ protected MimeMessage createMessage() throws MessagingException { // Create a new messsage from the received message MimeMessage messageOut = null; if (isMaxMessageSizeExceeded().booleanValue()) messageOut = createEmptyMessage(); else messageOut = new MimeMessage(getMessageIn()); // set the X-fetched headers // Note this is still required to detect bouncing mail and // for backwards compatibility with fetchPop messageOut.addHeader("X-fetched-from", getFetchTaskName()); return messageOut; } /** * Method createEmptyMessage answers a new * <code>MimeMessage</code> from the fetched message with the message * contents removed. * * @return MimeMessage * @throws MessagingException */ protected MimeMessage createEmptyMessage() throws MessagingException { // Create an empty messsage MimeMessage messageOut = new MimeMessage(getSession()); // Propogate the headers and subject Enumeration headersInEnum = getMessageIn().getAllHeaderLines(); while (headersInEnum.hasMoreElements()) messageOut.addHeaderLine((String) headersInEnum.nextElement()); messageOut.setSubject(getMessageIn().getSubject()); // Add empty text messageOut.setText(""); // Save messageOut.saveChanges(); return messageOut; } /** * Method createMail creates a new <code>Mail</code>. * * @param message * @param recipient * @return Mail * @throws MessagingException */ protected Mail createMail(MimeMessage message, MailAddress recipient) throws MessagingException, UnknownHostException { Collection recipients = new ArrayList(1); recipients.add(recipient); MailImpl mail = new MailImpl(getServer().getId(), getSender(), recipients, message); // Ensure the mail is created with non-null remote host name and address, // otherwise the Mailet chain may go splat! if (getRemoteAddress() == null || getRemoteHostName() == null) { mail.setRemoteAddr("127.0.0.1"); mail.setRemoteHost("localhost"); } else { mail.setRemoteAddr(getRemoteAddress()); mail.setRemoteHost(getRemoteHostName()); } if (getLogger().isDebugEnabled()) { StringBuffer messageBuffer = new StringBuffer("Created mail with name: "); messageBuffer.append(mail.getName()); messageBuffer.append(", sender: "); messageBuffer.append(mail.getSender()); messageBuffer.append(", recipients: "); Iterator recipientIterator = mail.getRecipients().iterator(); while (recipientIterator.hasNext()) { messageBuffer.append(recipientIterator.next()); messageBuffer.append(' '); } messageBuffer.append(", remote address: "); messageBuffer.append(mail.getRemoteAddr()); messageBuffer.append(", remote host name: "); messageBuffer.append(mail.getRemoteHost()); messageBuffer.append('.'); getLogger().debug(messageBuffer.toString()); } return mail; } /** * Method getSender answers a <code>MailAddress</code> for the sender. * * @return MailAddress * @throws MessagingException */ protected MailAddress getSender() throws MessagingException { String from = "FETCHMAIL-SERVICE"; try { from = ((InternetAddress) getMessageIn().getFrom()[0]).getAddress().trim(); } catch (Exception _) { getLogger().info("Could not identify sender -- using default value"); } InternetAddress internetAddress = null; // Check for domain part, add default if missing if (from.indexOf('@') < 0) { StringBuffer fromBuffer = new StringBuffer(from); fromBuffer.append('@'); fromBuffer.append(getDefaultDomainName()); internetAddress = new InternetAddress(fromBuffer.toString()); } else internetAddress = new InternetAddress(from); return new MailAddress(internetAddress); } /** * <p>Method computeRemoteDomain answers a <code>String</code> that is the * RFC2822 compliant "Received : from" domain extracted from the message * being processed.</p> * * <p>Normally this is the domain that sent the message to the host for the * message store as reported by the second "received" header. The index of * the header to use is specified by the configuration parameter * <code>RemoteReceivedHeaderIndex</code>. If a header at this index does * not exist, the domain of the successively closer "received" headers * is tried until they are exhausted, then "localhost" is used.</p> * * @return String */ protected String computeRemoteDomain() throws MessagingException { StringBuffer domainBuffer = new StringBuffer(); String[] headers = null; if (getRemoteReceivedHeaderIndex() > -1) getMessageIn().getHeader(RFC2822Headers.RECEIVED); if (null != headers) { // If there are RECEIVED headers and the index to begin at is greater // than -1, try and extract the domain if (headers.length > 0) { final String headerTokens = " \n\r"; // Search the headers for a domain for (int headerIndex = headers.length > getRemoteReceivedHeaderIndex() ? getRemoteReceivedHeaderIndex() : headers.length - 1; headerIndex >= 0 && domainBuffer.length() == 0; headerIndex--) { // Find the "from" token StringTokenizer tokenizer = new StringTokenizer(headers[headerIndex], headerTokens); boolean inFrom = false; while (!inFrom && tokenizer.hasMoreTokens()) inFrom = tokenizer.nextToken().equals("from"); // Add subsequent tokens to the domain buffer until another // field is encountered or there are no more tokens while (inFrom && tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (inFrom = getRFC2822RECEIVEDHeaderFields().indexOf(token) == -1) { domainBuffer.append(token); domainBuffer.append(' '); } } } } } // Default is "localhost" if (domainBuffer.length() == 0) domainBuffer.append("localhost"); return domainBuffer.toString().trim(); } /** * Method handleBouncing sets the Mail state to ERROR and delete from * the message store. * * @param mail */ protected void handleBouncing(Mail mail) throws MessagingException { mail.setState(Mail.ERROR); setMessageDeleted(); mail.setErrorMessage( "This mail from FetchMail task " + getFetchTaskName() + " seems to be bouncing!"); logStatusError("Message is bouncing! Deleted from message store and moved to the Error repository."); } /** * Method handleParseException. * @param ex * @throws MessagingException */ protected void handleParseException(ParseException ex) throws MessagingException { // Update the flags of the received message if (!isLeaveUndeliverable()) setMessageDeleted(); if (isMarkUndeliverableSeen()) setMessageSeen(); logStatusWarn("Message could not be delivered due to an error parsing a mail address."); if (getLogger().isDebugEnabled()) { StringBuffer messageBuffer = new StringBuffer("UNDELIVERABLE Message ID: "); messageBuffer.append(getMessageIn().getMessageID()); getLogger().debug(messageBuffer.toString(), ex); } } /** * Method handleUnknownHostException. * @param ex * @throws MessagingException */ protected void handleUnknownHostException(UnknownHostException ex) throws MessagingException { // Update the flags of the received message if (!isLeaveUndeliverable()) setMessageDeleted(); if (isMarkUndeliverableSeen()) setMessageSeen(); logStatusWarn("Message could not be delivered due to an error determining the remote domain."); if (getLogger().isDebugEnabled()) { StringBuffer messageBuffer = new StringBuffer("UNDELIVERABLE Message ID: "); messageBuffer.append(getMessageIn().getMessageID()); getLogger().debug(messageBuffer.toString(), ex); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -