View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.TreeMap;
22  
23  /**
24   * Contains various authentication data.
25   */
26  public class UserAuthenticationData {
27  
28      /**
29       * Represents a user authentication item.
30       */
31      public static class Type implements Comparable<Type> {
32          /** The type name */
33          private final String type;
34  
35          /**
36           * Creates a new Type.
37           *
38           * @param type the type
39           */
40          public Type(final String type) {
41              this.type = type;
42          }
43  
44          @Override
45          public boolean equals(final Object o) {
46              if (this == o) {
47                  return true;
48              }
49              if (o == null || getClass() != o.getClass()) {
50                  return false;
51              }
52  
53              final Type type1 = (Type) o;
54  
55              if (type != null ? !type.equals(type1.type) : type1.type != null) {
56                  return false;
57              }
58  
59              return true;
60          }
61  
62          @Override
63          public int compareTo(final Type o) {
64              return type.compareTo(o.type);
65          }
66  
67          /**
68           * @return The hash code.
69           * @since 2.0
70           */
71          @Override
72          public int hashCode() {
73              return type != null ? type.hashCode() : 0;
74          }
75  
76          /**
77           * @return The type.
78           * @since 2.0
79           */
80          @Override
81          public String toString() {
82              return type;
83          }
84      }
85  
86      /** The user name. */
87      public static final Type USERNAME = new Type("username");
88  
89      /** The password. */
90      public static final Type PASSWORD = new Type("password");
91  
92      /** The user's domain. */
93      public static final Type DOMAIN = new Type("domain");
94  
95      /** The authentication data. */
96      private final Map<Type, char[]> authenticationData = new TreeMap<>();
97  
98      /**
99       * Creates a new uninitialized instance.
100      */
101     public UserAuthenticationData() {
102         // do nothing
103     }
104 
105     /**
106      * Sets a data to this collection.
107      *
108      * @param type The Type to add
109      * @param data The data associated with the Type
110      */
111     public void setData(final Type type, final char[] data) {
112         authenticationData.put(type, data);
113     }
114 
115     /**
116      * Gets a data from the collection.
117      *
118      * @param type The Type to retrieve.
119      * @return a character array containing the data associated with the type.
120      */
121     public char[] getData(final Type type) {
122         return authenticationData.get(type);
123     }
124 
125     /**
126      * Deletes all data stored within this authenticator.
127      */
128     public void cleanup() {
129         // step 1: nullify character buffers
130         final Iterator<char[]> iterAuthenticationData = authenticationData.values().iterator();
131         while (iterAuthenticationData.hasNext()) {
132             final char[] data = iterAuthenticationData.next();
133             if (data == null) {
134                 continue;
135             }
136 
137             for (int i = 0; i < data.length; i++) {
138                 data[i] = 0;
139             }
140         }
141         // step 2: allow data itself to gc
142         authenticationData.clear();
143     }
144 }