CredenzialeClientAddress.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.transazioni.utils.TipoCredenzialeMittente;
  22. import org.openspcoop2.utils.UtilsException;

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

  31.     private String socketAddress;
  32.     private String transportAddress;
  33.        
  34.     public CredenzialeClientAddress(String socketAddress, String transportAddress) {
  35.         super(TipoCredenzialeMittente.CLIENT_ADDRESS);
  36.         this.socketAddress = socketAddress;
  37.         this.transportAddress = transportAddress;
  38.     }

  39.     @Override
  40.     public String getCredenziale() throws UtilsException {
  41.         boolean socketAddressDefined = this.socketAddress!=null && !"".equals(this.socketAddress);
  42.         boolean transportAddressDefined = this.transportAddress!=null && !"".equals(this.transportAddress);
  43.         if(socketAddressDefined && transportAddressDefined) {
  44.             return getSocketAddressDBValue(this.socketAddress)+" "+getTransportAddressDBValue(this.transportAddress);      
  45.         }
  46.         else if(socketAddressDefined) {
  47.             return getSocketAddressDBValue(this.socketAddress);    
  48.         }
  49.         else {
  50.             return getTransportAddressDBValue(this.transportAddress);
  51.         }
  52.     }
  53.    
  54.     private static final String PREFIX_SOCKET = "#S#";
  55.     private static final String PREFIX_TRANSPORT = "#T#";
  56.    
  57.     public static String getSocketAddressDBValue(String address) {
  58.         return getSocketAddressDBValue(address, true);
  59.     }
  60.     public static String getSocketAddressDBValue(String address, boolean ricercaEsatta) {
  61.         if(ricercaEsatta) {
  62.             return PREFIX_SOCKET + address + PREFIX_SOCKET;
  63.         }
  64.         else {
  65.             return PREFIX_SOCKET + "%" + address + "%" + PREFIX_SOCKET;
  66.         }
  67.     }
  68.     public static String getTransportAddressDBValue(String address) {
  69.         return getTransportAddressDBValue(address, true);
  70.     }
  71.     public static String getTransportAddressDBValue(String address, boolean ricercaEsatta) {
  72.         if(ricercaEsatta) {
  73.             return PREFIX_TRANSPORT + address + PREFIX_TRANSPORT;
  74.         }else {
  75.             return PREFIX_TRANSPORT + "%" + address + "%" + PREFIX_TRANSPORT;
  76.         }
  77.     }
  78.    
  79.     public static boolean isSocketAddressDBValue(String address) {
  80.         return address.contains(PREFIX_SOCKET);
  81.     }
  82.     public static boolean isTransportAddressDBValue(String address) {
  83.         return address.contains(PREFIX_TRANSPORT);
  84.     }
  85.    
  86.     public static String convertSocketDBValueToOriginal(String address) {
  87.         if(isSocketAddressDBValue(address)) {
  88.             address = address.trim();
  89.             String [] tmp = address.split(PREFIX_SOCKET);
  90.             for (int i = 0; i < tmp.length; i++) {
  91.                 String s = tmp[i];
  92.                 if(s!=null) {
  93.                     s = s.trim();
  94.                     if( (!"".equals(s)) &&
  95.                         (!isTransportAddressDBValue(s))  ) {
  96.                         return s;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.         return null;
  102.     }
  103.     public static String convertTransportDBValueToOriginal(String address) {
  104.         if(isTransportAddressDBValue(address)) {
  105.             address = address.trim();
  106.             String [] tmp = address.split(PREFIX_TRANSPORT);
  107.             for (int i = 0; i < tmp.length; i++) {
  108.                 String s = tmp[i];
  109.                 if(s!=null) {
  110.                     s = s.trim();
  111.                     if( (!"".equals(s)) &&
  112.                         (!isSocketAddressDBValue(s))  ) {
  113.                         return s;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         return null;
  119.     }
  120.    
  121.     @Override
  122.     public void updateCredenziale(String newCredential) throws UtilsException{
  123.         throw new UtilsException("Aggiornamento non supportato dal tipo di credenziale");
  124.     }
  125. }