PublicKeyStoreCache.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.PublicKeyStore;

  23. /**
  24.  * PublicKeyStoreCache
  25.  *
  26.  * @author Andrea Poli (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class PublicKeyStoreCache extends AbstractKeystoreCache<PublicKeyStore> {

  31.     @Override
  32.     public PublicKeyStore createKeystore(String key, Object... params) throws SecurityException{
  33.         if(params==null){
  34.             throw new SecurityException("Params is null");
  35.         }
  36.        
  37.         if(params.length==1){
  38.             return createFromPath(key, params);
  39.         }
  40.         else if(params.length==2){
  41.             return createFromByteArray(params);
  42.         }
  43.         else{
  44.             throw new SecurityException("Params [lenght:"+params.length+"] not supported");
  45.         }
  46.     }
  47.     private PublicKeyStore createFromPath(String key, Object... params) throws SecurityException {
  48.         if(params[0] instanceof String) {
  49.             String pathPublicKey = key;
  50.             String algorithm = (String) params[0];
  51.             return new PublicKeyStore(pathPublicKey, algorithm);
  52.         }
  53.         else {
  54.             throw new SecurityException("Param[0] must be String (algorithm)");
  55.         }
  56.     }
  57.     private PublicKeyStore createFromByteArray(Object... params) throws SecurityException {
  58.         if(params[0] instanceof byte[]) {
  59.             if( ! (params[1] instanceof String) ){
  60.                 throw new SecurityException("Param[1] must be String (algorithm)");
  61.             }
  62.             byte [] publicKey = (byte[]) params[0];
  63.             String algorithm = (String) params[1];
  64.             return new PublicKeyStore(publicKey, algorithm);
  65.         }
  66.         else {
  67.             throw new SecurityException("Param[0] must be byte[] (publicKey)");
  68.         }
  69.     }

  70.     @Override
  71.     public String getPrefixKey() {
  72.         return "PublicKey ";
  73.     }
  74. }