Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PostNoLoops |
|
| 2.6666666666666665;2.667 |
1 | /** | |
2 | * Copyright (c) 2014-2017, 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 java.util.ArrayList; | |
39 | import java.util.Arrays; | |
40 | import java.util.Collection; | |
41 | import javax.mail.Address; | |
42 | import javax.mail.Message; | |
43 | import javax.mail.MessagingException; | |
44 | import lombok.EqualsAndHashCode; | |
45 | import lombok.ToString; | |
46 | ||
47 | /** | |
48 | * Postman that ignores loops (sender equals to recipient). | |
49 | * | |
50 | * @author Yegor Bugayenko (yegor@teamed.io) | |
51 | * @version $Id: 270b070ccacc9a95b535aed55c51666b95681b8f $ | |
52 | * @since 1.6 | |
53 | */ | |
54 | @Immutable | |
55 | 0 | @ToString |
56 | 0 | @EqualsAndHashCode(of = "origin") |
57 | @Loggable(Loggable.DEBUG) | |
58 | public final class PostNoLoops implements Postman { | |
59 | ||
60 | /** | |
61 | * Original postman. | |
62 | */ | |
63 | private final transient Postman origin; | |
64 | ||
65 | /** | |
66 | * Ctor. | |
67 | * @param post Original postman | |
68 | */ | |
69 | 2 | public PostNoLoops(final Postman post) { |
70 | 2 | this.origin = post; |
71 | 2 | } |
72 | ||
73 | @Override | |
74 | public void send(final Envelope env) throws IOException { | |
75 | 2 | final Message msg = env.unwrap(); |
76 | try { | |
77 | 2 | final Address[] rcpts = msg.getAllRecipients(); |
78 | 2 | final Address[] reply = msg.getReplyTo(); |
79 | 2 | final Address[] from = msg.getFrom(); |
80 | 2 | final boolean intersects = |
81 | this.intersect(rcpts, reply, "Reply-To and Recipients") | |
82 | || this.intersect(rcpts, from, "Recipients and From"); | |
83 | 2 | if (!intersects) { |
84 | 1 | this.origin.send(env); |
85 | } | |
86 | 0 | } catch (final MessagingException ex) { |
87 | 0 | throw new IOException(ex); |
88 | 2 | } |
89 | 2 | } |
90 | ||
91 | /** | |
92 | * Check these two arrays for intersection. | |
93 | * @param first Array of addresses | |
94 | * @param second Second array | |
95 | * @param msg Text for log | |
96 | * @return TRUE if they intersect | |
97 | */ | |
98 | private boolean intersect(final Address[] first, final Address[] second, | |
99 | final String msg) { | |
100 | 3 | final Collection<Address> intersection = |
101 | new ArrayList<>(Arrays.asList(first)); | |
102 | 3 | intersection.retainAll(Arrays.asList(second)); |
103 | 3 | final boolean intersect = !intersection.isEmpty(); |
104 | 3 | if (intersect) { |
105 | 1 | Logger.info( |
106 | this, | |
107 | "Address(es) %s exist in both %s", | |
108 | intersection, msg | |
109 | ); | |
110 | } | |
111 | 3 | return intersect; |
112 | } | |
113 | ||
114 | } |