mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Fix for ALF-2118:
- Added new method to MimetypeService to retrieve a valid mimetype given any extension, handling case and missing values - Updated JUnit tests for MimetypeService methods - Fixed callers of MimetypeService to use new method to protect against varying case of file extensions (ALF-2118 root cause) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19400 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -26,16 +26,16 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.extensions.config.Config;
|
|
||||||
import org.springframework.extensions.config.ConfigElement;
|
|
||||||
import org.springframework.extensions.config.ConfigLookupContext;
|
|
||||||
import org.springframework.extensions.config.ConfigService;
|
|
||||||
import org.alfresco.error.AlfrescoRuntimeException;
|
import org.alfresco.error.AlfrescoRuntimeException;
|
||||||
import org.alfresco.repo.content.encoding.ContentCharsetFinder;
|
import org.alfresco.repo.content.encoding.ContentCharsetFinder;
|
||||||
import org.alfresco.service.cmr.repository.MimetypeService;
|
import org.alfresco.service.cmr.repository.MimetypeService;
|
||||||
import org.alfresco.util.PropertyCheck;
|
import org.alfresco.util.PropertyCheck;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.extensions.config.Config;
|
||||||
|
import org.springframework.extensions.config.ConfigElement;
|
||||||
|
import org.springframework.extensions.config.ConfigLookupContext;
|
||||||
|
import org.springframework.extensions.config.ConfigService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a bidirectional mapping between well-known mimetypes and
|
* Provides a bidirectional mapping between well-known mimetypes and
|
||||||
@@ -295,15 +295,28 @@ public class MimetypeMap implements MimetypeService
|
|||||||
public String getExtension(String mimetype)
|
public String getExtension(String mimetype)
|
||||||
{
|
{
|
||||||
String extension = extensionsByMimetype.get(mimetype);
|
String extension = extensionsByMimetype.get(mimetype);
|
||||||
if (extension == null)
|
return (extension == null ? EXTENSION_BINARY : extension);
|
||||||
{
|
|
||||||
return EXTENSION_BINARY;
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
/**
|
||||||
|
* Get the mimetype for the specified extension
|
||||||
|
*
|
||||||
|
* @param extension a valid file extension
|
||||||
|
* @return Returns a valid mimetype if found, or {@link #MIMETYPE_BINARY binary} as default.
|
||||||
|
*/
|
||||||
|
public String getMimetype(String extension)
|
||||||
{
|
{
|
||||||
return extension;
|
String mimetype = MIMETYPE_BINARY;
|
||||||
|
if (extension != null)
|
||||||
|
{
|
||||||
|
extension = extension.toLowerCase();
|
||||||
|
if (mimetypesByExtension.containsKey(extension))
|
||||||
|
{
|
||||||
|
mimetype = mimetypesByExtension.get(extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return mimetype;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, String> getDisplaysByExtension()
|
public Map<String, String> getDisplaysByExtension()
|
||||||
{
|
{
|
||||||
@@ -340,13 +353,12 @@ public class MimetypeMap implements MimetypeService
|
|||||||
*/
|
*/
|
||||||
public String guessMimetype(String filename)
|
public String guessMimetype(String filename)
|
||||||
{
|
{
|
||||||
filename = filename.toLowerCase();
|
|
||||||
String mimetype = MIMETYPE_BINARY;
|
String mimetype = MIMETYPE_BINARY;
|
||||||
// extract the extension
|
// extract the extension
|
||||||
int index = filename.lastIndexOf('.');
|
int index = filename.lastIndexOf('.');
|
||||||
if (index > -1 && (index < filename.length() - 1))
|
if (index > -1 && (index < filename.length() - 1))
|
||||||
{
|
{
|
||||||
String extension = filename.substring(index + 1);
|
String extension = filename.substring(index + 1).toLowerCase();
|
||||||
if (mimetypesByExtension.containsKey(extension))
|
if (mimetypesByExtension.containsKey(extension))
|
||||||
{
|
{
|
||||||
mimetype = mimetypesByExtension.get(extension);
|
mimetype = mimetypesByExtension.get(extension);
|
||||||
|
@@ -80,4 +80,23 @@ public class MimetypeMapTest extends TestCase
|
|||||||
assertNotNull("No charset finder", mimetypeService.getContentCharsetFinder());
|
assertNotNull("No charset finder", mimetypeService.getContentCharsetFinder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMimetypeFromExtension() throws Exception
|
||||||
|
{
|
||||||
|
// test known mimetype
|
||||||
|
assertEquals("application/msword", mimetypeService.getMimetype("doc"));
|
||||||
|
// test case insensitivity
|
||||||
|
assertEquals("application/msword", mimetypeService.getMimetype("DOC"));
|
||||||
|
|
||||||
|
// test fallback for unknown and missing
|
||||||
|
assertEquals(MimetypeMap.MIMETYPE_BINARY, mimetypeService.getMimetype(null));
|
||||||
|
assertEquals(MimetypeMap.MIMETYPE_BINARY, mimetypeService.getMimetype("unknownext"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGuessMimetypeForFilename() throws Exception
|
||||||
|
{
|
||||||
|
assertEquals("application/msword", mimetypeService.guessMimetype("something.doc"));
|
||||||
|
assertEquals("application/msword", mimetypeService.guessMimetype("SOMETHING.DOC"));
|
||||||
|
assertEquals(MimetypeMap.MIMETYPE_BINARY, mimetypeService.guessMimetype("noextension"));
|
||||||
|
assertEquals(MimetypeMap.MIMETYPE_BINARY, mimetypeService.guessMimetype("file.unknownext"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -31,7 +31,6 @@ import org.alfresco.service.PublicService;
|
|||||||
* This service interface provides support for Mimetypes.
|
* This service interface provides support for Mimetypes.
|
||||||
*
|
*
|
||||||
* @author Derek Hulley
|
* @author Derek Hulley
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@PublicService
|
@PublicService
|
||||||
public interface MimetypeService
|
public interface MimetypeService
|
||||||
@@ -41,11 +40,19 @@ public interface MimetypeService
|
|||||||
*
|
*
|
||||||
* @param mimetype a valid mimetype
|
* @param mimetype a valid mimetype
|
||||||
* @return Returns the default extension for the mimetype
|
* @return Returns the default extension for the mimetype
|
||||||
* @throws AlfrescoRuntimeException if the mimetype doesn't exist
|
|
||||||
*/
|
*/
|
||||||
@NotAuditable
|
@NotAuditable
|
||||||
public String getExtension(String mimetype);
|
public String getExtension(String mimetype);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mimetype for the specified extension
|
||||||
|
*
|
||||||
|
* @param extension a valid file extension
|
||||||
|
* @return Returns a valid mimetype if found, or null if does not exist
|
||||||
|
*/
|
||||||
|
@NotAuditable
|
||||||
|
public String getMimetype(String extension);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all human readable mimetype descriptions indexed by mimetype extension
|
* Get all human readable mimetype descriptions indexed by mimetype extension
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user