JAMES-1877 Extract 7 bit conversion
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/a62e460e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/a62e460e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/a62e460e Branch: refs/heads/master Commit: a62e460e668c72bd2a3e71624a8446c9e456a1aa Parents: 3b02d3f Author: Benoit Tellier <btell...@linagora.com> Authored: Thu Dec 1 15:50:50 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 10 15:12:49 2017 +0700 ---------------------------------------------------------------------- .../mailets/remoteDelivery/Converter7Bit.java | 65 ++++++++++++++++++++ .../remoteDelivery/DeliveryRunnable.java | 39 ++---------- 2 files changed, 69 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/a62e460e/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Converter7Bit.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Converter7Bit.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Converter7Bit.java new file mode 100644 index 0000000..ab798e6 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Converter7Bit.java @@ -0,0 +1,65 @@ +/**************************************************************** + * 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.IOException; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimePart; + +import org.apache.mailet.MailetContext; + +public class Converter7Bit { + + private final MailetContext mailetContext; + + public Converter7Bit(MailetContext mailetContext) { + this.mailetContext = mailetContext; + } + + public MimePart convertTo7Bit(MimePart part) throws MessagingException, IOException { + if (part.isMimeType("multipart/*")) { + MimeMultipart parts = (MimeMultipart) part.getContent(); + int count = parts.getCount(); + for (int i = 0; i < count; i++) { + convertTo7Bit((MimePart) parts.getBodyPart(i)); + } + } else if ("8bit".equals(part.getEncoding())) { + // The content may already be in encoded the form (likely with mail + // created from a + // stream). In that case, just changing the encoding to + // quoted-printable will mangle + // the result when this is transmitted. We must first convert the + // content into its + // native format, set it back, and only THEN set the transfer + // encoding to force the + // content to be encoded appropriately. + + // if the part doesn't contain text it will be base64 encoded. + String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" : "base64"; + part.setContent(part.getContent(), part.getContentType()); + part.setHeader("Content-Transfer-Encoding", contentTransferEncoding); + part.addHeader("X-MIME-Autoconverted", "from 8bit to " + contentTransferEncoding + " by " + mailetContext.getServerInfo()); + } + return part; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a62e460e/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java index 9c31696..d9db12c 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java @@ -36,8 +36,6 @@ import javax.mail.SendFailedException; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; -import javax.mail.internet.MimePart; import javax.mail.internet.ParseException; import org.apache.james.dnsservice.api.DNSService; @@ -69,6 +67,7 @@ public class DeliveryRunnable implements Runnable { private final MailetContext mailetContext; private final VolatileIsDestroyed volatileIsDestroyed; private final MessageComposer messageComposer; + private final Converter7Bit converter7Bit; public DeliveryRunnable(MailQueue queue, RemoteDeliveryConfiguration configuration, DNSService dnsServer, Metric outgoingMailsMetric, Logger logger, MailetContext mailetContext, VolatileIsDestroyed volatileIsDestroyed) { this.queue = queue; @@ -79,6 +78,7 @@ public class DeliveryRunnable implements Runnable { this.mailetContext = mailetContext; this.volatileIsDestroyed = volatileIsDestroyed; this.messageComposer = new MessageComposer(configuration); + this.converter7Bit = new Converter7Bit(mailetContext); } /** @@ -486,7 +486,7 @@ public class DeliveryRunnable implements Runnable { // Temporarily disabled. See JAMES-638 if (!transport.supportsExtension(BIT_MIME_8)) { try { - convertTo7Bit(message); + converter7Bit.convertTo7Bit(message); } catch (IOException e) { // An error has occured during the 7bit conversion. // The error is logged and the message is sent anyway. @@ -498,7 +498,7 @@ public class DeliveryRunnable implements Runnable { // If the transport is not the one developed by Sun we are not sure of how it // handles the 8 bit mime stuff, so I convert the message to 7bit. try { - convertTo7Bit(message); + converter7Bit.convertTo7Bit(message); } catch (IOException e) { logger.error("Error during the conversion to 7 bit.", e); } @@ -647,37 +647,6 @@ public class DeliveryRunnable implements Runnable { } /** - * Converts a message to 7 bit. - * - * @param part - */ - private void convertTo7Bit(MimePart part) throws MessagingException, IOException { - if (part.isMimeType("multipart/*")) { - MimeMultipart parts = (MimeMultipart) part.getContent(); - int count = parts.getCount(); - for (int i = 0; i < count; i++) { - convertTo7Bit((MimePart) parts.getBodyPart(i)); - } - } else if ("8bit".equals(part.getEncoding())) { - // The content may already be in encoded the form (likely with mail - // created from a - // stream). In that case, just changing the encoding to - // quoted-printable will mangle - // the result when this is transmitted. We must first convert the - // content into its - // native format, set it back, and only THEN set the transfer - // encoding to force the - // content to be encoded appropriately. - - // if the part doesn't contain text it will be base64 encoded. - String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" : "base64"; - part.setContent(part.getContent(), part.getContentType()); - part.setHeader("Content-Transfer-Encoding", contentTransferEncoding); - part.addHeader("X-MIME-Autoconverted", "from 8bit to " + contentTransferEncoding + " by " + mailetContext.getServerInfo()); - } - } - - /** * Insert the method's description here. * * @param mail org.apache.james.core.MailImpl --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org