WsuIdAllocator.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.security.message;

  21. import org.apache.xml.security.stax.impl.util.IDGenerator;

  22. import com.fasterxml.uuid.Generators;
  23. import com.fasterxml.uuid.impl.RandomBasedGenerator;


  24. /**
  25.  * WsuIdAllocator
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class WsuIdAllocator implements org.apache.wss4j.dom.WsuIdAllocator {
  32.    
  33.     private static String secureRandomAlgorithm = null;
  34.     public static String getSecureRandomAlgorithm() {
  35.         return secureRandomAlgorithm;
  36.     }
  37.     public static void setSecureRandomAlgorithm(String secureRandomAlgorithm) {
  38.         WsuIdAllocator.secureRandomAlgorithm = secureRandomAlgorithm;
  39.     }

  40.     private static ThreadLocal<RandomBasedGenerator> randomBasedGeneratorThreadLocal = new ThreadLocal<RandomBasedGenerator>();
  41.     public static RandomBasedGenerator getRandomBasedGenerator() {
  42.         if(secureRandomAlgorithm==null) {
  43.             return null;
  44.         }
  45.         RandomBasedGenerator randomGenerator = randomBasedGeneratorThreadLocal.get();
  46.         if(randomGenerator==null) {
  47.             try {
  48.                 randomGenerator = Generators.randomBasedGenerator(java.security.SecureRandom.getInstance(secureRandomAlgorithm));
  49.                 randomBasedGeneratorThreadLocal.set(randomGenerator);
  50.             }catch(Throwable t) {
  51.                 System.out.println("Inizializzazione RandomBasedGenerator fallita: "+t.getMessage());
  52.                 t.printStackTrace(System.out);
  53.             }
  54.         }
  55.         return randomGenerator;
  56.     }
  57.     public static void removeRandomBasedGenerator() {
  58.         if(randomBasedGeneratorThreadLocal!=null) {
  59.             randomBasedGeneratorThreadLocal.remove();
  60.         }
  61.     }
  62.    
  63.    
  64.    
  65.     private String prefixComponent;
  66.    
  67.     public WsuIdAllocator(String prefix){
  68.         this.prefixComponent=prefix;
  69.     }
  70.        
  71.     @Override
  72.     public String createId(String prefix, Object o) {
  73.         RandomBasedGenerator generator = null;
  74.         if(secureRandomAlgorithm!=null) {
  75.             generator = getRandomBasedGenerator();
  76.         }
  77.         if(generator!=null) {
  78.             return _generateID(generator, prefix);
  79.         }
  80.         else {
  81.             if (prefix == null) {
  82.                 return IDGenerator.generateID(this.prefixComponent);
  83.             }
  84.    
  85.             return IDGenerator.generateID(this.prefixComponent+prefix);
  86.         }
  87.     }

  88.     @Override
  89.     public String createSecureId(String prefix, Object o) {
  90.         RandomBasedGenerator generator = null;
  91.         if(secureRandomAlgorithm!=null) {
  92.             generator = getRandomBasedGenerator();
  93.         }
  94.         if(generator!=null) {
  95.             return _generateID(generator, prefix);
  96.         }
  97.         else {
  98.             if (prefix == null) {
  99.                 return IDGenerator.generateID(this.prefixComponent);
  100.             }
  101.    
  102.             return IDGenerator.generateID(this.prefixComponent+prefix);
  103.         }
  104.     }

  105.     private static String _generateID(RandomBasedGenerator generator, String prefix) {
  106.         // Implementazione uguale a quella presente in org.apache.xml.security.stax.impl.util.IDGenerator, con la differenza che viene usato un RandomBasedGenerator
  107.         String id = generator.generate().toString();
  108.         if (prefix != null) {
  109.             return prefix + id;
  110.         } else {
  111.             //always prepend a constant character to get a schema-valid id!:
  112.             return "G" + id;
  113.         }
  114.     }
  115. }