You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/07/17 02:24:32 UTC

[james-project] 18/31: [REFACTORING] Remove uneeded else blocks in IMAP FETCH code

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 a78237ba2542c02ee4c2108f0e2a288b660da1ed
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Jul 16 15:50:05 2020 +0700

    [REFACTORING] Remove uneeded else blocks in IMAP FETCH code
---
 .../imap/processor/fetch/EnvelopeBuilder.java      | 91 ++++++++++------------
 .../imap/processor/fetch/FetchResponseBuilder.java | 26 +++----
 2 files changed, 50 insertions(+), 67 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/EnvelopeBuilder.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/EnvelopeBuilder.java
index 7513443..1b2595c 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/EnvelopeBuilder.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/EnvelopeBuilder.java
@@ -65,33 +65,28 @@ public final class EnvelopeBuilder {
         final Header header = MessageResultUtils.getMatching(headerName, message.headers());
         if (header == null) {
             return null;
-        } else {
-            final String value = header.getValue();
-            if (value == null || "".equals(value)) {
-                return null;
-            } else {
-
-                // ENVELOPE header values must be unfolded
-                // See IMAP-269
-                //
-                //
-                // IMAP-Servers are advised to also replace tabs with single spaces while doing the unfolding. This is what javamails
-                // unfold does. mime4j's unfold does strictly follow the rfc and so preserve them
-                //
-                // See IMAP-327 and https://mailman2.u.washington.edu/mailman/htdig/imap-protocol/2010-July/001271.html
-                return MimeUtility.unfold(value);
-
-            }
         }
+        final String value = header.getValue();
+        if (value == null || "".equals(value)) {
+            return null;
+        }
+        // ENVELOPE header values must be unfolded
+        // See IMAP-269
+        //
+        //
+        // IMAP-Servers are advised to also replace tabs with single spaces while doing the unfolding. This is what javamails
+        // unfold does. mime4j's unfold does strictly follow the rfc and so preserve them
+        //
+        // See IMAP-327 and https://mailman2.u.washington.edu/mailman/htdig/imap-protocol/2010-July/001271.html
+        return MimeUtility.unfold(value);
     }
 
     private FetchResponse.Envelope.Address[] buildAddresses(Headers message, String headerName, FetchResponse.Envelope.Address[] defaults) throws MailboxException {
         final FetchResponse.Envelope.Address[] addresses = buildAddresses(message, headerName);
         if (addresses == null) {
             return defaults;
-        } else {
-            return addresses;
         }
+        return addresses;
     }
 
     /**
@@ -104,41 +99,37 @@ public final class EnvelopeBuilder {
         final Header header = MessageResultUtils.getMatching(headerName, message.headers());
         if (header == null) {
             return null;
-        } else {
+        }
+        // We need to unfold the header line.
+        // See https://issues.apache.org/jira/browse/IMAP-154
+        //
+        // IMAP-Servers are advised to also replace tabs with single spaces while doing the unfolding. This is what javamails
+        // unfold does. mime4j's unfold does strictly follow the rfc and so preserve them
+        //
+        // See IMAP-327 and https://mailman2.u.washington.edu/mailman/htdig/imap-protocol/2010-July/001271.html
+        String value = MimeUtility.unfold(header.getValue());
+        if ("".equals(value.trim())) {
+            return null;
+        }
+        AddressList addressList = LenientAddressParser.DEFAULT.parseAddressList(value);
+        final int size = addressList.size();
+        final List<FetchResponse.Envelope.Address> addresses = new ArrayList<>(size);
+        for (Address address : addressList) {
+            if (address instanceof Group) {
+                final Group group = (Group) address;
+                addAddresses(group, addresses);
+
+            } else if (address instanceof Mailbox) {
+                final Mailbox mailbox = (Mailbox) address;
+                final FetchResponse.Envelope.Address mailboxAddress = buildMailboxAddress(mailbox);
+                addresses.add(mailboxAddress);
 
-            // We need to unfold the header line.
-            // See https://issues.apache.org/jira/browse/IMAP-154
-            //
-            // IMAP-Servers are advised to also replace tabs with single spaces while doing the unfolding. This is what javamails
-            // unfold does. mime4j's unfold does strictly follow the rfc and so preserve them
-            //
-            // See IMAP-327 and https://mailman2.u.washington.edu/mailman/htdig/imap-protocol/2010-July/001271.html
-            String value = MimeUtility.unfold(header.getValue());
-
-            if ("".equals(value.trim())) {
-                return null;
             } else {
-                AddressList addressList = LenientAddressParser.DEFAULT.parseAddressList(value);
-                final int size = addressList.size();
-                final List<FetchResponse.Envelope.Address> addresses = new ArrayList<>(size);
-                for (Address address : addressList) {
-                    if (address instanceof Group) {
-                        final Group group = (Group) address;
-                        addAddresses(group, addresses);
-
-                    } else if (address instanceof Mailbox) {
-                        final Mailbox mailbox = (Mailbox) address;
-                        final FetchResponse.Envelope.Address mailboxAddress = buildMailboxAddress(mailbox);
-                        addresses.add(mailboxAddress);
-
-                    } else {
-                        LOGGER.warn("Unknown address type {}", address.getClass());
-                    }
-                }
-
-                return addresses.toArray(FetchResponse.Envelope.Address[]::new);
+                LOGGER.warn("Unknown address type {}", address.getClass());
             }
         }
+        return addresses.toArray(FetchResponse.Envelope.Address[]::new);
+
     }
 
     private FetchResponse.Envelope.Address buildMailboxAddress(org.apache.james.mime4j.dom.address.Mailbox mailbox) {
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchResponseBuilder.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchResponseBuilder.java
index 0156e7f..41a1d68 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchResponseBuilder.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchResponseBuilder.java
@@ -280,12 +280,10 @@ public final class FetchResponseBuilder {
     private FetchResponse.BodyElement wrapIfPartialFetch(Long firstOctet, Long numberOfOctets, FetchResponse.BodyElement fullResult) {
         if (firstOctet == null) {
             return fullResult;
-        } else {
-            final long numberOfOctetsAsLong = Objects.requireNonNullElse(numberOfOctets, Long.MAX_VALUE);
-            final long firstOctetAsLong = firstOctet;
-
-            return new PartialFetchBodyElement(fullResult, firstOctetAsLong, numberOfOctetsAsLong);
         }
+        final long numberOfOctetsAsLong = Objects.requireNonNullElse(numberOfOctets, Long.MAX_VALUE);
+        final long firstOctetAsLong = firstOctet;
+        return new PartialFetchBodyElement(fullResult, firstOctetAsLong, numberOfOctetsAsLong);
     }
 
     private FetchResponse.BodyElement text(MessageResult messageResult, String name, Optional<MimePath> path) throws MailboxException {
@@ -301,9 +299,8 @@ public final class FetchResponseBuilder {
             } catch (IOException e) {
                 throw new MailboxException("Unable to get TEXT of body", e);
             }
-        } else {
-            return messageResult.getBody(path.get());
         }
+        return messageResult.getBody(path.get());
     }
 
     private FetchResponse.BodyElement mimeHeaders(MessageResult messageResult, String name, Optional<MimePath> path) throws MailboxException {
@@ -349,18 +346,15 @@ public final class FetchResponseBuilder {
                 if (messageResult.getSize() - element.size() <= 0) {
                     // Seems like this mail has no body
                     element.noBody();
-
                 }
             } catch (IOException e) {
                 throw new MailboxException("Unable to get size of header body element", e);
-
             }
             return element;
-        } else {
-            final Iterator<Header> headers = getHeaders(messageResult, path);
-            List<Header> lines = MessageResultUtils.getAll(headers);
-            return headerBodyElement(messageResult, name, lines, path);
         }
+        final Iterator<Header> headers = getHeaders(messageResult, path);
+        List<Header> lines = MessageResultUtils.getAll(headers);
+        return headerBodyElement(messageResult, name, lines, path);
     }
 
     private FetchResponse.BodyElement fieldsNot(MessageResult messageResult, String name, Optional<MimePath> path, Collection<String> names) throws MailboxException {
@@ -379,9 +373,8 @@ public final class FetchResponseBuilder {
     private Iterator<Header> getHeaders(MessageResult messageResult, Optional<MimePath> path) throws MailboxException {
         if (path.isEmpty()) {
             return messageResult.getHeaders().headers();
-        } else {
-            return messageResult.iterateHeaders(path.get());
         }
+        return messageResult.iterateHeaders(path.get());
     }
 
     private Iterator<Header> getMimeHeaders(MessageResult messageResult, Optional<MimePath> path) throws MailboxException {
@@ -402,8 +395,7 @@ public final class FetchResponseBuilder {
             } catch (IOException e) {
                 throw new MailboxException("Unable to get content", e);
             }
-        } else {
-            return messageResult.getMimeBody(path.get());
         }
+        return messageResult.getMimeBody(path.get());
     }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org