TemplateFruizione.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.  * TemplateFruizione
  34.  *
  35.  * @author Poli Andrea (poli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class TemplateFruizione extends TemplateCore {

  40.     public TemplateFruizione() throws ProtocolException{
  41.        
  42.         super(false);
  43.        
  44.         try{
  45.             String baseUrl = "/"+CostantiAbstraction.TEMPLATES_DIR+"/"+CostantiAbstraction.TEMPLATES_FRUIZIONE_DIR+"."+CostantiAbstraction.ZIP_EXTENSION;
  46.             InputStream is = TemplateFruizione.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[]> serviziApplicativi = new ArrayList<byte[]>();
  60.     private List<byte[]> porteDelegate = new ArrayList<byte[]>();
  61.        
  62.     public List<byte[]> getServiziApplicativi() {
  63.         return this.serviziApplicativi;
  64.     }
  65.     public void setServiziApplicativi(List<byte[]> serviziApplicativi) {
  66.         this.serviziApplicativi = serviziApplicativi;
  67.     }
  68.     public List<Template> getTemplateServiziApplicativi() throws IOException {
  69.         List<Template> templates = new ArrayList<Template>();
  70.         for (int i = 0; i < this.serviziApplicativi.size(); i++) {
  71.             templates.add(TemplateUtils.buildTemplate("sa_"+"i", this.serviziApplicativi.get(i)));
  72.         }
  73.         return templates;
  74.     }
  75.    
  76.     public List<byte[]> getPorteDelegate() {
  77.         return this.porteDelegate;
  78.     }
  79.     public void setPorteDelegate(List<byte[]> porteDelegate) {
  80.         this.porteDelegate = porteDelegate;
  81.     }
  82.     public List<Template> getTemplatePorteDelegate() throws IOException {
  83.         List<Template> templates = new ArrayList<Template>();
  84.         for (int i = 0; i < this.porteDelegate.size(); i++) {
  85.             templates.add(TemplateUtils.buildTemplate("pd_"+"i", this.porteDelegate.get(i)));
  86.         }
  87.         return templates;
  88.     }
  89.    
  90.    
  91.    

  92.     @Override
  93.     public void updateOtherResource(String entryName,InputStream inputStream,byte[]xml,Map<String, Boolean> mapFound) throws ProtocolException{
  94.        
  95.         String rootDirNameExpected = this.getRootDirName()+File.separatorChar;
  96.        
  97.         if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_PORTE_DELEGATE+File.separatorChar) ){
  98.             if(mapFound.containsKey("pd")==false || mapFound.get("pd")==false){
  99.                 this.getPorteDelegate().clear();
  100.             }
  101.             this.getPorteDelegate().add(xml);
  102.             mapFound.put("pd", true);
  103.         }
  104.         else if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_SERVIZI_APPLICATIVI+File.separatorChar) ){
  105.             if(mapFound.containsKey("sa")==false || mapFound.get("sa")==false){
  106.                 this.getServiziApplicativi().clear();
  107.             }
  108.             this.getServiziApplicativi().add(xml);
  109.             mapFound.put("sa", true);
  110.         }
  111.         else{
  112.             throw new ProtocolException("Elemento ["+entryName+"] non atteso");
  113.         }
  114.        
  115.     }
  116.    
  117. }