Search in sources :

Example 1 with IdentityProviderNameEntity

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity)

Example 2 with IdentityProviderNameEntity

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());
}
Also used : IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 3 with IdentityProviderNameEntity

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);
    }
}
Also used : IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity)

Example 4 with IdentityProviderNameEntity

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();
}
Also used : IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) ClientResponse(com.sun.jersey.api.client.ClientResponse) LoggerFactory(org.slf4j.LoggerFactory) IdentityProviderManager(org.orcid.core.manager.IdentityProviderManager) XPathExpression(javax.xml.xpath.XPathExpression) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) WebResource(com.sun.jersey.api.client.WebResource) IdentityProviderDao(org.orcid.persistence.dao.IdentityProviderDao) Logger(org.slf4j.Logger) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) NodeList(org.w3c.dom.NodeList) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Transactional(javax.transaction.Transactional) Resource(javax.annotation.Resource) Collectors(java.util.stream.Collectors) NamespaceMap(org.orcid.core.utils.NamespaceMap) XPathFactory(javax.xml.xpath.XPathFactory) LocaleManager(org.orcid.core.locale.LocaleManager) List(java.util.List) Element(org.w3c.dom.Element) Client(com.sun.jersey.api.client.Client) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ReleaseNameUtils(org.orcid.utils.ReleaseNameUtils) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity) Transactional(javax.transaction.Transactional)

Example 5 with IdentityProviderNameEntity

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;
}
Also used : IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Aggregations

IdentityProviderNameEntity (org.orcid.persistence.jpa.entities.IdentityProviderNameEntity)6 IdentityProviderEntity (org.orcid.persistence.jpa.entities.IdentityProviderEntity)4 Element (org.w3c.dom.Element)4 Document (org.w3c.dom.Document)3 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 XPath (javax.xml.xpath.XPath)2 XPathExpression (javax.xml.xpath.XPathExpression)2 Test (org.junit.Test)2 NodeList (org.w3c.dom.NodeList)2 Client (com.sun.jersey.api.client.Client)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1