RemoteStoreCache.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.keystore.cache;

  21. import org.openspcoop2.security.SecurityException;
  22. import org.openspcoop2.security.keystore.RemoteStore;
  23. import org.openspcoop2.utils.certificate.remote.IRemoteStoreProvider;
  24. import org.openspcoop2.utils.certificate.remote.RemoteKeyType;
  25. import org.openspcoop2.utils.certificate.remote.RemoteStoreConfig;

  26. /**
  27.  * RemoteStoreCache
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class RemoteStoreCache extends AbstractKeystoreCache<RemoteStore> {  

  34.     @Override
  35.     public RemoteStore createKeystore(String key, Object... params) throws SecurityException{
  36.         if(params==null){
  37.             throw new SecurityException("Params is null");
  38.         }
  39.         if(params.length==4){
  40.             // key e' formata dalla tripla remoteStoreConfigName, keyId e RemoteKeyType
  41.             if( ! (params[0] instanceof String) ){
  42.                 throw new SecurityException("Param[0] must be String");
  43.             }
  44.             if( ! (params[1] instanceof RemoteKeyType) ){
  45.                 throw new SecurityException("Param[1] must be RemoteKeyType");
  46.             }
  47.             if( ! (params[2] instanceof RemoteStoreConfig) ){
  48.                 throw new SecurityException("Param[2] must be RemoteStoreConfig");
  49.             }
  50.             if( ! (params[3] instanceof IRemoteStoreProvider) ){
  51.                 throw new SecurityException("Param[3] must be IRemoteStoreProvider");
  52.             }
  53.             String keyId = (String) params[0];
  54.             RemoteKeyType keyType = (RemoteKeyType) params[1];
  55.             RemoteStoreConfig remoteStoreConfig = (RemoteStoreConfig) params[2];
  56.             IRemoteStoreProvider provider = (IRemoteStoreProvider) params[3];
  57.             return new RemoteStore(keyId, keyType, remoteStoreConfig, provider);
  58.         }
  59.         else{
  60.             throw new SecurityException("Params [lenght:"+params.length+"] not supported");
  61.         }
  62.     }

  63.     public static final String RESTORE_STORE_PREFIX = "RemoteStore ";
  64.    
  65.     @Override
  66.     public String getPrefixKey() {
  67.         return RESTORE_STORE_PREFIX;
  68.     }
  69.    
  70.     public static String getPrefixKeyCache(RemoteStoreConfig remoteStoreConfig, RemoteKeyType keyType) throws SecurityException {
  71.         if(remoteStoreConfig==null) {
  72.             throw new SecurityException("RemoteStoreConfig undefined");
  73.         }
  74.         String remoteStoreName = remoteStoreConfig.getStoreName();
  75.         if(remoteStoreName==null) {
  76.             throw new SecurityException("RemoteStoreConfig name undefined");
  77.         }
  78.        
  79.         if(keyType==null) {
  80.             throw new SecurityException("KeyType undefined");
  81.         }
  82.        
  83.         return remoteStoreName+"_"+keyType.name()+"_";
  84.     }
  85.     public static String getKeyCache(RemoteStoreConfig remoteStoreConfig, String keyId, RemoteKeyType keyType) throws SecurityException {
  86.         // key e' formata dalla tripla remoteStoreConfigName, keyId e RemoteKeyType
  87.        
  88.         if(keyId==null) {
  89.             throw new SecurityException("KeyId undefined");
  90.         }
  91.        
  92.         return getPrefixKeyCache(remoteStoreConfig, keyType)+keyId;
  93.     }
  94. }