ApiParameterSchema.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.rest.api;

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

  23. import org.openspcoop2.utils.UtilsException;

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

  33.     private static String SEPARATOR_COMPLEX_TYPE = "|||";
  34.     private static String SEPARATOR_COMPLEX_TYPE_SPLIT = "\\|\\|\\|";
  35.    
  36.     private ApiParameterSchemaComplexType complexType = ApiParameterSchemaComplexType.simple;
  37.     private List<ApiParameterTypeSchema> schemas = new ArrayList<ApiParameterTypeSchema>();
  38.    
  39.     public boolean isDefined() {
  40.         return this.schemas!=null && !this.schemas.isEmpty();
  41.     }
  42.    
  43.     public ApiParameterSchemaComplexType getComplexType() {
  44.         return this.complexType;
  45.     }
  46.     public void setComplexType(ApiParameterSchemaComplexType complexType) {
  47.         this.complexType = complexType;
  48.     }
  49.     public List<ApiParameterTypeSchema> getSchemas() {
  50.         return this.schemas;
  51.     }
  52.     public void setSchemas(List<ApiParameterTypeSchema> schemas) {
  53.         this.schemas = schemas;
  54.     }
  55.     public void addType(String type, ApiSchemaTypeRestriction restriction) {
  56.         ApiParameterTypeSchema apts = new ApiParameterTypeSchema();
  57.         apts.setType(type);
  58.         apts.setSchema(restriction);
  59.         this.schemas.add(apts);
  60.     }
  61.    
  62.     public String getType() {
  63.         if(this.schemas!=null && !this.schemas.isEmpty() &&
  64.                 this.schemas.get(0)!=null) {
  65.             return this.schemas.get(0).getType();
  66.         }
  67.         return null;
  68.     }
  69.    
  70.     public static ApiParameterSchema toApiParameterSchema(String info) throws UtilsException {
  71.         if(info!=null) {
  72.             info = info.trim();
  73.         }
  74.         if(info==null || "".equals(info)) {
  75.             throw new UtilsException("Formato non valido");
  76.         }
  77.        
  78.         ApiParameterSchema schema = new ApiParameterSchema();
  79.         schema.setComplexType(ApiParameterSchemaComplexType.simple);
  80.         if(info.contains(SEPARATOR_COMPLEX_TYPE)) {
  81.             String[]tmp = info.split(SEPARATOR_COMPLEX_TYPE_SPLIT);
  82.             if(tmp==null || tmp.length<2) {
  83.                 throw new UtilsException("Formato non valido ("+info+")");
  84.             }
  85.             String complexType = tmp[0];
  86.             if(complexType==null) {
  87.                 throw new UtilsException("Formato non valido ("+info+"): complex type undefined");
  88.             }
  89.             complexType = complexType.trim();
  90.             try {
  91.                 ApiParameterSchemaComplexType t = ApiParameterSchemaComplexType.valueOf(complexType);
  92.                 schema.setComplexType(t);
  93.             }catch(Throwable t) {
  94.                 throw new UtilsException("Formato non valido ("+info+"): complex type '"+complexType+"' invalid: "+t.getMessage());
  95.             }
  96.             for (int i = 1; i < tmp.length; i++) {
  97.                 String tmpI= tmp[i];
  98.                 if(tmpI==null) {
  99.                     throw new UtilsException("Formato non valido ("+info+"): i="+i+" undefined?");
  100.                 }
  101.                 tmpI = tmpI.trim();
  102.                 try {
  103.                     ApiSchemaTypeRestriction apiStr = ApiSchemaTypeRestriction.toApiSchemaTypeRestriction(tmpI);
  104.                     ApiParameterTypeSchema apiTs = new ApiParameterTypeSchema();
  105.                     apiTs.setSchema(apiStr);
  106.                     if(apiStr.getFormat()!=null) {
  107.                         apiTs.setType(apiStr.getFormat());
  108.                     }
  109.                     else {
  110.                         apiTs.setType(apiStr.getType());
  111.                     }
  112.                     schema.getSchemas().add(apiTs);
  113.                 }catch(Throwable t) {
  114.                     throw new UtilsException("Formato non valido ("+info+"): i="+i+" '"+tmpI+"' invalid: "+t.getMessage());
  115.                 }
  116.             }
  117.         }
  118.         else {
  119.             ApiSchemaTypeRestriction apiStr = ApiSchemaTypeRestriction.toApiSchemaTypeRestriction(info);
  120.             ApiParameterTypeSchema apiTs = new ApiParameterTypeSchema();
  121.             apiTs.setSchema(apiStr);
  122.             if(apiStr.getFormat()!=null) {
  123.                 apiTs.setType(apiStr.getFormat());
  124.             }
  125.             else {
  126.                 apiTs.setType(apiStr.getType());
  127.             }
  128.             schema.getSchemas().add(apiTs);
  129.         }
  130.        
  131.         return schema;
  132.     }
  133.     public static String toString(ApiParameterSchema schema) throws UtilsException {
  134.         return schema.toString();
  135.     }

  136.     @Override
  137.     public String toString() {
  138.         StringBuilder sb = new StringBuilder();
  139.        
  140.         if(this.complexType==null || ApiParameterSchemaComplexType.simple.equals(this.complexType)) {
  141.             if(this.schemas==null || this.schemas.isEmpty()) {
  142.                 throw new RuntimeException("Schema undefined");
  143.             }
  144.             if(this.schemas.size()!=1) {
  145.                 throw new RuntimeException("Uncorrect Schema (found:"+this.schemas.size()+")");
  146.             }
  147.             ApiParameterTypeSchema ts = this.schemas.get(0);
  148.             if(ts==null) {
  149.                 throw new RuntimeException("Schema is empty?");
  150.             }
  151.             if(ts.getSchema()!=null) {
  152.                 sb.append(ts.getSchema().toString());
  153.             }
  154.             else {
  155.                 ApiSchemaTypeRestriction serialization = new ApiSchemaTypeRestriction();
  156.                 serialization.setType(ts.getType());
  157.                 sb.append(serialization.toString());
  158.             }
  159.         }
  160.         else {
  161.             sb.append(this.complexType);
  162.             if(this.schemas==null || this.schemas.isEmpty()) {
  163.                 throw new RuntimeException("Schema undefined");
  164.             }
  165.             for (ApiParameterTypeSchema ts : this.schemas) {
  166.                 sb.append(SEPARATOR_COMPLEX_TYPE);
  167.                 if(ts==null) {
  168.                     throw new RuntimeException("Schema is empty?");
  169.                 }
  170.                 if(ts.getSchema()!=null) {
  171.                     sb.append(ts.getSchema().toString());
  172.                 }
  173.                 else {
  174.                     ApiSchemaTypeRestriction serialization = new ApiSchemaTypeRestriction();
  175.                     serialization.setType(ts.getType());
  176.                     sb.append(serialization.toString());
  177.                 }
  178.             }
  179.         }
  180.        
  181.         return sb.toString();
  182.     }
  183. }