Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Token |
|
| 1.25;1.25 | ||||
Token$Verification |
|
| 1.25;1.25 |
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 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 | * | |
45 | * <p>This is how you're supposed to use it: | |
46 | * | |
47 | * <pre> Postman postman = new Postman.Default( | |
48 | * new SMTP( | |
49 | * new Token("user", "password").access( | |
50 | * new Protocol.SMTP("bind", "port") | |
51 | * ) | |
52 | * ) | |
53 | * ); | |
54 | * </pre> | |
55 | * | |
56 | * @author Piotr Kotlicki (piotr.kotlicki@gmail.com) | |
57 | * @version $Id: ee6d6345243a811cae1674d55c3c81b7a37d9624 $ | |
58 | * @since 1.0 | |
59 | */ | |
60 | @Immutable | |
61 | 0 | @ToString |
62 | 0 | @EqualsAndHashCode(of = {"user", "password"}) |
63 | @Loggable(Loggable.DEBUG) | |
64 | public final class Token { | |
65 | ||
66 | /** | |
67 | * User name with access. | |
68 | */ | |
69 | private final transient String user; | |
70 | ||
71 | /** | |
72 | * User's password. | |
73 | */ | |
74 | private final transient String password; | |
75 | ||
76 | /** | |
77 | * Public ctor. | |
78 | * @param usr User name with access | |
79 | * @param pwd User's password | |
80 | */ | |
81 | 2 | public Token(final String usr, final String pwd) { |
82 | 2 | this.user = usr; |
83 | 2 | this.password = pwd; |
84 | 2 | } |
85 | ||
86 | /** | |
87 | * Access for given protocol. | |
88 | * @param protocol Protocol. | |
89 | * @return Session. | |
90 | */ | |
91 | public Session access(final Protocol protocol) { | |
92 | 2 | final Properties props = new Properties(); |
93 | for (final Map.Entry<String, String> entry | |
94 | 2 | : protocol.entries().entrySet()) { |
95 | 10 | props.setProperty(entry.getKey(), entry.getValue()); |
96 | 10 | } |
97 | 2 | return Session.getInstance( |
98 | props, new Token.Verification(this.user, this.password) | |
99 | ); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Authenticating credentials. | |
104 | */ | |
105 | private static final class Verification extends Authenticator { | |
106 | /** | |
107 | * User name. | |
108 | */ | |
109 | private final transient String user; | |
110 | /** | |
111 | * User password. | |
112 | */ | |
113 | private final transient String password; | |
114 | ||
115 | /** | |
116 | * Public ctor. | |
117 | * @param usr User name | |
118 | * @param pwd User password | |
119 | */ | |
120 | Verification(final String usr, final String pwd) { | |
121 | 2 | super(); |
122 | 2 | this.user = usr; |
123 | 2 | this.password = pwd; |
124 | 2 | } |
125 | ||
126 | @Override | |
127 | public PasswordAuthentication getPasswordAuthentication() { | |
128 | 2 | return new PasswordAuthentication(this.user, this.password); |
129 | } | |
130 | } | |
131 | } |