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.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 }