ModIProfiliInterazioneRESTConfig.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.protocol.modipa.properties;

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

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.registry.Resource;
  25. import org.openspcoop2.core.registry.ResourceResponse;
  26. import org.openspcoop2.protocol.modipa.config.ModIProperties;
  27. import org.openspcoop2.protocol.modipa.constants.ModICostanti;
  28. import org.openspcoop2.protocol.sdk.ProtocolException;
  29. import org.openspcoop2.utils.transport.http.HttpRequestMethod;

  30. /**
  31.  * ModIProfiliInterazioneRESTConfig
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class ModIProfiliInterazioneRESTConfig {

  38.     private HttpRequestMethod httpRequest = null;
  39.    
  40.     private List<HttpRequestMethod> supportedHttpMethodBloccante = null;
  41.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePush = null;
  42.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePushRequest = null;
  43.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePushResponse = null;
  44.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePull = null;
  45.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePullRequest = null;
  46.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePullRequestState = null;
  47.     private List<HttpRequestMethod> supportedHttpMethodNonBloccantePullResponse = null;
  48.    
  49.     boolean compatibileBloccante = true;
  50.     boolean compatibileNonBloccantePush = true;
  51.     boolean compatibileNonBloccantePushRequest = true;
  52.     boolean compatibileNonBloccantePushResponse = true;
  53.     boolean compatibileNonBloccantePull = true;
  54.     boolean compatibileNonBloccantePullRequest = true;
  55.     boolean compatibileNonBloccantePullRequestState = true;
  56.     boolean compatibileNonBloccantePullResponse = true;
  57.    
  58.     public ModIProfiliInterazioneRESTConfig(ModIProperties modiProperties, String httpMethod, Resource resource) throws ProtocolException{
  59.        
  60.         if(!modiProperties.isRestProfiliInterazioneCheckCompatibility()) {
  61.             return;
  62.         }
  63.        
  64.         try {
  65.             if(httpMethod!=null && !StringUtils.isEmpty(httpMethod)) {
  66.                 this.httpRequest = HttpRequestMethod.valueOf(httpMethod);
  67.             }
  68.         }catch(Throwable t) { // qualsiasi }
  69.         }
  70.        
  71.         this.supportedHttpMethodBloccante = modiProperties.getRestBloccanteHttpMethod();
  72.         if(this.supportedHttpMethodBloccante!=null && !this.supportedHttpMethodBloccante.isEmpty()) {
  73.             this.compatibileBloccante = this.httpRequest!=null && this.supportedHttpMethodBloccante.contains(this.httpRequest);
  74.         }
  75.        
  76.         this.supportedHttpMethodNonBloccantePush = modiProperties.getRestNonBloccantePushHttpMethod();
  77.         if(this.supportedHttpMethodNonBloccantePush!=null && !this.supportedHttpMethodNonBloccantePush.isEmpty()) {
  78.             this.compatibileNonBloccantePush = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePush.contains(this.httpRequest);
  79.         }
  80.         this.supportedHttpMethodNonBloccantePushRequest = modiProperties.getRestNonBloccantePushRequestHttpMethod();
  81.         if(this.supportedHttpMethodNonBloccantePushRequest!=null && !this.supportedHttpMethodNonBloccantePushRequest.isEmpty()) {
  82.             this.compatibileNonBloccantePushRequest = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePushRequest.contains(this.httpRequest);
  83.         }
  84.         this.supportedHttpMethodNonBloccantePushResponse = modiProperties.getRestNonBloccantePushResponseHttpMethod();
  85.         if(this.supportedHttpMethodNonBloccantePushResponse!=null && !this.supportedHttpMethodNonBloccantePushResponse.isEmpty()) {
  86.             this.compatibileNonBloccantePushResponse = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePushResponse.contains(this.httpRequest);
  87.         }
  88.    
  89.         this.supportedHttpMethodNonBloccantePull = modiProperties.getRestNonBloccantePullHttpMethod();
  90.         if(this.supportedHttpMethodNonBloccantePull!=null && !this.supportedHttpMethodNonBloccantePull.isEmpty()) {
  91.             this.compatibileNonBloccantePull = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePull.contains(this.httpRequest);
  92.         }
  93.         this.supportedHttpMethodNonBloccantePullRequest = modiProperties.getRestNonBloccantePullRequestHttpMethod();
  94.         if(this.supportedHttpMethodNonBloccantePullRequest!=null && !this.supportedHttpMethodNonBloccantePullRequest.isEmpty()) {
  95.             this.compatibileNonBloccantePullRequest = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePullRequest.contains(this.httpRequest);
  96.         }
  97.         this.supportedHttpMethodNonBloccantePullRequestState = modiProperties.getRestNonBloccantePullRequestStateHttpMethod();
  98.         if(this.supportedHttpMethodNonBloccantePullRequestState!=null && !this.supportedHttpMethodNonBloccantePullRequestState.isEmpty()) {
  99.             this.compatibileNonBloccantePullRequestState = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePullRequestState.contains(this.httpRequest);
  100.         }
  101.         this.supportedHttpMethodNonBloccantePullResponse = modiProperties.getRestNonBloccantePullResponseHttpMethod();
  102.         if(this.supportedHttpMethodNonBloccantePullResponse!=null && !this.supportedHttpMethodNonBloccantePullResponse.isEmpty()) {
  103.             this.compatibileNonBloccantePullResponse = this.httpRequest!=null && this.supportedHttpMethodNonBloccantePullResponse.contains(this.httpRequest);
  104.         }
  105.        
  106.         List<Integer> resourceHttpCodes = new ArrayList<Integer>();
  107.         boolean checkHttpCodes = false;
  108.         if(resource!=null && resource.sizeResponseList()>0) {
  109.             checkHttpCodes = true; // caricato via interfaccia o comunque definite le risposte a mano
  110.             for (ResourceResponse response : resource.getResponseList()) {
  111.                 if(response.getStatus()>0) {
  112.                     resourceHttpCodes.add(response.getStatus());
  113.                 }
  114.             }
  115.         }
  116.         if(checkHttpCodes) {
  117.             if(this.compatibileBloccante) {
  118.                 this.compatibileBloccante = isCompatible(resourceHttpCodes, modiProperties.getRestBloccanteHttpStatus());
  119.             }
  120.             if(this.compatibileNonBloccantePush) {
  121.                 if(this.compatibileNonBloccantePushRequest) {
  122.                     this.compatibileNonBloccantePushRequest = isCompatible(resourceHttpCodes, modiProperties.getRestNonBloccantePushRequestHttpStatus());
  123.                 }
  124.                 if(this.compatibileNonBloccantePushResponse) {
  125.                     this.compatibileNonBloccantePushResponse = isCompatible(resourceHttpCodes, modiProperties.getRestNonBloccantePushResponseHttpStatus());
  126.                 }
  127.                 if(!this.compatibileNonBloccantePushRequest && !this.compatibileNonBloccantePushResponse) {
  128.                     this.compatibileNonBloccantePush = false;
  129.                 }
  130.             }
  131.             if(this.compatibileNonBloccantePull) {
  132.                 if(this.compatibileNonBloccantePullRequest) {
  133.                     this.compatibileNonBloccantePullRequest = isCompatible(resourceHttpCodes, modiProperties.getRestNonBloccantePullRequestHttpStatus());
  134.                 }
  135.                 if(this.compatibileNonBloccantePullRequestState) {
  136.                     Integer [] ok = modiProperties.getRestNonBloccantePullRequestStateOkHttpStatus();
  137.                     Integer [] notReady = modiProperties.getRestNonBloccantePullRequestStateNotReadyHttpStatus();
  138.                     List<Integer> list = new ArrayList<Integer>();
  139.                     if(ok!=null && ok.length>0) {
  140.                         for (Integer v : ok) {
  141.                             list.add(v);
  142.                         }
  143.                     }
  144.                     if(notReady!=null && notReady.length>0) {
  145.                         for (Integer v : notReady) {
  146.                             list.add(v);
  147.                         }
  148.                     }
  149.                     this.compatibileNonBloccantePullRequestState = isCompatible(resourceHttpCodes, (!list.isEmpty()) ? list.toArray(new Integer[1]) : null );
  150.                 }
  151.                 if(this.compatibileNonBloccantePullResponse) {
  152.                     this.compatibileNonBloccantePullResponse = isCompatible(resourceHttpCodes, modiProperties.getRestNonBloccantePullResponseHttpStatus());
  153.                 }
  154.                 if(!this.compatibileNonBloccantePullRequest && !this.compatibileNonBloccantePullRequestState && !this.compatibileNonBloccantePullResponse) {
  155.                     this.compatibileNonBloccantePull = false;
  156.                 }
  157.             }
  158.         }
  159.     }
  160.    
  161.     private static boolean isCompatible(List<Integer> resourceHttpCodes, Integer [] returnCodeAttesi) {
  162.         if(resourceHttpCodes.isEmpty()) {
  163.             return false;
  164.         }
  165.         else {
  166.             if(returnCodeAttesi!=null) {
  167.                 boolean found = false;
  168.                 for (Integer integer : returnCodeAttesi) {
  169.                     if(integer.intValue() == ModICostanti.MODIPA_PROFILO_INTERAZIONE_HTTP_CODE_2XX_INT_VALUE) {
  170.                         for (Integer resourceHttpCode : resourceHttpCodes) {
  171.                             if((resourceHttpCode.intValue() >= 200) && (resourceHttpCode.intValue()<=299) ) {
  172.                                 found = true;
  173.                                 break;
  174.                             }
  175.                         }
  176.                         if(found) {
  177.                             break;
  178.                         }
  179.                     }
  180.                     else {
  181.                         for (Integer resourceHttpCode : resourceHttpCodes) {
  182.                             if( resourceHttpCode.intValue() == integer.intValue() ) {
  183.                                 found = true;
  184.                                 break;
  185.                             }
  186.                         }
  187.                         if(found) {
  188.                             break;
  189.                         }
  190.                     }
  191.                 }
  192.                 return found;
  193.             }
  194.         }
  195.        
  196.         return true;
  197.     }
  198.    
  199.     public boolean isCompatibileBloccante() {
  200.         return this.compatibileBloccante;
  201.     }

  202.     public boolean isCompatibileNonBloccantePush() {
  203.         return this.compatibileNonBloccantePush;
  204.     }

  205.     public boolean isCompatibileNonBloccantePushRequest() {
  206.         return this.compatibileNonBloccantePushRequest;
  207.     }

  208.     public boolean isCompatibileNonBloccantePushResponse() {
  209.         return this.compatibileNonBloccantePushResponse;
  210.     }

  211.     public boolean isCompatibileNonBloccantePull() {
  212.         return this.compatibileNonBloccantePull;
  213.     }

  214.     public boolean isCompatibileNonBloccantePullRequest() {
  215.         return this.compatibileNonBloccantePullRequest;
  216.     }

  217.     public boolean isCompatibileNonBloccantePullRequestState() {
  218.         return this.compatibileNonBloccantePullRequestState;
  219.     }

  220.     public boolean isCompatibileNonBloccantePullResponse() {
  221.         return this.compatibileNonBloccantePullResponse;
  222.     }
  223. }