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: */
017: package org.apache.commons.vfs.util;
018:
019: import org.apache.commons.vfs.UserAuthenticator;
020: import org.apache.commons.vfs.UserAuthenticationData;
021: import org.apache.commons.vfs.FileSystemOptions;
022: import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
023:
024: /**
025: * some helper
026: */
027: public class UserAuthenticatorUtils {
028: /**
029: * gets data of given type from the UserAuthenticationData or null if there is no data or data of this type available
030: */
031: public static char[] getData(UserAuthenticationData data,
032: UserAuthenticationData.Type type, char[] overwriddenValue) {
033: if (overwriddenValue != null) {
034: return overwriddenValue;
035: }
036:
037: if (data == null) {
038: return null;
039: }
040:
041: return data.getData(type);
042: }
043:
044: /**
045: * if there is a authenticator the authentication will take place, else null will be reutrned
046: */
047: public static UserAuthenticationData authenticate(
048: FileSystemOptions opts,
049: UserAuthenticationData.Type[] authenticatorTypes) {
050: UserAuthenticator auth = DefaultFileSystemConfigBuilder
051: .getInstance().getUserAuthenticator(opts);
052: return authenticate(auth, authenticatorTypes);
053: }
054:
055: /**
056: * if there is a authenticator the authentication will take place, else null will be reutrned
057: */
058: public static UserAuthenticationData authenticate(
059: UserAuthenticator auth,
060: UserAuthenticationData.Type[] authenticatorTypes) {
061: if (auth == null) {
062: return null;
063: }
064:
065: return auth.requestAuthentication(authenticatorTypes);
066: }
067:
068: /**
069: * converts a string to a char array (null safe)
070: */
071: public static char[] toChar(String string) {
072: if (string == null) {
073: return null;
074: }
075:
076: return string.toCharArray();
077: }
078:
079: /**
080: * cleanup the data in the UerAuthenticationData (null safe)
081: */
082: public static void cleanup(UserAuthenticationData authData) {
083: if (authData == null) {
084: return;
085: }
086:
087: authData.cleanup();
088: }
089:
090: /**
091: * converts the given data to a string (null safe)
092: */
093: public static String toString(char[] data) {
094: if (data == null) {
095: return null;
096: }
097:
098: return new String(data);
099: }
100: }
|