MailcapActivationReader.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.utils.dch;

  21. import java.net.URL;
  22. import java.util.Enumeration;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.slf4j.Logger;
  27. import org.openspcoop2.utils.Utilities;
  28. import org.openspcoop2.utils.UtilsException;


  29. /**
  30.  * Classe che permette di leggere il file mailcap presente nel MANIFEST di OpenSPCoop.ear
  31.  *
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class MailcapActivationReader {
  38.    
  39.      private static Map<String, String> mimeTypes = new HashMap<>();
  40.    
  41.      public static void initDataContentHandler(Logger log,boolean forceLoadMailcap) throws UtilsException{
  42.           try
  43.             {
  44.               DataContentHandlerManager dchManager = new DataContentHandlerManager(log);
  45.               List<String> typesRegistrati = dchManager.readMimeTypesRegistrati(true);
  46.              
  47.               java.io.InputStream is = MailcapActivationReader.class.getResourceAsStream("/META-INF/mailcap");
  48.               java.util.Properties prop= new java.util.Properties();
  49.               prop.load(is);
  50.               Enumeration<?> en =  prop.keys();
  51.               while (en.hasMoreElements()){
  52.                   String key = (String) en.nextElement();
  53.                   int index = key.indexOf(";;");
  54.                   if(index==-1){
  55.                       throw new Exception("Entry "+key+(String) prop.get(key)+ " definita con un formato scorretto [;;] (vedi javax/activation/MailcapCommandMap api)");
  56.                   }
  57.                   String keyMime = key.substring(0,index);
  58.                   keyMime = keyMime.trim();
  59.                  
  60.                   String value = (String) prop.get(key);
  61.                   index = value.indexOf("=");
  62.                   if(index==-1){
  63.                       throw new Exception("Entry "+key+(String) prop.get(key)+ " definita con un formato scorretto [=] (vedi javax/activation/MailcapCommandMap api)");
  64.                   }
  65.                   value=value.substring(index+1,value.length());
  66.                   value=value.trim();
  67.                  
  68.                   MailcapActivationReader.mimeTypes.put(keyMime, value);
  69.                  
  70.                   if(typesRegistrati.contains(keyMime)==false){
  71.                       if(forceLoadMailcap){
  72.                          log.info("DataContentHandler per mime-type "+keyMime+" non risulta registrato, caricamento in corso ...");            
  73.                          // La chiave non possiede il valore originale. Mancano i caratteri dopo la spazio: dchManager.addMimeTypeIntoMailcap(key+"="+value);
  74.                          URL url = MailcapActivationReader.class.getResource("/META-INF/mailcap");
  75.                          byte [] mailcap = Utilities.getAsByteArray(url);
  76.                          dchManager.addMimeTypesIntoMailcap(mailcap);
  77.                       }
  78.                       else{
  79.                           log.error("DataContentHandler per mime-type "+keyMime+" non risulta registrato");                  
  80.                       }
  81.                   }
  82.                  
  83.                   log.info("Caricato nel core di OpenSPCoop (!!differente dal MailcapEngine!!) DataContentHandler per mime-type "+keyMime+" gestito tramite la classe "+value);
  84.               }
  85.             }
  86.             catch(Exception e)
  87.             {
  88.                 throw new UtilsException(e.getMessage(),e);
  89.             }
  90.         }

  91.    
  92.      public static boolean existsDataContentHandler(String mimeType){
  93.          return MailcapActivationReader.mimeTypes.containsKey(mimeType);
  94.      }
  95. }