use of org.orcid.persistence.jpa.entities.IdentityProviderNameEntity in project ORCID-Source by ORCID.
the class IdentityProviderManagerImpl method createEntityFromXml.
@Override
public IdentityProviderEntity createEntityFromXml(Element idpElement) {
XPath xpath = createXPath();
XPathExpression mainDisplayNameXpath = compileXPath(xpath, "string(.//md:IDPSSODescriptor//mdui:DisplayName[1])");
XPathExpression displayNamesXpath = compileXPath(xpath, ".//md:IDPSSODescriptor//mdui:DisplayName");
XPathExpression legacyMainDisplayNameXpath = compileXPath(xpath, "string(.//md:OrganizationDisplayName[1])");
XPathExpression legacyDisplayNamesXpath = compileXPath(xpath, ".//md:OrganizationDisplayName");
XPathExpression supportContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='support'])[1]/md:EmailAddress[1])");
XPathExpression adminContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='administrative'])[1]/md:EmailAddress[1])");
XPathExpression techContactXpath = compileXPath(xpath, "string((.//md:ContactPerson[@contactType='technical'])[1]/md:EmailAddress[1])");
String entityId = idpElement.getAttribute("entityID");
String mainDisplayName = evaluateXPathString(mainDisplayNameXpath, idpElement);
if (StringUtils.isBlank(mainDisplayName)) {
mainDisplayName = evaluateXPathString(legacyMainDisplayNameXpath, idpElement);
}
String supportEmail = tidyEmail(evaluateXPathString(supportContactXpath, idpElement));
String adminEmail = tidyEmail(evaluateXPathString(adminContactXpath, idpElement));
String techEmail = tidyEmail(evaluateXPathString(techContactXpath, idpElement));
List<IdentityProviderNameEntity> nameEntities = createNameEntitiesFromXml(displayNamesXpath, idpElement);
if (nameEntities == null || nameEntities.isEmpty()) {
nameEntities = createNameEntitiesFromXml(legacyDisplayNamesXpath, idpElement);
}
IdentityProviderEntity identityProviderEntity = new IdentityProviderEntity();
identityProviderEntity.setProviderid(entityId);
identityProviderEntity.setDisplayName(mainDisplayName);
identityProviderEntity.setNames(nameEntities);
identityProviderEntity.setSupportEmail(supportEmail);
identityProviderEntity.setAdminEmail(adminEmail);
identityProviderEntity.setTechEmail(techEmail);
return identityProviderEntity;
}
use of org.orcid.persistence.jpa.entities.IdentityProviderNameEntity in project ORCID-Source by ORCID.
the class IdentityProviderManagerTest method testCreateEntityFromXmlWithLegacyNames.
@Test
public void testCreateEntityFromXmlWithLegacyNames() throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(getClass().getResourceAsStream("example_idp_legacy.xml"));
Element idpElement = doc.getDocumentElement();
IdentityProviderEntity result = identityProviderManager.createEntityFromXml(idpElement);
assertNotNull(result);
assertEquals("https://registry.shibboleth.example.ac.uk/idp", result.getProviderid());
List<IdentityProviderNameEntity> names = result.getNames();
assertNotNull(names);
assertEquals(1, names.size());
IdentityProviderNameEntity name = names.get(0);
assertEquals("University of Example Display", name.getDisplayName());
assertEquals("en", name.getLang());
assertEquals("University of Example Display", result.getDisplayName());
assertEquals("help@it.example.ac.uk", result.getSupportEmail());
assertEquals("admin@it.example.ac.uk", result.getAdminEmail());
assertEquals("sysdev@it.example.ac.uk", result.getTechEmail());
}
use of org.orcid.persistence.jpa.entities.IdentityProviderNameEntity in project ORCID-Source by ORCID.
the class IdentityProviderManagerImpl method mergeNames.
private void mergeNames(IdentityProviderEntity incoming, IdentityProviderEntity existing) {
List<IdentityProviderNameEntity> existingNames = existing.getNames();
List<IdentityProviderNameEntity> incomingNames = incoming.getNames();
if (existingNames != null) {
Map<String, IdentityProviderNameEntity> incomingNamesMappedByLang = incomingNames.stream().collect(Collectors.toMap(IdentityProviderNameEntity::getLang, Function.identity()));
// Update existing name entities
existingNames.stream().forEach(e -> {
IdentityProviderNameEntity incomingName = incomingNamesMappedByLang.get(e.getLang());
if (incomingName != null) {
e.setDisplayName(incoming.getDisplayName());
}
});
// Remove existing names that are not in the incoming
// list
existingNames.removeIf(e -> {
return incomingNames.stream().noneMatch(i -> {
String existingDisplayName = e.getDisplayName();
return i.getDisplayName().equals(existingDisplayName);
});
});
// Add new names
Map<String, IdentityProviderNameEntity> existingNamesMappedByLang = existingNames.stream().collect(Collectors.toMap(IdentityProviderNameEntity::getLang, Function.identity()));
incomingNames.stream().forEach(i -> {
if (!existingNamesMappedByLang.containsKey(i.getLang())) {
i.setIdentityProvider(existing);
existingNames.add(i);
}
});
} else {
incomingNames.stream().forEach(i -> i.setIdentityProvider(incoming));
existing.setNames(incomingNames);
}
}
use of org.orcid.persistence.jpa.entities.IdentityProviderNameEntity in project ORCID-Source by ORCID.
the class IdentityProviderManagerImpl method retrieveFreshIdentitifyProviderName.
@Override
@Transactional
public String retrieveFreshIdentitifyProviderName(String providerid, Locale locale) {
IdentityProviderEntity idp = identityProviderDao.findByProviderid(providerid);
List<IdentityProviderNameEntity> names = idp.getNames();
if (names != null) {
Optional<IdentityProviderNameEntity> idpNameEntity = names.stream().filter(n -> n.getLang().equals(locale.getLanguage())).findFirst();
if (idpNameEntity.isPresent()) {
return idpNameEntity.get().getDisplayName();
}
}
return idp.getDisplayName();
}
use of org.orcid.persistence.jpa.entities.IdentityProviderNameEntity in project ORCID-Source by ORCID.
the class IdentityProviderManagerImpl method createNameEntitiesFromXml.
private List<IdentityProviderNameEntity> createNameEntitiesFromXml(XPathExpression displayNamesXpath, Element idpElement) {
List<IdentityProviderNameEntity> nameEntities = new ArrayList<>();
NodeList displayNames = evaluateXPathNodeList(displayNamesXpath, idpElement);
if (displayNames != null) {
for (int i = 0; i < displayNames.getLength(); i++) {
Element displayNameElement = (Element) displayNames.item(i);
String lang = displayNameElement.getAttribute("xml:lang");
String displayName = displayNameElement.getTextContent();
IdentityProviderNameEntity nameEntity = new IdentityProviderNameEntity();
nameEntity.setLang(lang);
nameEntity.setDisplayName(displayName);
nameEntities.add(nameEntity);
}
}
return nameEntities;
}
Aggregations