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 return auth.requestAuthentication(authenticatorTypes); 055 } 056 057 /** 058 * Cleans up the data in the UerAuthenticationData (null-safe). 059 * 060 * @param authData The UserAuthenticationDAta. 061 */ 062 public static void cleanup(final UserAuthenticationData authData) { 063 if (authData != null) { 064 authData.cleanup(); 065 } 066 } 067 068 /** 069 * 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. 070 * 071 * @param data The UserAuthenticationData. 072 * @param type The type of the element to retrieve. 073 * @param overriddenValue The default value. 074 * @return The data of the given type as a character array or null if the data is not available. 075 */ 076 public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type, final char[] overriddenValue) { 077 if (overriddenValue != null) { 078 return overriddenValue; 079 } 080 if (data == null) { 081 return null; 082 } 083 return data.getData(type); 084 } 085 086 /** 087 * Converts a string to a char array (null-safe). 088 * 089 * @param string The String to convert. 090 * @return The character array. 091 */ 092 public static char[] toChar(final String string) { 093 if (string == null) { 094 return null; 095 } 096 return string.toCharArray(); 097 } 098 099 /** 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}