ProtocolPropertiesHelper.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.config.rs.server.api.impl;

  21. import java.util.Map;

  22. import org.openspcoop2.core.commons.CoreException;
  23. import org.openspcoop2.protocol.sdk.properties.AbstractProperty;
  24. import org.openspcoop2.protocol.sdk.properties.BinaryProperty;
  25. import org.openspcoop2.protocol.sdk.properties.BooleanProperty;
  26. import org.openspcoop2.protocol.sdk.properties.NumberProperty;
  27. import org.openspcoop2.protocol.sdk.properties.StringProperty;

  28. /**
  29.  * ProtocolPropertiesHelper
  30.  *
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class ProtocolPropertiesHelper {
  35.    
  36.     private ProtocolPropertiesHelper() {}

  37.     private static String getPropertyPrefix(String key) {
  38.         return "Property "+key+" ";
  39.     }
  40.    
  41.     private static Object returnProperty(boolean permitReturnNull, String key, AbstractProperty<?> prop, String expectedClassName) throws CoreException {
  42.         if(permitReturnNull && prop==null) {
  43.             return null;
  44.         }
  45.         throw new CoreException(getPropertyPrefix(key)+"non è "+expectedClassName+": " + (prop!=null ? prop.getClass().getName() : "null prop" ) );
  46.     }
  47.    
  48.     public static Boolean getBooleanProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {
  49.         return getBooleanProperty(p, key, required, false);
  50.     }
  51.     public static Boolean getBooleanProperty(Map<String, AbstractProperty<?>> p, String key, boolean required, boolean permitReturnNull) throws CoreException {
  52.         AbstractProperty<?> prop = getProperty(p, key, required);
  53.         if(prop instanceof BooleanProperty) {
  54.             return ((BooleanProperty)prop).getValue();
  55.         } else {
  56.             return (Boolean) returnProperty(permitReturnNull, key, prop, "Boolean");
  57.         }
  58.     }


  59.     public static String getStringProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {

  60.         AbstractProperty<?> prop = getProperty(p, key, required);
  61.         if(prop == null) return null;
  62.         if(prop instanceof StringProperty) {
  63.             return ((StringProperty)prop).getValue();
  64.         } else {
  65.             throw new CoreException(getPropertyPrefix(key)+"non è una StringProperty:" + prop.getClass().getName());
  66.         }
  67.     }

  68.     public static byte[] getByteArrayProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {

  69.         byte[] empty = null;
  70.        
  71.         AbstractProperty<?> prop = getProperty(p, key, required);
  72.         if(prop == null)
  73.             return empty;
  74.         if(prop instanceof BinaryProperty) {
  75.             return ((BinaryProperty)prop).getValue();
  76.         } else {
  77.             throw new CoreException(getPropertyPrefix(key)+"non è una BinaryProperty:" + prop.getClass().getName());
  78.         }
  79.     }

  80.     public static Integer getIntegerProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {

  81.         AbstractProperty<?> prop = getProperty(p, key, required);
  82.         if(prop == null) return null;
  83.         if(prop instanceof NumberProperty) {
  84.             return ((NumberProperty)prop).getValue().intValue();
  85.         } else {
  86.             throw new CoreException(getPropertyPrefix(key)+"non è una NumberProperty:" + prop.getClass().getName());
  87.         }
  88.     }

  89.     @SuppressWarnings("rawtypes")
  90.     public static AbstractProperty getProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {
  91.         if(p.containsKey(key)) {
  92.             return p.get(key);
  93.         } else {
  94.             if(required) {
  95.                 throw new CoreException(getPropertyPrefix(key)+"non trovata");
  96.             } else {
  97.                 return null;
  98.             }
  99.         }
  100.     }
  101. }