Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpPrincipal.java
41159 views
/*1* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.net.httpserver;26import java.security.Principal;2728/**29* Represents a user authenticated by HTTP Basic or Digest30* authentication.31*/32public class HttpPrincipal implements Principal {33private String username, realm;3435/**36* Creates a {@code HttpPrincipal} from the given {@code username} and37* {@code realm}.38*39* @param username the name of the user within the realm40* @param realm the realm for this user41* @throws NullPointerException if either username or realm are {@code null}42*/43public HttpPrincipal(String username, String realm) {44if (username == null || realm == null) {45throw new NullPointerException();46}47this.username = username;48this.realm = realm;49}5051/**52* Compare two instances of {@code HttpPrincipal}. Returns {@code true} if53* <i>another</i> is an instance of {@code HttpPrincipal}, and its username54* and realm are equal to this object's username and realm. Returns {@code false}55* otherwise.56*57* @param another the object to compare this instance of {@code HttpPrincipal} against58* @return {@code true} or {@code false} depending on whether objects are59* equal or not60*/61public boolean equals(Object another) {62if (!(another instanceof HttpPrincipal)) {63return false;64}65HttpPrincipal theother = (HttpPrincipal)another;66return (username.equals(theother.username) &&67realm.equals(theother.realm));68}6970/**71* Returns the contents of this principal in the form72* <i>realm:username</i>.73*74* @return the contents of this principal in the form realm:username75*/76public String getName() {77return String.format("%s:%s", realm, username);78}7980/**81* Returns the {@code username} this object was created with.82*83* @return the name of the user associated with this object84*/85public String getUsername() {86return username;87}8889/**90* Returns the {@code realm} this object was created with.91*92* @return the realm associated with this object93*/94public String getRealm() {95return realm;96}9798/**99* Returns a hashcode for this {@code HttpPrincipal}. This is calculated100* as {@code (getUsername()+getRealm()).hashCode()}.101*102* @return the hashcode for this object103*/104public int hashCode() {105return (username+realm).hashCode();106}107108/**109* Returns the same string as {@link #getName()}.110*111* @return the name associated with this object112*/113public String toString() {114return getName();115}116}117118119