EsitoTransportContextIdentification.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.utils;

  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.openspcoop2.protocol.sdk.ProtocolException;
  25. import org.openspcoop2.utils.regexp.RegExpException;
  26. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  27. import org.openspcoop2.utils.regexp.RegExpNotValidException;
  28. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  29. import org.openspcoop2.utils.regexp.RegularExpressionPatternCompileMode;
  30. import org.openspcoop2.utils.transport.TransportUtils;



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

  39.     private String name;
  40.     private EsitoTransportContextIdentificationMode mode;
  41.     private String regularExpr;
  42.     private String value;
  43.     private String type;
  44.    
  45.     public String getName() {
  46.         return this.name;
  47.     }
  48.     public void setName(String name) {
  49.         this.name = name;
  50.     }
  51.     public EsitoTransportContextIdentificationMode getMode() {
  52.         return this.mode;
  53.     }
  54.     public void setMode(EsitoTransportContextIdentificationMode mode) {
  55.         this.mode = mode;
  56.     }
  57.     public String getRegularExpr() {
  58.         return this.regularExpr;
  59.     }
  60.     public void setRegularExpr(String regularExpr) {
  61.         this.regularExpr = regularExpr;
  62.     }
  63.     public String getValue() {
  64.         return this.value;
  65.     }
  66.     public void setValue(String value) {
  67.         this.value = value;
  68.     }
  69.     public String getType() {
  70.         return this.type;
  71.     }
  72.     public void setType(String type) {
  73.         this.type = type;
  74.     }
  75.    
  76.     public boolean match(Map<String, List<String>> p) throws ProtocolException{
  77.         Iterator<String> keys = p.keySet().iterator();
  78.         while (keys.hasNext()) {
  79.             String key = keys.next();
  80.             List<String> values = TransportUtils.getRawObject(p, key);
  81.             if(values!=null && !values.isEmpty()) {
  82.                 for (String valueKey : values) {
  83.                     if(key.equalsIgnoreCase(this.name)){
  84.                        
  85.                         // trovato header con nome atteso
  86.                         switch (this.mode) {
  87.                         case EXISTS:
  88.                             return true;
  89.                         case MATCH:
  90.                             try{
  91.                                 if(RegularExpressionEngine.isMatch(valueKey, this.regularExpr, RegularExpressionPatternCompileMode.CASE_INSENSITIVE)){
  92.                                     return true;
  93.                                 }
  94.                             }catch(RegExpNotFoundException notFound){  
  95.                                 continue;
  96.                             }catch(RegExpException | RegExpNotValidException exp){
  97.                                 throw new ProtocolException(exp.getMessage(),exp);
  98.                             }
  99.                             break;
  100.                         case CONTAINS:
  101.                             if(this.regularExpr==null){
  102.                                 if(this.value!=null && valueKey!=null && valueKey.toLowerCase().contains(this.value.toLowerCase())){
  103.                                     return true;
  104.                                 }
  105.                                 // else devo iterare sulla prossima key
  106.                             }
  107.                             else{
  108.                                 String valueRexExp = null;
  109.                                 try{
  110.                                     valueRexExp = RegularExpressionEngine.getStringMatchPattern(valueKey, this.regularExpr, RegularExpressionPatternCompileMode.CASE_INSENSITIVE);
  111.                                 }catch(RegExpNotFoundException notFound){  
  112.                                     continue;
  113.                                 }catch(RegExpException | RegExpNotValidException exp){
  114.                                     throw new ProtocolException(exp.getMessage(),exp);
  115.                                 }
  116.                                 if(this.value!=null && valueRexExp!=null && valueRexExp.toLowerCase().contains(this.value.toLowerCase())){
  117.                                     return true;
  118.                                 }
  119.                                 // else devo iterare sulla prossima key
  120.                             }
  121.                             break;
  122.                         case EQUALS:
  123.                             if(this.regularExpr==null){
  124.                                 if(this.value==null){
  125.                                     if(valueKey==null){
  126.                                         return true;
  127.                                     }
  128.                                     // else devo iterare sulla prossima key
  129.                                 }
  130.                                 else{
  131.                                     if(this.value.equalsIgnoreCase(valueKey)){
  132.                                         return true;
  133.                                     }
  134.                                     // else devo iterare sulla prossima key
  135.                                 }
  136.                             }
  137.                             else{
  138.                                 String valueRexExp = null;
  139.                                 try{
  140.                                     valueRexExp = RegularExpressionEngine.getStringMatchPattern(valueKey, this.regularExpr, RegularExpressionPatternCompileMode.CASE_INSENSITIVE);
  141.                                 }catch(RegExpNotFoundException notFound){  
  142.                                     continue;
  143.                                 }catch(RegExpException | RegExpNotValidException exp){
  144.                                     throw new ProtocolException(exp.getMessage(),exp);
  145.                                 }
  146.                                 if(this.value==null){
  147.                                     if(valueRexExp==null){
  148.                                         return true;
  149.                                     }
  150.                                     // else devo iterare sulla prossima key
  151.                                 }
  152.                                 else{
  153.                                     if(this.value.equalsIgnoreCase(valueRexExp)){
  154.                                         return true;
  155.                                     }
  156.                                     // else devo iterare sulla prossima key
  157.                                 }
  158.                             }
  159.                         }
  160.                        
  161.                     }      
  162.                 }
  163.             }
  164.         }
  165.         return false;
  166.     }
  167. }