InformazioniTokenUserInfo.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.pdd.core.token;

  21. import java.io.Serializable;
  22. import java.lang.reflect.Method;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.pdd.core.token.parser.ITokenUserInfoParser;
  27. import org.openspcoop2.utils.UtilsException;

  28. /**    
  29.  * InformazioniTokenUserInfo
  30.  *
  31.  * @author Poli Andrea (poli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class InformazioniTokenUserInfo extends org.openspcoop2.utils.beans.BaseBean implements Serializable , Cloneable {

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.    
  41.     public InformazioniTokenUserInfo(InformazioniToken informazioniToken, ITokenUserInfoParser tokenUserInfoParser) {
  42.         String rawResponse = informazioniToken.getRawResponse();
  43.         Map<String, Serializable> claims = informazioniToken.getClaims();
  44.         tokenUserInfoParser.init(rawResponse, claims);
  45.         this.fullName = tokenUserInfoParser.getFullName();
  46.         this.firstName = tokenUserInfoParser.getFirstName();
  47.         this.middleName = tokenUserInfoParser.getMiddleName();
  48.         this.familyName = tokenUserInfoParser.getFamilyName();
  49.         this.eMail = tokenUserInfoParser.getEMail();
  50.        
  51.     }
  52.     public InformazioniTokenUserInfo(InformazioniTokenUserInfo ... informazioniTokens ) throws UtilsException {
  53.         if(informazioniTokens!=null && informazioniTokens.length>0) {
  54.            
  55.             Object fullNameO = getValue("fullName", informazioniTokens);
  56.             if(fullNameO instanceof String) {
  57.                 this.fullName = (String) fullNameO;
  58.             }
  59.            
  60.             Object firstNameO = getValue("firstName", informazioniTokens);
  61.             if(firstNameO instanceof String) {
  62.                 this.firstName = (String) firstNameO;
  63.             }
  64.            
  65.             Object middleNameO = getValue("middleName", informazioniTokens);
  66.             if(middleNameO instanceof String) {
  67.                 this.middleName = (String) middleNameO;
  68.             }
  69.            
  70.             Object familyNameO = getValue("familyName", informazioniTokens);
  71.             if(familyNameO instanceof String) {
  72.                 this.familyName = (String) familyNameO;
  73.             }
  74.            
  75.             Object eMailO = getValue("eMail", informazioniTokens);
  76.             if(eMailO instanceof String) {
  77.                 this.eMail = (String) eMailO;
  78.             }
  79.            
  80.         }
  81.     }
  82.     private static Object getValue(String field, InformazioniTokenUserInfo ... informazioniTokens) throws UtilsException {
  83.         Object tmp = null;
  84.         List<Object> listTmp = new ArrayList<>();
  85.         String getMethodName = "get"+((field.charAt(0)+"").toUpperCase())+field.substring(1);
  86.         Method getMethod = null;
  87.         try {
  88.             getMethod = InformazioniTokenUserInfo.class.getMethod(getMethodName);
  89.         }catch(Exception e) {
  90.             throw new UtilsException(e.getMessage(),e);
  91.         }
  92.        
  93.         // La fusione avviene per precedenza dall'ultimo fino al primo (a meno che non sia una lista)
  94.         tmp = getValue(getMethod, listTmp, informazioniTokens);
  95.         if(!listTmp.isEmpty()) {
  96.             return listTmp;
  97.         }
  98.         else {
  99.             return tmp;
  100.         }
  101.     }
  102.     private static Object getValue(Method getMethod, List<Object> listTmp, InformazioniTokenUserInfo ... informazioniTokens) throws UtilsException {
  103.         Object tmp = null;
  104.         for (int i = 0; i < informazioniTokens.length; i++) {
  105.             InformazioniTokenUserInfo infoToken = informazioniTokens[i];
  106.             Object o = null;
  107.             try {
  108.                 o = getMethod.invoke(infoToken);
  109.             }catch(Exception e) {
  110.                 throw new UtilsException(e.getMessage(),e);
  111.             }
  112.             if(o!=null) {
  113.                 tmp = getValue(o, listTmp);
  114.             }
  115.         }
  116.         return tmp;
  117.     }
  118.     private static Object getValue(Object o, List<Object> listTmp) {
  119.         Object tmp = null;
  120.         if(o instanceof List<?>) {
  121.             List<?> list = (List<?>) o;
  122.             if(!list.isEmpty()) {
  123.                 for (Object object : list) {
  124.                     if(!listTmp.contains(object)) {
  125.                         listTmp.add(object);
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.         else {
  131.             tmp = o;
  132.         }
  133.         return tmp;
  134.     }
  135.        
  136.     /**  End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences. */
  137.     private String fullName;
  138.    
  139.     /**  Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names;
  140.          all can be present, with the names being separated by space characters.*/
  141.     private String firstName;

  142.     /**  Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names;
  143.          all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used. */
  144.     private String middleName;
  145.    
  146.     /**  Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name;
  147.          all can be present, with the names being separated by space characters. */
  148.     private String familyName;
  149.    
  150.     /**  End-User's preferred e-mail address. */
  151.     private String eMail;
  152.    
  153.    
  154.     public String getFullName() {
  155.         return this.fullName;
  156.     }
  157.     public void setFullName(String fullName) {
  158.         this.fullName = fullName;
  159.     }
  160.    
  161.     public String getFirstName() {
  162.         return this.firstName;
  163.     }
  164.     public void setFirstName(String firstName) {
  165.         this.firstName = firstName;
  166.     }
  167.    
  168.     public String getMiddleName() {
  169.         return this.middleName;
  170.     }
  171.     public void setMiddleName(String middleName) {
  172.         this.middleName = middleName;
  173.     }
  174.    
  175.     public String getFamilyName() {
  176.         return this.familyName;
  177.     }
  178.     public void setFamilyName(String familyName) {
  179.         this.familyName = familyName;
  180.     }
  181.    
  182.     public String getEMail() {
  183.         return this.eMail;
  184.     }
  185.     public void setEMail(String eMail) {
  186.         this.eMail = eMail;
  187.     }
  188.     public String geteMail() { // clone
  189.         return this.eMail;
  190.     }
  191.     public void seteMail(String eMail) { // clone
  192.         this.eMail = eMail;
  193.     }
  194. }