Home | History | Annotate | Download | only in auth
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java $
      3  * $Revision: 658430 $
      4  * $Date: 2008-05-20 14:04:27 -0700 (Tue, 20 May 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.auth;
     32 
     33 import java.security.Principal;
     34 
     35 import org.apache.http.util.LangUtils;
     36 
     37 /**
     38  * Username and password {@link Credentials}
     39  *
     40  * @author <a href="mailto:remm (at) apache.org">Remy Maucherat</a>
     41  * @author Sean C. Sullivan
     42  * @author <a href="mailto:mbowler (at) GargoyleSoftware.com">Mike Bowler</a>
     43  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     44  *
     45  * @version $Revision: 658430 $ $Date: 2008-05-20 14:04:27 -0700 (Tue, 20 May 2008) $
     46  *
     47  */
     48 public class UsernamePasswordCredentials implements Credentials {
     49 
     50     private final BasicUserPrincipal principal;
     51     private final String password;
     52 
     53     /**
     54      * The constructor with the username and password combined string argument.
     55      *
     56      * @param usernamePassword the username:password formed string
     57      * @see #toString
     58      */
     59     public UsernamePasswordCredentials(String usernamePassword) {
     60         super();
     61         if (usernamePassword == null) {
     62             throw new IllegalArgumentException("Username:password string may not be null");
     63         }
     64         int atColon = usernamePassword.indexOf(':');
     65         if (atColon >= 0) {
     66             this.principal = new BasicUserPrincipal(usernamePassword.substring(0, atColon));
     67             this.password = usernamePassword.substring(atColon + 1);
     68         } else {
     69             this.principal = new BasicUserPrincipal(usernamePassword);
     70             this.password = null;
     71         }
     72     }
     73 
     74 
     75     /**
     76      * The constructor with the username and password arguments.
     77      *
     78      * @param userName the user name
     79      * @param password the password
     80      */
     81     public UsernamePasswordCredentials(String userName, String password) {
     82         super();
     83         if (userName == null) {
     84             throw new IllegalArgumentException("Username may not be null");
     85         }
     86         this.principal = new BasicUserPrincipal(userName);
     87         this.password = password;
     88     }
     89 
     90     public Principal getUserPrincipal() {
     91         return this.principal;
     92     }
     93 
     94     public String getUserName() {
     95         return this.principal.getName();
     96     }
     97 
     98     public String getPassword() {
     99         return password;
    100     }
    101 
    102     @Override
    103     public int hashCode() {
    104         return this.principal.hashCode();
    105     }
    106 
    107     @Override
    108     public boolean equals(Object o) {
    109         if (o == null) return false;
    110         if (this == o) return true;
    111         if (o instanceof UsernamePasswordCredentials) {
    112             UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
    113             if (LangUtils.equals(this.principal, that.principal)) {
    114                 return true;
    115             }
    116         }
    117         return false;
    118     }
    119 
    120     @Override
    121     public String toString() {
    122         return this.principal.toString();
    123     }
    124 
    125 }
    126 
    127