1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.util;
18
19 import org.apache.commons.vfs.UserAuthenticator;
20 import org.apache.commons.vfs.UserAuthenticationData;
21 import org.apache.commons.vfs.FileSystemOptions;
22 import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
23
24 /***
25 * some helper
26 */
27 public class UserAuthenticatorUtils
28 {
29 /***
30 * gets data of given type from the UserAuthenticationData or null if there is no data or data of this type available
31 */
32 public static char[] getData(UserAuthenticationData data, UserAuthenticationData.Type type, char[] overwriddenValue)
33 {
34 if (overwriddenValue != null)
35 {
36 return overwriddenValue;
37 }
38
39 if (data == null)
40 {
41 return null;
42 }
43
44 return data.getData(type);
45 }
46
47 /***
48 * if there is a authenticator the authentication will take place, else null will be reutrned
49 */
50 public static UserAuthenticationData authenticate(FileSystemOptions opts, UserAuthenticationData.Type[] authenticatorTypes)
51 {
52 UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(opts);
53 return authenticate(auth, authenticatorTypes);
54 }
55
56 /***
57 * if there is a authenticator the authentication will take place, else null will be reutrned
58 */
59 public static UserAuthenticationData authenticate(UserAuthenticator auth, UserAuthenticationData.Type[] authenticatorTypes)
60 {
61 if (auth == null)
62 {
63 return null;
64 }
65
66 return auth.requestAuthentication(authenticatorTypes);
67 }
68
69 /***
70 * converts a string to a char array (null safe)
71 */
72 public static char[] toChar(String string)
73 {
74 if (string == null)
75 {
76 return null;
77 }
78
79 return string.toCharArray();
80 }
81
82 /***
83 * cleanup the data in the UerAuthenticationData (null safe)
84 */
85 public static void cleanup(UserAuthenticationData authData)
86 {
87 if (authData == null)
88 {
89 return;
90 }
91
92 authData.cleanup();
93 }
94
95 /***
96 * converts the given data to a string (null safe)
97 */
98 public static String toString(char[] data)
99 {
100 if (data == null)
101 {
102 return null;
103 }
104
105 return new String(data);
106 }
107 }