APIUtils.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.protocol.basic.archive;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.core.registry.constants.HttpMethod;
  24. import org.openspcoop2.utils.Utilities;
  25. import org.openspcoop2.utils.transport.http.HttpRequestMethod;

  26. /**
  27.  * APIUtils
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class APIUtils {

  34.     public static String normalizeResourceName(HttpMethod requestMethod, String path) {
  35.         if(requestMethod==null || path==null) {
  36.             return null;
  37.         }
  38.         HttpRequestMethod httpMethodCheck = HttpRequestMethod.valueOf(requestMethod.getValue());
  39.         return normalizeResourceName(httpMethodCheck, path);
  40.     }
  41.    
  42.     public static String normalizeResourceName(HttpRequestMethod requestMethod, String path) {
  43.        
  44.         if(requestMethod==null || path==null) {
  45.             return null;
  46.         }
  47.        
  48.         List<Character> permit = new ArrayList<Character>();
  49.         permit.add('.');
  50.         permit.add('_');
  51.         permit.add('-');
  52.         Character cJolly = '.'; // uso un carattere come jolly '.'
  53.        
  54.         String nome = Utilities.convertNameToSistemaOperativoCompatible(path,true,cJolly,permit,false);
  55.         // Elimino caratteri iniziali
  56.         while(nome.startsWith((cJolly+"")) && nome.length()>1) {
  57.             nome = nome.substring(1);
  58.         }
  59.         // Elimino caratteri finali
  60.         while(nome.endsWith((cJolly+"")) && nome.length()>1) {
  61.             nome = nome.substring(0,nome.length()-1);
  62.         }
  63.         // elimino eventuali caratteri speciali se presenti piu' di una volta consecutivamente
  64.         int count = 0;
  65.         StringBuilder bf = new StringBuilder();
  66.         for (int i = 0; i < nome.length(); i++) {
  67.             char c = nome.charAt(i);
  68.             if(c == cJolly.charValue()) {
  69.                 count++;
  70.                 if(count==1) {
  71.                     bf.append(c);
  72.                 }
  73.             }
  74.             else {
  75.                 count = 0;
  76.                 bf.append(c);
  77.             }
  78.         }
  79.        
  80.         String nomeAzione = requestMethod.name() + "_" + bf.toString();
  81.        
  82.         if(nomeAzione.length()>255) {
  83.             nomeAzione = nomeAzione.substring(0, 255);
  84.         }
  85.         return nomeAzione;
  86.     }
  87.    
  88. }