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.auth;
18  
19  import java.util.Objects;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.commons.vfs2.UserAuthenticationData;
24  import org.apache.commons.vfs2.UserAuthenticator;
25  import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
26  
27  /**
28   * Provides always the same credentials data passed in with the constructor.
29   */
30  public class StaticUserAuthenticator implements UserAuthenticator, Comparable<StaticUserAuthenticator> {
31  
32      private static final Log LOG = LogFactory.getLog(StaticUserAuthenticator.class);
33  
34      /** The user name */
35      private final String userName;
36  
37      /** The password */
38      private final String password;
39  
40      /** The user domain */
41      private final String domain;
42  
43      /**
44       * Constructs a new instance.
45       *
46       * @param domain The user domain.
47       * @param userName The user name.
48       * @param password The user password.
49       */
50      public StaticUserAuthenticator(final String domain, final String userName, final String password) {
51          this.userName = userName;
52          this.password = password;
53          this.domain = domain;
54      }
55  
56      private int compareStringOrNull(final String thisString, final String otherString) {
57          if (thisString != null) {
58              if (otherString == null) {
59                  return 1;
60              }
61  
62              return thisString.compareTo(otherString);
63          }
64          if (otherString != null) {
65              return -1;
66          }
67  
68          return 0;
69      }
70  
71      /**
72       * {@inheritDoc}
73       *
74       * @since 2.0
75       */
76      @Override
77      public int compareTo(final StaticUserAuthenticator other) {
78          int result = compareStringOrNull(domain, other.domain);
79          result = result == 0 ? compareStringOrNull(userName, other.userName) : result;
80          return result == 0 ? compareStringOrNull(password, other.password) : result;
81      }
82  
83      /**
84       * {@inheritDoc}
85       *
86       * @since 2.0
87       */
88      @Override
89      public boolean equals(final Object obj) {
90          if (this == obj) {
91              return true;
92          }
93  
94          if (obj == null) {
95              return false;
96          }
97  
98          if (getClass() != obj.getClass()) {
99              return false;
100         }
101 
102         final StaticUserAuthenticator other = (StaticUserAuthenticator) obj;
103         return Objects.equals(domain, other.domain) && Objects.equals(userName, other.userName)
104             && Objects.equals(password, other.password);
105     }
106 
107     /**
108      * {@inheritDoc}
109      *
110      * @since 2.0
111      */
112     @Override
113     public int hashCode() {
114         final int prime = 37;
115         int result = 1;
116         result = prime * result + (domain == null ? 0 : domain.hashCode());
117         result = prime * result + (password == null ? 0 : password.hashCode());
118         return prime * result + (userName == null ? 0 : userName.hashCode());
119     }
120 
121     @Override
122     public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
123         final UserAuthenticationData data = new UserAuthenticationData();
124         for (final UserAuthenticationData.Type type : types) {
125             if (type == UserAuthenticationData.DOMAIN) {
126                 data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
127             } else if (type == UserAuthenticationData.USERNAME) {
128                 data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(userName));
129             } else if (type == UserAuthenticationData.PASSWORD) {
130                 data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
131             } else if (LOG.isDebugEnabled()) {
132                 LOG.debug(StaticUserAuthenticator.class.getSimpleName()
133                         + " does not support authentication data type '" + type
134                         + "'; authentication request for this type ignored.");
135             }
136         }
137         return data;
138     }
139 
140     /**
141      * {@inheritDoc}
142      *
143      * @since 2.0
144      */
145     @Override
146     public String toString() {
147         final StringBuilder buffer = new StringBuilder();
148         if (domain != null) {
149             buffer.append(domain).append('\\');
150         }
151         if (userName != null) {
152             buffer.append(userName);
153         } else {
154             buffer.append("(null)");
155         }
156         if (password != null) {
157             buffer.append(":***");
158         }
159         return buffer.toString();
160     }
161 }