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