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
31package org.apache.http.auth;
32
33import java.security.Principal;
34
35import org.apache.http.util.LangUtils;
36
37/**
38 * Username and password {@link Credentials}
39 *
40 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
41 * @author Sean C. Sullivan
42 * @author <a href="mailto:mbowler@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 * @deprecated Please use {@link java.net.URL#openConnection} instead.
49 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
50 *     for further details.
51 */
52@Deprecated
53public class UsernamePasswordCredentials implements Credentials {
54
55    private final BasicUserPrincipal principal;
56    private final String password;
57
58    /**
59     * The constructor with the username and password combined string argument.
60     *
61     * @param usernamePassword the username:password formed string
62     * @see #toString
63     */
64    public UsernamePasswordCredentials(String usernamePassword) {
65        super();
66        if (usernamePassword == null) {
67            throw new IllegalArgumentException("Username:password string may not be null");
68        }
69        int atColon = usernamePassword.indexOf(':');
70        if (atColon >= 0) {
71            this.principal = new BasicUserPrincipal(usernamePassword.substring(0, atColon));
72            this.password = usernamePassword.substring(atColon + 1);
73        } else {
74            this.principal = new BasicUserPrincipal(usernamePassword);
75            this.password = null;
76        }
77    }
78
79
80    /**
81     * The constructor with the username and password arguments.
82     *
83     * @param userName the user name
84     * @param password the password
85     */
86    public UsernamePasswordCredentials(String userName, String password) {
87        super();
88        if (userName == null) {
89            throw new IllegalArgumentException("Username may not be null");
90        }
91        this.principal = new BasicUserPrincipal(userName);
92        this.password = password;
93    }
94
95    public Principal getUserPrincipal() {
96        return this.principal;
97    }
98
99    public String getUserName() {
100        return this.principal.getName();
101    }
102
103    public String getPassword() {
104        return password;
105    }
106
107    @Override
108    public int hashCode() {
109        return this.principal.hashCode();
110    }
111
112    @Override
113    public boolean equals(Object o) {
114        if (o == null) return false;
115        if (this == o) return true;
116        if (o instanceof UsernamePasswordCredentials) {
117            UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
118            if (LangUtils.equals(this.principal, that.principal)) {
119                return true;
120            }
121        }
122        return false;
123    }
124
125    @Override
126    public String toString() {
127        return this.principal.toString();
128    }
129
130}
131
132