Coverage Report - com.jcabi.email.Token
 
Classes in this File Line Coverage Branch Coverage Complexity
Token
81%
9/11
9%
2/22
1.25
Token$Verification
100%
5/5
N/A
1.25
 
 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 java.util.Map;
 35  
 import java.util.Properties;
 36  
 import javax.mail.Authenticator;
 37  
 import javax.mail.PasswordAuthentication;
 38  
 import javax.mail.Session;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import lombok.ToString;
 41  
 
 42  
 /**
 43  
  * User signature to network node.
 44  
  * @author Piotr Kotlicki (piotr.kotlicki@gmail.com)
 45  
  * @version $Id: d43a1576a36990a587a967187da7830c9b614b0d $
 46  
  * @since 1.0
 47  
  */
 48  
 @Immutable
 49  0
 @ToString
 50  0
 @EqualsAndHashCode(of = {"user", "password"})
 51  
 @Loggable(Loggable.DEBUG)
 52  
 public final class Token {
 53  
 
 54  
     /**
 55  
      * User name with access.
 56  
      */
 57  
     private final transient String user;
 58  
 
 59  
     /**
 60  
      * User's password.
 61  
      */
 62  
     private final transient String password;
 63  
 
 64  
     /**
 65  
      * Public ctor.
 66  
      * @param usr User name with access
 67  
      * @param pwd User's password
 68  
      */
 69  1
     public Token(final String usr, final String pwd) {
 70  1
         this.user = usr;
 71  1
         this.password = pwd;
 72  1
     }
 73  
 
 74  
     /**
 75  
      * Access for given protocol.
 76  
      * @param protocol Protocol.
 77  
      * @return Session.
 78  
      */
 79  
     public Session access(final Protocol protocol) {
 80  1
         final Properties props = new Properties();
 81  1
         for (final Map.Entry<String, String> entry : protocol.entries()
 82  
             .entrySet()) {
 83  3
             props.setProperty(entry.getKey(), entry.getValue());
 84  3
         }
 85  1
         return Session.getInstance(
 86  
             props,
 87  
             new Token.Verification(this.user, this.password)
 88  
         );
 89  
     }
 90  
 
 91  
     /**
 92  
      * Authenticating credentials.
 93  
      */
 94  
     private static final class Verification extends Authenticator {
 95  
         /**
 96  
          * User name.
 97  
          */
 98  
         private final transient String user;
 99  
         /**
 100  
          * User password.
 101  
          */
 102  
         private final transient String password;
 103  
 
 104  
         /**
 105  
          * Public ctor.
 106  
          * @param usr User name
 107  
          * @param pwd User password
 108  
          */
 109  
         Verification(final String usr, final String pwd) {
 110  1
             super();
 111  1
             this.user = usr;
 112  1
             this.password = pwd;
 113  1
         }
 114  
 
 115  
         @Override
 116  
         public PasswordAuthentication getPasswordAuthentication() {
 117  1
             return new PasswordAuthentication(this.user, this.password);
 118  
         }
 119  
     }
 120  
 }