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's domain */
41      private final String domain;
42  
43      public StaticUserAuthenticator(final String domain, final String username, final String password) {
44          this.username = username;
45          this.password = password;
46          this.domain = domain;
47      }
48  
49      @Override
50      public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
51          final UserAuthenticationDataionData.html#UserAuthenticationData">UserAuthenticationData data = new UserAuthenticationData();
52          for (final UserAuthenticationData.Type type : types) {
53              if (type == UserAuthenticationData.DOMAIN) {
54                  data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
55              } else if (type == UserAuthenticationData.USERNAME) {
56                  data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
57              } else if (type == UserAuthenticationData.PASSWORD) {
58                  data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
59              } else if (LOG.isDebugEnabled()) {
60                  LOG.debug(StaticUserAuthenticator.class.getSimpleName()
61                          + " does not support authentication data type '" + type
62                          + "'; authentication request for this type ignored.");
63              }
64          }
65          return data;
66      }
67  
68      /**
69       * {@inheritDoc}
70       *
71       * @since 2.0
72       */
73      @Override
74      public int hashCode() {
75          final int prime = 37;
76          int result = 1;
77          result = prime * result + (domain == null ? 0 : domain.hashCode());
78          result = prime * result + (password == null ? 0 : password.hashCode());
79          result = prime * result + (username == null ? 0 : username.hashCode());
80  
81          return result;
82      }
83  
84      /**
85       * {@inheritDoc}
86       *
87       * @since 2.0
88       */
89      @Override
90      public boolean equals(final Object obj) {
91          if (this == obj) {
92              return true;
93          }
94  
95          if (obj == null) {
96              return false;
97          }
98  
99          if (getClass() != obj.getClass()) {
100             return false;
101         }
102 
103         final StaticUserAuthenticatorrg/apache/commons/vfs2/auth/StaticUserAuthenticator.html#StaticUserAuthenticator">StaticUserAuthenticator other = (StaticUserAuthenticator) obj;
104         return Objects.equals(domain, other.domain) && Objects.equals(username, other.username)
105             && Objects.equals(password, other.password);
106     }
107 
108     /**
109      * {@inheritDoc}
110      *
111      * @since 2.0
112      */
113     @Override
114     public int compareTo(final StaticUserAuthenticator other) {
115         int result = compareStringOrNull(domain, other.domain);
116         result = result == 0 ? compareStringOrNull(username, other.username) : result;
117         result = result == 0 ? compareStringOrNull(password, other.password) : result;
118 
119         return result;
120     }
121 
122     private int compareStringOrNull(final String thisString, final String otherString) {
123         if (thisString != null) {
124             if (otherString == null) {
125                 return 1;
126             }
127 
128             return thisString.compareTo(otherString);
129         }
130         if (otherString != null) {
131             return -1;
132         }
133 
134         return 0;
135     }
136 
137     /**
138      * {@inheritDoc}
139      *
140      * @since 2.0
141      */
142     @Override
143     public String toString() {
144         final StringBuilder buffer = new StringBuilder();
145         if (domain != null) {
146             buffer.append(domain).append('\\');
147         }
148         if (username != null) {
149             buffer.append(username);
150         } else {
151             buffer.append("(null)");
152         }
153         if (password != null) {
154             buffer.append(":***");
155         }
156         return buffer.toString();
157     }
158 }