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.Arrays;
20  import java.util.Map;
21  import java.util.Objects;
22  import java.util.TreeMap;
23  
24  /**
25   * Contains various authentication data.
26   */
27  public class UserAuthenticationData {
28  
29      /**
30       * Represents a user authentication item.
31       */
32      public static class Type implements Comparable<Type> {
33  
34          /** The type name */
35          private final String type;
36  
37          /**
38           * Creates a new Type.
39           *
40           * @param type the type
41           */
42          public Type(final String type) {
43              this.type = type;
44          }
45  
46          @Override
47          public boolean equals(final Object o) {
48              if (this == o) {
49                  return true;
50              }
51              if (o == null || getClass() != o.getClass()) {
52                  return false;
53              }
54              return Objects.equals(type, ((Type) o).type);
55          }
56  
57          @Override
58          public int compareTo(final Type o) {
59              return type.compareTo(o.type);
60          }
61  
62          /**
63           * @return The hash code.
64           * @since 2.0
65           */
66          @Override
67          public int hashCode() {
68              return type != null ? type.hashCode() : 0;
69          }
70  
71          /**
72           * @return The type.
73           * @since 2.0
74           */
75          @Override
76          public String toString() {
77              return type;
78          }
79      }
80  
81      /** The user name. */
82      public static final Type USERNAME = new Type("username");
83  
84      /** The password. */
85      public static final Type PASSWORD = new Type("password");
86  
87      /** The user's domain. */
88      public static final Type DOMAIN = new Type("domain");
89  
90      /** The authentication data. */
91      private final Map<Type, char[]> authenticationData = new TreeMap<>();
92  
93      /**
94       * Creates a new uninitialized instance.
95       */
96      public UserAuthenticationData() {
97          // do nothing
98      }
99  
100     /**
101      * Sets a data to this collection.
102      *
103      * @param type The Type to add
104      * @param data The data associated with the Type
105      */
106     public void setData(final Type type, final char[] data) {
107         authenticationData.put(type, data);
108     }
109 
110     /**
111      * Gets a data from the collection.
112      *
113      * @param type The Type to retrieve.
114      * @return a character array containing the data associated with the type.
115      */
116     public char[] getData(final Type type) {
117         return authenticationData.get(type);
118     }
119 
120     /**
121      * Deletes all data stored within this authenticator.
122      */
123     public void cleanup() {
124         // step 1: nullify character buffers
125         for (final char[] data : authenticationData.values()) {
126             if (data == null) {
127                 continue;
128             }
129 
130             Arrays.fill(data, (char) 0);
131         }
132         // step 2: allow data itself to gc
133         authenticationData.clear();
134     }
135 }