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    private UserAuthenticatorUtils() {
029    }
030
031    /**
032     * Gets data of given type from the UserAuthenticationData or null if there is no data or data of this type
033     * available.
034     *
035     * @param data The UserAuthenticationData.
036     * @param type The type of the element to retrieve.
037     * @param overriddenValue The default value.
038     * @return The data of the given type as a character array or null if the data is not available.
039     */
040    public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type,
041            final char[] overriddenValue) {
042        if (overriddenValue != null) {
043            return overriddenValue;
044        }
045
046        if (data == null) {
047            return null;
048        }
049
050        return data.getData(type);
051    }
052
053    /**
054     * Authenticates if there is an authenticator, else returns null.
055     *
056     * @param opts The FileSystemOptions.
057     * @param authenticatorTypes An array of types describing the data to be retrieved.
058     * @return A UserAuthenticationData object containing the data requested.
059     */
060    public static UserAuthenticationData authenticate(final FileSystemOptions opts,
061            final UserAuthenticationData.Type[] authenticatorTypes) {
062        final UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(opts);
063        return authenticate(auth, authenticatorTypes);
064    }
065
066    /**
067     * Authenticates if there is an authenticator, else returns null.
068     *
069     * @param auth The UserAuthenticator.
070     * @param authenticatorTypes An array of types describing the data to be retrieved.
071     * @return A UserAuthenticationData object containing the data requested.
072     */
073    public static UserAuthenticationData authenticate(final UserAuthenticator auth,
074            final UserAuthenticationData.Type[] authenticatorTypes) {
075        if (auth == null) {
076            return null;
077        }
078
079        return auth.requestAuthentication(authenticatorTypes);
080    }
081
082    /**
083     * Converts a string to a char array (null-safe).
084     *
085     * @param string The String to convert.
086     * @return The character array.
087     */
088    public static char[] toChar(final String string) {
089        if (string == null) {
090            return null;
091        }
092
093        return string.toCharArray();
094    }
095
096    /**
097     * Cleans up the data in the UerAuthenticationData (null-safe).
098     *
099     * @param authData The UserAuthenticationDAta.
100     */
101    public static void cleanup(final UserAuthenticationData authData) {
102        if (authData == null) {
103            return;
104        }
105
106        authData.cleanup();
107    }
108
109    /**
110     * Converts the given data to a string (null-safe).
111     *
112     * @param data A character array containing the data to convert to a String.
113     * @return The String.
114     */
115    public static String toString(final char[] data) {
116        if (data == null) {
117            return null;
118        }
119
120        return new String(data);
121    }
122}