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      /**
30       * Authenticates if there is an authenticator, else returns null.
31       *
32       * @param options The FileSystemOptions.
33       * @param authenticatorTypes An array of types describing the data to be retrieved.
34       * @return A UserAuthenticationData object containing the data requested.
35       */
36      public static UserAuthenticationData authenticate(final FileSystemOptions options,
37          final UserAuthenticationData.Type[] authenticatorTypes) {
38          final UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(options);
39          return authenticate(auth, authenticatorTypes);
40      }
41  
42      /**
43       * Authenticates if there is an authenticator, else returns null.
44       *
45       * @param auth The UserAuthenticator.
46       * @param authenticatorTypes An array of types describing the data to be retrieved.
47       * @return A UserAuthenticationData object containing the data requested.
48       */
49      public static UserAuthenticationData authenticate(final UserAuthenticator auth,
50              final UserAuthenticationData.Type[] authenticatorTypes) {
51          if (auth == null) {
52              return null;
53          }
54          return auth.requestAuthentication(authenticatorTypes);
55      }
56  
57      /**
58       * Cleans up the data in the UerAuthenticationData (null-safe).
59       *
60       * @param authData The UserAuthenticationDAta.
61       */
62      public static void cleanup(final UserAuthenticationData authData) {
63          if (authData != null) {
64              authData.cleanup();
65          }
66      }
67  
68      /**
69       * Gets a copy of the data of a given type from the UserAuthenticationData or null if there is no data or data of this type available.
70       *
71       * @param data            The UserAuthenticationData.
72       * @param type            The type of the element to retrieve.
73       * @param overriddenValue The default value.
74       * @return The data of the given type as a character array or null if the data is not available.
75       */
76      public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type, final char[] overriddenValue) {
77          if (overriddenValue != null) {
78              return overriddenValue;
79          }
80          if (data == null) {
81              return null;
82          }
83          return data.getData(type);
84      }
85  
86      /**
87       * Converts a string to a char array (null-safe).
88       *
89       * @param string The String to convert.
90       * @return The character array.
91       */
92      public static char[] toChar(final String string) {
93          if (string == null) {
94              return null;
95          }
96          return string.toCharArray();
97      }
98  
99      /**
100      * Converts the given data to a string (null-safe).
101      *
102      * @param data A character array containing the data to convert to a String.
103      * @return The String.
104      */
105     public static String toString(final char[] data) {
106         if (data == null) {
107             return null;
108         }
109         return new String(data);
110     }
111 
112     private UserAuthenticatorUtils() {
113     }
114 }