001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.auth; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.apache.commons.vfs2.UserAuthenticationData; 022import org.apache.commons.vfs2.UserAuthenticator; 023import org.apache.commons.vfs2.util.UserAuthenticatorUtils; 024 025/** 026 * Provides always the same credentials data passed in with the constructor. 027 */ 028public class StaticUserAuthenticator implements UserAuthenticator, Comparable<StaticUserAuthenticator> { 029 private static final Log LOG = LogFactory.getLog(StaticUserAuthenticator.class); 030 031 /** The user name */ 032 private final String username; 033 034 /** The password */ 035 private final String password; 036 037 /** The user's domain */ 038 private final String domain; 039 040 public StaticUserAuthenticator(final String domain, final String username, final String password) { 041 this.username = username; 042 this.password = password; 043 this.domain = domain; 044 } 045 046 @Override 047 public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) { 048 final UserAuthenticationData data = new UserAuthenticationData(); 049 for (final UserAuthenticationData.Type type : types) { 050 if (type == UserAuthenticationData.DOMAIN) { 051 data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain)); 052 } else if (type == UserAuthenticationData.USERNAME) { 053 data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username)); 054 } else if (type == UserAuthenticationData.PASSWORD) { 055 data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password)); 056 } else { 057 if (LOG.isDebugEnabled()) { 058 LOG.debug(StaticUserAuthenticator.class.getSimpleName() 059 + " does not support authentication data type '" + type 060 + "'; authentication request for this type ignored."); 061 } 062 } 063 } 064 return data; 065 } 066 067 /** 068 * {@inheritDoc} 069 * 070 * @since 2.0 071 */ 072 @Override 073 public int hashCode() { 074 final int prime = 37; 075 int result = 1; 076 result = prime * result + (domain == null ? 0 : domain.hashCode()); 077 result = prime * result + (password == null ? 0 : password.hashCode()); 078 result = prime * result + (username == null ? 0 : username.hashCode()); 079 080 return result; 081 } 082 083 /** 084 * {@inheritDoc} 085 * 086 * @since 2.0 087 */ 088 @Override 089 public boolean equals(final Object obj) { 090 if (this == obj) { 091 return true; 092 } 093 094 if (obj == null) { 095 return false; 096 } 097 098 if (getClass() != obj.getClass()) { 099 return false; 100 } 101 102 final StaticUserAuthenticator other = (StaticUserAuthenticator) obj; 103 return equalsNullsafe(domain, other.domain) && equalsNullsafe(username, other.username) 104 && equalsNullsafe(password, other.password); 105 } 106 107 private boolean equalsNullsafe(final String thisString, final String otherString) { 108 if (thisString == null) { 109 if (otherString != null) { 110 return false; 111 } 112 } else if (!thisString.equals(otherString)) { 113 return false; 114 } 115 return true; 116 } 117 118 /** 119 * {@inheritDoc} 120 * 121 * @since 2.0 122 */ 123 @Override 124 public int compareTo(final StaticUserAuthenticator other) { 125 int result = compareStringOrNull(domain, other.domain); 126 result = result == 0 ? compareStringOrNull(username, other.username) : result; 127 result = result == 0 ? compareStringOrNull(password, other.password) : result; 128 129 return result; 130 } 131 132 private int compareStringOrNull(final String thisString, final String otherString) { 133 if (thisString == null) { 134 if (otherString != null) { 135 return -1; 136 } 137 } else { 138 if (otherString == null) { 139 return 1; 140 } 141 142 final int result = thisString.compareTo(otherString); 143 if (result != 0) { 144 return result; 145 } 146 } 147 148 return 0; 149 } 150 151 /** 152 * {@inheritDoc} 153 * 154 * @since 2.0 155 */ 156 @Override 157 public String toString() { 158 final StringBuilder buffer = new StringBuilder(); 159 if (domain != null) { 160 buffer.append(domain).append('\\'); 161 } 162 if (username != null) { 163 buffer.append(username); 164 } else { 165 buffer.append("(null)"); 166 } 167 if (password != null) { 168 buffer.append(":***"); 169 } 170 return buffer.toString(); 171 } 172}