001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.auth; 028 029import java.io.Serializable; 030import java.security.Principal; 031import java.util.Locale; 032 033import org.apache.http.annotation.Contract; 034import org.apache.http.annotation.ThreadingBehavior; 035import org.apache.http.util.Args; 036import org.apache.http.util.LangUtils; 037 038/** 039 * Microsoft Windows specific user principal implementation. 040 * 041 * @since 4.0 042 */ 043@Contract(threading = ThreadingBehavior.IMMUTABLE) 044public class NTUserPrincipal implements Principal, Serializable { 045 046 private static final long serialVersionUID = -6870169797924406894L; 047 048 private final String username; 049 private final String domain; 050 private final String ntname; 051 052 public NTUserPrincipal( 053 final String domain, 054 final String username) { 055 super(); 056 Args.notNull(username, "User name"); 057 this.username = username; 058 if (domain != null) { 059 this.domain = domain.toUpperCase(Locale.ROOT); 060 } else { 061 this.domain = null; 062 } 063 if (this.domain != null && !this.domain.isEmpty()) { 064 final StringBuilder buffer = new StringBuilder(); 065 buffer.append(this.domain); 066 buffer.append('\\'); 067 buffer.append(this.username); 068 this.ntname = buffer.toString(); 069 } else { 070 this.ntname = this.username; 071 } 072 } 073 074 @Override 075 public String getName() { 076 return this.ntname; 077 } 078 079 public String getDomain() { 080 return this.domain; 081 } 082 083 public String getUsername() { 084 return this.username; 085 } 086 087 @Override 088 public int hashCode() { 089 int hash = LangUtils.HASH_SEED; 090 hash = LangUtils.hashCode(hash, this.username); 091 hash = LangUtils.hashCode(hash, this.domain); 092 return hash; 093 } 094 095 @Override 096 public boolean equals(final Object o) { 097 if (this == o) { 098 return true; 099 } 100 if (o instanceof NTUserPrincipal) { 101 final NTUserPrincipal that = (NTUserPrincipal) o; 102 if (LangUtils.equals(this.username, that.username) 103 && LangUtils.equals(this.domain, that.domain)) { 104 return true; 105 } 106 } 107 return false; 108 } 109 110 @Override 111 public String toString() { 112 return this.ntname; 113 } 114 115}