RegExpUtilities.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.regexp;

  21. import java.io.File;

  22. import org.openspcoop2.utils.UtilsException;

  23. /**
  24.  * Contiene utilities
  25.  *
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */

  30. public class RegExpUtilities {

  31.     /* VALIDAZIONI */
  32.    
  33.     private static final String oneAlpha = "(.)*((\\p{Alpha})|[-])(.)*";
  34.     private static final String domainIdentifier = "((\\p{Alnum})([-]|(\\p{Alnum}))*(\\p{Alnum}))|(\\p{Alnum})";
  35.     private static final String domainNameRule = "(" + RegExpUtilities.domainIdentifier + ")((\\.)(" + RegExpUtilities.domainIdentifier + "))*";
  36.    
  37.     public static boolean isDefinedByVariable(String value) throws java.net.MalformedURLException{
  38.         boolean definedByVariable = value!=null && value.contains("${") && value.contains("}");
  39.         return definedByVariable;
  40.     }
  41.    
  42.     public static boolean isUrlDefinedByVariable(String url) throws java.net.MalformedURLException{
  43.         return isDefinedByVariable(url);
  44.     }
  45.     public static void validateUrl(String url) throws java.net.MalformedURLException{
  46.         validateUrl(url, false);
  47.     }
  48.     public static void validateUrl(String url, boolean skipCheckUrlDefinedByVariable) throws java.net.MalformedURLException{
  49.         boolean urlDefinedByVariable = isUrlDefinedByVariable(url);
  50.         if( (!urlDefinedByVariable) || (!skipCheckUrlDefinedByVariable) ) {
  51.             // se sono presenti le { } la url può essere costruita dinamicamente
  52.             java.net.URL testUrl = new java.net.URL(url);
  53.             testUrl.toString();
  54.         }
  55.     }
  56.    
  57.     public static void validateUri(String uri) throws UtilsException,java.net.MalformedURLException{
  58.         validateUri(uri, false);
  59.     }
  60.     public static void validateUri(String uri, boolean skipCheckUrlDefinedByVariable) throws UtilsException,java.net.MalformedURLException{
  61.         if (uri.startsWith("http://")
  62.                 || uri.startsWith("file://")) {

  63.             RegExpUtilities.validateUrl(uri);

  64.         } else {
  65.             File f = new File(uri);
  66.             f.getAbsolutePath();
  67.         }
  68.     }
  69.    
  70.     public static boolean validateDomainName(String domainName) {
  71.         if ((domainName == null) || (domainName.length() > 63)) {
  72.             return false;
  73.         }
  74.         return domainName.matches(RegExpUtilities.domainNameRule) && domainName.matches(RegExpUtilities.oneAlpha);
  75.     }

  76.     public static boolean isIPOrHostname(String stringToCheck) {
  77.         boolean ok = true;

  78.         try {
  79.             if (! RegularExpressionEngine.isMatch(stringToCheck, "^[0-9]+.[0-9]+.[0-9]+.[0-9]+$") &&
  80.                     !RegExpUtilities.validateDomainName(stringToCheck))
  81.                 ok = false;
  82.         } catch (Exception e) {
  83.             java.util.Date now = new java.util.Date();
  84.             System.err.println(now + " Exception " + e.getMessage());
  85.         }

  86.         return ok;
  87.     }
  88.    
  89. }