BYOKRemoteUtils.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.utils.certificate.byok;

  21. import org.apache.commons.lang.StringUtils;
  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.io.Base64Utilities;
  24. import org.openspcoop2.utils.io.HexBinaryUtilities;
  25. import org.openspcoop2.utils.json.JSONUtils;
  26. import org.openspcoop2.utils.json.JsonPathExpressionEngine;
  27. import org.slf4j.Logger;

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

  38.     public static byte[] normalizeResponse(BYOKInstance instance, byte [] content, Logger log) throws UtilsException {
  39.         if(content!=null && content.length>0) {
  40.             if(instance.getConfig().getRemoteConfig().isHttpResponseBase64Encoded()) {
  41.                 content = Base64Utilities.decode(content);
  42.             }
  43.             else if(instance.getConfig().getRemoteConfig().isHttpResponseHexEncoded()) {
  44.                 content = HexBinaryUtilities.decode(new String(content).toCharArray());
  45.             }
  46.            
  47.             return normalizeResponseByJsonPath(instance, content, log);
  48.         }
  49.         return content;
  50.     }
  51.     private static byte[] normalizeResponseByJsonPath(BYOKInstance instance, byte [] content, Logger log) throws UtilsException {
  52.         String pattern = instance.getConfig().getRemoteConfig().getHttpResponseJsonPath();
  53.         if(pattern!=null && StringUtils.isNotEmpty(pattern)) {
  54.             JSONUtils jsonUtils = JSONUtils.getInstance();
  55.             if(jsonUtils.isJson(content)) {
  56.                 String elementJson = new String(content);
  57.                 try {
  58.                     String valoreEstratto = JsonPathExpressionEngine.extractAndConvertResultAsString(elementJson, pattern, log);
  59.                     if(valoreEstratto==null || StringUtils.isEmpty(valoreEstratto)) {
  60.                         throw new UtilsException("Read failure with pattern '"+pattern+"'");
  61.                     }
  62.                     content = valoreEstratto.getBytes();
  63.                     if(instance.getConfig().getRemoteConfig().isHttpResponseJsonPathBase64Encoded()) {
  64.                         content = Base64Utilities.decode(content);
  65.                     }
  66.                     else if(instance.getConfig().getRemoteConfig().isHttpResponseJsonPathHexEncoded()) {
  67.                         content = HexBinaryUtilities.decode(new String(content).toCharArray());
  68.                     }
  69.                 }catch(Exception e) {
  70.                     throw new UtilsException(e.getMessage(),e);
  71.                 }
  72.             }
  73.         }
  74.         return content;
  75.     }
  76.    
  77. }