This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit ceca8ea5b64a35faebd40324249b5e675d23812a Author: Matthieu Baechler <[email protected]> AuthorDate: Fri Apr 3 15:53:34 2020 +0200 [Refactoring] move methods/attributes out of SeparatingDataLineFilter to its only subclass --- .../smtp/core/AbstractAddHeadersFilter.java | 153 --------------------- .../smtp/core/ReceivedDataLineFilter.java | 110 ++++++++++++++- 2 files changed, 105 insertions(+), 158 deletions(-) diff --git a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/AbstractAddHeadersFilter.java b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/AbstractAddHeadersFilter.java deleted file mode 100644 index 8f0e8e3..0000000 --- a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/AbstractAddHeadersFilter.java +++ /dev/null @@ -1,153 +0,0 @@ -/**************************************************************** - * 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.protocols.smtp.core; - -import java.io.UnsupportedEncodingException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.james.protocols.api.ProtocolSession; -import org.apache.james.protocols.api.ProtocolSession.State; -import org.apache.james.protocols.api.Response; -import org.apache.james.protocols.api.handler.LineHandler; -import org.apache.james.protocols.smtp.SMTPSession; - -/** - * Abstract base class for {@link SeparatingDataLineFilter} implementations that add headers to a message - * - * - */ -public abstract class AbstractAddHeadersFilter extends SeparatingDataLineFilter { - - private static final AtomicInteger COUNTER = new AtomicInteger(0); - - private final ProtocolSession.AttachmentKey<Boolean> headersPrefixAdded = ProtocolSession.AttachmentKey.of("HEADERS_PREFIX_ADDED" + COUNTER.incrementAndGet(), Boolean.class); - private final ProtocolSession.AttachmentKey<Boolean> headersSuffixAdded = ProtocolSession.AttachmentKey.of("HEADERS_SUFFIX_ADDED" + COUNTER.incrementAndGet(), Boolean.class); - - enum Location { - Prefix, - Suffix - } - - /** - * Return the {@link Location} to add the headers in - * - * @return location - */ - protected abstract Location getLocation(); - - - @Override - protected Response onSeparatorLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { - if (getLocation() == Location.Suffix && !session.getAttachment(headersSuffixAdded, State.Transaction).isPresent()) { - session.setAttachment(headersSuffixAdded, Boolean.TRUE, State.Transaction); - return addHeaders(session, line, next); - } - return super.onSeparatorLine(session, line, next); - } - - @Override - protected Response onHeadersLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { - if (getLocation() == Location.Prefix && !session.getAttachment(headersPrefixAdded, State.Transaction).isPresent()) { - session.setAttachment(headersPrefixAdded, Boolean.TRUE, State.Transaction); - return addHeaders(session, line, next); - } - return super.onHeadersLine(session, line, next); - } - - /** - * Add headers to the message - * - * @return response - */ - private Response addHeaders(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { - Response response; - for (Header header: headers(session)) { - response = header.transferTo(session, next); - if (response != null) { - return response; - } - } - return next.onLine(session, line); - } - - /** - * Return the {@link Header}'s to operate on - * - * @return headers - */ - protected abstract Collection<Header> headers(SMTPSession session); - - public static final class Header { - public static final String MULTI_LINE_PREFIX = " "; - - public final String name; - public final List<String> values = new ArrayList<>(); - - public Header(String name, String value) { - this.name = name; - this.values.add(value); - } - - /** - * Add the value to the header - */ - public Header add(String value) { - values.add(value); - return this; - } - - - /** - * Transfer the content of the {@link Header} to the given {@link LineHandler}. - * - * This is done for each line of the {@link Header} until the end is reached or the {@link LineHandler#onLine(org.apache.james.protocols.api.ProtocolSession, ByteBuffer)} - * return <code>non-null</code> - * - * @return response - */ - public Response transferTo(SMTPSession session, LineHandler<SMTPSession> handler) { - String charset = session.getCharset().name(); - - try { - Response response = null; - for (int i = 0; i < values.size(); i++) { - String line; - if (i == 0) { - line = name + ": " + values.get(i); - } else { - line = MULTI_LINE_PREFIX + values.get(i); - } - response = handler.onLine(session, ByteBuffer.wrap((line + session.getLineDelimiter()).getBytes(charset))); - if (response != null) { - break; - } - } - return response; - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("NO " + charset + " support ?", e); - } - } - } - - -} diff --git a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/ReceivedDataLineFilter.java b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/ReceivedDataLineFilter.java index 3c24277..e08369c 100644 --- a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/ReceivedDataLineFilter.java +++ b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/ReceivedDataLineFilter.java @@ -18,25 +18,33 @@ ****************************************************************/ package org.apache.james.protocols.smtp.core; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.james.core.MailAddress; +import org.apache.james.protocols.api.ProtocolSession; import org.apache.james.protocols.api.ProtocolSession.State; +import org.apache.james.protocols.api.Response; +import org.apache.james.protocols.api.handler.LineHandler; import org.apache.james.protocols.smtp.SMTPSession; import com.google.common.collect.ImmutableList; /** - * {@link AbstractAddHeadersFilter} which adds the Received header for the message. + * {@link SeparatingDataLineFilter} which adds the Received header for the message. */ -public class ReceivedDataLineFilter extends AbstractAddHeadersFilter { +public class ReceivedDataLineFilter extends SeparatingDataLineFilter { + private static final String EHLO = "EHLO"; private static final String SMTP = "SMTP"; private static final String ESMTPA = "ESMTPA"; @@ -47,6 +55,10 @@ public class ReceivedDataLineFilter extends AbstractAddHeadersFilter { return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z (zzz)", Locale.US); }); + private static final AtomicInteger COUNTER = new AtomicInteger(0); + private final ProtocolSession.AttachmentKey<Boolean> headersPrefixAdded = ProtocolSession.AttachmentKey.of("HEADERS_PREFIX_ADDED" + COUNTER.incrementAndGet(), Boolean.class); + private final ProtocolSession.AttachmentKey<Boolean> headersSuffixAdded = ProtocolSession.AttachmentKey.of("HEADERS_SUFFIX_ADDED" + COUNTER.incrementAndGet(), Boolean.class); + /** * Return the service type which will be used in the Received headers. */ @@ -74,7 +86,6 @@ public class ReceivedDataLineFilter extends AbstractAddHeadersFilter { /** * The Received header is added in front of the received headers. So returns {@link Location#Suffix} */ - @Override protected Location getLocation() { return Location.Prefix; } @@ -83,7 +94,6 @@ public class ReceivedDataLineFilter extends AbstractAddHeadersFilter { * Returns the Received header for the message. */ @SuppressWarnings("unchecked") - @Override protected Collection<Header> headers(SMTPSession session) { StringBuilder headerLineBuffer = new StringBuilder(); @@ -127,5 +137,95 @@ public class ReceivedDataLineFilter extends AbstractAddHeadersFilter { return Arrays.asList(header); } - + + @Override + protected Response onSeparatorLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { + if (getLocation() == Location.Suffix && !session.getAttachment(headersSuffixAdded, State.Transaction).isPresent()) { + session.setAttachment(headersSuffixAdded, Boolean.TRUE, State.Transaction); + return addHeaders(session, line, next); + } + return super.onSeparatorLine(session, line, next); + } + + @Override + protected Response onHeadersLine(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { + if (getLocation() == Location.Prefix && !session.getAttachment(headersPrefixAdded, State.Transaction).isPresent()) { + session.setAttachment(headersPrefixAdded, Boolean.TRUE, State.Transaction); + return addHeaders(session, line, next); + } + return super.onHeadersLine(session, line, next); + } + + /** + * Add headers to the message + * + * @return response + */ + private Response addHeaders(SMTPSession session, ByteBuffer line, LineHandler<SMTPSession> next) { + Response response; + for (Header header: headers(session)) { + response = header.transferTo(session, next); + if (response != null) { + return response; + } + } + return next.onLine(session, line); + } + + enum Location { + Prefix, + Suffix + } + + public static final class Header { + public static final String MULTI_LINE_PREFIX = " "; + + public final String name; + public final List<String> values = new ArrayList<>(); + + public Header(String name, String value) { + this.name = name; + this.values.add(value); + } + + /** + * Add the value to the header + */ + public Header add(String value) { + values.add(value); + return this; + } + + + /** + * Transfer the content of the {@link Header} to the given {@link LineHandler}. + * + * This is done for each line of the {@link Header} until the end is reached or the {@link LineHandler#onLine(org.apache.james.protocols.api.ProtocolSession, ByteBuffer)} + * return <code>non-null</code> + * + * @return response + */ + public Response transferTo(SMTPSession session, LineHandler<SMTPSession> handler) { + String charset = session.getCharset().name(); + + try { + Response response = null; + for (int i = 0; i < values.size(); i++) { + String line; + if (i == 0) { + line = name + ": " + values.get(i); + } else { + line = MULTI_LINE_PREFIX + values.get(i); + } + response = handler.onLine(session, ByteBuffer.wrap((line + session.getLineDelimiter()).getBytes(charset))); + if (response != null) { + break; + } + } + return response; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("NO " + charset + " support ?", e); + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
