/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.uis.internal.io.util;

import org.wso2.carbon.uis.api.exception.UISRuntimeException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * This class lazily loads the 'mime-map.properties' file and maps file extension to mime type using the file.
 *
 * @since 0.8.0
 */
public class MimeMapper {

    private static final String MIME_PROPERTY_FILE = "mime-map.properties";
    private static volatile Properties mimeMap = null;

    private MimeMapper() {
    }

    private static Properties loadMimeMap() throws UISRuntimeException {
        Properties mimeMap = new Properties();
        try (InputStream inputStream = MimeMapper.class.getClassLoader().getResourceAsStream(MIME_PROPERTY_FILE)) {
            if (inputStream == null) {
                throw new UISRuntimeException("Cannot find MIME types property file '" + MIME_PROPERTY_FILE + "'");
            }
            mimeMap.load(inputStream);
        } catch (IllegalArgumentException e) {
            throw new UISRuntimeException("MIME types property file is '" + MIME_PROPERTY_FILE + "' is invalid.", e);
        } catch (IOException e) {
            throw new UISRuntimeException("Cannot read MIME types property file '" + MIME_PROPERTY_FILE + "'.", e);
        }
        return mimeMap;
    }

    /**
     * Returns the MIME type for the given file extension.
     *
     * @param extension file extension
     * @return MIME type for the given file extension
     * @throws UISRuntimeException if cannot find or read the MIME types property file, or it is invalid
     */
    public static Optional<String> getMimeType(String extension) throws UISRuntimeException {
        if (mimeMap == null) {
            /* Here, class object 'MimeMapper.class' is used as the synchronization lock because 'getMimeType()' is
            the only is public method. */
            synchronized (MimeMapper.class) {
                if (mimeMap == null) {
                    mimeMap = loadMimeMap();
                }
            }
        }
        return Optional.ofNullable(mimeMap.getProperty(extension));
    }
}

@@ -775,16 +775,48 @@
 ception;
+%0Aimport org.yaml.snakeyaml.Yaml;
 %0A%0Aimport
@@ -865,16 +865,38 @@
 Stream;%0A
+import java.util.Map;%0A
 import j
@@ -918,398 +918,334 @@
 al;%0A
-import java.util.Properties;%0A%0A/**%0A * This class lazily loads the 'mime-map.properties' file and maps file extension to mime type using the file.%0A *%0A * @since 0.8.0%0A */%0Apublic class MimeMapper %7B%0A%0A    private static final String MIME_PROPERTY_FILE = %22mime-map.properties%22;%0A    private static volatile Properties mimeMap = null;%0A%0A    private MimeMapper() %7B%0A    %7D%0A%0A    private static Properties
+%0A/**%0A * Maps MIME types to file extensions.%0A *%0A * @since 0.8.0%0A */%0Apublic class MimeMapper %7B%0A%0A    private static final String FILE_NAME_MIME_TYPES = %22mime-types.yaml%22;%0A    private static Map%3CString, String%3E mimeTypes;%0A%0A    private MimeMapper() %7B%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private static Map%3CString, String%3E
  loa
@@ -1249,19 +1249,21 @@
 loadMime
-Map
+Types
 () throw
@@ -1290,55 +1290,8 @@
 n %7B%0A
-        Properties mimeMap = new Properties();%0A
     
@@ -1379,34 +1379,36 @@
 sStream(
-MIME_PROPERTY_FILE
+FILE_NAME_MIME_TYPES
 )) %7B%0A   
@@ -1481,32 +1481,57 @@
 untimeException(
+%0A                        
 %22Cannot find MIM
@@ -1534,33 +1534,24 @@
  MIME types 
-property 
 file '%22 + MI
@@ -1548,39 +1548,56 @@
 le '%22 + 
-MIME_PROPERTY_FILE + %22'
+FILE_NAME_MIME_TYPES + %22' in class path.
 %22);%0A    
@@ -1622,20 +1622,32 @@
     
-mimeMap
+return new Yaml()
 .load
+As
 (inp
@@ -1654,16 +1654,27 @@
 utStream
+, Map.class
 );%0A     
@@ -1690,22 +1690,9 @@
 h (I
-llegalArgument
+O
 Exce
@@ -1745,16 +1745,28 @@
 eption(%22
+Cannot read 
 MIME typ
@@ -1772,64 +1772,43 @@
 pes 
-property 
 file 
-is 
 '%22 + 
-MIME_PROPERTY_FILE + %22' is invalid
+FILE_NAME_MIME_TYPES + %22'
 .%22, 
@@ -1820,34 +1820,32 @@
        %7D catch (
-IO
 Exception e) %7B%0A 
@@ -1882,36 +1882,24 @@
 eException(%22
-Cannot read 
 MIME types p
@@ -1901,91 +1901,74 @@
 pes 
-property 
 file 
+is 
 '%22 + 
-MIME_PROPERTY_FILE + %22'.%22, e);%0A        %7D%0A        return mimeMap;
+FILE_NAME_MIME_TYPES + %22' is invalid.%22, e);%0A        %7D
 %0A   
@@ -2215,17 +2215,8 @@
 pes 
-property 
 file
@@ -2344,35 +2344,37 @@
         if (mime
-Map
+Types
  == null) %7B%0A    
@@ -2373,166 +2373,8 @@
 ) %7B%0A
-            /* Here, class object 'MimeMapper.class' is used as the synchronization lock because 'getMimeType()' is%0A            the only is public method. */%0A
     
@@ -2439,19 +2439,21 @@
 if (mime
-Map
+Types
  == null
@@ -2480,19 +2480,21 @@
     mime
-Map
+Types
  = loadM
@@ -2496,19 +2496,21 @@
 loadMime
-Map
+Types
 ();%0A    
@@ -2590,23 +2590,17 @@
 mime
-Map.getProperty
+Types.get
 (ext

remove usage of java.util.Properties in MimeMapper class