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: *
017: */
018: package org.apache.ivy.util;
019:
020: /**
021: *
022: */
023: public class Credentials {
024: private String realm;
025:
026: private String host;
027:
028: private String userName;
029:
030: private String passwd;
031:
032: public Credentials(String realm, String host, String userName,
033: String passwd) {
034: this .realm = realm;
035: this .host = host;
036: this .userName = userName;
037: this .passwd = passwd;
038: }
039:
040: public String getHost() {
041: return host;
042: }
043:
044: public String getPasswd() {
045: return passwd;
046: }
047:
048: public String getRealm() {
049: return realm;
050: }
051:
052: public String getUserName() {
053: return userName;
054: }
055:
056: public static String buildKey(String realm, String host) {
057: if (realm == null || "".equals(realm.trim())) {
058: return host;
059: } else {
060: return realm + "@" + host;
061: }
062: }
063:
064: /**
065: * Return a string that can be used for debug purpose. It contains only stars for each password
066: * character.
067: */
068: public String toString() {
069: return getKey() + " " + getUserName() + "/"
070: + getPasswdAsStars();
071: }
072:
073: private String getPasswdAsStars() {
074: if (passwd == null) {
075: return null;
076: }
077: StringBuffer sb = new StringBuffer();
078: for (int i = passwd.length(); i > 0; i--) {
079: sb.append('*');
080: }
081: return sb.toString();
082: }
083:
084: public boolean equals(Object o) {
085: if (o == null) {
086: return false;
087: }
088:
089: if (o instanceof Credentials) {
090: Credentials c = (Credentials) o;
091: return getKey().equals(c.getKey());
092: }
093:
094: return false;
095: }
096:
097: public int hashCode() {
098: return getKey().hashCode();
099: }
100:
101: public String getKey() {
102: return buildKey(realm, host);
103: }
104: }
|