blob: 418a8edd9c894eacc13bb21378bf7ca8a4fd2ca7 [file] [log] [blame]
/*
* 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;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
/**
* Contains various authentication data.
*/
public class UserAuthenticationData {
/**
* Represents a user authentication item.
*/
public static class Type implements Comparable<Type> {
/** The type name */
private final String type;
/**
* Creates a new Type.
*
* @param type the type
*/
public Type(final String type) {
this.type = type;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return Objects.equals(type, ((Type) o).type);
}
@Override
public int compareTo(final Type o) {
return type.compareTo(o.type);
}
/**
* @return The hash code.
* @since 2.0
*/
@Override
public int hashCode() {
return type != null ? type.hashCode() : 0;
}
/**
* @return The type.
* @since 2.0
*/
@Override
public String toString() {
return type;
}
}
/** The user name. */
public static final Type USERNAME = new Type("username");
/** The password. */
public static final Type PASSWORD = new Type("password");
/** The user's domain. */
public static final Type DOMAIN = new Type("domain");
/** The authentication data. */
private final Map<Type, char[]> authenticationData = new TreeMap<>();
/**
* Creates a new uninitialized instance.
*/
public UserAuthenticationData() {
// do nothing
}
/**
* Sets a data to this collection.
*
* @param type The Type to add
* @param data The data associated with the Type
*/
public void setData(final Type type, final char[] data) {
authenticationData.put(type, data);
}
/**
* Gets a data from the collection.
*
* @param type The Type to retrieve.
* @return a character array containing the data associated with the type.
*/
public char[] getData(final Type type) {
return authenticationData.get(type);
}
/**
* Deletes all data stored within this authenticator.
*/
public void cleanup() {
// step 1: nullify character buffers
for (final char[] data : authenticationData.values()) {
if (data == null) {
continue;
}
Arrays.fill(data, (char) 0);
}
// step 2: allow data itself to gc
authenticationData.clear();
}
}