📄 messageprocessor.java
字号:
{ // 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"); setDefaultRemoteAddress(true); logStatusInfo("Remote address could not be determined. Using localhost/127.0.0.1"); } else { mail.setRemoteAddr(getRemoteAddress()); mail.setRemoteHost(getRemoteHostName()); setDefaultRemoteAddress(false); } 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; } /** * <p> * Method getSender answers a <code>MailAddress</code> for the sender. * When the sender local part and/or domain part can not be obtained * from the mail, default values are used. The flags * 'defaultSenderLocalPart' and 'defaultSenderDomainPart' are set * accordingly. * </p> * * @return MailAddress * @throws MessagingException */ protected MailAddress getSender() throws MessagingException { String from = null; InternetAddress internetAddress = null; try { from = ((InternetAddress) getMessageIn().getFrom()[0]).getAddress().trim(); setDefaultSenderLocalPart(false); } catch (Exception _) { from = getDefaultLocalPart(); setDefaultSenderLocalPart(true); StringBuffer buffer = new StringBuffer(32); buffer.append("Sender localpart is absent. Using default value ("); buffer.append(getDefaultLocalPart()); buffer.append(')'); logStatusInfo(buffer.toString()); } // 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()); setDefaultSenderDomainPart(true); StringBuffer buffer = new StringBuffer(32); buffer.append("Sender domain is absent. Using default value ("); buffer.append(getDefaultDomainName()); buffer.append(')'); logStatusInfo(buffer.toString()); } else { internetAddress = new InternetAddress(from); setDefaultSenderDomainPart(false); } 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 for the remote domain that sent the message.</p> * * <p>Often the remote domain is the domain that sent the message to the * host of the message store, the second "received" header, which has an * index of 1. Other times, messages may be received by a edge mail server * and relayed internally through one or more internal mail servers prior * to arriving at the message store host. In these cases the index is * 1 + the number of internal servers through which a mail passes. * </p> * <p>The index of the header to use is specified by the configuration * parameter <code>RemoteReceivedHeaderIndex</code>. This is set to * point to the received header prior to the remote mail server, the one * prior to the edge mail server. * </p> * <p>"received" headers are searched starting at the specified index. * If a domain in the "received" header is not found, successively closer * "received" headers are tried. If a domain is not found in this way, the * local machine is used as the domain. Finally, if the local domain cannot * be determined, the local address 127.0.0.1 is used. * </p> * * @return String An RFC2822 compliant "Received : from" domain name */ protected String computeRemoteDomain() throws MessagingException { StringBuffer domainBuffer = new StringBuffer(); String[] headers = null; if (getRemoteReceivedHeaderIndex() > -1) headers = getMessageIn().getHeader(RFC2822Headers.RECEIVED); // There are RECEIVED headers if the array is not null // and its length at is greater than 0 boolean hasHeaders = (null == headers ? false : headers.length > 0); // If there are RECEIVED headers try and extract the domain if (hasHeaders) { 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(' '); } } } } // If a domain was not found, the default is the local host and // if we cannot resolve this, the local address 127.0.0.1 // Note that earlier versions of this code simply used 'localhost' // which works fine with java.net but is not resolved by dnsjava // which was introduced in v2.2.0. See Jira issue JAMES-302. if (domainBuffer.length() == 0) { try { InetAddress addr1 = java.net.InetAddress.getLocalHost(); // These shenanigans are required to get the fully qualified // hostname prior to JDK 1.4 in which getCanonicalHostName() // does the job for us InetAddress addr2 = java.net.InetAddress.getByName(addr1.getHostAddress()); InetAddress addr3 = java.net.InetAddress.getByName(addr2.getHostName()); domainBuffer.append(addr3.getHostName()); } catch (UnknownHostException ue) { domainBuffer.append("[127.0.0.1]"); } } 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());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -