View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.util;
18  
19  import org.apache.commons.vfs2.FileSystemOptions;
20  import org.apache.commons.vfs2.UserAuthenticationData;
21  import org.apache.commons.vfs2.UserAuthenticator;
22  import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
23  
24  /**
25   * Helps with authentication.
26   */
27  public final class UserAuthenticatorUtils {
28  
29      private UserAuthenticatorUtils() {
30      }
31  
32      /**
33       * Gets data of given type from the UserAuthenticationData or null if there is no data or data of this type
34       * available.
35       *
36       * @param data The UserAuthenticationData.
37       * @param type The type of the element to retrieve.
38       * @param overriddenValue The default value.
39       * @return The data of the given type as a character array or null if the data is not available.
40       */
41      public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type,
42              final char[] overriddenValue) {
43          if (overriddenValue != null) {
44              return overriddenValue;
45          }
46  
47          if (data == null) {
48              return null;
49          }
50  
51          return data.getData(type);
52      }
53  
54      /**
55       * Authenticates if there is an authenticator, else returns null.
56       *
57       * @param options The FileSystemOptions.
58       * @param authenticatorTypes An array of types describing the data to be retrieved.
59       * @return A UserAuthenticationData object containing the data requested.
60       */
61      public static UserAuthenticationData authenticate(final FileSystemOptions options,
62          final UserAuthenticationData.Type[] authenticatorTypes) {
63          final UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(options);
64          return authenticate(auth, authenticatorTypes);
65      }
66  
67      /**
68       * Authenticates if there is an authenticator, else returns null.
69       *
70       * @param auth The UserAuthenticator.
71       * @param authenticatorTypes An array of types describing the data to be retrieved.
72       * @return A UserAuthenticationData object containing the data requested.
73       */
74      public static UserAuthenticationData authenticate(final UserAuthenticator auth,
75              final UserAuthenticationData.Type[] authenticatorTypes) {
76          if (auth == null) {
77              return null;
78          }
79  
80          return auth.requestAuthentication(authenticatorTypes);
81      }
82  
83      /**
84       * Converts a string to a char array (null-safe).
85       *
86       * @param string The String to convert.
87       * @return The character array.
88       */
89      public static char[] toChar(final String string) {
90          if (string == null) {
91              return null;
92          }
93  
94          return string.toCharArray();
95      }
96  
97      /**
98       * Cleans up the data in the UerAuthenticationData (null-safe).
99       *
100      * @param authData The UserAuthenticationDAta.
101      */
102     public static void cleanup(final UserAuthenticationData authData) {
103         if (authData != null) {
104             authData.cleanup();
105         }
106     }
107 
108     /**
109      * Converts the given data to a string (null-safe).
110      *
111      * @param data A character array containing the data to convert to a String.
112      * @return The String.
113      */
114     public static String toString(final char[] data) {
115         if (data == null) {
116             return null;
117         }
118 
119         return new String(data);
120     }
121 }