Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1877 Extract a utility for sending bounce
- Loading branch information
Showing
5 changed files
with
641 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ilet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Bouncer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.transport.mailets.remoteDelivery; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.net.ConnectException; | ||
import java.net.SocketException; | ||
import java.net.UnknownHostException; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.SendFailedException; | ||
|
||
import org.apache.mailet.Mail; | ||
import org.apache.mailet.MailAddress; | ||
import org.apache.mailet.MailetContext; | ||
import org.slf4j.Logger; | ||
|
||
public class Bouncer { | ||
|
||
public static final String DELIVERY_ERROR = "delivery-error"; | ||
private final RemoteDeliveryConfiguration configuration; | ||
private final MessageComposer messageComposer; | ||
private final MailetContext mailetContext; | ||
private final Logger logger; | ||
|
||
public Bouncer(RemoteDeliveryConfiguration configuration, MessageComposer messageComposer, MailetContext mailetContext, Logger logger) { | ||
this.configuration = configuration; | ||
this.messageComposer = messageComposer; | ||
this.mailetContext = mailetContext; | ||
this.logger = logger; | ||
} | ||
|
||
public void bounce(Mail mail, Exception ex) { | ||
if (mail.getSender() == null) { | ||
logger.debug("Null Sender: no bounce will be generated for " + mail.getName()); | ||
} else { | ||
if (configuration.getBounceProcessor() != null) { | ||
mail.setAttribute(DELIVERY_ERROR, messageComposer.getErrorMsg(ex)); | ||
mail.setState(configuration.getBounceProcessor()); | ||
try { | ||
mailetContext.sendMail(mail); | ||
} catch (MessagingException e) { | ||
logger.debug("Exception re-inserting failed mail: ", e); | ||
} | ||
} else { | ||
bounceWithMailetContext(mail, ex); | ||
} | ||
} | ||
} | ||
|
||
|
||
private void bounceWithMailetContext(Mail mail, Exception ex) { | ||
logger.debug("Sending failure message " + mail.getName()); | ||
try { | ||
mailetContext.bounce(mail, explanationText(mail, ex)); | ||
} catch (MessagingException me) { | ||
logger.debug("Encountered unexpected messaging exception while bouncing message: " + me.getMessage()); | ||
} catch (Exception e) { | ||
logger.debug("Encountered unexpected exception while bouncing message: " + e.getMessage()); | ||
} | ||
} | ||
|
||
public String explanationText(Mail mail, Exception ex) { | ||
StringWriter sout = new StringWriter(); | ||
PrintWriter out = new PrintWriter(sout, true); | ||
String machine; | ||
try { | ||
machine = configuration.getHeloNameProvider().getHeloName(); | ||
|
||
} catch (Exception e) { | ||
machine = "[address unknown]"; | ||
} | ||
String bounceBuffer = "Hi. This is the James mail server at " + machine + "."; | ||
out.println(bounceBuffer); | ||
out.println("I'm afraid I wasn't able to deliver your message to the following addresses."); | ||
out.println("This is a permanent error; I've given up. Sorry it didn't work out. Below"); | ||
out.println("I include the list of recipients and the reason why I was unable to deliver"); | ||
out.println("your message."); | ||
out.println(); | ||
for (MailAddress mailAddress : mail.getRecipients()) { | ||
out.println(mailAddress); | ||
} | ||
if (ex instanceof MessagingException) { | ||
if (((MessagingException) ex).getNextException() == null) { | ||
out.println(ex.getMessage().trim()); | ||
} else { | ||
Exception ex1 = ((MessagingException) ex).getNextException(); | ||
if (ex1 instanceof SendFailedException) { | ||
out.println("Remote mail server told me: " + ex1.getMessage().trim()); | ||
} else if (ex1 instanceof UnknownHostException) { | ||
out.println("Unknown host: " + ex1.getMessage().trim()); | ||
out.println("This could be a DNS server error, a typo, or a problem with the recipient's mail server."); | ||
} else if (ex1 instanceof ConnectException) { | ||
// Already formatted as "Connection timed out: connect" | ||
out.println(ex1.getMessage().trim()); | ||
} else if (ex1 instanceof SocketException) { | ||
out.println("Socket exception: " + ex1.getMessage().trim()); | ||
} else { | ||
out.println(ex1.getMessage().trim()); | ||
} | ||
} | ||
} | ||
out.println(); | ||
return sout.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.