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 public StaticUserAuthenticator(final String domain, final String username, final String password) {
44 this.username = username;
45 this.password = password;
46 this.domain = domain;
47 }
48
49 @Override
50 public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
51 final UserAuthenticationDataionData.html#UserAuthenticationData">UserAuthenticationData data = new UserAuthenticationData();
52 for (final UserAuthenticationData.Type type : types) {
53 if (type == UserAuthenticationData.DOMAIN) {
54 data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
55 } else if (type == UserAuthenticationData.USERNAME) {
56 data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
57 } else if (type == UserAuthenticationData.PASSWORD) {
58 data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
59 } else if (LOG.isDebugEnabled()) {
60 LOG.debug(StaticUserAuthenticator.class.getSimpleName()
61 + " does not support authentication data type '" + type
62 + "'; authentication request for this type ignored.");
63 }
64 }
65 return data;
66 }
67
68
69
70
71
72
73 @Override
74 public int hashCode() {
75 final int prime = 37;
76 int result = 1;
77 result = prime * result + (domain == null ? 0 : domain.hashCode());
78 result = prime * result + (password == null ? 0 : password.hashCode());
79 result = prime * result + (username == null ? 0 : username.hashCode());
80
81 return result;
82 }
83
84
85
86
87
88
89 @Override
90 public boolean equals(final Object obj) {
91 if (this == obj) {
92 return true;
93 }
94
95 if (obj == null) {
96 return false;
97 }
98
99 if (getClass() != obj.getClass()) {
100 return false;
101 }
102
103 final StaticUserAuthenticatorrg/apache/commons/vfs2/auth/StaticUserAuthenticator.html#StaticUserAuthenticator">StaticUserAuthenticator other = (StaticUserAuthenticator) obj;
104 return Objects.equals(domain, other.domain) && Objects.equals(username, other.username)
105 && Objects.equals(password, other.password);
106 }
107
108
109
110
111
112
113 @Override
114 public int compareTo(final StaticUserAuthenticator other) {
115 int result = compareStringOrNull(domain, other.domain);
116 result = result == 0 ? compareStringOrNull(username, other.username) : result;
117 result = result == 0 ? compareStringOrNull(password, other.password) : result;
118
119 return result;
120 }
121
122 private int compareStringOrNull(final String thisString, final String otherString) {
123 if (thisString != null) {
124 if (otherString == null) {
125 return 1;
126 }
127
128 return thisString.compareTo(otherString);
129 }
130 if (otherString != null) {
131 return -1;
132 }
133
134 return 0;
135 }
136
137
138
139
140
141
142 @Override
143 public String toString() {
144 final StringBuilder buffer = new StringBuilder();
145 if (domain != null) {
146 buffer.append(domain).append('\\');
147 }
148 if (username != null) {
149 buffer.append(username);
150 } else {
151 buffer.append("(null)");
152 }
153 if (password != null) {
154 buffer.append(":***");
155 }
156 return buffer.toString();
157 }
158 }