diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/util/MimeTypeUtility.java b/src/main/java/org/gcube/portlets/user/workspace/server/util/MimeTypeUtility.java index ec5a58e..ae295d4 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/util/MimeTypeUtility.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/util/MimeTypeUtility.java @@ -45,12 +45,14 @@ public class MimeTypeUtility { "application/x-compressed", "application/x-zip-compressed", "application/x-gzip", "application/x-winzip", "application/x-zip", "application/zip", "multipart/x-zip" }; - protected static final Map> mimetype_extension_map = new LinkedHashMap>(); + protected static final Map> mimetype_extension_map = new LinkedHashMap>(); protected static final Map extension_mimetype_map = new LinkedHashMap(); static { - InputStream extensionToMimetype = MimeTypeUtility.class.getResourceAsStream("/WsExtensionToMimeTypeMap.properties"); - InputStream mimetypeToExtension = MimeTypeUtility.class.getResourceAsStream("/WsMimeTypeToExtensionMap.properties"); + InputStream extensionToMimetype = MimeTypeUtility.class + .getResourceAsStream("/WsExtensionToMimeTypeMap.properties"); + InputStream mimetypeToExtension = MimeTypeUtility.class + .getResourceAsStream("/WsMimeTypeToExtensionMap.properties"); try { loadExtensions(extensionToMimetype); loadMimeTypes(mimetypeToExtension); @@ -100,12 +102,12 @@ public class MimeTypeUtility { String mimeType = split[0]; String extension = split[1]; List toExtensions = mimetype_extension_map.get(mimeType); - if(toExtensions==null) { + if (toExtensions == null) { toExtensions = new ArrayList(); } toExtensions.add(extension); mimetype_extension_map.put(mimeType, toExtensions); - //mimetype_extension_map.put(mimeType, extension); + // mimetype_extension_map.put(mimeType, extension); } line = br.readLine(); } @@ -133,52 +135,60 @@ public class MimeTypeUtility { public static String getNameWithExtension(String name, String mimeType) throws IOException { logger.debug("Deriving the file extension for file name: " + name + ", with mimeType: " + mimeType); - + if (mimeType == null || mimeType.isEmpty()) { - logger.debug("Input mime type is null or empty returning passed name: "+name); + logger.debug("Input mime type is null or empty returning passed name: " + name); return name; } - + String declaredExtension = FilenameUtils.getExtension(name); - logger.debug("The name "+name+" contains the extension: "+declaredExtension); - + logger.debug("The name " + name + " contains the extension: " + declaredExtension); + + if (declaredExtension.equals("exe")) + return name; + List extensions = MimeTypeUtility.getExtension(mimeType); - logger.trace("Extension/s available for input mimetype: "+mimeType+" into map is/are: " + extensions); - + logger.trace("Extension/s available for input mimetype: " + mimeType + " into map is/are: " + extensions); + String toMimeTypeExtension = ""; - if(extensions!=null) { - toMimeTypeExtension = extensions.get(extensions.size()-1); //I'm reading the last extension in the map (myme type - list of extensions) + if (extensions != null) { + toMimeTypeExtension = extensions.get(extensions.size() - 1); // I'm reading the last extension in the map + // (myme type - list of extensions) logger.debug("Using the last extension read into list of available extensions: " + toMimeTypeExtension); } - - //MANAGING ALREADY DECLARED EXTENSION IN THE FILE NAME - if(!declaredExtension.isEmpty() && !toMimeTypeExtension.isEmpty()) { + + // MANAGING ALREADY DECLARED EXTENSION IN THE FILE NAME + if (!declaredExtension.isEmpty() && !toMimeTypeExtension.isEmpty()) { String dEextL = declaredExtension.toLowerCase(); String mtExtL = toMimeTypeExtension.toLowerCase(); - - //The extension writes in the file name is matching the mime type extension declared in the map - if(dEextL.equals(mtExtL)) { - logger.trace("The Extension declared in the name "+name+" is matching derived mime type extension so returning the input name: "+name); + + // The extension writes in the file name is matching the mime type extension + // declared in the map + if (dEextL.equals(mtExtL)) { + logger.trace("The Extension declared in the name " + name + + " is matching derived mime type extension so returning the input name: " + name); return name; } - + // if(!dEextL.trim().contains(" ")) { // logger.trace("The Extension declared in the name "+name+" seems a valid suffix (without other spaces) so returning the input name: "+name); // return name; // } - - logger.debug("No logic seems to match the extension declared in the name "+declaredExtension+" as a valid extension so I'm adding the extension derived from mime type map: "+toMimeTypeExtension); + + logger.debug("No logic seems to match the extension declared in the name " + declaredExtension + + " as a valid extension so I'm adding the extension derived from mime type map: " + + toMimeTypeExtension); } - - //CHECKING THE FOLLOWING IN ORDER TO AVOID THE DOT AS LAST CHAR OF FILENAME - if(toMimeTypeExtension.isEmpty()) { + + // CHECKING THE FOLLOWING IN ORDER TO AVOID THE DOT AS LAST CHAR OF FILENAME + if (toMimeTypeExtension.isEmpty()) { return name; } - + String fullname = String.format("%s%s%s", name, DOT_STRING, toMimeTypeExtension); - logger.trace("returning full name:" +fullname); + logger.trace("returning full name:" + fullname); return fullname; - + } /** @@ -250,7 +260,7 @@ public class MimeTypeUtility { return mediaType.getBaseType().toString(); } - + /** * Gets the mime type to extension map. * @@ -259,8 +269,7 @@ public class MimeTypeUtility { public static Map> getMimeTypeToExtensionMap() { return mimetype_extension_map; } - - + /** * Gets the extension to mime type map. *