TemplateErogazione.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.abstraction.template;

  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.protocol.abstraction.constants.CostantiAbstraction;
  28. import org.openspcoop2.protocol.sdk.ProtocolException;
  29. import org.openspcoop2.utils.Utilities;
  30. import org.openspcoop2.utils.resources.TemplateUtils;

  31. import freemarker.template.Template;

  32. /**    
  33.  * TemplateErogazione
  34.  *
  35.  * @author Poli Andrea (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class TemplateErogazione extends TemplateCore {

  40.     public TemplateErogazione() throws ProtocolException{
  41.        
  42.         super(true);
  43.        
  44.         try{
  45.             String baseUrl = "/"+CostantiAbstraction.TEMPLATES_DIR+"/"+CostantiAbstraction.TEMPLATES_EROGAZIONE_DIR+"."+CostantiAbstraction.ZIP_EXTENSION;
  46.             InputStream is = TemplateErogazione.class.getResourceAsStream(baseUrl);
  47.             if(is==null){
  48.                 throw new ProtocolException("Resource with url ["+baseUrl+"] not found");
  49.             }
  50.             byte[]zipFile = Utilities.getAsByteArray(is);
  51.             is.close();
  52.             this.updateTemplates(zipFile);
  53.         }catch(Exception e){
  54.             throw new ProtocolException(e.getMessage(),e);
  55.         }
  56.        
  57.     }
  58.    
  59.     private List<byte[]> accordiParteSpecifica = new ArrayList<byte[]>();
  60.     private List<byte[]> serviziApplicativi = new ArrayList<byte[]>();
  61.     private List<byte[]> porteApplicative = new ArrayList<byte[]>();
  62.    
  63.    
  64.     public List<byte[]> getAccordiParteSpecifica() {
  65.         return this.accordiParteSpecifica;
  66.     }
  67.     public void setAccordiParteSpecifica(List<byte[]> accordiParteSpecifica) {
  68.         this.accordiParteSpecifica = accordiParteSpecifica;
  69.     }
  70.     public List<Template> getTemplateAccordiParteSpecifica() throws IOException {
  71.         List<Template> templates = new ArrayList<Template>();
  72.         for (int i = 0; i < this.accordiParteSpecifica.size(); i++) {
  73.             templates.add(TemplateUtils.buildTemplate("asps_"+"i", this.accordiParteSpecifica.get(i)));
  74.         }
  75.         return templates;
  76.     }
  77.    
  78.    
  79.     public List<byte[]> getServiziApplicativi() {
  80.         return this.serviziApplicativi;
  81.     }
  82.     public void setServiziApplicativi(List<byte[]> serviziApplicativi) {
  83.         this.serviziApplicativi = serviziApplicativi;
  84.     }
  85.     public List<Template> getTemplateServiziApplicativi() throws IOException {
  86.         List<Template> templates = new ArrayList<Template>();
  87.         for (int i = 0; i < this.serviziApplicativi.size(); i++) {
  88.             templates.add(TemplateUtils.buildTemplate("sa_"+"i", this.serviziApplicativi.get(i)));
  89.         }
  90.         return templates;
  91.     }
  92.    
  93.     public List<byte[]> getPorteApplicative() {
  94.         return this.porteApplicative;
  95.     }
  96.     public void setPorteApplicative(List<byte[]> porteApplicative) {
  97.         this.porteApplicative = porteApplicative;
  98.     }
  99.     public List<Template> getTemplatePorteApplicative() throws IOException {
  100.         List<Template> templates = new ArrayList<Template>();
  101.         for (int i = 0; i < this.porteApplicative.size(); i++) {
  102.             templates.add(TemplateUtils.buildTemplate("pa_"+"i", this.porteApplicative.get(i)));
  103.         }
  104.         return templates;
  105.     }
  106.    
  107.    
  108.     @Override
  109.     public void updateOtherResource(String entryName,InputStream inputStream,byte[]xml,Map<String, Boolean> mapFound) throws ProtocolException{
  110.        
  111.         String rootDirNameExpected = this.getRootDirName()+File.separatorChar;
  112.        
  113.         if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_ACCORDI_PARTE_SPECIFICA+File.separatorChar) ){
  114.             if(mapFound.containsKey("asps")==false || mapFound.get("asps")==false){
  115.                 this.getAccordiParteSpecifica().clear();
  116.             }
  117.             this.getAccordiParteSpecifica().add(xml);
  118.             mapFound.put("asps", true);
  119.         }
  120.         else if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_PORTE_APPLICATIVE+File.separatorChar) ){
  121.             if(mapFound.containsKey("pa")==false || mapFound.get("pa")==false){
  122.                 this.getPorteApplicative().clear();
  123.             }
  124.             this.getPorteApplicative().add(xml);
  125.             mapFound.put("pa", true);
  126.         }
  127.         else if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_SERVIZI_APPLICATIVI+File.separatorChar) ){
  128.             if(mapFound.containsKey("sa")==false || mapFound.get("sa")==false){
  129.                 this.getServiziApplicativi().clear();
  130.             }
  131.             this.getServiziApplicativi().add(xml);
  132.             mapFound.put("sa", true);
  133.         }
  134.         else{
  135.             throw new ProtocolException("Elemento ["+entryName+"] non atteso");
  136.         }
  137.        
  138.     }
  139. }