001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.util;
018
019import org.apache.commons.vfs2.FileSystemOptions;
020import org.apache.commons.vfs2.UserAuthenticationData;
021import org.apache.commons.vfs2.UserAuthenticator;
022import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
023
024/**
025 * Helps with authentication.
026 */
027public final class UserAuthenticatorUtils {
028
029    /**
030     * Authenticates if there is an authenticator, else returns null.
031     *
032     * @param options The FileSystemOptions.
033     * @param authenticatorTypes An array of types describing the data to be retrieved.
034     * @return A UserAuthenticationData object containing the data requested.
035     */
036    public static UserAuthenticationData authenticate(final FileSystemOptions options,
037        final UserAuthenticationData.Type[] authenticatorTypes) {
038        final UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(options);
039        return authenticate(auth, authenticatorTypes);
040    }
041
042    /**
043     * Authenticates if there is an authenticator, else returns null.
044     *
045     * @param auth The UserAuthenticator.
046     * @param authenticatorTypes An array of types describing the data to be retrieved.
047     * @return A UserAuthenticationData object containing the data requested.
048     */
049    public static UserAuthenticationData authenticate(final UserAuthenticator auth,
050            final UserAuthenticationData.Type[] authenticatorTypes) {
051        if (auth == null) {
052            return null;
053        }
054
055        return auth.requestAuthentication(authenticatorTypes);
056    }
057
058    /**
059     * Cleans up the data in the UerAuthenticationData (null-safe).
060     *
061     * @param authData The UserAuthenticationDAta.
062     */
063    public static void cleanup(final UserAuthenticationData authData) {
064        if (authData != null) {
065            authData.cleanup();
066        }
067    }
068
069    /**
070     * Gets data of given type from the UserAuthenticationData or null if there is no data or data of this type
071     * available.
072     *
073     * @param data The UserAuthenticationData.
074     * @param type The type of the element to retrieve.
075     * @param overriddenValue The default value.
076     * @return The data of the given type as a character array or null if the data is not available.
077     */
078    public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type,
079            final char[] overriddenValue) {
080        if (overriddenValue != null) {
081            return overriddenValue;
082        }
083
084        if (data == null) {
085            return null;
086        }
087
088        return data.getData(type);
089    }
090
091    /**
092     * Converts a string to a char array (null-safe).
093     *
094     * @param string The String to convert.
095     * @return The character array.
096     */
097    public static char[] toChar(final String string) {
098        if (string == null) {
099            return null;
100        }
101
102        return string.toCharArray();
103    }
104
105    /**
106     * Converts the given data to a string (null-safe).
107     *
108     * @param data A character array containing the data to convert to a String.
109     * @return The String.
110     */
111    public static String toString(final char[] data) {
112        if (data == null) {
113            return null;
114        }
115
116        return new String(data);
117    }
118
119    private UserAuthenticatorUtils() {
120    }
121}