use of javax.naming.InvalidNameException in project nifi-registry by apache.
the class CertificateUtils method compareDNs.
/**
* Returns true if the two provided DNs are equivalent, regardless of the order of the elements. Returns false if one or both are invalid DNs.
*
* Example:
*
* CN=test1, O=testOrg, C=US compared to CN=test1, O=testOrg, C=US -> true
* CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test1, C=US -> true
* CN=test1, O=testOrg, C=US compared to CN=test2, O=testOrg, C=US -> false
* CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test2, C=US -> false
* CN=test1, O=testOrg, C=US compared to -> false
* compared to -> true
*
* @param dn1 the first DN to compare
* @param dn2 the second DN to compare
* @return true if the DNs are equivalent, false otherwise
*/
public static boolean compareDNs(String dn1, String dn2) {
if (dn1 == null) {
dn1 = "";
}
if (dn2 == null) {
dn2 = "";
}
if (StringUtils.isEmpty(dn1) || StringUtils.isEmpty(dn2)) {
return dn1.equals(dn2);
}
try {
List<Rdn> rdn1 = new LdapName(dn1).getRdns();
List<Rdn> rdn2 = new LdapName(dn2).getRdns();
return rdn1.size() == rdn2.size() && rdn1.containsAll(rdn2);
} catch (InvalidNameException e) {
logger.warn("Cannot compare DNs: {} and {} because one or both is not a valid DN", dn1, dn2);
return false;
}
}
use of javax.naming.InvalidNameException in project druid by druid-io.
the class LDAPRoleProvider method getRoles.
@VisibleForTesting
public Set<String> getRoles(Map<String, BasicAuthorizerGroupMapping> groupMappingMap, Set<LdapName> groupNamesFromLdap) {
Set<String> roles = new HashSet<>();
if (groupMappingMap.size() == 0) {
return roles;
}
for (LdapName groupName : groupNamesFromLdap) {
for (Map.Entry<String, BasicAuthorizerGroupMapping> groupMappingEntry : groupMappingMap.entrySet()) {
BasicAuthorizerGroupMapping groupMapping = groupMappingEntry.getValue();
String mask = groupMapping.getGroupPattern();
try {
if (mask.startsWith("*,")) {
LdapName ln = new LdapName(mask.substring(2));
if (groupName.startsWith(ln)) {
roles.addAll(groupMapping.getRoles());
}
} else if (mask.endsWith(",*")) {
LdapName ln = new LdapName(mask.substring(0, mask.length() - 2));
if (groupName.endsWith(ln)) {
roles.addAll(groupMapping.getRoles());
}
} else {
LdapName ln = new LdapName(mask);
if (groupName.equals(ln)) {
roles.addAll(groupMapping.getRoles());
}
}
} catch (InvalidNameException e) {
throw new RuntimeException(String.format(Locale.getDefault(), "Configuration problem - Invalid groupMapping '%s'", mask));
}
}
}
return roles;
}
use of javax.naming.InvalidNameException in project druid by druid-io.
the class LDAPRoleProvider method getGroupsFromLdap.
Set<LdapName> getGroupsFromLdap(SearchResult userResult) throws NamingException {
Set<LdapName> groups = new TreeSet<>();
Attribute memberOf = userResult.getAttributes().get("memberOf");
if (memberOf == null) {
LOG.debug("No memberOf attributes");
// not part of any groups
return groups;
}
for (int i = 0; i < memberOf.size(); i++) {
String memberDn = memberOf.get(i).toString();
LdapName ln;
try {
ln = new LdapName(memberDn);
} catch (InvalidNameException e) {
LOG.debug("Invalid LDAP name: %s", memberDn);
continue;
}
if (this.groupFilters != null) {
if (allowedLdapGroup(ln, new TreeSet<>(Arrays.asList(this.groupFilters)))) {
groups.add(ln);
}
} else {
groups.add(ln);
}
}
return groups;
}
use of javax.naming.InvalidNameException in project Towny by ElgarL.
the class NationCommand method newNation.
/**
* Create a new nation. Command: /nation new [nation] *[capital]
*
* @param player
*/
public void newNation(Player player, String name, String capitalName) {
TownyUniverse universe = plugin.getTownyUniverse();
try {
Town town = TownyUniverse.getDataSource().getTown(capitalName);
if (town.hasNation())
throw new TownyException(TownySettings.getLangString("msg_err_already_nation"));
// Check the name is valid and doesn't already exist.
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(name);
} catch (InvalidNameException e) {
filteredName = null;
}
if ((filteredName == null) || TownyUniverse.getDataSource().hasNation(filteredName))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_name"), name));
if (TownySettings.isUsingEconomy() && !town.pay(TownySettings.getNewNationPrice(), "New Nation Cost"))
throw new TownyException(TownySettings.getLangString("msg_no_funds_new_nation"));
newNation(universe, name, town);
/*
* universe.newNation(name); Nation nation =
* universe.getNation(name); nation.addTown(town);
* nation.setCapital(town);
*
* universe.getDataSource().saveTown(town);
* universe.getDataSource().saveNation(nation);
* universe.getDataSource().saveNationList();
*/
TownyMessaging.sendGlobalMessage(TownySettings.getNewNationMsg(player.getName(), name));
} catch (TownyException x) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
// TODO: delete town data that might have been done
} catch (EconomyException x) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
}
}
use of javax.naming.InvalidNameException in project Towny by ElgarL.
the class TownyDatabaseHandler method renamePlayer.
@Override
public void renamePlayer(Resident resident, String newName) throws AlreadyRegisteredException, NotRegisteredException {
lock.lock();
String oldName = resident.getName();
try {
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(newName);
} catch (InvalidNameException e) {
throw new NotRegisteredException(e.getMessage());
}
// data needed for a new resident
double balance = 0.0D;
Town town = null;
long registered = 0L;
long lastOnline = 0L;
boolean isMayor = false;
// get data needed for resident
if (TownySettings.isUsingEconomy()) {
try {
balance = resident.getHoldingBalance();
resident.removeAccount();
} catch (EconomyException e) {
}
}
List<Resident> friends = resident.getFriends();
List<String> nationRanks = resident.getNationRanks();
TownyPermission permissions = resident.getPermissions();
String surname = resident.getSurname();
String title = resident.getTitle();
if (resident.hasTown()) {
town = resident.getTown();
}
List<TownBlock> townBlocks = resident.getTownBlocks();
List<String> townRanks = resident.getTownRanks();
registered = resident.getRegistered();
lastOnline = resident.getLastOnline();
isMayor = resident.isMayor();
// delete the resident and tidy up files
deleteResident(resident);
// remove old resident from residentsMap
// rename the resident
universe.getResidentMap().remove(oldName.toLowerCase());
resident.setName(filteredName);
universe.getResidentMap().put(filteredName.toLowerCase(), resident);
// add everything back to the resident
if (TownySettings.isUsingEconomy()) {
// TODO
try {
resident.setBalance(balance, "Rename Player - Transfer to new account");
} catch (EconomyException e) {
e.printStackTrace();
}
}
resident.setFriends(friends);
resident.setNationRanks(nationRanks);
// not sure if this will work
resident.setPermissions(permissions.toString());
resident.setSurname(surname);
resident.setTitle(title);
resident.setTown(town);
resident.setTownblocks(townBlocks);
resident.setTownRanks(townRanks);
resident.setRegistered(registered);
resident.setLastOnline(lastOnline);
if (isMayor) {
try {
town.setMayor(resident);
} catch (TownyException e) {
}
}
// save stuff
saveResidentList();
saveResident(resident);
if (town != null) {
saveTown(town);
}
for (TownBlock tb : townBlocks) {
saveTownBlock(tb);
}
// search and update all friends lists
Resident oldResident = new Resident(oldName);
List<Resident> toSaveResident = new ArrayList<Resident>(getResidents());
for (Resident toCheck : toSaveResident) {
if (toCheck.hasFriend(oldResident)) {
try {
toCheck.removeFriend(oldResident);
toCheck.addFriend(resident);
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for (Resident toCheck : toSaveResident) saveResident(toCheck);
} finally {
lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameResidentEvent(oldName, resident));
universe.setChangedNotify(RENAME_RESIDENT);
}
Aggregations