ApiOperation.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.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.utils.beans.BaseBean;
  25. import org.openspcoop2.utils.rest.ProcessingException;
  26. import org.openspcoop2.utils.transport.http.HttpRequestMethod;

  27. /**
  28.  * ApiOperation
  29.  *
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ApiOperation extends BaseBean implements Serializable {

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.    
  41.     private HttpRequestMethod httpMethod;
  42.     private String path;
  43.     private String description;
  44.    
  45.     private ApiRequest request;
  46.    
  47.     private List<ApiResponse> responses = new ArrayList<ApiResponse>();
  48.    
  49.     public ApiOperation(HttpRequestMethod httpMethod,String path){
  50.         this.httpMethod = httpMethod;
  51.         this.path = path;
  52.         this.normalizePath();
  53.     }
  54.    
  55.     private void normalizePath(){
  56.         if(this.path!=null) {
  57.             this.path = normalizePath(this.path);
  58.         }
  59.     }
  60.     public static String normalizePath(String pathParam){
  61.         String path = pathParam;
  62.         if(path!=null) {
  63.             path = path.trim();
  64.             if(path.startsWith("/")==false){
  65.                 path = "/"+path;
  66.             }
  67.             while(path.contains("${")){
  68.                 // in wadl viene usato ${xx}
  69.                 // in swagger {xx}
  70.                 path = path.replace("${", "{");
  71.             }
  72.             if(path.length()>1 && path.endsWith("/")) {
  73.                 path = path.substring(0, path.length()-1);
  74.             }
  75.         }
  76.         return path;
  77.     }
  78.    
  79.     public String getDescription() {
  80.         return this.description;
  81.     }
  82.     public void setDescription(String description) {
  83.         this.description = description;
  84.     }
  85.     public HttpRequestMethod getHttpMethod() {
  86.         return this.httpMethod;
  87.     }
  88.     public void setHttpMethod(HttpRequestMethod httpMethod) {
  89.         this.httpMethod = httpMethod;
  90.     }
  91.     public String getPath() {
  92.         return this.path;
  93.     }
  94.     public void setPath(String path) {
  95.         this.path = path;
  96.     }
  97.     public int sizePath(){
  98.         return this._split(this.path).size();
  99.     }
  100.     public String getPath(int position) throws ProcessingException {
  101.         List<String> tmp = _split(this.path);
  102.         if(position>=tmp.size()){
  103.             throw new ProcessingException("Path ["+this.path+"] (split:"+tmp.size()+") non contiene posizione indicata ("+position+")");
  104.         }
  105.         return tmp.get(position);
  106.     }
  107.     public String getDynamicPathId(int position) throws ProcessingException {
  108.         List<String> tmp = _split(this.path);
  109.         if(position>=tmp.size()){
  110.             throw new ProcessingException("Path ["+this.path+"] (split:"+tmp.size()+") non contiene posizione indicata ("+position+")");
  111.         }
  112.         String p = tmp.get(position);
  113.         return p.substring(0,p.length()-1).substring(1);
  114.     }
  115.     public boolean isDynamicPath(){
  116.         return this.path.contains("{");
  117.     }  
  118.     public boolean isDynamicPath(int position) throws ProcessingException {
  119.         List<String> tmp = _split(this.path);
  120.         if(position>=tmp.size()){
  121.             throw new ProcessingException("Path ["+this.path+"] (split:"+tmp.size()+") non contiene posizione indicata ("+position+")");
  122.         }
  123.         return tmp.get(position).startsWith("{") && tmp.get(position).endsWith("}");
  124.     }
  125.    
  126.     private List<String> _split(String url){
  127.         List<String> l = new ArrayList<>();
  128.         for(String s : url.split("/")) {
  129.             if(s!=null && !s.equals("")) {
  130.                 l.add(s);
  131.             }
  132.         }
  133.         return l;
  134.     }
  135.        
  136.    
  137.     public ApiRequest getRequest() {
  138.         return this.request;
  139.     }

  140.     public void setRequest(ApiRequest request) {
  141.         this.request = request;
  142.     }
  143.    
  144.    
  145.     public void addResponse(ApiResponse response) {
  146.         this.responses.add(response);
  147.     }

  148.     public ApiResponse getResponse(int index) {
  149.         return this.responses.get( index );
  150.     }

  151.     public ApiResponse removeResponse(int index) {
  152.         return this.responses.remove( index );
  153.     }

  154.     public List<ApiResponse> getResponses() {
  155.         return this.responses;
  156.     }

  157.     public void setResponses(List<ApiResponse> responses) {
  158.         this.responses=responses;
  159.     }

  160.     public int sizeResponses() {
  161.         return this.responses.size();
  162.     }
  163. }