MimeTypes.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.mime;

  21. import java.io.BufferedReader;
  22. import java.io.File;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.util.HashMap;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.UtilsException;

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

  36.     private Map<String, Object> mapMimeToExt = new HashMap<>();
  37.     private Map<String, String> mapExtToMime = new HashMap<>();
  38.    
  39.     private MimeTypes() throws UtilsException{
  40.        
  41.         String file = "/org/openspcoop2/utils/mime/mime.types";
  42.         try (InputStream is = MimeTypes.class.getResourceAsStream(file);){
  43.             if(is==null){
  44.                 throw new UtilsException("File ["+file+"] in classpath not found");
  45.             }
  46.             init(is, this.mapMimeToExt, this.mapExtToMime);
  47.         }catch(Exception e){
  48.             throw new UtilsException(e.getMessage(),e);
  49.         }
  50.        
  51.     }
  52.     private static void init(InputStream is, Map<String, Object> mapMimeToExt, Map<String, String> mapExtToMime) throws UtilsException {
  53.         try (
  54.                 InputStreamReader ir = new InputStreamReader(is);
  55.                 BufferedReader br = new BufferedReader(ir);
  56.             ){
  57.                 String line;
  58.                 while ((line = br.readLine()) != null) {
  59.                     line = line.trim();
  60.                     if(!line.startsWith("#")){
  61.                         if(line.contains("\t")){
  62.                             throw new UtilsException("Line["+line+"] contains tabs");
  63.                         }
  64.                         String [] tmp = line.split(" ");
  65.                         if(tmp.length<2){
  66.                             /** System.out.println("TYPE["+tmp[0]+"] without exts"); */
  67.                             mapMimeToExt.put(tmp[0].trim(), org.apache.commons.lang.ObjectUtils.NULL);
  68.                         }else{
  69.                             StringBuilder bf = new StringBuilder();
  70.                             for (int i = 1; i < tmp.length; i++) {
  71.                                 bf.append(" EXT-"+i+"=["+tmp[i].trim()+"]");
  72.                                 mapExtToMime.put(tmp[i].trim(),tmp[0].trim());
  73.                             }
  74.                             /** System.out.println("TYPE["+tmp[0]+"]"+bf.toString()); */
  75.                             mapMimeToExt.put(tmp[0].trim(), tmp[1].trim());
  76.                         }
  77.                     }
  78.                 }
  79.         }catch(Exception e){
  80.             throw new UtilsException(e.getMessage(),e);
  81.         }
  82.     }
  83.    
  84.     public String getMimeType(File file){
  85.         String ext = null;
  86.         try{
  87.             String fileName = file.getName();
  88.             ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
  89.         }catch(Exception e){
  90.             // close
  91.         }
  92.         if(ext==null){
  93.             ext = "bin";
  94.         }
  95.         return this.getMimeType(ext);
  96.     }
  97.     public String getMimeType(String ext){
  98.         if("xml".equalsIgnoreCase(ext)) {
  99.             return MimeTypeConstants.MEDIA_TYPE_SOAP_1_1;
  100.         }
  101.         else if("json".equalsIgnoreCase(ext)) {
  102.             return MimeTypeConstants.MEDIA_TYPE_JSON;
  103.         }
  104.         return this.mapExtToMime.get(ext.trim().toLowerCase());
  105.     }
  106.     public String getExtension(String mime){
  107.         Object o = this.mapMimeToExt.get(mime.trim().toLowerCase());
  108.         if(o==null){
  109.             return null;
  110.         }
  111.         if(o instanceof org.apache.commons.lang.ObjectUtils.Null){
  112.             return null;
  113.         }
  114.         return (String) o;
  115.     }
  116.    
  117.     public boolean existsExtension(String ext){
  118.         return this.mapExtToMime.containsKey(ext.trim().toLowerCase());
  119.     }
  120.     public boolean existsMimeType(String mime, boolean checkExistsExtension){
  121.         if(this.mapMimeToExt.containsKey(mime.trim().toLowerCase())){
  122.             if(checkExistsExtension){
  123.                 String ext = this.getExtension(mime);
  124.                 return ext!=null;
  125.             }
  126.             else{
  127.                 return true;
  128.             }
  129.         }else{
  130.             return false;
  131.         }
  132.     }
  133.    
  134.    
  135.    
  136.     // static
  137.    
  138.     private static MimeTypes mimeTypes = null;
  139.     private static synchronized void init() throws UtilsException{
  140.         if(mimeTypes==null){
  141.             mimeTypes = new MimeTypes();
  142.         }
  143.     }
  144.     public static MimeTypes getInstance() throws UtilsException{
  145.         if(mimeTypes==null){
  146.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  147.             synchronized (MimeTypes.class) {
  148.                 init();
  149.             }
  150.         }
  151.         return mimeTypes;
  152.     }
  153.    
  154. }