michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.auth; michael@0: michael@0: import java.io.Serializable; michael@0: import java.security.Principal; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.util.LangUtils; michael@0: michael@0: /** michael@0: * Simple {@link Credentials} implementation based on a user name / password michael@0: * pair. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class UsernamePasswordCredentials implements Credentials, Serializable { michael@0: michael@0: private static final long serialVersionUID = 243343858802739403L; michael@0: michael@0: private final BasicUserPrincipal principal; michael@0: private final String password; michael@0: michael@0: /** michael@0: * The constructor with the username and password combined string argument. michael@0: * michael@0: * @param usernamePassword the username:password formed string michael@0: * @see #toString michael@0: */ michael@0: public UsernamePasswordCredentials(String usernamePassword) { michael@0: super(); michael@0: if (usernamePassword == null) { michael@0: throw new IllegalArgumentException("Username:password string may not be null"); michael@0: } michael@0: int atColon = usernamePassword.indexOf(':'); michael@0: if (atColon >= 0) { michael@0: this.principal = new BasicUserPrincipal(usernamePassword.substring(0, atColon)); michael@0: this.password = usernamePassword.substring(atColon + 1); michael@0: } else { michael@0: this.principal = new BasicUserPrincipal(usernamePassword); michael@0: this.password = null; michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * The constructor with the username and password arguments. michael@0: * michael@0: * @param userName the user name michael@0: * @param password the password michael@0: */ michael@0: public UsernamePasswordCredentials(String userName, String password) { michael@0: super(); michael@0: if (userName == null) { michael@0: throw new IllegalArgumentException("Username may not be null"); michael@0: } michael@0: this.principal = new BasicUserPrincipal(userName); michael@0: this.password = password; michael@0: } michael@0: michael@0: public Principal getUserPrincipal() { michael@0: return this.principal; michael@0: } michael@0: michael@0: public String getUserName() { michael@0: return this.principal.getName(); michael@0: } michael@0: michael@0: public String getPassword() { michael@0: return password; michael@0: } michael@0: michael@0: @Override michael@0: public int hashCode() { michael@0: return this.principal.hashCode(); michael@0: } michael@0: michael@0: @Override michael@0: public boolean equals(Object o) { michael@0: if (this == o) return true; michael@0: if (o instanceof UsernamePasswordCredentials) { michael@0: UsernamePasswordCredentials that = (UsernamePasswordCredentials) o; michael@0: if (LangUtils.equals(this.principal, that.principal)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return this.principal.toString(); michael@0: } michael@0: michael@0: } michael@0: