/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2.util;

import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.UserAuthenticationData;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;

/**
 * Some helper methods used for authentication.
 */
public final class UserAuthenticatorUtils
{
    private UserAuthenticatorUtils()
    {
    }

    /**
     * gets data of given type from the UserAuthenticationData or null if there is no data or data
     * of this type available.
     * @param data The UserAuthenticationData.
     * @param type The type of the element to retrieve.
     * @param overriddenValue The default value.
     * @return The data of the given type as a character array or null if the data is not available.
     */
    public static char[] getData(UserAuthenticationData data, UserAuthenticationData.Type type,
                                 char[] overriddenValue)
    {
        if (overriddenValue != null)
        {
            return overriddenValue;
        }

        if (data == null)
        {
            return null;
        }

        return data.getData(type);
    }

    /**
     * if there is a authenticator the authentication will take place, else null will be returned.
     * @param opts The FileSystemOptions.
     * @param authenticatorTypes An array of types describing the data to be retrieved.
     * @return A UserAuthenticationData object containing the data requested.
     */
    public static UserAuthenticationData authenticate(FileSystemOptions opts,
                                                      UserAuthenticationData.Type[] authenticatorTypes)
    {
        UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(opts);
        return authenticate(auth, authenticatorTypes);
    }

    /**
     * if there is a authenticator the authentication will take place, else null will be returned.
     * @param auth The UserAuthenticator.
     * @param authenticatorTypes An array of types describing the data to be retrieved.
     * @return A UserAuthenticationData object containing the data requested.
     */
    public static UserAuthenticationData authenticate(UserAuthenticator auth,
                                                      UserAuthenticationData.Type[] authenticatorTypes)
    {
        if (auth == null)
        {
            return null;
        }

        return auth.requestAuthentication(authenticatorTypes);
    }

    /**
     * Converts a string to a char array (null safe).
     * @param string The String to convert.
     * @return The character array.
     */
    public static char[] toChar(String string)
    {
        if (string == null)
        {
            return null;
        }

        return string.toCharArray();
    }

    /**
     * cleanup the data in the UerAuthenticationData (null safe).
     * @param authData The UserAuthenticationDAta.
     */
    public static void cleanup(UserAuthenticationData authData)
    {
        if (authData == null)
        {
            return;
        }

        authData.cleanup();
    }

    /**
     * converts the given data to a string (null safe).
     * @param data A character array containing the data to convert to a String.
     * @return The String.
     */
    public static String toString(char[] data)
    {
        if (data == null)
        {
            return null;
        }

        return new String(data);
    }
}

@@ -1070,36 +1070,18 @@
 %0A * 
-Some helper methods used for
+Helps with
  aut
@@ -1206,17 +1206,17 @@
 %0A     * 
-g
+G
 ets data
@@ -1325,24 +1325,32 @@
  available.%0A
+     * %0A
      * @para
@@ -1971,32 +1971,46 @@
 %0A%0A    /**%0A     *
+ Authenticates
  if there is a a
@@ -1999,32 +1999,33 @@
 es if there is a
+n
  authenticator t
@@ -2026,72 +2026,36 @@
 ator
- the authentication will take place, else null will be returned.
+, else returns null.%0A     * 
 %0A   
@@ -2637,16 +2637,30 @@
 *%0A     *
+ Authenticates
  if ther
@@ -2665,16 +2665,17 @@
 ere is a
+n
  authent
@@ -2684,72 +2684,36 @@
 ator
- the authentication will take place, else null will be returned.
+, else returns null.%0A     * 
 %0A   
@@ -3305,32 +3305,40 @@
  array (null
- 
+-
 safe).%0A
+     * %0A
      * @para
@@ -3599,21 +3599,23 @@
 %0A     * 
-c
+C
 lean
+s 
 up the d
@@ -3648,32 +3648,40 @@
 onData (null
- 
+-
 safe).%0A
+     * %0A
      * @para
@@ -3918,17 +3918,17 @@
 %0A     * 
-c
+C
 onverts 
@@ -3963,16 +3963,24 @@
 null
- 
+-
 safe).%0A
+     * %0A
     

Use the active voice in Javadocs and fix typos.