Coverage Report - com.jcabi.email.Postman
 
Classes in this File Line Coverage Branch Coverage Complexity
Postman
N/A
N/A
2.2
Postman$1
0%
0/7
N/A
2.2
Postman$Default
70%
14/20
0%
0/12
2.2
 
 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;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.log.Logger;
 35  
 import java.io.IOException;
 36  
 import java.util.Arrays;
 37  
 import javax.mail.Address;
 38  
 import javax.mail.Message;
 39  
 import javax.mail.MessagingException;
 40  
 import javax.mail.Transport;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 
 44  
 /**
 45  
  * Postman.
 46  
  *
 47  
  * <p>The best way to use it is to make an instance of
 48  
  * {@link com.jcabi.email.Postman.Default}:
 49  
  *
 50  
  * <pre> Postman postman = new Postman.Default(
 51  
  *   new SMTP(
 52  
  *     new Token("user", "password").access(
 53  
  *       new Protocol.SMTPS("smtp.gmail.com", 587)
 54  
  *     )
 55  
  *   )
 56  
  * );
 57  
  * postman.send(
 58  
  *   new Envelope.MIME(
 59  
  *     new Array&lt;Stamp&gt;(
 60  
  *       new StSender("Yegor Bugayenko &lt;yegor@jcabi.com&gt;"),
 61  
  *       new StRecipient("Jeff Lebowski &lt;jeff@gmail.com&gt;"),
 62  
  *       new StSubject("dude, how are you?"),
 63  
  *       new StBCC("my-boss@jcabi.com")
 64  
  *     ),
 65  
  *     new Array&lt;Enclosure&gt;(
 66  
  *       new EnPlain("Hi, long time no see! :) Check my pic!"),
 67  
  *       new EnBinary(
 68  
  *         new File("/tmp/picture.gif"),
 69  
  *         "my-picture.gif",
 70  
  *         "image/gif"
 71  
  *       )
 72  
  *     )
 73  
  *   )
 74  
  * );</pre>
 75  
  *
 76  
  * @author Yegor Bugayenko (yegor@teamed.io)
 77  
  * @version $Id: 51a49d772a8de096e541515ac78a3d3faaac1e1a $
 78  
  * @since 1.0
 79  
  */
 80  
 @Immutable
 81  
 public interface Postman {
 82  
 
 83  
     /**
 84  
      * Doesn't send anything, just logs to console.
 85  
      * @since 1.1
 86  
      */
 87  0
     Postman CONSOLE = new Postman() {
 88  
         @Override
 89  
         public void send(final Envelope env) throws IOException {
 90  0
             final Message msg = new Envelope.Strict(env).unwrap();
 91  
             try {
 92  0
                 Logger.info(
 93  
                     this, "fake email \"%s\" from %s to %s",
 94  
                     msg.getSubject(),
 95  
                     Arrays.asList(msg.getFrom()),
 96  
                     Arrays.asList(msg.getAllRecipients())
 97  
                 );
 98  0
             } catch (final MessagingException ex) {
 99  0
                 throw new IOException(ex);
 100  0
             }
 101  0
         }
 102  
     };
 103  
 
 104  
     /**
 105  
      * Send this envelope.
 106  
      * @param env Envelope to send
 107  
      * @throws IOException If fails
 108  
      */
 109  
     void send(Envelope env) throws IOException;
 110  
 
 111  
     /**
 112  
      * Default postman.
 113  
      */
 114  
     @Immutable
 115  0
     @ToString
 116  0
     @EqualsAndHashCode(of = "wire")
 117  
     @Loggable(Loggable.DEBUG)
 118  
     final class Default implements Postman {
 119  
         /**
 120  
          * Wire.
 121  
          */
 122  
         private final transient Wire wire;
 123  
         /**
 124  
          * Ctor.
 125  
          * @param wre Wire
 126  
          */
 127  2
         public Default(final Wire wre) {
 128  2
             this.wire = wre;
 129  2
         }
 130  
         @Override
 131  
         public void send(final Envelope env) throws IOException {
 132  2
             final Message message = new Envelope.Strict(env).unwrap();
 133  2
             final Transport transport = this.wire.connect();
 134  
             try {
 135  2
                 final Address[] rcpts = message.getAllRecipients();
 136  2
                 transport.sendMessage(message, rcpts);
 137  2
                 Logger.info(
 138  
                     this, "email sent from %s to %[list]s",
 139  
                     Arrays.asList(message.getFrom()),
 140  
                     Arrays.asList(rcpts)
 141  
                 );
 142  0
             } catch (final MessagingException ex) {
 143  0
                 throw new IOException(ex);
 144  
             } finally {
 145  2
                 Postman.Default.close(transport);
 146  2
             }
 147  2
         }
 148  
         /**
 149  
          * Close transport.
 150  
          * @param transport Transport to close
 151  
          * @throws IOException If fails
 152  
          */
 153  
         private static void close(final Transport transport)
 154  
             throws IOException {
 155  
             try {
 156  2
                 transport.close();
 157  0
             } catch (final MessagingException ex) {
 158  0
                 throw new IOException(ex);
 159  2
             }
 160  2
         }
 161  
     }
 162  
 
 163  
 }