001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.platform.mimetype.detectors; 022 023import java.io.File; 024import java.io.IOException; 025import java.util.Map; 026 027import org.apache.commons.io.FileUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.runtime.api.Framework; 031 032import net.sf.jmimemagic.MagicDetector; 033 034public class MsoXmlMimetypeSniffer implements MagicDetector { 035 036 private static final Log log = LogFactory.getLog(MsoXmlMimetypeSniffer.class); 037 038 @Override 039 public String getDisplayName() { 040 return "XML Microsoft 2003 MimeType Detector"; 041 } 042 043 @Override 044 public String[] getHandledExtensions() { 045 return new String[] { "xml" }; 046 } 047 048 @Override 049 public String[] getHandledTypes() { 050 return new String[] { "application/vnd.ms-excel", "application/msword", }; 051 } 052 053 @Override 054 public String getName() { 055 return "msoxml2003detector"; 056 } 057 058 @Override 059 public String getVersion() { 060 return "0.1"; 061 } 062 063 @Override 064 public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType, 065 Map params) { 066 String[] mimetypes = {}; 067 File file = null; 068 try { 069 file = Framework.createTempFile("magicdetector", ".xml"); 070 FileUtils.writeByteArrayToFile(file, data); 071 mimetypes = guessMsoXml(file); 072 } catch (IOException e) { 073 log.error(e); 074 } finally { 075 if (file != null) { 076 file.delete(); 077 } 078 } 079 return mimetypes; 080 } 081 082 @Override 083 public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, 084 Map params) { 085 return guessMsoXml(file); 086 } 087 088 public String[] guessMsoXml(File file) { 089 String[] mimetype = {}; 090 try { 091 String content = FileUtils.readFileToString(file); 092 if (content.contains("<?mso-application progid=\"Word.Document\"?>")) { 093 String[] type = { getHandledTypes()[1] }; 094 mimetype = type; 095 } else { 096 if (content.contains("<?mso-application progid=\"Excel.Sheet\"?>")) { 097 String[] type = { getHandledTypes()[0] }; 098 mimetype = type; 099 } 100 } 101 } catch (IOException e) { 102 log.error(e); 103 } 104 return mimetype; 105 } 106 107}