Api.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.io.Serializable;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.beans.BaseBean;
  28. import org.openspcoop2.utils.rest.ParseWarningException;
  29. import org.openspcoop2.utils.rest.ProcessingException;
  30. import org.openspcoop2.utils.transport.http.HttpRequestMethod;

  31. /**
  32.  * ApiOperation
  33.  *
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public abstract class Api extends BaseBean implements Serializable {

  40.     /**
  41.      *
  42.      */
  43.     private static final long serialVersionUID = 1L;
  44.    
  45.     private String name;
  46.     private String description;
  47.     private URL baseURL;
  48.    
  49.     private List<ApiSchema> schemas = new ArrayList<>();

  50.    
  51.     private Map<String, Serializable> mapSerializableVendorImpl = new HashMap<String, Serializable>();
  52.    
  53.     public Serializable getVendorImpl(String key) {
  54.         return this.mapSerializableVendorImpl.get(key);
  55.     }
  56.     public void removeVendorImpl(String key) {
  57.         this.mapSerializableVendorImpl.remove(key);
  58.     }
  59.     public void addVendorImpl(String key, Serializable serializable) {
  60.         this.mapSerializableVendorImpl.put(key, serializable);
  61.     }
  62.     public boolean containsKey(String key) {
  63.         return this.mapSerializableVendorImpl.containsKey(key);
  64.     }
  65.    
  66.    
  67.     public void addSchema(ApiSchema schema) {
  68.         this.schemas.add(schema);
  69.     }

  70.     public ApiSchema getSchema(int index) {
  71.         return this.schemas.get( index );
  72.     }

  73.     public ApiSchema removeSchema(int index) {
  74.         return this.schemas.remove( index );
  75.     }

  76.     public List<ApiSchema> getSchemas() {
  77.         return this.schemas;
  78.     }

  79.     public void setSchemas(List<ApiSchema> schemas) {
  80.         this.schemas=schemas;
  81.     }

  82.     public int sizeSchemas() {
  83.         return this.schemas.size();
  84.     }
  85.    
  86.    
  87.    
  88.     private List<ApiOperation> operations = new ArrayList<>();

  89.     public void addOperation(ApiOperation operation) {
  90.         this.operations.add(operation);
  91.     }

  92.     public ApiOperation getOperation(int index) {
  93.         return this.operations.get( index );
  94.     }

  95.     public ApiOperation removeOperation(int index) {
  96.         return this.operations.remove( index );
  97.     }

  98.     public List<ApiOperation> getOperations() {
  99.         return this.operations;
  100.     }

  101.     public void setOperations(List<ApiOperation> operations) {
  102.         this.operations=operations;
  103.     }

  104.     public int sizeOperations() {
  105.         return this.operations.size();
  106.     }
  107.    
  108.     public ApiOperation findOperation(HttpRequestMethod httpMethod,String url) throws ProcessingException{
  109.         return ApiUtilities.findOperation(this, httpMethod, url, true);
  110.     }
  111.    
  112.    
  113.     public String getName() {
  114.         return this.name;
  115.     }

  116.     public void setName(String name) {
  117.         this.name = name;
  118.     }

  119.     public String getDescription() {
  120.         return this.description;
  121.     }

  122.     public void setDescription(String description) {
  123.         this.description = description;
  124.     }

  125.     public URL getBaseURL() {
  126.         return this.baseURL;
  127.     }

  128.     public void setBaseURL(URL baseURL) {
  129.         this.baseURL = baseURL;
  130.     }
  131.    
  132.     public void validate() throws ProcessingException,ParseWarningException {
  133.         this.validate(false);
  134.     }
  135.     public void validate(boolean validateBodyParameterElement) throws ProcessingException, ParseWarningException {
  136.         this.validate(false, false);
  137.     }
  138.     public void validate(boolean usingFromSetProtocolInfo, boolean validateBodyParameterElement) throws ProcessingException, ParseWarningException {
  139.        
  140.         if(this.operations.size()<=0) {
  141.             throw new ProcessingException("Paths and Operations undefined");
  142.         }
  143.        
  144.         for (int i = 0; i < this.operations.size(); i++) {
  145.            
  146.             ApiOperation op = this.operations.get(i);
  147.            
  148.             String prefix = "Operation["+i+"] ";
  149.            
  150.             if(op.getHttpMethod()==null) {
  151.                 throw new ProcessingException("HttpMethod is null");
  152.             }
  153.             if(op.getPath()==null) {
  154.                 throw new ProcessingException("Path is null");
  155.             }
  156.             prefix = "Operation["+op.getHttpMethod().name()+" "+op.getPath()+"] ";
  157.            
  158.             if(op.getRequest()!=null) {
  159.                 String pRequest = prefix +"[request] ";
  160.                
  161.                 for (int j = 0; j < op.getRequest().sizeBodyParameters(); j++) {
  162.                     ApiBodyParameter bodyParm = op.getRequest().getBodyParameter(j);
  163.                     if(bodyParm.getMediaType()==null) {
  164.                         throw new ProcessingException(pRequest+"MediaType undefined in body parameter (position '"+j+"')");
  165.                     }
  166.                     if(validateBodyParameterElement) {
  167.                         if(bodyParm.getElement()==null) {
  168.                             throw new ProcessingException(pRequest+"Element undefined in body parameter '"+bodyParm.getMediaType()+"'");
  169.                         }
  170.                     }
  171.                 }
  172.                
  173.                 for (int j = 0; j < op.getRequest().sizeCookieParameters(); j++) {
  174.                     ApiCookieParameter par = op.getRequest().getCookieParameter(j);
  175.                     if(par.getName()==null) {
  176.                         throw new ProcessingException(pRequest+"Name undefined in cookie parameter (position '"+j+"')");
  177.                     }
  178.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  179.                         throw new ProcessingException(pRequest+"Type undefined in cookie parameter '"+par.getName()+"'");
  180.                     }
  181.                 }
  182.                
  183.                 for (int j = 0; j < op.getRequest().sizeDynamicPathParameters(); j++) {
  184.                     ApiRequestDynamicPathParameter par = op.getRequest().getDynamicPathParameter(j);
  185.                     if(par.getName()==null) {
  186.                         throw new ProcessingException(pRequest+"Name undefined in dynamic path parameter (position '"+j+"')");
  187.                     }
  188.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  189.                         throw new ProcessingException(pRequest+"Type undefined in dynamic path parameter '"+par.getName()+"'");
  190.                     }
  191.                 }
  192.                
  193.                 for (int j = 0; j < op.getRequest().sizeFormParameters(); j++) {
  194.                     ApiRequestFormParameter par = op.getRequest().getFormParameter(j);
  195.                     if(par.getName()==null) {
  196.                         throw new ProcessingException(pRequest+"Name undefined in form parameter (position '"+j+"')");
  197.                     }
  198.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  199.                         throw new ProcessingException(pRequest+"Type undefined in form parameter '"+par.getName()+"'");
  200.                     }
  201.                 }
  202.                
  203.                 for (int j = 0; j < op.getRequest().sizeHeaderParameters(); j++) {
  204.                     ApiHeaderParameter par = op.getRequest().getHeaderParameter(j);
  205.                     if(par.getName()==null) {
  206.                         throw new ProcessingException(pRequest+"Name undefined in header parameter (position '"+j+"')");
  207.                     }
  208.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  209.                         throw new ProcessingException(pRequest+"Type undefined in header parameter '"+par.getName()+"'");
  210.                     }
  211.                 }
  212.                
  213.                 for (int j = 0; j < op.getRequest().sizeQueryParameters(); j++) {
  214.                     ApiRequestQueryParameter par = op.getRequest().getQueryParameter(j);
  215.                     if(par.getName()==null) {
  216.                         throw new ProcessingException(pRequest+"Name undefined in query parameter (position '"+j+"')");
  217.                     }
  218.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  219.                         throw new ProcessingException(pRequest+"Type undefined in query parameter '"+par.getName()+"'");
  220.                     }
  221.                 }
  222.             }
  223.            
  224.             boolean defaultResponse = false;
  225.            
  226.             for (int k = 0; k < op.sizeResponses(); k++) {
  227.        
  228.                 String pResponse = prefix +"[response '"+k+"'] ";
  229.                
  230.                 ApiResponse response = op.getResponse(k);
  231.                
  232.                 if(response.isDefaultHttpReturnCode()) {
  233.                     if(defaultResponse) {
  234.                         throw new ProcessingException(pResponse+"Http Return Code Default already defined");
  235.                     }
  236.                     else {
  237.                         defaultResponse = true;
  238.                     }
  239.                 }
  240.                 if(response.getHttpReturnCode()<=0 && !response.isDefaultHttpReturnCode()) {
  241.                     throw new ProcessingException(pResponse+"Http Return Code undefined");
  242.                 }
  243.        
  244.                 if(response.isDefaultHttpReturnCode()) {
  245.                     pResponse = prefix +"[response status 'default'] ";
  246.                 }
  247.                 else {
  248.                     pResponse = prefix +"[response status '"+response.getHttpReturnCode()+"'] ";
  249.                 }
  250.                
  251.                 for (int j = 0; j < response.sizeBodyParameters(); j++) {
  252.                     ApiBodyParameter bodyParm = response.getBodyParameter(j);
  253.                     if(bodyParm.getMediaType()==null) {
  254.                         throw new ProcessingException(pResponse+"MediaType undefined in body parameter (position '"+j+"')");
  255.                     }
  256.                     if(validateBodyParameterElement) {
  257.                         if(bodyParm.getElement()==null) {
  258.                             throw new ProcessingException(pResponse+"Element undefined in body parameter '"+bodyParm.getMediaType()+"'");
  259.                         }
  260.                     }
  261.                 }
  262.                
  263.                 for (int j = 0; j < response.sizeCookieParameters(); j++) {
  264.                     ApiCookieParameter par = response.getCookieParameter(j);
  265.                     if(par.getName()==null) {
  266.                         throw new ProcessingException(pResponse+"Name undefined in cookie parameter (position '"+j+"')");
  267.                     }
  268.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  269.                         throw new ProcessingException(pResponse+"Type undefined in cookie parameter '"+par.getName()+"'");
  270.                     }
  271.                 }
  272.                
  273.                 for (int j = 0; j < response.sizeHeaderParameters(); j++) {
  274.                     ApiHeaderParameter par = response.getHeaderParameter(j);
  275.                     if(par.getName()==null) {
  276.                         throw new ProcessingException(pResponse+"Name undefined in header parameter (position '"+j+"')");
  277.                     }
  278.                     if(par.getApiParameterSchema()==null || !par.getApiParameterSchema().isDefined()) {
  279.                         throw new ProcessingException(pResponse+"Type undefined in header parameter '"+par.getName()+"'");
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.     }
  285. }