ContextUrlCollection.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.message.config;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.message.constants.MessageType;
  25. import org.openspcoop2.message.exception.MessageException;

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

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.    
  39.     // Vengono utilizzate due liste per preservare l'ordine di inserimento che si perde in una hashtable,
  40.     private List<String> map_contextUrl = new ArrayList<>();
  41.     private List<MessageType> map_messageProcessor = new ArrayList<MessageType>();
  42.     private List<List<String>> map_contentTypesRestriction = new ArrayList<List<String>>();

  43.     private SoapBinding soapBinding;
  44.     private RestBinding restBinding;
  45.    
  46.     public ContextUrlCollection(SoapBinding soapBinding, RestBinding restBinding){
  47.         this.soapBinding = soapBinding;
  48.         this.restBinding = restBinding;
  49.     }
  50.    
  51.     public int sizeContextUrl(){
  52.         return this.map_contextUrl.size();
  53.     }
  54.    
  55.     private void checkVersion(MessageType version) throws MessageException{
  56.         if(MessageType.XML.equals(version)){
  57.             if(this.restBinding==null){
  58.                 throw new MessageException("Rest disabled");
  59.             }
  60.             if(!this.restBinding.isBinding_xml()){
  61.                 throw new MessageException("MessageType ["+version+"] not supported in RestBinding; Xml disabled");
  62.             }
  63.         }
  64.         else if(MessageType.JSON.equals(version)){
  65.             if(this.restBinding==null){
  66.                 throw new MessageException("Rest disabled");
  67.             }
  68.             if(!this.restBinding.isBinding_json()){
  69.                 throw new MessageException("MessageType ["+version+"] not supported in RestBinding; Json disabled");
  70.             }
  71.         }
  72.         else if(MessageType.BINARY.equals(version)){
  73.             if(this.restBinding==null){
  74.                 throw new MessageException("Rest disabled");
  75.             }
  76.             if(!this.restBinding.isBinding_binary()){
  77.                 throw new MessageException("MessageType ["+version+"] not supported in RestBinding; Binary disabled");
  78.             }
  79.         }
  80.         else if(MessageType.MIME_MULTIPART.equals(version)){
  81.             if(this.restBinding==null){
  82.                 throw new MessageException("Rest disabled");
  83.             }
  84.             if(!this.restBinding.isBinding_mimeMultipart()){
  85.                 throw new MessageException("MessageType ["+version+"] not supported in RestBinding; MimeMultipart disabled");
  86.             }
  87.         }
  88.         else if(MessageType.SOAP_11.equals(version)){
  89.             if(this.soapBinding==null){
  90.                 throw new MessageException("Soap disabled");
  91.             }
  92.             if(!this.soapBinding.isBinding_soap11()){
  93.                 throw new MessageException("MessageType ["+version+"] not supported in SoapBinding; Soap11 disabled");
  94.             }
  95.         }
  96.         else if(MessageType.SOAP_12.equals(version)){
  97.             if(this.soapBinding==null){
  98.                 throw new MessageException("Soap disabled");
  99.             }
  100.             if(!this.soapBinding.isBinding_soap12()){
  101.                 throw new MessageException("MessageType ["+version+"] not supported in SoapBinding; Soap12 disabled");
  102.             }
  103.         }
  104.         else{
  105.             throw new MessageException("MessageType ["+version+"] not supported in Binding");
  106.         }
  107.     }
  108.    
  109.     public void addContext(String protocol, String function, String functionParam,MessageType version,String ... contentTypesRestriction) throws MessageException{
  110.         String context = ServiceBindingConfiguration.normalizeContext(protocol);
  111.         String f = ServiceBindingConfiguration.normalizeContext(function);
  112.         String subContext_functionParam = ServiceBindingConfiguration.normalizeContext(functionParam);
  113.        
  114.         String key = context +" - " + f +" - " +subContext_functionParam;
  115.        
  116.         if(version==null){
  117.             throw new MessageException("MessageProcessorVersion not defined");
  118.         }
  119.         this.checkVersion(version);
  120.         if(this.map_contextUrl.contains(key)){
  121.             throw new MessageException("ContextUrl already defined for MessageType "+this.getMessageType(protocol,function,functionParam));
  122.         }
  123.         this.map_contextUrl.add(key);
  124.         this.map_messageProcessor.add(version);
  125.        
  126.         List<String> contentTypesRestrictionList = new ArrayList<>();
  127.         if(contentTypesRestriction!=null){
  128.             for (int i = 0; i < contentTypesRestriction.length; i++) {
  129.                 contentTypesRestrictionList.add(contentTypesRestriction[i]);
  130.             }
  131.         }
  132.         this.map_contentTypesRestriction.add(contentTypesRestrictionList);
  133.     }
  134.    
  135.     public MessageType getMessageType(String protocol, String function, String functionParam) throws MessageException{
  136.         String context = ServiceBindingConfiguration.normalizeContext(protocol);
  137.         String f = ServiceBindingConfiguration.normalizeContext(function);
  138.         String subContext_functionParam = ServiceBindingConfiguration.normalizeContext(functionParam);
  139.        
  140.         String key = context +" - " + f +" - " +subContext_functionParam;
  141.        
  142.         for (int i = 0; i < this.map_contextUrl.size(); i++) {
  143.             if(this.map_contextUrl.get(i).equals(key)){
  144.                 return this.map_messageProcessor.get(i);
  145.             }
  146.         }  
  147.        
  148.         // provo a cercarlo senza utilizzare la funzione specifica
  149.         if(function!=null && !"".equals(function)){
  150.             function = null;
  151.             f = ServiceBindingConfiguration.normalizeContext(function);
  152.             key = context +" - " + f +" - " +subContext_functionParam;
  153.            
  154.             for (int i = 0; i < this.map_contextUrl.size(); i++) {
  155.                 if(this.map_contextUrl.get(i).equals(key)){
  156.                     return this.map_messageProcessor.get(i);
  157.                 }
  158.             }  
  159.         }
  160.        
  161.         return null; // ritorno anzi null per gestire la differenza rispetto all'eccezione sopra
  162.     }
  163.    
  164.     public List<String> getContentTypesRestriction(String protocol, String function, String functionParam) {
  165.         String context = ServiceBindingConfiguration.normalizeContext(protocol);
  166.         String f = ServiceBindingConfiguration.normalizeContext(function);
  167.         String subContext_functionParam = ServiceBindingConfiguration.normalizeContext(functionParam);
  168.        
  169.         String key = context +" - " + f +" - " +subContext_functionParam;
  170.        
  171.         for (int i = 0; i < this.map_contextUrl.size(); i++) {
  172.             if(this.map_contextUrl.get(i).equals(key)){
  173.                 return this.map_contentTypesRestriction.get(i);
  174.             }
  175.         }
  176.        
  177.         // provo a cercarlo senza utilizzare la funzione specifica
  178.         if(function!=null && !"".equals(function)){
  179.             function = null;
  180.             f = ServiceBindingConfiguration.normalizeContext(function);
  181.             key = context +" - " + f +" - " +subContext_functionParam;
  182.            
  183.             for (int i = 0; i < this.map_contextUrl.size(); i++) {
  184.                 if(this.map_contextUrl.get(i).equals(key)){
  185.                     return this.map_contentTypesRestriction.get(i);
  186.                 }
  187.             }  
  188.         }
  189.        
  190.         return null;
  191.     }
  192. }