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 java.util.Objects; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.apache.commons.vfs2.UserAuthenticationData; 024import org.apache.commons.vfs2.UserAuthenticator; 025import org.apache.commons.vfs2.util.UserAuthenticatorUtils; 026 027/** 028 * Provides always the same credentials data passed in with the constructor. 029 */ 030public class StaticUserAuthenticator implements UserAuthenticator, Comparable<StaticUserAuthenticator> { 031 032 private static final Log LOG = LogFactory.getLog(StaticUserAuthenticator.class); 033 034 /** The user name */ 035 private final String userName; 036 037 /** The password */ 038 private final String password; 039 040 /** The user domain */ 041 private final String domain; 042 043 /** 044 * Constructs a new instance. 045 * 046 * @param domain The user domain. 047 * @param userName The user name. 048 * @param password The user password. 049 */ 050 public StaticUserAuthenticator(final String domain, final String userName, final String password) { 051 this.userName = userName; 052 this.password = password; 053 this.domain = domain; 054 } 055 056 private int compareStringOrNull(final String thisString, final String otherString) { 057 if (thisString != null) { 058 if (otherString == null) { 059 return 1; 060 } 061 062 return thisString.compareTo(otherString); 063 } 064 if (otherString != null) { 065 return -1; 066 } 067 068 return 0; 069 } 070 071 /** 072 * {@inheritDoc} 073 * 074 * @since 2.0 075 */ 076 @Override 077 public int compareTo(final StaticUserAuthenticator other) { 078 int result = compareStringOrNull(domain, other.domain); 079 result = result == 0 ? compareStringOrNull(userName, other.userName) : result; 080 return result == 0 ? compareStringOrNull(password, other.password) : 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 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}