Coverage Report - com.jcabi.email.postman.PostNoLoops
 
Classes in this File Line Coverage Branch Coverage Complexity
PostNoLoops
84%
22/26
46%
13/28
4
 
 1  
 /**
 2  
  * Copyright (c) 2014-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.email.postman;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.email.Envelope;
 35  
 import com.jcabi.email.Postman;
 36  
 import com.jcabi.log.Logger;
 37  
 import java.io.IOException;
 38  
 import javax.mail.Address;
 39  
 import javax.mail.Message;
 40  
 import javax.mail.MessagingException;
 41  
 import javax.mail.internet.InternetAddress;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 
 45  
 /**
 46  
  * Postman that ignores loops (sender equals to recipient).
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@teamed.io)
 49  
  * @version $Id: c13f86e7c5e04373063faea6c30148befb64c7ed $
 50  
  * @since 1.6
 51  
  */
 52  
 @Immutable
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = "origin")
 55  
 @Loggable(Loggable.DEBUG)
 56  
 public final class PostNoLoops implements Postman {
 57  
 
 58  
     /**
 59  
      * Original postman.
 60  
      */
 61  
     private final transient Postman origin;
 62  
 
 63  
     /**
 64  
      * Ctor.
 65  
      * @param post Original postman
 66  
      */
 67  2
     public PostNoLoops(final Postman post) {
 68  2
         this.origin = post;
 69  2
     }
 70  
 
 71  
     @Override
 72  
     public void send(final Envelope env) throws IOException {
 73  2
         final Message msg = env.unwrap();
 74  
         try {
 75  2
             final Address[] rcpts = msg.getAllRecipients();
 76  2
             final Address[] reply = msg.getReplyTo();
 77  2
             final Address[] from = msg.getFrom();
 78  2
             final boolean intersects =
 79  
                 this.intersect(rcpts, reply, "Reply-To and Recipients")
 80  
                 || this.intersect(rcpts, from, "Recipients and From");
 81  2
             if (!intersects) {
 82  1
                 this.origin.send(env);
 83  
             }
 84  0
         } catch (final MessagingException ex) {
 85  0
             throw new IOException(ex);
 86  2
         }
 87  2
     }
 88  
 
 89  
     /**
 90  
      * Check these two arrays for intersection.
 91  
      * @param first Array of addresses
 92  
      * @param second Second array
 93  
      * @param msg Text for log
 94  
      * @return TRUE if they intersect
 95  
      */
 96  
     private boolean intersect(final Address[] first, final Address[] second,
 97  
         final String msg) {
 98  3
         boolean intersect = false;
 99  3
         if (first != null && second != null) {
 100  7
             for (final Address left : first) {
 101  8
                 for (final Address right : second) {
 102  4
                     final String lft = InternetAddress.class.cast(left)
 103  
                         .getAddress();
 104  4
                     final String rht = InternetAddress.class.cast(right)
 105  
                         .getAddress();
 106  4
                     if (lft.equals(rht)) {
 107  1
                         Logger.info(
 108  
                             this,
 109  
                             "%s exists in %s as %s and %s",
 110  
                             lft, msg, left, right
 111  
                         );
 112  1
                         intersect = true;
 113  
                     }
 114  
                 }
 115  
             }
 116  
         }
 117  3
         return intersect;
 118  
     }
 119  
 
 120  
 }