PropertiesUtils.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.utils;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.util.Map;
  25. import java.util.Properties;

  26. import org.openspcoop2.generic_project.exception.NotFoundException;
  27. import org.openspcoop2.security.SecurityException;
  28. import org.openspcoop2.security.message.MessageSecurityContext;
  29. import org.openspcoop2.security.message.constants.SecurityConstants;

  30. /**
  31.  * PropertiesUtils
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class PropertiesUtils {

  38.     public static EncryptionBean getSenderEncryptionBean(MessageSecurityContext messageSecurityContext) throws NotFoundException, SecurityException {
  39.        
  40.         Properties p = _readProperties(messageSecurityContext.getOutgoingProperties(), SecurityConstants.ENCRYPTION_PROPERTY_REF_ID);
  41.         if(p==null) {
  42.             p = _readProperties(messageSecurityContext.getOutgoingProperties(), SecurityConstants.ENCRYPTION_PROPERTY_FILE);
  43.         }
  44.        
  45.         if(p==null) {
  46.             throw new NotFoundException(SecurityConstants.ENCRYPTION_PROPERTY_REF_ID+"/"+SecurityConstants.ENCRYPTION_PROPERTY_FILE+" non fornita");
  47.         }

  48.         EncryptionBean bean = new EncryptionBean();
  49.        
  50.         bean.setProperties(p);
  51.        
  52.         return bean;
  53.     }
  54.     public static EncryptionBean getReceiverEncryptionBean(MessageSecurityContext messageSecurityContext) throws NotFoundException, SecurityException {

  55.         Properties p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.DECRYPTION_PROPERTY_REF_ID);
  56.         if(p==null) {
  57.             p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.DECRYPTION_PROPERTY_FILE);
  58.         }
  59.        
  60.         if(p==null) {
  61.             throw new NotFoundException(SecurityConstants.DECRYPTION_PROPERTY_REF_ID+"/"+SecurityConstants.DECRYPTION_PROPERTY_FILE+" non fornita");
  62.         }

  63.         EncryptionBean bean = new EncryptionBean();
  64.        
  65.         bean.setProperties(p);
  66.        
  67.         return bean;
  68.        
  69.     }

  70.     public static SignatureBean getSenderSignatureBean(MessageSecurityContext messageSecurityContext) throws NotFoundException, SecurityException {

  71.         Properties p = _readProperties(messageSecurityContext.getOutgoingProperties(), SecurityConstants.SIGNATURE_PROPERTY_REF_ID);
  72.         if(p==null) {
  73.             p = _readProperties(messageSecurityContext.getOutgoingProperties(), SecurityConstants.SIGNATURE_PROPERTY_FILE);
  74.         }
  75.        
  76.         if(p==null) {
  77.             throw new NotFoundException(SecurityConstants.SIGNATURE_PROPERTY_REF_ID+"/"+SecurityConstants.SIGNATURE_PROPERTY_FILE+" non fornita");
  78.         }
  79.         SignatureBean bean = new SignatureBean();
  80.        
  81.         bean.setProperties(p);
  82.        
  83.         return bean;
  84.        
  85.     }  
  86.     public static SignatureBean getReceiverSignatureBean(MessageSecurityContext messageSecurityContext) throws NotFoundException, SecurityException {

  87.         Properties p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.SIGNATURE_VERIFICATION_PROPERTY_REF_ID);
  88.         if(p==null) {
  89.             p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.SIGNATURE_VERIFICATION_PROPERTY_FILE);
  90.         }
  91.         if(p==null) {
  92.             p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.SIGNATURE_PROPERTY_REF_ID);
  93.         }
  94.         if(p==null) {
  95.             p = _readProperties(messageSecurityContext.getIncomingProperties(), SecurityConstants.SIGNATURE_PROPERTY_FILE);
  96.         }
  97.        
  98.         if(p==null) {
  99.             throw new NotFoundException(SecurityConstants.SIGNATURE_VERIFICATION_PROPERTY_REF_ID+"/"+SecurityConstants.SIGNATURE_VERIFICATION_PROPERTY_FILE+"/"+
  100.                     SecurityConstants.SIGNATURE_PROPERTY_REF_ID+"/"+SecurityConstants.SIGNATURE_PROPERTY_FILE+" non fornita");
  101.         }

  102.         SignatureBean bean = new SignatureBean();
  103.        
  104.         bean.setProperties(p);
  105.        
  106.         return bean;
  107.        
  108.     }
  109.    
  110.     private static Properties _readProperties(Map<String,Object> map, String property) throws SecurityException {
  111.    
  112.         String path = null;
  113.         File fPath = null;
  114.        
  115.         if(map.containsKey(property)) {
  116.             Object o = map.get(property);
  117.             if(o instanceof Properties) {
  118.                 return (Properties) o;
  119.             }
  120.             else if(o instanceof File) {
  121.                 fPath = (File) o;
  122.             }
  123.             else if(o instanceof String) {
  124.                 path = (String) o;
  125.             }
  126.             else {
  127.                 throw new SecurityException("Found property ["+property+"] with wrong type");
  128.             }
  129.         }
  130.        
  131.         if(fPath!=null) {
  132.             try {
  133.                 return _readProperties(fPath);
  134.             }catch(Exception e) {
  135.                 throw new SecurityException("Occurs error (property ["+property+"]): "+e.getMessage(),e);
  136.             }
  137.         }
  138.         else if(path!=null) {
  139.             File check = new File(path);
  140.             if(check.exists()) {
  141.                 return _readProperties(check);
  142.             }
  143.             else {
  144.                 InputStream is = PropertiesUtils.class.getResourceAsStream(path);
  145.                 if(is==null && path.startsWith("/")==false) {
  146.                     is = PropertiesUtils.class.getResourceAsStream("/"+path);
  147.                 }
  148.                 if(is==null) {
  149.                     throw new SecurityException("Occurs error (property ["+property+"]): resource ["+path+"] not found");
  150.                 }
  151.             }
  152.         }
  153.         return null;
  154.     }
  155.     private static Properties _readProperties(File file) throws SecurityException {
  156.         if(file.exists()==false) {
  157.             throw new SecurityException("Cannot exists");
  158.         }
  159.         if(file.canRead()==false) {
  160.             throw new SecurityException("Cannot read");
  161.         }
  162.         if(file.isFile()==false) {
  163.             throw new SecurityException("Isn't file");
  164.         }
  165.         Properties p = new Properties();
  166.         FileInputStream fin = null;
  167.         try {
  168.             fin = new FileInputStream(file);
  169.             p.load(fin);
  170.         }
  171.         catch(Exception e) {
  172.             throw new SecurityException(e.getMessage(),e);
  173.         }
  174.         finally {
  175.             try {
  176.                 if(fin!=null) {
  177.                     fin.close();
  178.                 }
  179.             }catch(Exception eClose) {
  180.                 // ignore
  181.             }
  182.         }
  183.         return p;
  184.     }
  185. }