TemplateCore.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.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.Map;
  28. import java.util.zip.ZipEntry;
  29. import java.util.zip.ZipFile;

  30. import javax.activation.FileDataSource;

  31. import org.openspcoop2.protocol.abstraction.constants.CostantiAbstraction;
  32. import org.openspcoop2.protocol.sdk.ProtocolException;
  33. import org.openspcoop2.utils.Utilities;
  34. import org.openspcoop2.utils.io.ZipUtilities;
  35. import org.openspcoop2.utils.resources.FileSystemUtilities;
  36. import org.openspcoop2.utils.resources.TemplateUtils;

  37. import freemarker.template.Template;

  38. /**    
  39.  * TemplateCore
  40.  *
  41.  * @author Poli Andrea (poli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class TemplateCore {

  46.     protected TemplateCore(boolean erogazione) throws ProtocolException{
  47.        
  48.         this.zipName = "openspcoop_core_template";
  49.         this.rootDirName = CostantiAbstraction.TEMPLATES_CORE_DIR;
  50.         try{
  51.             String baseUrl = "/"+CostantiAbstraction.TEMPLATES_DIR+"/"+CostantiAbstraction.TEMPLATES_CORE_DIR+"."+CostantiAbstraction.ZIP_EXTENSION;
  52.             InputStream is = TemplateCore.class.getResourceAsStream(baseUrl);
  53.             if(is==null){
  54.                 throw new ProtocolException("Resource with url ["+baseUrl+"] not found");
  55.             }
  56.             byte[]zipFile = Utilities.getAsByteArray(is);
  57.             is.close();
  58.             this.updateTemplates(zipFile);
  59.         }catch(Exception e){
  60.             throw new ProtocolException(e.getMessage(),e);
  61.         }
  62.        
  63.         if(erogazione){
  64.             this.zipName = "openspcoop_erogazione_template";
  65.             this.rootDirName = CostantiAbstraction.TEMPLATES_EROGAZIONE_DIR;
  66.         }
  67.         else{
  68.             this.zipName = "openspcoop_fruizione_template";
  69.             this.rootDirName = CostantiAbstraction.TEMPLATES_FRUIZIONE_DIR;
  70.         }
  71.     }
  72.    
  73.     private String zipName;
  74.     private String rootDirName;
  75.     public String getZipName() {
  76.         return this.zipName;
  77.     }
  78.     public String getRootDirName() {
  79.         return this.rootDirName;
  80.     }

  81.     private byte[] pdd;
  82.     private byte[] soggetto;
  83.     private byte[] fruitore;
  84.    
  85.     public byte[] getPdd() {
  86.         return this.pdd;
  87.     }
  88.     public void setPdd(byte[] pdd) {
  89.         this.pdd = pdd;
  90.     }
  91.     public Template getTemplatePdd() throws IOException {
  92.         return TemplateUtils.buildTemplate("pdd",this.pdd);
  93.     }
  94.    
  95.     public byte[] getSoggetto() {
  96.         return this.soggetto;
  97.     }
  98.     public void setSoggetto(byte[] soggetto) {
  99.         this.soggetto = soggetto;
  100.     }
  101.     public Template getTemplateSoggetto() throws IOException {
  102.         return TemplateUtils.buildTemplate("soggetto",this.soggetto);
  103.     }
  104.    
  105.    
  106.     public byte[] getFruitore() {
  107.         return this.fruitore;
  108.     }
  109.     public void setFruitore(byte[] fruitore) {
  110.         this.fruitore = fruitore;
  111.     }
  112.     public Template getTemplateFruitore() throws IOException {
  113.         return TemplateUtils.buildTemplate("fruitore",this.fruitore);
  114.     }
  115.    
  116.    
  117.    
  118.    
  119.    
  120.     public void updateTemplates(byte[] zip) throws ProtocolException{
  121.         File tmp = null;
  122.         FileOutputStream fout = null;
  123.         try{
  124.             tmp = FileSystemUtilities.createTempFile(this.zipName, ".zip");
  125.            
  126.             fout = new FileOutputStream(tmp);
  127.             fout.write(zip);
  128.             fout.flush();
  129.             fout.close();

  130.             this.updateTemplates(tmp);
  131.            
  132.             if(tmp!=null) {
  133.                 if(!tmp.delete()) {
  134.                     // ignore
  135.                 }
  136.             }
  137.            
  138.         }catch(Exception e){
  139.             if(tmp!=null)
  140.                 throw new ProtocolException("["+tmp.getAbsolutePath()+"] "+ e.getMessage(),e);
  141.             else
  142.                 throw new ProtocolException(e.getMessage(),e);
  143.         }finally{
  144.             try{
  145.                 if(fout!=null)
  146.                     fout.close();
  147.             }catch(Exception eClose){
  148.                 // close
  149.             }
  150.         }
  151.        
  152.     }
  153.     public void updateTemplates(File zip) throws ProtocolException{
  154.         ZipFile zipFile = null;
  155.         try{
  156.             zipFile = new ZipFile(zip);
  157.             this.updateTemplates(zipFile);
  158.         }catch(Exception e){
  159.             throw new ProtocolException(e.getMessage(),e);
  160.         }finally{
  161.             try{
  162.                 if(zipFile!=null)
  163.                     zipFile.close();
  164.             }catch(Exception eClose){
  165.                 // close
  166.             }
  167.         }
  168.     }
  169.     public void updateTemplates(ZipFile zip) throws ProtocolException{
  170.         try{
  171.            
  172.             String rootDir = null;
  173.            
  174.             boolean foundPdd = false;
  175.             boolean foundSoggetto = false;
  176.             boolean foundFruitore = false;
  177.             Map<String, Boolean> mapFound = new HashMap<String, Boolean>();
  178.            
  179.             Iterator<ZipEntry> it = ZipUtilities.entries(zip, true);
  180.             while (it.hasNext()) {
  181.                 ZipEntry zipEntry = (ZipEntry) it.next();
  182.                 String entryName = ZipUtilities.operativeSystemConversion(zipEntry.getName());
  183.                
  184.                 //System.out.println("FILE NAME:  "+entryName);
  185.                 //System.out.println("SIZE:  "+entry.getSize());

  186.                 // Il codice dopo fissa il problema di inserire una directory nel package.
  187.                 // Commentare la riga sotto per ripristinare il vecchio comportamento.
  188.                 if(rootDir==null){
  189.                     // Calcolo ROOT DIR
  190.                     rootDir=ZipUtilities.getRootDir(entryName);
  191.                 }
  192.                
  193.                 if(zipEntry.isDirectory()) {
  194.                     continue; // directory
  195.                 }
  196.                 else {
  197.                     FileDataSource fds = new FileDataSource(entryName);
  198.                     String nome = fds.getName();
  199.                     String tipo = nome.substring(nome.lastIndexOf(".")+1,nome.length());
  200.                     tipo = tipo.toUpperCase();
  201.                     //System.out.println("VERIFICARE NAME["+nome+"] TIPO["+tipo+"]");
  202.                    
  203.                     InputStream inputStream = zip.getInputStream(zipEntry);
  204.                     byte[]xml = Utilities.getAsByteArray(inputStream);

  205.                     try{
  206.                        
  207.                         // ********** configurazione ****************
  208.                         String rootDirNameExpected = this.rootDirName+File.separatorChar;
  209.                         if(rootDir.equals(rootDirNameExpected) ){
  210.                                                        
  211.                             if(entryName.endsWith("."+CostantiAbstraction.FTL_EXTENSION) == false ){
  212.                                 throw new ProtocolException("Elemento ["+entryName+"] non atteso (E' richiesto un file template .ftl)");
  213.                             }
  214.                            
  215.                             if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_PDD+File.separatorChar) ){
  216.                                 if(foundPdd==true){
  217.                                     throw new ProtocolException("Elemento ["+entryName+"] non atteso (E' possibile indicare solamente una definizione di porta di dominio)");
  218.                                 }
  219.                                 this.pdd = xml;
  220.                                 foundPdd = true;
  221.                             }
  222.                             else if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_SOGGETTO+File.separatorChar) ){
  223.                                 if(foundSoggetto==true){
  224.                                     throw new ProtocolException("Elemento ["+entryName+"] non atteso (E' possibile indicare solamente una definizione di soggetto)");
  225.                                 }
  226.                                 this.soggetto = xml;
  227.                                 foundSoggetto = true;
  228.                             }
  229.                             else if(entryName.startsWith(rootDirNameExpected+CostantiAbstraction.TEMPLATES_FRUITORE+File.separatorChar) ){
  230.                                 if(foundFruitore==true){
  231.                                     throw new ProtocolException("Elemento ["+entryName+"] non atteso (E' possibile indicare solamente una definizione di fruitore)");
  232.                                 }
  233.                                 this.fruitore = xml;
  234.                                 foundFruitore = true;
  235.                             }
  236.                             else{
  237.                                 this.updateOtherResource(entryName, inputStream, xml, mapFound);
  238.                             }
  239.                            
  240.                         }
  241.                         else{
  242.                             throw new ProtocolException("Elemento ["+entryName+"] non atteso");
  243.                         }
  244.                        
  245.                     }finally{
  246.                         try{
  247.                             if(inputStream!=null){
  248.                                 inputStream.close();
  249.                             }
  250.                         }catch(Exception eClose){
  251.                             // close
  252.                         }
  253.                     }
  254.                 }
  255.             }

  256.         }catch(Exception e){
  257.             throw new ProtocolException(e.getMessage(),e);
  258.         }
  259.     }
  260.    
  261.     public void updateOtherResource(String entryName,InputStream inputStream,byte[]xml,Map<String, Boolean> mapFound) throws ProtocolException{
  262.         throw new ProtocolException("Elemento ["+entryName+"] non atteso");
  263.     }
  264. }