ServiceBindingConfiguration.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.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import javax.mail.internet.ContentType;

  27. import org.openspcoop2.message.constants.Costanti;
  28. import org.openspcoop2.message.constants.MessageRole;
  29. import org.openspcoop2.message.constants.MessageType;
  30. import org.openspcoop2.message.constants.ServiceBinding;
  31. import org.openspcoop2.message.exception.MessageException;
  32. import org.openspcoop2.utils.transport.TransportRequestContext;
  33. import org.openspcoop2.utils.transport.http.ContentTypeUtilities;

  34. /**
  35.  * MessageConfiguration
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class ServiceBindingConfiguration implements Serializable {

  42.     public static String normalizeContext(String context){
  43.         if(context==null || "".equals(context)){
  44.             return Costanti.CONTEXT_EMPTY;
  45.         }
  46.         return context;
  47.     }
  48.    
  49.     /**
  50.      *
  51.      */
  52.     private static final long serialVersionUID = 1L;
  53.    
  54.     private ServiceBinding defaultBinding;
  55.     private ConfigurationServiceBindingSoap soap;  
  56.     private ConfigurationServiceBindingRest rest;
  57.     private ContextUrlCollection contextUrlCollection;
  58.    
  59.     private Map<String, ServiceBinding> restrictedServiceBindingContextUrl = new HashMap<String, ServiceBinding>();
  60.     private Map<String, ServiceBinding> restrictedServiceBindingServiceType = new HashMap<String, ServiceBinding>();
  61.    
  62.     public ServiceBindingConfiguration(ServiceBinding defaultBinding,
  63.             ConfigurationServiceBindingSoap soap, ConfigurationServiceBindingRest rest,
  64.             ContextUrlCollection contextUrlCollection) throws MessageException{
  65.         if(defaultBinding==null){
  66.             throw new MessageException("Default ServiceBinding not defined");
  67.         }
  68.         if(soap==null){
  69.             throw new MessageException("SOAP-ServiceBinding not defined");
  70.         }
  71.         if(rest==null){
  72.             throw new MessageException("REST-ServiceBinding not defined");
  73.         }
  74.         this.defaultBinding = defaultBinding;
  75.         this.soap = soap;
  76.         this.rest = rest;
  77.         if(contextUrlCollection==null){
  78.             this.contextUrlCollection = new ContextUrlCollection(this.soap.getBinding(),this.rest.getBinding());
  79.         }
  80.         else{
  81.             this.contextUrlCollection = contextUrlCollection;
  82.         }
  83.     }
  84.    
  85.    
  86.     // ----- SERVICE BINDING ------
  87.    
  88.     public ServiceBinding getDefaultBinding() {
  89.         return this.defaultBinding;
  90.     }
  91.    
  92.     public boolean isServiceBindingSupported(ServiceBinding serviceBinding){
  93.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  94.             return this.soap.isEnabled();
  95.         }
  96.         else {
  97.             return this.rest.isEnabled();
  98.         }
  99.     }
  100.    
  101.     public List<MessageType> getMessageTypeSupported(ServiceBinding serviceBinding){
  102.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  103.             return this.soap.getMessageTypeSupported();
  104.         }
  105.         else {
  106.             return this.rest.getMessageTypeSupported();
  107.         }
  108.     }
  109.    
  110.     public boolean isServiceBindingContextEnabled(ServiceBinding serviceBinding, String contextParam){
  111.         String context = normalizeContext(contextParam);
  112.         if(this.restrictedServiceBindingContextUrl.containsKey(context)){
  113.             if(ServiceBinding.SOAP.equals(this.restrictedServiceBindingContextUrl.get(context))){
  114.                 return ServiceBinding.SOAP.equals(serviceBinding);
  115.             }
  116.             else {
  117.                 return ServiceBinding.REST.equals(serviceBinding);
  118.             }
  119.         }
  120.         return true;
  121.     }
  122.        

  123.     public boolean isServiceBindingServiceTypeEnabled(ServiceBinding serviceBinding, String serviceType){
  124.         if(this.restrictedServiceBindingServiceType.containsKey(serviceType)){
  125.             if(ServiceBinding.SOAP.equals(this.restrictedServiceBindingServiceType.get(serviceType))){
  126.                 return ServiceBinding.SOAP.equals(serviceBinding);
  127.             }
  128.             else {
  129.                 return ServiceBinding.REST.equals(serviceBinding);
  130.             }
  131.         }
  132.         return true;
  133.     }
  134.    
  135.     public void addServiceBindingContextRestriction(ServiceBinding serviceBinding, String contextParam){
  136.         String context = normalizeContext(contextParam);
  137.         if(this.restrictedServiceBindingContextUrl.containsKey(context)){
  138.             this.restrictedServiceBindingContextUrl.remove(context);
  139.         }
  140.         this.restrictedServiceBindingContextUrl.put(context, serviceBinding);
  141.     }
  142.    
  143.     public void addServiceBindingEmptyContextRestriction(ServiceBinding serviceBinding){
  144.         this.addServiceBindingContextRestriction(serviceBinding, null);
  145.     }
  146.    
  147.     public void addServiceBindingServiceTypeRestriction(ServiceBinding serviceBinding, String serviceType){
  148.         if(this.restrictedServiceBindingServiceType.containsKey(serviceType)){
  149.             this.restrictedServiceBindingServiceType.remove(serviceType);
  150.         }
  151.         this.restrictedServiceBindingServiceType.put(serviceType, serviceBinding);
  152.     }
  153.    
  154.    
  155.    
  156.     // ----- INTEGRATION ERROR ------
  157.    
  158.     public IntegrationErrorCollection getInternalIntegrationErrorConfiguration(ServiceBinding serviceBinding) {
  159.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  160.             return this.soap.getInternalIntegrationErrorConfiguration();
  161.         }
  162.         else {
  163.             return this.rest.getInternalIntegrationErrorConfiguration();
  164.         }
  165.     }
  166.     public IntegrationErrorCollection getExternalIntegrationErrorConfiguration(ServiceBinding serviceBinding) {
  167.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  168.             return this.soap.getExternalIntegrationErrorConfiguration();
  169.         }
  170.         else {
  171.             return this.rest.getExternalIntegrationErrorConfiguration();
  172.         }
  173.     }
  174.    

  175.    
  176.     // ----- MESSAGE TYPES ------
  177.    
  178.     public boolean existsContextUrlMapping(){
  179.         return this.contextUrlCollection!=null && this.contextUrlCollection.sizeContextUrl()>0;
  180.     }
  181.    
  182.     public List<String> getContentTypesSupported(ServiceBinding serviceBinding, MessageRole messageType,
  183.             TransportRequestContext transportContext) throws MessageException{
  184.         return getContentTypesSupported(serviceBinding, messageType,
  185.                 transportContext.getProtocolWebContext(), transportContext.getFunction(), transportContext.getFunctionParameters());
  186.     }
  187.     public List<String> getContentTypesSupported(ServiceBinding serviceBinding, MessageRole messageType,
  188.             String protocol, String function, String functionParameters) throws MessageException{
  189.        
  190.         if(serviceBinding==null){
  191.             throw new MessageException("ServiceBinding not defined");
  192.         }
  193.         if(messageType==null){
  194.             throw new MessageException("MessageType not defined");
  195.         }
  196.        
  197.         AbstractMediaTypeCollection configurationFlow;

  198.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  199.             if(this.soap.isEnabled()==false){
  200.                 throw new MessageException("Typology ["+serviceBinding+"] not supported");
  201.             }
  202.             if(MessageRole.REQUEST.equals(messageType)){
  203.                 configurationFlow = this.soap.getRequest();
  204.             }
  205.             else{
  206.                 configurationFlow = this.soap.getResponse();
  207.             }
  208.         }
  209.         else{
  210.             if(this.rest.isEnabled()==false){
  211.                 throw new MessageException("Typology ["+serviceBinding+"] not supported");
  212.             }
  213.             if(MessageRole.REQUEST.equals(messageType)){
  214.                 configurationFlow = this.rest.getRequest();
  215.             }
  216.             else{
  217.                 configurationFlow = this.rest.getResponse();
  218.             }
  219.         }

  220.         MessageType mpv = this.contextUrlCollection.getMessageType(protocol,function,functionParameters);
  221.         if(mpv!=null){
  222.             List<String> ct = this.contextUrlCollection.getContentTypesRestriction(protocol,function,functionParameters);
  223.             if(ct==null){
  224.                 ct = new ArrayList<>();
  225.             }
  226.             if(ct.size()<=0){
  227.                 ct.add(Costanti.CONTENT_TYPE_ALL);
  228.             }
  229.             return ct;
  230.         }
  231.         return configurationFlow.getContentTypes();

  232.     }
  233.    
  234.     public String getContentTypesSupportedAsString(ServiceBinding serviceBinding, MessageRole messageType,
  235.             TransportRequestContext transportContext) throws MessageException{
  236.         return getContentTypesSupportedAsString(serviceBinding, messageType,
  237.                 transportContext.getProtocolWebContext(), transportContext.getFunction(), transportContext.getFunctionParameters());
  238.     }
  239.     public String getContentTypesSupportedAsString(ServiceBinding serviceBinding, MessageRole messageType,
  240.             String protocol, String function, String functionParameters) throws MessageException{
  241.         StringBuilder bf = new StringBuilder();
  242.         for (String ct : this.getContentTypesSupported(serviceBinding, messageType,
  243.                 protocol,function,functionParameters)) {
  244.             if(bf.length()>0){
  245.                 bf.append(", ");
  246.             }
  247.             bf.append(ct);
  248.         }
  249.         return bf.toString();
  250.     }
  251.    
  252.     public MessageType getRequestMessageType(ServiceBinding serviceBinding,
  253.             TransportRequestContext transportContext,
  254.             String contentType) throws MessageException{
  255.         return this.getMessageType(serviceBinding, MessageRole.REQUEST,
  256.                 transportContext.getProtocolWebContext(), transportContext.getFunction(), transportContext.getFunctionParameters(),
  257.                 contentType, null);
  258.     }
  259.     public MessageType getResponseMessageType(ServiceBinding serviceBinding,
  260.             TransportRequestContext transportContext,
  261.             String contentType, Integer status) throws MessageException{
  262.         return this.getMessageType(serviceBinding, MessageRole.RESPONSE,
  263.                 transportContext!=null ? transportContext.getProtocolWebContext() : null,
  264.                 transportContext!=null ? transportContext.getFunction() : null,
  265.                 transportContext!=null ? transportContext.getFunctionParameters() : null,
  266.                 contentType, status);
  267.     }
  268.     public MessageType getMessageType(ServiceBinding serviceBinding, MessageRole messageRole,
  269.             TransportRequestContext transportContext,
  270.             String contentType, Integer status) throws MessageException{
  271.         return this.getMessageType(serviceBinding, messageRole,
  272.                 transportContext.getProtocolWebContext(), transportContext.getFunction(), transportContext.getFunctionParameters(),
  273.                 contentType, status);
  274.     }
  275.     public MessageType getMessageType(ServiceBinding serviceBinding, MessageRole messageRole,
  276.             String protocol, String function, String functionParameters,
  277.             String contentType, Integer status) throws MessageException{
  278.        
  279.         if(serviceBinding==null){
  280.             throw new MessageException("ServiceBinding not defined");
  281.         }
  282.         if(messageRole==null){
  283.             throw new MessageException("MessageRole not defined");
  284.         }
  285.        
  286.         String mediaType = contentType;
  287.         boolean withAttachments = false;
  288.         boolean mtom = false;
  289.         if(ServiceBinding.SOAP.equals(serviceBinding) && mediaType!=null){
  290.             try{
  291.                 if(ContentTypeUtilities.isMultipartRelated(mediaType)){
  292.                     withAttachments = true;
  293.                     String internal = ContentTypeUtilities.getInternalMultipartContentType(mediaType);
  294.                     if(internal!=null){
  295.                         mediaType = internal;
  296.                         mtom = ContentTypeUtilities.isMtom(mediaType);
  297.                         if(mtom){
  298.                             withAttachments = false;
  299.                         }
  300.                     }
  301.                 }
  302.             }catch(Exception e){
  303.                 throw new MessageException(e.getMessage(),e);
  304.             }
  305.         }
  306.         try{
  307.             if(mediaType!=null){
  308.                 ContentType ct = new ContentType(mediaType);
  309.                 mediaType = ct.getBaseType();
  310.             }
  311.         }catch(Exception e){
  312.             throw new MessageException(e.getMessage(),e);
  313.         }
  314.        
  315.         MessageType messageType = readMessageTypeEngine(serviceBinding, messageRole,
  316.                 protocol, function, functionParameters,
  317.                 status,
  318.                 mediaType);
  319.        
  320.         // check compatibilità rispetto alla tipologia
  321.            
  322.         if(messageType!=null && ServiceBinding.SOAP.equals(serviceBinding)){
  323.             if(withAttachments){
  324.                 if(MessageType.SOAP_11.equals(messageType)){
  325.                     if(this.soap.getBinding().isBinding_soap11_withAttachments()==false){
  326.                         throw new MessageException("SOAPWithAttachments disabled in Soap11 Binding");
  327.                     }
  328.                 }
  329.                 else{
  330.                     if(this.soap.getBinding().isBinding_soap12_withAttachments()==false){
  331.                         throw new MessageException("SOAPWithAttachments disabled in Soap12 Binding");
  332.                     }
  333.                 }
  334.             }
  335.             if(mtom){
  336.                 if(MessageType.SOAP_11.equals(messageType)){
  337.                     if(this.soap.getBinding().isBinding_soap11_mtom()==false){
  338.                         throw new MessageException("MTOM disabled in Soap11 Binding");
  339.                     }
  340.                 }
  341.                 else{
  342.                     if(this.soap.getBinding().isBinding_soap12_mtom()==false){
  343.                         throw new MessageException("MTOM disabled in Soap12 Binding");
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.        
  349.         return messageType;

  350.     }

  351.     private MessageType readMessageTypeEngine(ServiceBinding serviceBinding, MessageRole messageRole,
  352.             String protocol, String function, String functionParameters,
  353.             Integer status,
  354.             String mediaType) throws MessageException {
  355.         MessageType messageType = this.contextUrlCollection.getMessageType(protocol,function,functionParameters);
  356.         if(messageType!=null){
  357.            
  358.             // check sulla restrizione dei content-type
  359.             List<String> ct = this.contextUrlCollection.getContentTypesRestriction(protocol,function,functionParameters);
  360.             if(ct!=null && ct.size()>0){
  361.                 if(mediaType!=null && ct.contains(mediaType)){
  362.                     return messageType;
  363.                 }
  364.             }
  365.             else{
  366.                 return messageType;
  367.             }
  368.            
  369.         }
  370.        
  371.         AbstractMediaTypeCollection mediaTypeCollecton;

  372.         if(ServiceBinding.SOAP.equals(serviceBinding)){
  373.             if(this.soap.isEnabled()==false){
  374.                 throw new MessageException("Typology ["+serviceBinding+"] not supported");
  375.             }
  376.             if(MessageRole.REQUEST.equals(messageRole)){
  377.                 mediaTypeCollecton = this.soap.getRequest();
  378.             }
  379.             else{
  380.                 mediaTypeCollecton = this.soap.getResponse();
  381.             }
  382.         }
  383.         else{
  384.             if(this.rest.isEnabled()==false){
  385.                 throw new MessageException("Typology ["+serviceBinding+"] not supported");
  386.             }
  387.             if(MessageRole.REQUEST.equals(messageRole)){
  388.                 mediaTypeCollecton = this.rest.getRequest();
  389.             }
  390.             else{
  391.                 mediaTypeCollecton = this.rest.getResponse();
  392.             }
  393.         }
  394.                
  395.         messageType = mediaTypeCollecton.getMessageProcessor(mediaType,status); // può essere null
  396.         return messageType;
  397.     }
  398. }