MainValidatorOpenAPI4.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.openapi.validator;
  21. import java.io.File;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.apache.logging.log4j.Level;
  26. import org.openspcoop2.utils.LoggerWrapperFactory;
  27. import org.openspcoop2.utils.resources.FileSystemUtilities;
  28. import org.openspcoop2.utils.rest.ApiFactory;
  29. import org.openspcoop2.utils.rest.ApiFormats;
  30. import org.openspcoop2.utils.rest.ApiReaderConfig;
  31. import org.openspcoop2.utils.rest.IApiReader;
  32. import org.openspcoop2.utils.rest.IApiValidator;
  33. import org.openspcoop2.utils.rest.api.Api;
  34. import org.openspcoop2.utils.rest.api.ApiSchema;
  35. import org.openspcoop2.utils.rest.api.ApiSchemaType;

  36. /**
  37.  * Test (Estende i test giĆ  effettuati in TestOpenApi3
  38.  *
  39.  * @author Andrea Poli (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  *
  43.  */
  44. public class MainValidatorOpenAPI4 {

  45.     public static void main(String[] args) throws Exception {
  46.        
  47.         OpenAPILibrary openApiLibrary = OpenAPILibrary.swagger_request_validator;
  48.                
  49.         LoggerWrapperFactory.setDefaultConsoleLogConfiguration(Level.ERROR);
  50.        
  51.         if(args==null || args.length<1) {
  52.             throw new Exception("Usage: MainValidatorOpenAPI4 <file> [dirIncludes]");
  53.         }
  54.        
  55.         String file = args[0].trim();
  56.         File f = new File(file);
  57.         if(f.exists()==false) {
  58.             throw new Exception("File '"+f.getAbsolutePath()+"' not exists");
  59.         }
  60.         if(f.canRead()==false) {
  61.             throw new Exception("File '"+f.getAbsolutePath()+"' cannot read");
  62.         }
  63.        
  64.         File fDir = null;
  65.         if(args.length>1) {
  66.             fDir = new File(args[1].trim());
  67.             if(fDir.exists()==false) {
  68.                 throw new Exception("File '"+fDir.getAbsolutePath()+"' not exists");
  69.             }
  70.             if(fDir.canRead()==false) {
  71.                 throw new Exception("File '"+fDir.getAbsolutePath()+"' cannot read");
  72.             }
  73.             if(fDir.isDirectory()==false) {
  74.                 throw new Exception("File '"+fDir.getAbsolutePath()+"' is not directory");
  75.             }
  76.         }
  77.        
  78.         boolean output = false;
  79.         if(args.length>2) {
  80.             output = Boolean.valueOf(args[2]);
  81.         }
  82.        
  83.         try {
  84.        
  85.             URL url = f.toURI().toURL();
  86.            
  87.             List<ApiSchema> listApiSchema = null;
  88.             if(fDir!=null) {
  89.                 File[] files = fDir.listFiles();
  90.                 if(files!=null) {
  91.                     for (File fChild : files) {
  92.                         if(fChild.exists()==false) {
  93.                             throw new Exception("File '"+fChild.getAbsolutePath()+"' not exists");
  94.                         }
  95.                         if(fChild.canRead()==false) {
  96.                             throw new Exception("File '"+fChild.getAbsolutePath()+"' cannot read");
  97.                         }
  98.                        
  99.                         // evito file master openapi se risiede nella stessa directory
  100.                         if(fChild.getName().equals(f.getName())){
  101.                             continue;
  102.                         }
  103.                        
  104.                         ApiSchema apiSchema = null;
  105.                         if(fChild.getName().toLowerCase().endsWith(".yaml")) {
  106.                             apiSchema= new ApiSchema(fChild.getName(),
  107.                                     FileSystemUtilities.readBytesFromFile(fChild),
  108.                                     ApiSchemaType.YAML);
  109.                         }
  110.                         else if(fChild.getName().toLowerCase().endsWith(".json")) {
  111.                             apiSchema= new ApiSchema(fChild.getName(),
  112.                                     FileSystemUtilities.readBytesFromFile(fChild),
  113.                                     ApiSchemaType.JSON);
  114.                         }
  115.                         if(apiSchema!=null) {
  116.                             if(listApiSchema==null) {
  117.                                 listApiSchema = new ArrayList<ApiSchema>();
  118.                             }
  119.                             listApiSchema.add(apiSchema);
  120.                         }
  121.                     }
  122.                 }
  123.             }
  124.             ApiSchema [] schemas = null;
  125.             if(listApiSchema!=null && !listApiSchema.isEmpty()) {
  126.                 schemas = listApiSchema.toArray(new ApiSchema[1]);
  127.             }
  128.            
  129.             IApiReader apiReader = ApiFactory.newApiReader(ApiFormats.OPEN_API_3);
  130.             ApiReaderConfig config = new ApiReaderConfig();
  131.             config.setProcessInclude(false);
  132.             apiReader.init(LoggerWrapperFactory.getLogger(MainValidatorOpenAPI4.class), new File(url.toURI()), config, schemas);
  133.             Api api = apiReader.read();
  134.             IApiValidator apiValidator = ApiFactory.newApiValidator(ApiFormats.OPEN_API_3);
  135.             OpenapiApiValidatorConfig configO = new OpenapiApiValidatorConfig();
  136.             configO.setOpenApiValidatorConfig(new OpenapiLibraryValidatorConfig());
  137.             configO.getOpenApiValidatorConfig().setOpenApiLibrary(openApiLibrary);
  138.            
  139.             apiValidator.init(LoggerWrapperFactory.getLogger(MainValidatorOpenAPI4.class), api, configO);
  140.            
  141.             System.err.println("Validazione effettuata con successo");
  142.            
  143.         }catch(Exception e) {
  144.             if(output) {
  145.                 System.err.println("!ERRORE! Validazione fallita");
  146.                 e.printStackTrace(System.err);
  147.             }
  148.             else {
  149.                 throw e;
  150.             }
  151.         }
  152.        
  153.     }

  154. }