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; 018 019import java.util.Iterator; 020import java.util.Map; 021import java.util.TreeMap; 022 023/** 024 * Contains various authentication data. 025 */ 026public class UserAuthenticationData { 027 /** 028 * Represents a user authentication item. 029 */ 030 public static class Type implements Comparable<Type> { 031 /** The type name */ 032 private final String type; 033 034 /** 035 * Creates a new Type. 036 * 037 * @param type the type 038 */ 039 public Type(final String type) { 040 this.type = type; 041 } 042 043 @Override 044 public boolean equals(final Object o) { 045 if (this == o) { 046 return true; 047 } 048 if (o == null || getClass() != o.getClass()) { 049 return false; 050 } 051 052 final Type type1 = (Type) o; 053 054 if (type != null ? !type.equals(type1.type) : type1.type != null) { 055 return false; 056 } 057 058 return true; 059 } 060 061 @Override 062 public int compareTo(final Type o) { 063 return type.compareTo(o.type); 064 } 065 066 /** 067 * @return The hash code. 068 * @since 2.0 069 */ 070 @Override 071 public int hashCode() { 072 return type != null ? type.hashCode() : 0; 073 } 074 075 /** 076 * @return The type. 077 * @since 2.0 078 */ 079 @Override 080 public String toString() { 081 return type; 082 } 083 } 084 085 /** The user name. */ 086 public static final Type USERNAME = new Type("username"); 087 088 /** The password. */ 089 public static final Type PASSWORD = new Type("password"); 090 091 /** The user's domain. */ 092 public static final Type DOMAIN = new Type("domain"); 093 094 /** The authentication data. */ 095 private final Map<Type, char[]> authenticationData = new TreeMap<>(); 096 097 /** 098 * Creates a new uninitialized instance. 099 */ 100 public UserAuthenticationData() { 101 // do nothing 102 } 103 104 /** 105 * Sets a data to this collection. 106 * 107 * @param type The Type to add 108 * @param data The data associated with the Type 109 */ 110 public void setData(final Type type, final char[] data) { 111 authenticationData.put(type, data); 112 } 113 114 /** 115 * Gets a data from the collection. 116 * 117 * @param type The Type to retrieve. 118 * @return a character array containing the data associated with the type. 119 */ 120 public char[] getData(final Type type) { 121 return authenticationData.get(type); 122 } 123 124 /** 125 * Deletes all data stored within this authenticator. 126 */ 127 public void cleanup() { 128 // step 1: nullify character buffers 129 final Iterator<char[]> iterAuthenticationData = authenticationData.values().iterator(); 130 while (iterAuthenticationData.hasNext()) { 131 final char[] data = iterAuthenticationData.next(); 132 if (data == null || data.length < 0) { 133 continue; 134 } 135 136 for (int i = 0; i < data.length; i++) { 137 data[i] = 0; 138 } 139 } 140 // step 2: allow data itself to gc 141 authenticationData.clear(); 142 } 143}