ServiceBindingConfigurationReader.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.basic.config;

  21. import org.openspcoop2.core.id.IDAccordo;
  22. import org.openspcoop2.core.id.IDServizio;
  23. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  24. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  25. import org.openspcoop2.core.registry.PortType;
  26. import org.openspcoop2.core.registry.Resource;
  27. import org.openspcoop2.core.registry.ResourceRepresentation;
  28. import org.openspcoop2.core.registry.ResourceResponse;
  29. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  30. import org.openspcoop2.message.config.ConfigurationRFC7807;
  31. import org.openspcoop2.message.config.ConfigurationServiceBindingRest;
  32. import org.openspcoop2.message.config.ConfigurationServiceBindingSoap;
  33. import org.openspcoop2.message.config.ContextUrlCollection;
  34. import org.openspcoop2.message.config.IntegrationErrorCollection;
  35. import org.openspcoop2.message.config.IntegrationErrorReturnConfiguration;
  36. import org.openspcoop2.message.config.RestBinding;
  37. import org.openspcoop2.message.config.ServiceBindingConfiguration;
  38. import org.openspcoop2.message.config.SoapBinding;
  39. import org.openspcoop2.message.constants.IntegrationError;
  40. import org.openspcoop2.message.constants.MessageType;
  41. import org.openspcoop2.message.constants.ServiceBinding;
  42. import org.openspcoop2.message.exception.MessageException;
  43. import org.openspcoop2.protocol.manifest.Context;
  44. import org.openspcoop2.protocol.manifest.EmptySubContextMapping;
  45. import org.openspcoop2.protocol.manifest.IntegrationErrorCode;
  46. import org.openspcoop2.protocol.manifest.Openspcoop2;
  47. import org.openspcoop2.protocol.manifest.RFC7807;
  48. import org.openspcoop2.protocol.manifest.RestConfiguration;
  49. import org.openspcoop2.protocol.manifest.RestMediaTypeCollection;
  50. import org.openspcoop2.protocol.manifest.RestMediaTypeDefaultMapping;
  51. import org.openspcoop2.protocol.manifest.RestMediaTypeMapping;
  52. import org.openspcoop2.protocol.manifest.RestMediaTypeUndefinedMapping;
  53. import org.openspcoop2.protocol.manifest.SoapConfiguration;
  54. import org.openspcoop2.protocol.manifest.SoapMediaTypeCollection;
  55. import org.openspcoop2.protocol.manifest.SoapMediaTypeDefaultMapping;
  56. import org.openspcoop2.protocol.manifest.SoapMediaTypeMapping;
  57. import org.openspcoop2.protocol.manifest.SoapMediaTypeUndefinedMapping;
  58. import org.openspcoop2.protocol.manifest.SubContextMapping;
  59. import org.openspcoop2.protocol.manifest.constants.Costanti;
  60. import org.openspcoop2.protocol.manifest.constants.DefaultIntegrationErrorMessageType;
  61. import org.openspcoop2.protocol.manifest.constants.IntegrationErrorMessageDetailType;
  62. import org.openspcoop2.protocol.manifest.constants.IntegrationErrorMessageType;
  63. import org.openspcoop2.protocol.manifest.constants.IntegrationErrorProblemType;
  64. import org.openspcoop2.protocol.manifest.constants.RestMessageType;
  65. import org.openspcoop2.protocol.manifest.constants.SoapMessageType;
  66. import org.openspcoop2.protocol.sdk.ProtocolException;
  67. import org.openspcoop2.protocol.sdk.registry.IRegistryReader;
  68. import org.openspcoop2.protocol.sdk.registry.RegistryNotFound;
  69. import org.openspcoop2.utils.transport.TransportRequestContext;

  70. /**
  71.  * ServiceBindingConfigurationReader
  72.  *
  73.  * @author Poli Andrea (apoli@link.it)
  74.  * @author $Author$
  75.  * @version $Rev$, $Date$
  76.  */
  77. public class ServiceBindingConfigurationReader  {

  78.     public static ServiceBinding getServiceBinding(IDServizio idServizio, IRegistryReader registryReader) throws ProtocolException, RegistryNotFound{
  79.         try{
  80.            
  81.             AccordoServizioParteSpecifica aps = registryReader.getAccordoServizioParteSpecifica(idServizio);
  82.             IDAccordo idAccordoParteComune = IDAccordoFactory.getInstance().getIDAccordoFromUri(aps.getAccordoServizioParteComune());
  83.             AccordoServizioParteComune apc = registryReader.getAccordoServizioParteComune(idAccordoParteComune);
  84.             switch (apc.getServiceBinding()) {
  85.                 case SOAP:
  86.                     return ServiceBinding.SOAP;
  87.                 case REST:
  88.                     return ServiceBinding.REST;
  89.             }
  90.             throw new ProtocolException("Service ["+idServizio+"] not found");
  91.            
  92.         }
  93.         catch(RegistryNotFound notFound) {
  94.             throw notFound;
  95.         }
  96.         catch(Exception e){
  97.             throw new ProtocolException(e.getMessage(),e);
  98.         }
  99.     }

  100.     public static ServiceBindingConfiguration getDefaultServiceBindingConfiguration(Openspcoop2 manifest, TransportRequestContext transportRequest) throws ProtocolException{
  101.         return getServiceBindingConfiguration(manifest, transportRequest, null, null, null);
  102.     }
  103.    
  104.     private static MessageType convertMessageType(org.openspcoop2.core.registry.constants.MessageType mt) {
  105.         if(mt!=null) {
  106.             switch (mt) {
  107.             case SOAP_11:
  108.                 return MessageType.SOAP_11;
  109.             case SOAP_12:
  110.                 return MessageType.SOAP_12;
  111.             case XML:
  112.                 return MessageType.XML;
  113.             case JSON:
  114.                 return MessageType.JSON;
  115.             case BINARY:
  116.                 return MessageType.BINARY;
  117.             case MIME_MULTIPART:
  118.                 return MessageType.MIME_MULTIPART;
  119.             }
  120.         }
  121.         return null;
  122.     }
  123.    
  124.     public static ServiceBindingConfiguration getServiceBindingConfiguration(Openspcoop2 manifest, TransportRequestContext transportRequest,
  125.             ServiceBinding serviceBinding, IDServizio idServizio, IRegistryReader registryReader) throws ProtocolException{
  126.        
  127.         try{
  128.        
  129.             String protocolWebContext = null;
  130.             if(transportRequest!=null && transportRequest.getProtocolWebContext()!=null && !"".equals(transportRequest.getProtocolWebContext())) {
  131.                 protocolWebContext = transportRequest.getProtocolWebContext();
  132.             }
  133.             ServiceBinding defaultBinding = getServiceBindingDefault(manifest, protocolWebContext);
  134.             ConfigurationServiceBindingSoap soap = readConfigurationServiceBindingSoap(manifest);
  135.             ConfigurationServiceBindingRest rest = readConfigurationServiceBindingRest(manifest);
  136.             ContextUrlCollection contextUrlCollection = readContextUrlCollection(manifest,soap,rest);
  137.            
  138.             // aggiornamento per contesto
  139.             String context = null;
  140.             if(transportRequest!=null) {
  141.                 context = transportRequest.getProtocolWebContext();
  142.             }
  143.             if(context!=null && !Costanti.CONTEXT_EMPTY.equals(context)){
  144.                 for (int i = 0; i < manifest.getWeb().sizeContextList(); i++) {
  145.                     if(manifest.getWeb().getContext(i).getName().equals(context)){
  146.                         if(manifest.getWeb().getContext(i).getSoapMediaTypeCollection()!=null){
  147.                             updateMediaTypeCollection(soap, manifest.getWeb().getContext(i).getSoapMediaTypeCollection(), true, true);
  148.                         }
  149.                         if(manifest.getWeb().getContext(i).getRestMediaTypeCollection()!=null){
  150.                             updateMediaTypeCollection(rest, manifest.getWeb().getContext(i).getRestMediaTypeCollection(), true, true);
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             else if(manifest.getWeb().getEmptyContext()!=null){
  156.                 if(manifest.getWeb().getEmptyContext().getSoapMediaTypeCollection()!=null){
  157.                     updateMediaTypeCollection(soap, manifest.getWeb().getEmptyContext().getSoapMediaTypeCollection(), true, true);
  158.                 }
  159.                 if(manifest.getWeb().getEmptyContext().getRestMediaTypeCollection()!=null){
  160.                     updateMediaTypeCollection(rest, manifest.getWeb().getEmptyContext().getRestMediaTypeCollection(), true, true);
  161.                 }
  162.             }
  163.            
  164.             if(serviceBinding!=null && idServizio!=null){
  165.                
  166.                 // aggiornamento per tipo servizio
  167.                 for (int i = 0; i < manifest.getRegistry().getService().getTypes().sizeTypeList(); i++) {
  168.                     String serviceType = manifest.getRegistry().getService().getTypes().getType(i).getName();
  169.                     if(serviceType.equals(idServizio.getTipo())){
  170.                         if( manifest.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection()!=null){
  171.                             updateMediaTypeCollection(soap, manifest.getRegistry().getService().getTypes().getType(i).getSoapMediaTypeCollection(), true, true);
  172.                         }
  173.                         if( manifest.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection()!=null){
  174.                             updateMediaTypeCollection(rest, manifest.getRegistry().getService().getTypes().getType(i).getRestMediaTypeCollection(), true, true);
  175.                         }
  176.                     }
  177.                 }              
  178.                
  179.                 // Accordi
  180.                 AccordoServizioParteComune aspc = null;
  181.                 AccordoServizioParteSpecifica asps = null;
  182.                 try {
  183.                     asps = registryReader.getAccordoServizioParteSpecifica(idServizio, false);
  184.                 }catch(RegistryNotFound notFound) {}
  185.                 if(asps!=null) {
  186.                     try {
  187.                         IDAccordo idAccordo = IDAccordoFactory.getInstance().getIDAccordoFromUri(asps.getAccordoServizioParteComune());
  188.                         aspc = registryReader.getAccordoServizioParteComune(idAccordo, false,
  189.                                 false); // non si legge i dati di request e response per API REST. L'override del MessageType e' stato eliminato per default dalla console
  190.                     }catch(RegistryNotFound notFound) {}
  191.                 }
  192.                
  193.                 // ricerca per servizio dell'accordo parte comune
  194.                 // Se presente viene forzato un message type indipendente dal mediaType e valido sia per la richiesta che per la risposta
  195.                 if(aspc!=null) {
  196.                     MessageType messageTypeService = convertMessageType(aspc.getMessageType());
  197.                     if(messageTypeService!=null){
  198.                         if(ServiceBinding.SOAP.equals(serviceBinding)){
  199.                             updateSoapMediaTypeCollection(soap, messageTypeService, true, true);
  200.                         }
  201.                         else if(ServiceBinding.REST.equals(serviceBinding)){
  202.                             updateRestMediaTypeCollection(rest, messageTypeService, true, true);
  203.                         }
  204.                     }
  205.                 }
  206.                
  207.                 if(ServiceBinding.SOAP.equals(serviceBinding) && aspc!=null && asps!=null && asps.getPortType()!=null) {
  208.                     // ricerca per port-type dell'accordo parte comune
  209.                     // Se presente viene forzato un message type indipendente dal mediaType e valido sia per la richiesta che per la risposta
  210.                     for (PortType pt : aspc.getPortTypeList()) {
  211.                         if(pt.getNome().equals(asps.getPortType())) {
  212.                             MessageType messageTypeService = convertMessageType(pt.getMessageType());
  213.                             if(messageTypeService!=null){
  214.                                 if(ServiceBinding.SOAP.equals(serviceBinding)){
  215.                                     updateSoapMediaTypeCollection(soap, messageTypeService, true, true);
  216.                                 }
  217.                             }
  218.                             break;
  219.                         }
  220.                     }
  221.                 }
  222.                
  223.                 // ricerca per servizio dell'accordo parte specifica
  224.                 // Se presente viene forzato un message type indipendente dal mediaType e valido sia per la richiesta che per la risposta
  225.                 if(asps!=null) {
  226.                     MessageType messageTypeParteSpecifica = convertMessageType(asps.getMessageType());
  227.                     if(messageTypeParteSpecifica!=null){
  228.                         if(ServiceBinding.SOAP.equals(serviceBinding)){
  229.                             updateSoapMediaTypeCollection(soap, messageTypeParteSpecifica, true, true);
  230.                         }
  231.                         else if(ServiceBinding.REST.equals(serviceBinding)){
  232.                             updateRestMediaTypeCollection(rest, messageTypeParteSpecifica, true, true);
  233.                         }
  234.                     }
  235.                 }
  236.                
  237.                 if(idServizio.getAzione()!=null && aspc!=null && ServiceBinding.REST.equals(serviceBinding)) {
  238.                
  239.                     if(aspc.sizeResourceList()>0) {
  240.                         for (Resource resource : aspc.getResourceList()) {
  241.                             if(resource.getNome().equals(idServizio.getAzione())) {
  242.                                
  243.                                 // Se presente viene forzato un message type indipendente dal mediaType e valido sia per la richiesta che per la risposta
  244.                                 MessageType messageTypeRisorsa = convertMessageType(resource.getMessageType());
  245.                                 if(messageTypeRisorsa!=null){
  246.                                     updateRestMediaTypeCollection(rest, messageTypeRisorsa, true, true);
  247.                                 }
  248.                                
  249.                                 // Se presente viene forzato un message type indipendente dal mediaType ma valido solo per la richiesta
  250.                                 MessageType messageTypeAzioneRichiesta = convertMessageType(resource.getRequestMessageType());
  251.                                 if(messageTypeAzioneRichiesta!=null){
  252.                                     updateRestMediaTypeCollection(rest, messageTypeAzioneRichiesta, true, false);
  253.                                 }
  254.                                
  255.                                 // Se presente viene forzato un message type indipendente dal mediaType ma valido solo per la risposta
  256.                                 MessageType messageTypeAzioneRisposta = convertMessageType(resource.getResponseMessageType());
  257.                                 if(messageTypeAzioneRisposta!=null){
  258.                                     updateRestMediaTypeCollection(rest, messageTypeAzioneRisposta, false, true);
  259.                                 }
  260.                                
  261.                                 // ultimo step di replace singolo mediaType sulla richiesta o sulla risposta (solo per REST)
  262.                                 if(resource.getRequest()!=null && resource.getRequest().sizeRepresentationList()>0) {
  263.                                     for (ResourceRepresentation rr : resource.getRequest().getRepresentationList()) {
  264.                                         MessageType messageTypeMediaType = convertMessageType(rr.getMessageType());
  265.                                         if(messageTypeMediaType!=null) {
  266.                                             rest.getRequest().addOrReplaceMediaType(rr.getMediaType(), messageTypeMediaType, false);
  267.                                         }
  268.                                     }
  269.                                 }
  270.                                 if(resource.sizeResponseList()>0) {
  271.                                     for (ResourceResponse response : resource.getResponseList()) {
  272.                                         if(response.sizeRepresentationList()>0) {
  273.                                             for (ResourceRepresentation rr : response.getRepresentationList()) {
  274.                                                 MessageType messageTypeMediaType = convertMessageType(rr.getMessageType());
  275.                                                 if(messageTypeMediaType!=null) {
  276.                                                     rest.getResponse().addOrReplaceMediaType(rr.getMediaType(), response.getStatus(), messageTypeMediaType, false);
  277.                                                 }
  278.                                             }
  279.                                         }
  280.                                     }
  281.                                 }
  282.                                 break;
  283.                             }
  284.                         }
  285.                     }

  286.                 }
  287.             }
  288.            
  289.             ServiceBindingConfiguration config = new ServiceBindingConfiguration(defaultBinding, soap, rest, contextUrlCollection);
  290.             addServiceBindingRestriction(manifest, config);
  291.             return config;
  292.            
  293.         }catch(Exception e){
  294.             throw new ProtocolException(e.getMessage(),e);
  295.         }
  296.     }
  297.        
  298.     private static ServiceBinding getServiceBindingDefault(Openspcoop2 manifest, String protocolWebContext){
  299.        
  300.         ServiceBinding defaultBinding = null;
  301.        
  302.         // Default
  303.         org.openspcoop2.protocol.manifest.constants.ServiceBinding defaultBindingManifest = manifest.getBinding().getDefault();
  304.         if(defaultBindingManifest!=null){
  305.             switch (defaultBindingManifest) {
  306.             case SOAP:
  307.                 defaultBinding = ServiceBinding.SOAP;
  308.                 break;
  309.             case REST:
  310.                 defaultBinding = ServiceBinding.REST;
  311.                 break;
  312.             }
  313.         }
  314.         else{
  315.             if(manifest.getBinding().getSoap()!=null){
  316.                 defaultBinding = ServiceBinding.SOAP;
  317.             }
  318.             else{
  319.                 defaultBinding = ServiceBinding.REST;
  320.             }
  321.         }
  322.        
  323.         if(protocolWebContext!=null) {
  324.             if(manifest.getWeb()!=null && manifest.getWeb().sizeContextList()>0) {
  325.                 for (Context webContext : manifest.getWeb().getContextList()) {
  326.                     if(webContext.getName()!=null && webContext.getName().equals(protocolWebContext)) {
  327.                         if(webContext.getBinding()!=null) {
  328.                             switch (webContext.getBinding()) {
  329.                             case SOAP:
  330.                                 defaultBinding = ServiceBinding.SOAP;
  331.                                 break;
  332.                             case REST:
  333.                                 defaultBinding = ServiceBinding.REST;
  334.                                 break;
  335.                             }
  336.                         }
  337.                         break;
  338.                     }
  339.                 }
  340.             }
  341.         }
  342.        
  343.         return defaultBinding;
  344.     }
  345.    
  346.     private static ConfigurationServiceBindingSoap readConfigurationServiceBindingSoap(Openspcoop2 manifest) throws MessageException{
  347.        
  348.         SoapConfiguration soapConfig = manifest.getBinding().getSoap();
  349.         boolean enabled = soapConfig!=null;
  350.         SoapBinding binding = null;
  351.         IntegrationErrorCollection internalIntegrationErrorConfiguration = null;
  352.         IntegrationErrorCollection externalIntegrationErrorConfiguration = null;
  353.        
  354.         if(enabled){
  355.            
  356.             binding = new SoapBinding(soapConfig.isSoap11(), soapConfig.isSoap11WithAttachments(), soapConfig.isSoap11Mtom(),
  357.                     soapConfig.isSoap12(), soapConfig.isSoap12WithAttachments(), soapConfig.isSoap12Mtom());
  358.            
  359.             internalIntegrationErrorConfiguration = readIntegrationErrorConfiguration(soapConfig.getIntegrationError().getInternal());
  360.             externalIntegrationErrorConfiguration = readIntegrationErrorConfiguration(soapConfig.getIntegrationError().getExternal());
  361.            
  362.         }
  363.        
  364.         ConfigurationServiceBindingSoap soap = new ConfigurationServiceBindingSoap(enabled, binding, internalIntegrationErrorConfiguration, externalIntegrationErrorConfiguration);
  365.         soap.init();
  366.        
  367.         if(enabled){
  368.            
  369.             if(soapConfig.getMediaTypeCollection()!=null){
  370.                 updateMediaTypeCollection(soap, soapConfig.getMediaTypeCollection(), true, true);
  371.             }
  372.            
  373.         }
  374.        
  375.         return soap;
  376.            
  377.     }
  378.    
  379.     private static void updateMediaTypeCollection(ConfigurationServiceBindingSoap soap, SoapMediaTypeCollection mediaTypesCollection,
  380.             boolean request, boolean response) throws MessageException{
  381.        
  382.         if(mediaTypesCollection.sizeMediaTypeList()<=0 && mediaTypesCollection.getDefault()==null && mediaTypesCollection.getUndefined()==null){
  383.             return;
  384.         }
  385.         if(request){
  386.             soap.getRequest().clear();
  387.         }
  388.         if(response){
  389.             soap.getResponse().clear();
  390.         }
  391.        
  392.         for (int i = 0; i < mediaTypesCollection.sizeMediaTypeList(); i++) {
  393.             SoapMediaTypeMapping mapping = mediaTypesCollection.getMediaType(i);
  394.             if(request){
  395.                 soap.getRequest().addMediaType(mapping.getBase(),  convertToMessageType(mapping.getMessageType()), mapping.isRegExpr());
  396.             }
  397.             if(response){
  398.                 soap.getResponse().addMediaType(mapping.getBase(), convertToMessageType(mapping.getMessageType()), mapping.isRegExpr());
  399.             }
  400.         }
  401.        
  402.         if(mediaTypesCollection.getDefault()!=null){
  403.             if(request){
  404.                 soap.getRequest().addDefaultMediaType(convertToMessageType(mediaTypesCollection.getDefault().getMessageType()));
  405.             }
  406.             if(response){
  407.                 soap.getResponse().addDefaultMediaType(convertToMessageType(mediaTypesCollection.getDefault().getMessageType()));
  408.             }
  409.         }
  410.        
  411.         if(mediaTypesCollection.getUndefined()!=null){
  412.             if(request){
  413.                 soap.getRequest().addUndefinedMediaType(convertToMessageType(mediaTypesCollection.getUndefined().getMessageType()));
  414.             }
  415.             if(response){
  416.                 soap.getResponse().addUndefinedMediaType(convertToMessageType(mediaTypesCollection.getUndefined().getMessageType()));
  417.             }
  418.         }
  419.     }
  420.    
  421.     private static void updateSoapMediaTypeCollection(ConfigurationServiceBindingSoap soap, MessageType messageType, boolean request, boolean response) throws MessageException{
  422.         SoapMediaTypeCollection mediaTypeCollection = new SoapMediaTypeCollection();
  423.        
  424.         SoapMediaTypeDefaultMapping defaultMapping = new SoapMediaTypeDefaultMapping();
  425.         defaultMapping.setMessageType(convertToSoapMessageType(messageType));
  426.         mediaTypeCollection.setDefault(defaultMapping);
  427.        
  428.         SoapMediaTypeUndefinedMapping undefinedMapping = new SoapMediaTypeUndefinedMapping();
  429.         undefinedMapping.setMessageType(convertToSoapMessageType(messageType));
  430.         mediaTypeCollection.setUndefined(undefinedMapping);
  431.        
  432.         updateMediaTypeCollection(soap, mediaTypeCollection, request, response);
  433.     }
  434.    
  435.     private static MessageType convertToMessageType(SoapMessageType messageType){
  436.         switch (messageType) {
  437.         case SOAP_11:
  438.             return MessageType.SOAP_11;
  439.         case SOAP_12:
  440.             return MessageType.SOAP_12;
  441.         }
  442.         return null;
  443.     }
  444.    
  445.     @SuppressWarnings("incomplete-switch")
  446.     private static SoapMessageType convertToSoapMessageType(MessageType messageType){
  447.         switch (messageType) {
  448.         case SOAP_11:
  449.             return SoapMessageType.SOAP_11;
  450.         case SOAP_12:
  451.             return SoapMessageType.SOAP_12;
  452.         }
  453.         return null;
  454.     }
  455.    
  456.     private static ConfigurationServiceBindingRest readConfigurationServiceBindingRest(Openspcoop2 manifest) throws MessageException{
  457.        
  458.         RestConfiguration restConfig = manifest.getBinding().getRest();
  459.         boolean enabled = restConfig!=null;
  460.         RestBinding binding = null;
  461.         IntegrationErrorCollection internalIntegrationErrorConfiguration = null;
  462.         IntegrationErrorCollection externalIntegrationErrorConfiguration = null;
  463.        
  464.         if(enabled){
  465.            
  466.             binding = new RestBinding(restConfig.isXml(), restConfig.isJson(), restConfig.isBinary(), restConfig.isMimeMultipart());
  467.            
  468.             internalIntegrationErrorConfiguration = readIntegrationErrorConfiguration(restConfig.getIntegrationError().getInternal());
  469.             externalIntegrationErrorConfiguration = readIntegrationErrorConfiguration(restConfig.getIntegrationError().getExternal());
  470.         }
  471.        
  472.         ConfigurationServiceBindingRest rest = new ConfigurationServiceBindingRest(enabled, binding, internalIntegrationErrorConfiguration, externalIntegrationErrorConfiguration);
  473.         rest.init();
  474.        
  475.         if(enabled){
  476.            
  477.             if(restConfig.getMediaTypeCollection()!=null){
  478.                 updateMediaTypeCollection(rest, restConfig.getMediaTypeCollection(), true, true);
  479.             }
  480.            
  481.         }
  482.        
  483.         return rest;
  484.            
  485.     }
  486.    
  487.     private static void updateMediaTypeCollection(ConfigurationServiceBindingRest rest, RestMediaTypeCollection mediaTypesCollection, boolean request, boolean response) throws MessageException{
  488.        
  489.         if(mediaTypesCollection.sizeMediaTypeList()<=0 && mediaTypesCollection.getDefault()==null && mediaTypesCollection.getUndefined()==null){
  490.             return;
  491.         }
  492.         if(request){
  493.             rest.getRequest().clear();
  494.         }
  495.         if(response){
  496.             rest.getResponse().clear();
  497.         }
  498.        
  499.         for (int i = 0; i < mediaTypesCollection.sizeMediaTypeList(); i++) {
  500.             RestMediaTypeMapping mapping = mediaTypesCollection.getMediaType(i);
  501.             if(request){
  502.                 rest.getRequest().addMediaType(mapping.getBase(), convertToMessageType(mapping.getMessageType()), mapping.isRegExpr());
  503.             }
  504.             if(response){
  505.                 rest.getResponse().addMediaType(mapping.getBase(), convertToMessageType(mapping.getMessageType()), mapping.isRegExpr());
  506.             }
  507.         }
  508.        
  509.         if(mediaTypesCollection.getDefault()!=null){
  510.             if(request){
  511.                 rest.getRequest().addDefaultMediaType(convertToMessageType(mediaTypesCollection.getDefault().getMessageType()));
  512.             }
  513.             if(response){
  514.                 rest.getResponse().addDefaultMediaType(convertToMessageType(mediaTypesCollection.getDefault().getMessageType()));
  515.             }
  516.         }
  517.        
  518.         if(mediaTypesCollection.getUndefined()!=null){
  519.             if(request){
  520.                 rest.getRequest().addUndefinedMediaType(convertToMessageType(mediaTypesCollection.getUndefined().getMessageType()));
  521.             }
  522.             if(response){
  523.                 rest.getResponse().addUndefinedMediaType(convertToMessageType(mediaTypesCollection.getUndefined().getMessageType()));
  524.             }
  525.         }
  526.     }
  527.    
  528.     private static void updateRestMediaTypeCollection(ConfigurationServiceBindingRest rest, MessageType messageType, boolean request, boolean response) throws MessageException{
  529.         RestMediaTypeCollection mediaTypeCollection = new RestMediaTypeCollection();
  530.        
  531.         RestMediaTypeDefaultMapping defaultMapping = new RestMediaTypeDefaultMapping();
  532.         defaultMapping.setMessageType(convertToRestMessageType(messageType));
  533.         mediaTypeCollection.setDefault(defaultMapping);
  534.        
  535.         RestMediaTypeUndefinedMapping undefinedMapping = new RestMediaTypeUndefinedMapping();
  536.         undefinedMapping.setMessageType(convertToRestMessageType(messageType));
  537.         mediaTypeCollection.setUndefined(undefinedMapping);
  538.        
  539.         updateMediaTypeCollection(rest, mediaTypeCollection, request, response);
  540.     }
  541.    
  542.     private static MessageType convertToMessageType(RestMessageType messageType){
  543.         switch (messageType) {
  544.         case XML:
  545.             return MessageType.XML;
  546.         case JSON:
  547.             return MessageType.JSON;
  548.         case BINARY:
  549.             return MessageType.BINARY;
  550.         case MIME_MULTIPART:
  551.             return MessageType.MIME_MULTIPART;
  552.         }
  553.         return null;
  554.     }
  555.    
  556.     @SuppressWarnings("incomplete-switch")
  557.     private static RestMessageType convertToRestMessageType(MessageType messageType){
  558.         switch (messageType) {
  559.         case XML:
  560.             return RestMessageType.XML;
  561.         case JSON:
  562.             return RestMessageType.JSON;
  563.         case BINARY:
  564.             return RestMessageType.BINARY;
  565.         case MIME_MULTIPART:
  566.             return RestMessageType.MIME_MULTIPART;
  567.         }
  568.         return null;
  569.     }
  570.    
  571.     private static IntegrationErrorReturnConfiguration to(IntegrationErrorCode errorCode, boolean retry, IntegrationErrorMessageDetailType errorMessage) {
  572.         IntegrationErrorReturnConfiguration conf = new IntegrationErrorReturnConfiguration();
  573.         conf.setHttpReturnCode(errorCode.getHttp());
  574.         conf.setGovwayReturnCode(errorCode.getGovway());
  575.         conf.setRetry(retry);
  576.         conf.setGenericDetails(IntegrationErrorMessageDetailType.GENERIC.equals(errorMessage));
  577.         return conf;
  578.     }
  579.    
  580.     private static IntegrationErrorCollection readIntegrationErrorConfiguration(org.openspcoop2.protocol.manifest.IntegrationErrorCollection config) throws MessageException{
  581.         IntegrationErrorCollection integrationErrorConfiguration = new IntegrationErrorCollection();
  582.        
  583.         ConfigurationRFC7807 rfc7807 = null;
  584.         if(IntegrationErrorProblemType.RFC_7807.equals(config.getProblemType())) {
  585.             RFC7807 rfc7807_protocolManifest = config.getRfc7807();
  586.             rfc7807 = new ConfigurationRFC7807();
  587.             rfc7807.setType(rfc7807_protocolManifest.isType());
  588.             rfc7807.setTypeFormat(rfc7807_protocolManifest.getTypeFormat());
  589.             rfc7807.setUseAcceptHeader(rfc7807_protocolManifest.getUseAcceptHeader());
  590.             rfc7807.setInstance(rfc7807_protocolManifest.getInstance());
  591.             rfc7807.setGovwayType(rfc7807_protocolManifest.getGovwayType());
  592.             rfc7807.setGovwayStatus(rfc7807_protocolManifest.getGovwayStatus());
  593.             rfc7807.setGovwayTransactionId(rfc7807_protocolManifest.getGovwayTransactionId());
  594.             rfc7807.setDetails(rfc7807_protocolManifest.getDetails());
  595.         }
  596.        
  597.         if(config.getDefault()!=null){
  598.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.DEFAULT,
  599.                     convertToMessageType(config.getDefault().getMessageType()),
  600.                     to(config.getDefault().getErrorCode(), config.getDefault().getRetry(), config.getDefault().getErrorMessage()),
  601.                     config.isUseInternalFault());
  602.         }
  603.         if(config.getAuthentication()!=null){
  604.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.AUTHENTICATION,
  605.                     convertToMessageType(config.getAuthentication().getMessageType()),
  606.                     to(config.getAuthentication().getErrorCode(), config.getAuthentication().getRetry(), config.getAuthentication().getErrorMessage()),
  607.                     config.isUseInternalFault());
  608.         }
  609.         if(config.getAuthorization()!=null){
  610.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.AUTHORIZATION,
  611.                     convertToMessageType(config.getAuthorization().getMessageType()),
  612.                     to(config.getAuthorization().getErrorCode(), config.getAuthorization().getRetry(), config.getAuthorization().getErrorMessage()),
  613.                     config.isUseInternalFault());
  614.         }
  615.         if(config.getNotFound()!=null){
  616.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.NOT_FOUND,
  617.                     convertToMessageType(config.getNotFound().getMessageType()),
  618.                     to(config.getNotFound().getErrorCode(), config.getNotFound().getRetry(), config.getNotFound().getErrorMessage()),
  619.                     config.isUseInternalFault());
  620.         }
  621.         if(config.getBadRequest()!=null){
  622.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.BAD_REQUEST,
  623.                     convertToMessageType(config.getBadRequest().getMessageType()),
  624.                     to(config.getBadRequest().getErrorCode(), config.getBadRequest().getRetry(), config.getBadRequest().getErrorMessage()),
  625.                     config.isUseInternalFault());
  626.         }
  627.         if(config.getConflict()!=null){
  628.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.CONFLICT,
  629.                     convertToMessageType(config.getConflict().getMessageType()),
  630.                     to(config.getConflict().getErrorCode(), config.getConflict().getRetry(), config.getConflict().getErrorMessage()),
  631.                     config.isUseInternalFault());
  632.         }
  633.         if(config.getRequestTooLarge()!=null){
  634.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.REQUEST_TOO_LARGE,
  635.                     convertToMessageType(config.getRequestTooLarge().getMessageType()),
  636.                     to(config.getRequestTooLarge().getErrorCode(), config.getRequestTooLarge().getRetry(), config.getRequestTooLarge().getErrorMessage()),
  637.                     config.isUseInternalFault());
  638.         }
  639.         if(config.getLimitExceeded()!=null){
  640.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.LIMIT_EXCEEDED,
  641.                     convertToMessageType(config.getLimitExceeded().getMessageType()),
  642.                     to(config.getLimitExceeded().getErrorCode(), config.getLimitExceeded().getRetry(), config.getLimitExceeded().getErrorMessage()),
  643.                     config.isUseInternalFault());
  644.         }
  645.         if(config.getTooManyRequests()!=null){
  646.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.TOO_MANY_REQUESTS,
  647.                     convertToMessageType(config.getTooManyRequests().getMessageType()),
  648.                     to(config.getTooManyRequests().getErrorCode(), config.getTooManyRequests().getRetry(), config.getTooManyRequests().getErrorMessage()),
  649.                     config.isUseInternalFault());
  650.         }
  651.         if(config.getServiceUnavailable()!=null){
  652.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.SERVICE_UNAVAILABLE,
  653.                     convertToMessageType(config.getServiceUnavailable().getMessageType()),
  654.                     to(config.getServiceUnavailable().getErrorCode(), config.getServiceUnavailable().getRetry(), config.getServiceUnavailable().getErrorMessage()),
  655.                     config.isUseInternalFault());
  656.         }
  657.         if(config.getEndpointRequestTimedOut()!=null){
  658.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.ENDPOINT_REQUEST_TIMED_OUT,
  659.                     convertToMessageType(config.getEndpointRequestTimedOut().getMessageType()),
  660.                     to(config.getEndpointRequestTimedOut().getErrorCode(), config.getEndpointRequestTimedOut().getRetry(), config.getEndpointRequestTimedOut().getErrorMessage()),
  661.                     config.isUseInternalFault());
  662.         }
  663.         if(config.getBadResponse()!=null){
  664.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.BAD_RESPONSE,
  665.                     convertToMessageType(config.getBadResponse().getMessageType()),
  666.                     to(config.getBadResponse().getErrorCode(), config.getBadResponse().getRetry(), config.getBadResponse().getErrorMessage()),
  667.                     config.isUseInternalFault());
  668.         }
  669.         if(config.getInternalRequestError()!=null){
  670.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.INTERNAL_REQUEST_ERROR,
  671.                     convertToMessageType(config.getInternalRequestError().getMessageType()),
  672.                     to(config.getInternalRequestError().getErrorCode(), config.getInternalRequestError().getRetry(), config.getInternalRequestError().getErrorMessage()),
  673.                     config.isUseInternalFault());
  674.         }
  675.         if(config.getInternalResponseError()!=null){
  676.             integrationErrorConfiguration.addIntegrationError(rfc7807, IntegrationError.INTERNAL_RESPONSE_ERROR,
  677.                     convertToMessageType(config.getInternalResponseError().getMessageType()),
  678.                     to(config.getInternalResponseError().getErrorCode(), config.getInternalResponseError().getRetry(), config.getInternalResponseError().getErrorMessage()),
  679.                     config.isUseInternalFault());
  680.         }

  681.         return integrationErrorConfiguration;
  682.     }
  683.    
  684.     private static org.openspcoop2.message.constants.IntegrationErrorMessageType convertToMessageType(DefaultIntegrationErrorMessageType messageType){
  685.         switch (messageType) {
  686.         case SOAP_11:
  687.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SOAP_11;
  688.         case SOAP_12:
  689.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SOAP_12;
  690.         case XML:
  691.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.XML;
  692.         case JSON:
  693.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.JSON;
  694.         case NONE:
  695.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.NONE;
  696.         }
  697.         return null;
  698.     }
  699.    
  700.     private static org.openspcoop2.message.constants.IntegrationErrorMessageType convertToMessageType(IntegrationErrorMessageType messageType){
  701.         switch (messageType) {
  702.         case SOAP_AS_REQUEST:
  703.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SOAP_AS_REQUEST;
  704.         case SOAP_11:
  705.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SOAP_11;
  706.         case SOAP_12:
  707.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SOAP_12;
  708.         case XML:
  709.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.XML;
  710.         case JSON:
  711.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.JSON;
  712.         case NONE:
  713.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.NONE;
  714.         case SAME_AS_REQUEST:
  715.             return org.openspcoop2.message.constants.IntegrationErrorMessageType.SAME_AS_REQUEST;
  716.         }
  717.         return null;
  718.     }
  719.    
  720.     private static void addServiceBindingRestriction(Openspcoop2 manifest,ServiceBindingConfiguration config){
  721.        
  722.         for (int i = 0; i < manifest.getWeb().sizeContextList(); i++) {
  723.             String context = manifest.getWeb().getContext(i).getName();
  724.             org.openspcoop2.protocol.manifest.constants.ServiceBinding serviceBinding = manifest.getWeb().getContext(i).getBinding();
  725.             if(serviceBinding!=null){
  726.                 config.addServiceBindingContextRestriction(convertToServiceBinding(serviceBinding), context);
  727.             }
  728.         }
  729.        
  730.         if(manifest.getWeb().getEmptyContext()!=null){
  731.             org.openspcoop2.protocol.manifest.constants.ServiceBinding serviceBinding = manifest.getWeb().getEmptyContext().getBinding();
  732.             if(serviceBinding!=null){
  733.                 config.addServiceBindingEmptyContextRestriction(convertToServiceBinding(serviceBinding));
  734.             }
  735.         }
  736.        
  737.         for (int i = 0; i < manifest.getRegistry().getService().getTypes().sizeTypeList(); i++) {
  738.             String serviceType = manifest.getRegistry().getService().getTypes().getType(i).getName();
  739.             org.openspcoop2.protocol.manifest.constants.ServiceBinding serviceBinding = manifest.getRegistry().getService().getTypes().getType(i).getBinding();
  740.             if(serviceBinding!=null){
  741.                 config.addServiceBindingServiceTypeRestriction(convertToServiceBinding(serviceBinding), serviceType);
  742.             }
  743.         }
  744.        
  745.     }
  746.    
  747.     private static ServiceBinding convertToServiceBinding(org.openspcoop2.protocol.manifest.constants.ServiceBinding serviceBinding){
  748.         switch (serviceBinding) {
  749.         case SOAP:
  750.             return ServiceBinding.SOAP;
  751.         case REST:
  752.             return ServiceBinding.REST;
  753.         }
  754.         return null;
  755.     }
  756.    
  757.     private static ContextUrlCollection readContextUrlCollection(Openspcoop2 manifest, ConfigurationServiceBindingSoap soap, ConfigurationServiceBindingRest rest) throws MessageException{
  758.        
  759.         ContextUrlCollection urlCollection = new ContextUrlCollection(soap.getBinding(), rest.getBinding());    
  760.        
  761.         for (int i = 0; i < manifest.getWeb().sizeContextList(); i++) {
  762.             String context = manifest.getWeb().getContext(i).getName();
  763.             if(manifest.getWeb().getContext(i).sizeSubContextList()>0){
  764.                 for (int j = 0; j < manifest.getWeb().getContext(i).sizeSubContextList(); j++) {
  765.                     SubContextMapping subContext = manifest.getWeb().getContext(i).getSubContext(j);
  766.                     urlCollection.addContext(context,subContext.getFunctionRawEnumValue(),subContext.getBase(),convertToMessageType(subContext.getMessageType()));
  767.                 }
  768.             }
  769.             if(manifest.getWeb().getContext(i).getEmptySubContext()!=null){
  770.                 EmptySubContextMapping subContext = manifest.getWeb().getContext(i).getEmptySubContext();
  771.                 urlCollection.addContext(context,subContext.getFunctionRawEnumValue(),null, convertToMessageType(subContext.getMessageType()));
  772.             }
  773.         }
  774.        
  775.         if(manifest.getWeb().getEmptyContext()!=null){
  776.             if(manifest.getWeb().getEmptyContext().sizeSubContextList()>0){
  777.                 for (int j = 0; j < manifest.getWeb().getEmptyContext().sizeSubContextList(); j++) {
  778.                     SubContextMapping subContext = manifest.getWeb().getEmptyContext().getSubContext(j);
  779.                     urlCollection.addContext(null,subContext.getFunctionRawEnumValue(),subContext.getBase(), convertToMessageType(subContext.getMessageType()));
  780.                 }
  781.             }
  782.             if(manifest.getWeb().getEmptyContext().getEmptySubContext()!=null){
  783.                 EmptySubContextMapping subContext = manifest.getWeb().getEmptyContext().getEmptySubContext();
  784.                 urlCollection.addContext(null,subContext.getFunctionRawEnumValue(),null, convertToMessageType(subContext.getMessageType()));
  785.             }
  786.         }
  787.        
  788.         return urlCollection;
  789.            
  790.     }
  791.    
  792.     private static MessageType convertToMessageType(org.openspcoop2.protocol.manifest.constants.MessageType messageType){
  793.         switch (messageType) {
  794.         case SOAP_11:
  795.             return MessageType.SOAP_11;
  796.         case SOAP_12:
  797.             return MessageType.SOAP_12;
  798.         case XML:
  799.             return MessageType.XML;
  800.         case JSON:
  801.             return MessageType.JSON;
  802.         case BINARY:
  803.             return MessageType.BINARY;
  804.         case MIME_MULTIPART:
  805.             return MessageType.MIME_MULTIPART;
  806.         }
  807.         return null;
  808.     }
  809. }