CredenzialeTokenClient.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.core.transazioni.utils.credenziali;

  21. import org.openspcoop2.core.commons.CoreException;
  22. import org.openspcoop2.core.id.IDServizioApplicativo;
  23. import org.openspcoop2.core.transazioni.utils.TipoCredenzialeMittente;
  24. import org.openspcoop2.utils.UtilsException;

  25. /**    
  26.  * CredenzialeTokenApplicativoClient
  27.  *
  28.  * @author Poli Andrea (poli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class CredenzialeTokenClient extends AbstractCredenziale {

  33.     private String clientId;
  34.     private IDServizioApplicativo applicativo;
  35.        
  36.     public CredenzialeTokenClient(String clientId, IDServizioApplicativo applicativo) {
  37.         super(TipoCredenzialeMittente.TOKEN_CLIENT_ID);
  38.         this.clientId = clientId;
  39.         this.applicativo = applicativo;
  40.     }

  41.     @Override
  42.     public String getCredenziale() throws UtilsException {
  43.         boolean clientIdDefined = this.clientId!=null && !"".equals(this.clientId);
  44.         boolean applicativoDefined = this.applicativo!=null;
  45.         if(clientIdDefined && applicativoDefined) {
  46.             return getClientIdDBValue(this.clientId)+" "+getApplicationDBValue(getApplicationAsString(this.applicativo));      
  47.         }
  48.         else if(clientIdDefined) {
  49.             return getClientIdDBValue(this.clientId);      
  50.         }
  51.         else {
  52.             return getApplicationDBValue(getApplicationAsString(this.applicativo));
  53.         }
  54.     }
  55.    
  56.     public static String getApplicationAsString(IDServizioApplicativo applicativo) {
  57.         return applicativo.toFormatString();
  58.     }
  59.     public static IDServizioApplicativo getApplicationFromStringValue(String v) throws CoreException {
  60.         try {
  61.             return IDServizioApplicativo.toIDServizioApplicativo(v);
  62.         }catch(Exception e) {
  63.             throw new CoreException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67.     private static final String PREFIX_CLIENT_ID = "#C#";
  68.     private static final String PREFIX_APPLICATION = "#A#";
  69.    
  70.     public static String getClientIdDBValue(String clientId) {
  71.         return getClientIdDBValue(clientId, true);
  72.     }
  73.     public static String getClientIdDBValue(String clientId, boolean ricercaEsatta) {
  74.         if(ricercaEsatta) {
  75.             return PREFIX_CLIENT_ID + clientId + PREFIX_CLIENT_ID;
  76.         }
  77.         else {
  78.             return PREFIX_CLIENT_ID + "%" + clientId + "%" + PREFIX_CLIENT_ID;
  79.         }
  80.     }
  81.     public static String getApplicationDBValue(String applicative) {
  82.         return getApplicationDBValue(applicative, true);
  83.     }
  84.     public static String getApplicationDBValue(String applicative, boolean ricercaEsatta) {
  85.         if(ricercaEsatta) {
  86.             return PREFIX_APPLICATION + applicative + PREFIX_APPLICATION;
  87.         }
  88.         else {
  89.             return PREFIX_APPLICATION + "%" + applicative + "%" + PREFIX_APPLICATION;
  90.         }
  91.     }
  92.    
  93.     public static boolean isClientIdDBValue(String clientId) {
  94.         return clientId.contains(PREFIX_CLIENT_ID);
  95.     }
  96.     public static boolean isApplicationDBValue(String applicative) {
  97.         return applicative.contains(PREFIX_APPLICATION);
  98.     }
  99.    
  100.     public static String convertClientIdDBValueToOriginal(String id) {
  101.         if(isClientIdDBValue(id)) {
  102.             id = id.trim();
  103.             String [] tmp = id.split(PREFIX_CLIENT_ID);
  104.             for (int i = 0; i < tmp.length; i++) {
  105.                 String s = tmp[i];
  106.                 if(s!=null) {
  107.                     s = s.trim();
  108.                     if( (!"".equals(s)) &&
  109.                         (!isApplicationDBValue(s)) ){
  110.                         return s;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         return id; // ritorno lo stesso valore poichè l'informazione è stata salvata non strutturata (solo con il client id)
  116.     }
  117.     public static IDServizioApplicativo convertApplicationDBValueToOriginal(String id) throws CoreException {
  118.         String s = convertApplicationDBValueToOriginalAsString(id);
  119.         if(s!=null) {
  120.             return getApplicationFromStringValue(s);
  121.         }
  122.         return null;
  123.     }
  124.     public static String convertApplicationDBValueToOriginalAsString(String id) {
  125.         if(isApplicationDBValue(id)) {
  126.             id = id.trim();
  127.             String [] tmp = id.split(PREFIX_APPLICATION);
  128.             for (int i = 0; i < tmp.length; i++) {
  129.                 String s = tmp[i];
  130.                 if(s!=null) {
  131.                     s = s.trim();
  132.                     if( (!"".equals(s)) &&
  133.                         (!isClientIdDBValue(s))
  134.                         ){
  135.                         return s;
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.         return null;
  141.     }
  142.    
  143.     @Override
  144.     public void updateCredenziale(String newCredential) throws UtilsException{
  145.         throw new UtilsException("Aggiornamento non supportato dal tipo di credenziale");
  146.     }
  147. }