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.     public static Boolean getBooleanProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {
  42.         AbstractProperty<?> prop = getProperty(p, key, required);
  43.         if(prop instanceof BooleanProperty) {
  44.             return ((BooleanProperty)prop).getValue();
  45.         } else {
  46.             throw new CoreException(getPropertyPrefix(key)+"non è una Boolean:" + (prop!=null ? prop.getClass().getName() : "null prop" ) );
  47.         }
  48.     }


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

  50.         AbstractProperty<?> prop = getProperty(p, key, required);
  51.         if(prop == null) return null;
  52.         if(prop instanceof StringProperty) {
  53.             return ((StringProperty)prop).getValue();
  54.         } else {
  55.             throw new CoreException(getPropertyPrefix(key)+"non è una StringProperty:" + prop.getClass().getName());
  56.         }
  57.     }

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

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

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

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

  79.     @SuppressWarnings("rawtypes")
  80.     public static AbstractProperty getProperty(Map<String, AbstractProperty<?>> p, String key, boolean required) throws CoreException {
  81.         if(p.containsKey(key)) {
  82.             return p.get(key);
  83.         } else {
  84.             if(required) {
  85.                 throw new CoreException(getPropertyPrefix(key)+"non trovata");
  86.             } else {
  87.                 return null;
  88.             }
  89.         }
  90.     }
  91. }