Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpPrincipal.java
41159 views
1
/*
2
* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.net.httpserver;
27
import java.security.Principal;
28
29
/**
30
* Represents a user authenticated by HTTP Basic or Digest
31
* authentication.
32
*/
33
public class HttpPrincipal implements Principal {
34
private String username, realm;
35
36
/**
37
* Creates a {@code HttpPrincipal} from the given {@code username} and
38
* {@code realm}.
39
*
40
* @param username the name of the user within the realm
41
* @param realm the realm for this user
42
* @throws NullPointerException if either username or realm are {@code null}
43
*/
44
public HttpPrincipal(String username, String realm) {
45
if (username == null || realm == null) {
46
throw new NullPointerException();
47
}
48
this.username = username;
49
this.realm = realm;
50
}
51
52
/**
53
* Compare two instances of {@code HttpPrincipal}. Returns {@code true} if
54
* <i>another</i> is an instance of {@code HttpPrincipal}, and its username
55
* and realm are equal to this object's username and realm. Returns {@code false}
56
* otherwise.
57
*
58
* @param another the object to compare this instance of {@code HttpPrincipal} against
59
* @return {@code true} or {@code false} depending on whether objects are
60
* equal or not
61
*/
62
public boolean equals(Object another) {
63
if (!(another instanceof HttpPrincipal)) {
64
return false;
65
}
66
HttpPrincipal theother = (HttpPrincipal)another;
67
return (username.equals(theother.username) &&
68
realm.equals(theother.realm));
69
}
70
71
/**
72
* Returns the contents of this principal in the form
73
* <i>realm:username</i>.
74
*
75
* @return the contents of this principal in the form realm:username
76
*/
77
public String getName() {
78
return String.format("%s:%s", realm, username);
79
}
80
81
/**
82
* Returns the {@code username} this object was created with.
83
*
84
* @return the name of the user associated with this object
85
*/
86
public String getUsername() {
87
return username;
88
}
89
90
/**
91
* Returns the {@code realm} this object was created with.
92
*
93
* @return the realm associated with this object
94
*/
95
public String getRealm() {
96
return realm;
97
}
98
99
/**
100
* Returns a hashcode for this {@code HttpPrincipal}. This is calculated
101
* as {@code (getUsername()+getRealm()).hashCode()}.
102
*
103
* @return the hashcode for this object
104
*/
105
public int hashCode() {
106
return (username+realm).hashCode();
107
}
108
109
/**
110
* Returns the same string as {@link #getName()}.
111
*
112
* @return the name associated with this object
113
*/
114
public String toString() {
115
return getName();
116
}
117
}
118
119