package org.ow2.chameleon.wisdom.api.http;

import org.apache.commons.io.IOUtils;
import org.ow2.chameleon.wisdom.api.utils.KnownMimeTypes;

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

/**
 * Common HTTP MIME types
 */
public class MimeTypes {

    /**
     * Content-Type of text.
     */
    public final static String TEXT = "text/plain";
    /**
     * Content-Type of html.
     */
    public final static String HTML = "text/html";
    /**
     * Content-Type of json.
     */
    public final static String JSON = "application/json";
    /**
     * Content-Type of xml.
     */
    public final static String XML = "application/xml";
    /**
     * Content-Type of css.
     */
    public final static String CSS = "text/css";
    /**
     * Content-Type of javascript.
     */
    public final static String JAVASCRIPT = "text/javascript";
    /**
     * Content-Type of form-urlencoded.
     */
    public final static String FORM = "application/x-www-form-urlencoded";
    /**
     * Content-Type of server sent events.
     */
    public final static String EVENT_STREAM = "text/event-stream";
    /**
     * Content-Type of binary data.
     */
    public final static String BINARY = "application/octet-stream";

    /**
     * Multipart.
     */
    public final static String MULTIPART = "multipart/form-data";

    public static String getMimeTypeForFile(File file) {
        if (file.getName().indexOf('.') == -1) {
            return BINARY;
        } else {
            String ext = file.getName().substring(file.getName().lastIndexOf('.') + 1);
            String mime = KnownMimeTypes.getMimeTypeByExtension(ext);
            if (mime == null) {
                return BINARY;
            } else {
                return mime;
            }
        }
    }
}

@@ -211,16 +211,37 @@
 Stream;%0A
+import java.net.URL;%0A
 import j
@@ -1473,26 +1473,54 @@
     
-if (file.getName()
+String name = file.getName();%0A        if (name
 .ind
@@ -1611,47 +1611,794 @@
 t = 
-file.getName().substring(file.getName()
+name.substring(name.lastIndexOf('.') + 1);%0A            String mime = KnownMimeTypes.getMimeTypeByExtension(ext);%0A            if (mime == null) %7B%0A                return BINARY;%0A            %7D else %7B%0A                return mime;%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Makes an educated guess of the mime type of the resource pointed by this url.%0A     * It tries to extract an 'extension' part and confronts this extension to the list of known extensions.%0A     * @param url the url%0A     * @return the mime type, BINARY if not found.%0A     */%0A    public static String getMimeTypeForFile(URL url) %7B%0A        String external = url.toExternalForm();%0A        if (external.indexOf('.') == -1) %7B%0A            return BINARY;%0A        %7D else %7B%0A            String ext = external.substring(external
 .las

Let deduce the mime type from url objects