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-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;
 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("smtp.googlemail.com", 465, "username", "password")
 52  
  * );
 53  
  * postman.send(
 54  
  *   new Envelope.MIME(
 55  
  *     new Array&lt;Stamp&gt;(
 56  
  *       new StSender("Yegor Bugayenko &lt;yegor@jcabi.com&gt;"),
 57  
  *       new StRecipient("Jeff Lebowski &lt;jeff@gmail.com&gt;"),
 58  
  *       new StSubject("dude, how are you?"),
 59  
  *       new StBCC("my-boss@jcabi.com")
 60  
  *     ),
 61  
  *     new Array&lt;Enclosure&gt;(
 62  
  *       new EnPlain("Hi, long time no see! :) Check my pic!"),
 63  
  *       new EnBinary(
 64  
  *         new File("/tmp/picture.gif"),
 65  
  *         "my-picture.gif",
 66  
  *         "image/gif"
 67  
  *       )
 68  
  *     )
 69  
  *   )
 70  
  * );</pre>
 71  
  *
 72  
  * @author Yegor Bugayenko (yegor@teamed.io)
 73  
  * @version $Id: 75058793716e299a30ffe4b3d01d8c6489b2698e $
 74  
  * @since 1.0
 75  
  */
 76  
 @Immutable
 77  
 public interface Postman {
 78  
 
 79  
     /**
 80  
      * Doesn't send anything, just logs to console.
 81  
      * @since 1.1
 82  
      */
 83  0
     Postman CONSOLE = new Postman() {
 84  
         @Override
 85  
         public void send(final Envelope env) throws IOException {
 86  0
             final Message msg = new Envelope.Strict(env).unwrap();
 87  
             try {
 88  0
                 Logger.info(
 89  
                     this, "fake email \"%s\" from %s to %s",
 90  
                     msg.getSubject(),
 91  
                     Arrays.asList(msg.getFrom()),
 92  
                     Arrays.asList(msg.getAllRecipients())
 93  
                 );
 94  0
             } catch (final MessagingException ex) {
 95  0
                 throw new IOException(ex);
 96  0
             }
 97  0
         }
 98  
     };
 99  
 
 100  
     /**
 101  
      * Send this envelope.
 102  
      * @param env Envelope to send
 103  
      * @throws IOException If fails
 104  
      */
 105  
     void send(Envelope env) throws IOException;
 106  
 
 107  
     /**
 108  
      * Default postman.
 109  
      */
 110  
     @Immutable
 111  0
     @ToString
 112  0
     @EqualsAndHashCode(of = "wire")
 113  
     @Loggable(Loggable.DEBUG)
 114  
     final class Default implements Postman {
 115  
         /**
 116  
          * Wire.
 117  
          */
 118  
         private final transient Wire wire;
 119  
         /**
 120  
          * Ctor.
 121  
          * @param wre Wire
 122  
          */
 123  1
         public Default(final Wire wre) {
 124  1
             this.wire = wre;
 125  1
         }
 126  
         @Override
 127  
         public void send(final Envelope env) throws IOException {
 128  1
             final Message message = new Envelope.Strict(env).unwrap();
 129  1
             final Transport transport = this.wire.connect();
 130  
             try {
 131  1
                 final Address[] rcpts = message.getAllRecipients();
 132  1
                 transport.sendMessage(message, rcpts);
 133  1
                 Logger.info(
 134  
                     this, "email sent from %s to %[list]s",
 135  
                     Arrays.asList(message.getFrom()),
 136  
                     Arrays.asList(rcpts)
 137  
                 );
 138  0
             } catch (final MessagingException ex) {
 139  0
                 throw new IOException(ex);
 140  
             } finally {
 141  1
                 Postman.Default.close(transport);
 142  1
             }
 143  1
         }
 144  
         /**
 145  
          * Close transport.
 146  
          * @param transport Transport to close
 147  
          * @throws IOException If fails
 148  
          */
 149  
         private static void close(final Transport transport)
 150  
             throws IOException {
 151  
             try {
 152  1
                 transport.close();
 153  0
             } catch (final MessagingException ex) {
 154  0
                 throw new IOException(ex);
 155  1
             }
 156  1
         }
 157  
     }
 158  
 
 159  
 }