ZipTemplate.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.pdd.core.dynamic;

  21. import java.io.Serializable;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.utils.SemaphoreLock;
  26. import org.openspcoop2.utils.io.Entry;
  27. import org.openspcoop2.utils.io.ZipUtilities;

  28. /**
  29.  * ZipTemplate
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ZipTemplate implements Serializable {

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;

  40.     private String name;
  41.     private byte[] zip;
  42.    
  43.     public ZipTemplate(String name, byte[] zip) {
  44.         this.name = name;
  45.         this.zip = zip;
  46.     }

  47.     public String getName() {
  48.         return this.name;
  49.     }
  50.     public byte[] getZip() {
  51.         return this.zip;
  52.     }
  53.    
  54.    
  55.     private Template templateFreeMarker;
  56.     private Template templateVelocity;
  57.    
  58.     private transient org.openspcoop2.utils.Semaphore _lock = null;
  59.     private synchronized void initLock() {
  60.         if(this._lock==null) {
  61.             this._lock = new org.openspcoop2.utils.Semaphore("ZipTemplate");
  62.         }
  63.     }
  64.     public org.openspcoop2.utils.Semaphore getLock(){
  65.         if(this._lock==null) {
  66.             initLock();
  67.         }
  68.         return this._lock;
  69.     }
  70.    
  71.     private void initTemplateFreeMarker() throws DynamicException{
  72.         if(this.templateFreeMarker==null) {
  73.             SemaphoreLock lock = null;
  74.             try {
  75.                 lock = this.getLock().acquire("initTemplateFreeMarker");
  76.             }catch(Throwable t) {
  77.                 throw new DynamicException(t.getMessage(),t);
  78.             }
  79.             try {
  80.                 this.templateFreeMarker = buildTemplate(Costanti.ZIP_INDEX_ENTRY_FREEMARKER);
  81.             }finally {
  82.                 this.getLock().release(lock, "initTemplateFreeMarker");
  83.             }
  84.         }
  85.     }
  86.     public Template getTemplateFreeMarker() throws DynamicException{
  87.         if(this.templateFreeMarker==null) {
  88.             initTemplateFreeMarker();
  89.         }
  90.         return this.templateFreeMarker;
  91.     }
  92.    
  93.     private void initTemplateVelocity() throws DynamicException{
  94.         if(this.templateVelocity==null) {
  95.             SemaphoreLock lock = null;
  96.             try {
  97.                 lock = this.getLock().acquire("initTemplateVelocity");
  98.             }catch(Throwable t) {
  99.                 throw new DynamicException(t.getMessage(),t);
  100.             }
  101.             try {
  102.                 this.templateVelocity = buildTemplate(Costanti.ZIP_INDEX_ENTRY_VELOCITY);
  103.             }finally {
  104.                 this.getLock().release(lock, "initTemplateVelocity");
  105.             }
  106.         }
  107.     }
  108.     public Template getTemplateVelocity() throws DynamicException{
  109.         if(this.templateVelocity==null) {
  110.             initTemplateVelocity();
  111.         }
  112.         return this.templateVelocity;
  113.     }
  114.    
  115.    
  116.    
  117.     private Template buildTemplate(String indexEntryName) throws DynamicException {
  118.         List<Entry> entries = null;
  119.         try {
  120.             entries = ZipUtilities.read(this.zip);
  121.         }catch(Exception e) {
  122.             throw new DynamicException(e.getMessage(),e);
  123.         }
  124.         if(entries.isEmpty()) {
  125.             throw new DynamicException("Entries not found");
  126.         }
  127.         byte[] template = null;
  128.         Map<String, byte[]> templateIncludes = new HashMap<>();
  129.         for (Entry entry : entries) {
  130.             if(indexEntryName.equals(entry.getName())) {
  131.                 template = entry.getContent();
  132.             }
  133.             else if(!entry.getName().contains("/") && !entry.getName().contains("\\") && template==null) {
  134.                 // prende il primo
  135.                 template = entry.getContent();
  136.             }
  137.             else {
  138.                 templateIncludes.put(entry.getName(), entry.getContent());
  139.             }
  140.         }
  141.        
  142.         Template t = new Template(this.name, template, templateIncludes);
  143.        
  144.         return t;
  145.     }
  146.    
  147. }