DataContentHandlerManager.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.io.BufferedReader;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.net.URL;
  26. import java.util.ArrayList;
  27. import java.util.Enumeration;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;

  31. import javax.activation.CommandInfo;
  32. import javax.activation.CommandMap;
  33. import javax.activation.MailcapCommandMap;

  34. import org.slf4j.Logger;
  35. import org.openspcoop2.utils.Utilities;
  36. import org.openspcoop2.utils.UtilsException;

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

  46.     private Logger log;
  47.    
  48.     public DataContentHandlerManager(Logger log){
  49.         this.log = log;
  50.     }
  51.    
  52.     public void initMailcap(){
  53.         // NOTA, anche se gia' esistenti le registrazioni, è importante farlo caricare per fargliele vedere.
  54.        
  55.         try{
  56.             this.log.info("Search mailcap.default ...");
  57.             Enumeration<URL> en = DataContentHandlerManager.class.getClassLoader().getResources("/META-INF/mailcap.default");
  58.             if(en!=null){
  59.                 while (en.hasMoreElements()) {
  60.                     URL url = (URL) en.nextElement();
  61.                     this.log.info("Find mailcap.default ["+url.toString()+"]");
  62.                     this.log.info("Read mailcap.default ["+url.toString()+"] ...");
  63.                     byte[] tmp = Utilities.getAsByteArray(url);
  64.                     this.log.info("Read mailcap.default ["+url.toString()+"]: "+new String(tmp));
  65.                     addMimeTypesIntoMailcap(tmp);
  66.                 }
  67.             }
  68.         }catch(Exception e){
  69.             this.log.error("Mailcap.default search error: "+e.getMessage(),e);
  70.         }
  71.        
  72.         try{
  73.             this.log.info("Search mailcap ...");
  74.             Enumeration<URL> en = DataContentHandlerManager.class.getClassLoader().getResources("/META-INF/mailcap");
  75.             if(en!=null){
  76.                 while (en.hasMoreElements()) {
  77.                     URL url = (URL) en.nextElement();
  78.                     this.log.info("Find mailcap ["+url.toString()+"]");
  79.                     this.log.info("Read mailcap ["+url.toString()+"] ...");
  80.                     byte[] tmp = Utilities.getAsByteArray(url);
  81.                     this.log.info("Read mailcap ["+url.toString()+"]: "+new String(tmp));
  82.                     addMimeTypesIntoMailcap(tmp);
  83.                 }
  84.             }
  85.         }catch(Exception e){
  86.             this.log.error("Mailcap search error: "+e.getMessage(),e);
  87.         }
  88.     }
  89.    
  90.    
  91.     public List<String> readMimeTypesRegistrati() {
  92.         return readMimeTypesRegistrati(true);
  93.     }
  94.     public List<String> readMimeTypesRegistrati(boolean debug) {
  95.        
  96.         MailcapCommandMap mcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  97.        
  98.         String [] gestiti = mcap.getMimeTypes();
  99.         List<String> gestitiAsList = new ArrayList<>();
  100.         if(gestiti!=null){
  101.             if(debug) {
  102.                 this.log.info("MimeTypes registrati: ["+gestiti.length+"]");
  103.             }
  104.             for (int i = 0; i < gestiti.length; i++) {
  105.                 if(debug) {
  106.                     this.log.info("MimeType registrato: ["+gestiti[i]+"]");
  107.                 }
  108.                 gestitiAsList.add(gestiti[i]);
  109.                 CommandInfo [] cis = mcap.getAllCommands(gestiti[i]);
  110.                 for (int j = 0; j < cis.length; j++) {
  111.                     if(debug) {
  112.                         this.log.info("\t["+j+"] "+cis[j].getCommandName()+" = "+cis[j].getCommandClass());
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         else{
  118.             if(debug) {
  119.                 this.log.info("Non risultano registrati MimeTypes");
  120.             }
  121.         }
  122.        
  123.         return gestitiAsList;
  124.     }
  125.    
  126.     public Map<String,String> readMimeTypesContentHandler() {
  127.         return readMimeTypesContentHandler(true);
  128.     }
  129.     public Map<String,String> readMimeTypesContentHandler(boolean debug) {
  130.         return _readMimeTypesClass(debug, "content-handler");
  131.     }
  132.    
  133.     public Map<String,String> readMimeTypesView() {
  134.         return readMimeTypesView(true);
  135.     }
  136.     public Map<String,String> readMimeTypesView(boolean debug) {
  137.         return _readMimeTypesClass(debug, "view");
  138.     }
  139.    
  140.     public Map<String,String> readMimeTypesEdit() {
  141.         return readMimeTypesEdit(true);
  142.     }
  143.     public Map<String,String> readMimeTypesEdit(boolean debug) {
  144.         return _readMimeTypesClass(debug, "edit");
  145.     }
  146.    
  147.     private Map<String,String> _readMimeTypesClass(boolean debug,String commandName) {
  148.         MailcapCommandMap mcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  149.        
  150.         String [] gestiti = mcap.getMimeTypes();
  151.         Map<String,String> map = new HashMap<>();
  152.         if(gestiti!=null){
  153.             if(debug) {
  154.                 this.log.info("MimeTypes ("+commandName+") registrati: ["+gestiti.length+"]");
  155.             }
  156.             for (int i = 0; i < gestiti.length; i++) {
  157.                 if(debug) {
  158.                     this.log.info("MimeType ("+commandName+") registrato: ["+gestiti[i]+"]");
  159.                 }
  160.                 CommandInfo [] cis = mcap.getAllCommands(gestiti[i]);
  161.                 if(cis!=null && cis.length>0) {
  162.                     for (int j = 0; j < cis.length; j++) {
  163.                         if(debug) {
  164.                             this.log.info("\t["+j+"] "+cis[j].getCommandName()+" = "+cis[j].getCommandClass());
  165.                         }
  166.                         if(commandName.equalsIgnoreCase(cis[j].getCommandName())) {
  167.                             map.put(gestiti[i],cis[j].getCommandClass());
  168.                         }
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.         else{
  174.             if(debug) {
  175.                 this.log.info("Non risultano registrati MimeTypes ("+commandName+")");
  176.             }
  177.         }
  178.         return map;
  179.     }
  180.    
  181.     public void addMimeTypeIntoMailcap(String commandInfo) throws UtilsException{
  182.        
  183.         if(commandInfo.contains(";;")==false){
  184.             throw new UtilsException("Formato non corretto, non è stato riscontrato il carattere ';;'");
  185.         }
  186.         String [] split = commandInfo.split(";;");
  187.         if(split==null || split.length!=2){
  188.             throw new UtilsException("Formato non corretto");
  189.         }
  190.        
  191.         MailcapCommandMap mcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  192.         mcap.addMailcap(commandInfo);
  193.         CommandMap.setDefaultCommandMap(mcap);
  194.        
  195.         this.log.info("Registrato in Mailcap il mimetype ["+split[0]+"]: "+commandInfo);
  196.     }
  197.    
  198.     public void addMimeTypesIntoMailcap(byte[] mailcap) throws IOException, UtilsException{
  199.        
  200.         List<String> gestitiAsList = this.readMimeTypesRegistrati(false);
  201.        
  202.         BufferedReader br = null;
  203.         InputStreamReader isr = null;
  204.         ByteArrayInputStream bin = null;
  205.         try{
  206.             bin = new ByteArrayInputStream(mailcap);
  207.             isr = new InputStreamReader(bin);
  208.             br = new BufferedReader(isr);
  209.             String strLine;
  210.             //Read File Line By Line
  211.             while ((strLine = br.readLine()) != null)   {
  212.                 // Print the content on the console
  213.                 //System.out.println (strLine);
  214.                 if(strLine!=null && strLine.startsWith("#")==false){
  215.                     if(strLine.contains(";;")){
  216.                         String [] split = strLine.split(";;");
  217.                         if(gestitiAsList.contains(split[0])==false){
  218.                             gestitiAsList.add(split[0]);
  219.                             this.addMimeTypeIntoMailcap(strLine);
  220.                         }
  221.                     }
  222.                 }
  223.             }
  224.         }finally{
  225.             try{
  226.                 br.close();
  227.             }catch(Exception eClose){}
  228.             try{
  229.                 isr.close();
  230.             }catch(Exception eClose){}
  231.             try{
  232.                 bin.close();
  233.             }catch(Exception eClose){}
  234.         }
  235.        
  236.     }
  237.    
  238. }