FunctionContextCustom.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.sdk.state;

  21. import java.util.HashMap;
  22. import java.util.Iterator;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.protocol.sdk.ProtocolException;
  25. import org.openspcoop2.protocol.sdk.constants.IDService;

  26. /**
  27.  * URL Protocol Context
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */


  33. public class FunctionContextCustom implements java.io.Serializable {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.     private String context;
  39.     private IDService idService;
  40.     private HashMap<String,IDService> subcontext;
  41.    
  42.     public FunctionContextCustom(String context,IDService idService) throws ProtocolException {
  43.         this(context,idService,null);
  44.     }
  45.     public FunctionContextCustom(String context,HashMap<String,IDService> subcontext) throws ProtocolException {
  46.         this(context,null,subcontext);
  47.     }
  48.     private FunctionContextCustom(String context,IDService idServiceParam, HashMap<String,IDService> subcontextMap) throws ProtocolException {
  49.        
  50.         if(context==null) {
  51.             throw new ProtocolException("Context undefined");
  52.         }
  53.         context = context.trim();
  54.         if(!StringUtils.isAlphanumeric(context)) {
  55.             throw new ProtocolException("Context '"+context+"' unsupported: only alphanumeric character permitted");
  56.         }
  57.         this.context = context;
  58.        
  59.         if(idServiceParam==null && (subcontextMap==null || subcontextMap.size()<=0)) {
  60.             throw new ProtocolException("IDService and Subcontext not found");
  61.         }
  62.        
  63.         if(idServiceParam!=null) {
  64.             checkIDService(idServiceParam);
  65.             this.idService = idServiceParam;
  66.         }

  67.         if(subcontextMap!=null && subcontextMap.size()>0) {
  68.             Iterator<String> it = subcontextMap.keySet().iterator();
  69.             while (it.hasNext()) {
  70.                 String subcontext = (String) it.next();
  71.                 IDService idService = subcontextMap.get(subcontext);
  72.                            
  73.                 // controllo che il subcontext non possieda un prefisso gia' utilizzato in un altro subcontext
  74.                 int count = 0;
  75.                 Iterator<String> itCheck = subcontextMap.keySet().iterator();
  76.                 while (itCheck.hasNext()) {
  77.                     String subcontextRegistered = (String) itCheck.next();
  78.                     if(subcontextRegistered.equals(subcontext)) {
  79.                         count++;
  80.                         continue;
  81.                     }
  82.                     if(subcontext.length()>=subcontextRegistered.length()) {
  83.                         if(subcontext.startsWith(subcontextRegistered)) {
  84.                             throw new ProtocolException("Subcontext '"+subcontext+"' not valid: exists another subcontext '"+subcontextRegistered+"' with same prefix");
  85.                         }
  86.                     }
  87.                 }
  88.                 if(count>1) {
  89.                     throw new ProtocolException("Subcontext '"+subcontext+"' registered more than one (founded:"+count+")");
  90.                 }
  91.            
  92.                 String tmp = new String(subcontext);
  93.                 while(tmp.contains("/")) {
  94.                     tmp = tmp.replace("/", "");
  95.                 }
  96.                 if(!StringUtils.isAlphanumeric(tmp)) {
  97.                     throw new ProtocolException("Subcontext '"+subcontext+"' unsupported: only alphanumeric character or '/' permitted");
  98.                 }
  99.                 if(subcontext.startsWith("/")) {
  100.                     throw new ProtocolException("Subcontext '"+subcontext+"' unsupported: cannot start with '/' character");
  101.                 }
  102.                 if(subcontext.endsWith("/")) {
  103.                     throw new ProtocolException("Subcontext '"+subcontext+"' unsupported: cannot ends with '/' character");
  104.                 }
  105.                
  106.                 checkIDService(idService);
  107.             }
  108.             this.subcontext = subcontextMap;
  109.         }
  110.     }
  111.    
  112.     private static void checkIDService(IDService idService) throws ProtocolException {
  113.         if(!IDService.PORTA_DELEGATA.equals(idService) &&
  114.                 !IDService.PORTA_DELEGATA_NIO.equals(idService) &&
  115.                 !IDService.PORTA_DELEGATA_XML_TO_SOAP.equals(idService) &&
  116.                 !IDService.PORTA_DELEGATA_XML_TO_SOAP_NIO.equals(idService) &&
  117.                 !IDService.PORTA_APPLICATIVA.equals(idService) &&
  118.                 !IDService.PORTA_APPLICATIVA_NIO.equals(idService) &&
  119.                 !IDService.INTEGRATION_MANAGER_SOAP.equals(idService) ) {
  120.             throw new ProtocolException("IDService '"+idService+"' unsupported");
  121.         }
  122.     }
  123.    
  124.     public boolean isMatch(String contextParam,String subContextParam) {
  125.         return this.getServiceMatch(contextParam, subContextParam)!=null;
  126.     }
  127.     public IDService getServiceMatch(String contextParam,String subContextParam) {
  128.         if(this.context.equals(contextParam)) {
  129.            
  130.             // cerco prima nel subcontext
  131.             if(subContextParam!=null && !"".equals(subContextParam) &&
  132.                     this.subcontext!=null && this.subcontext.size()>0) {
  133.                 Iterator<String> it = this.subcontext.keySet().iterator();
  134.                 while (it.hasNext()) {
  135.                     String subcontext = (String) it.next();
  136.                     if(subContextParam.startsWith(subcontext)) {
  137.                         IDService idService = this.subcontext.get(subcontext);
  138.                         return idService;
  139.                     }
  140.                 }
  141.             }
  142.            
  143.             // poi senza
  144.             if(this.idService!=null) {
  145.                 return this.idService;
  146.             }

  147.         }
  148.         return null;
  149.     }
  150.     public String getFunctionMatch(String contextParam,String subContextParam) {
  151.         if(this.context.equals(contextParam)) {
  152.            
  153.             // cerco prima nel subcontext
  154.             if(subContextParam!=null && !"".equals(subContextParam) &&
  155.                     this.subcontext!=null && this.subcontext.size()>0) {
  156.                 Iterator<String> it = this.subcontext.keySet().iterator();
  157.                 while (it.hasNext()) {
  158.                     String subcontext = (String) it.next();
  159.                     if(subContextParam.startsWith(subcontext)) {
  160.                         return this.context+"/"+subcontext;
  161.                     }
  162.                 }
  163.             }
  164.            
  165.             // poi senza
  166.             if(this.idService!=null) {
  167.                 return this.context;
  168.             }

  169.         }
  170.         return null;
  171.     }
  172.    
  173.     public String getContext() {
  174.         return this.context;
  175.     }
  176.     public void setContext(String context) {
  177.         this.context = context;
  178.     }

  179.     public IDService getIdService() {
  180.         return this.idService;
  181.     }
  182.     public void setIdService(IDService idService) {
  183.         this.idService = idService;
  184.     }
  185.    
  186.     public HashMap<String, IDService> getSubcontext() {
  187.         return this.subcontext;
  188.     }
  189. }