1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.auth;
18
19 import java.util.Objects;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.commons.vfs2.UserAuthenticationData;
24 import org.apache.commons.vfs2.UserAuthenticator;
25 import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
26
27
28
29
30 public class StaticUserAuthenticator implements UserAuthenticator, Comparable<StaticUserAuthenticator> {
31
32 private static final Log LOG = LogFactory.getLog(StaticUserAuthenticator.class);
33
34
35 private final String userName;
36
37
38 private final String password;
39
40
41 private final String domain;
42
43
44
45
46
47
48
49
50 public StaticUserAuthenticator(final String domain, final String userName, final String password) {
51 this.userName = userName;
52 this.password = password;
53 this.domain = domain;
54 }
55
56 private int compareStringOrNull(final String thisString, final String otherString) {
57 if (thisString != null) {
58 if (otherString == null) {
59 return 1;
60 }
61
62 return thisString.compareTo(otherString);
63 }
64 if (otherString != null) {
65 return -1;
66 }
67
68 return 0;
69 }
70
71
72
73
74
75
76 @Override
77 public int compareTo(final StaticUserAuthenticator other) {
78 int result = compareStringOrNull(domain, other.domain);
79 result = result == 0 ? compareStringOrNull(userName, other.userName) : result;
80 return result == 0 ? compareStringOrNull(password, other.password) : result;
81 }
82
83
84
85
86
87
88 @Override
89 public boolean equals(final Object obj) {
90 if (this == obj) {
91 return true;
92 }
93
94 if (obj == null) {
95 return false;
96 }
97
98 if (getClass() != obj.getClass()) {
99 return false;
100 }
101
102 final StaticUserAuthenticator other = (StaticUserAuthenticator) obj;
103 return Objects.equals(domain, other.domain) && Objects.equals(userName, other.userName)
104 && Objects.equals(password, other.password);
105 }
106
107
108
109
110
111
112 @Override
113 public int hashCode() {
114 final int prime = 37;
115 int result = 1;
116 result = prime * result + (domain == null ? 0 : domain.hashCode());
117 result = prime * result + (password == null ? 0 : password.hashCode());
118 return prime * result + (userName == null ? 0 : userName.hashCode());
119 }
120
121 @Override
122 public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
123 final UserAuthenticationData data = new UserAuthenticationData();
124 for (final UserAuthenticationData.Type type : types) {
125 if (type == UserAuthenticationData.DOMAIN) {
126 data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
127 } else if (type == UserAuthenticationData.USERNAME) {
128 data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(userName));
129 } else if (type == UserAuthenticationData.PASSWORD) {
130 data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
131 } else if (LOG.isDebugEnabled()) {
132 LOG.debug(StaticUserAuthenticator.class.getSimpleName()
133 + " does not support authentication data type '" + type
134 + "'; authentication request for this type ignored.");
135 }
136 }
137 return data;
138 }
139
140
141
142
143
144
145 @Override
146 public String toString() {
147 final StringBuilder buffer = new StringBuilder();
148 if (domain != null) {
149 buffer.append(domain).append('\\');
150 }
151 if (userName != null) {
152 buffer.append(userName);
153 } else {
154 buffer.append("(null)");
155 }
156 if (password != null) {
157 buffer.append(":***");
158 }
159 return buffer.toString();
160 }
161 }