/** * Get the Content type for this file * * @return Http Response content type */ public String getContentType() { String contentType; if (outputFormat != null) { contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(fullName + "." + outputFormat); } else { contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(fullName); } if (contentType != null && contentType.startsWith("text")) { contentType += ";charset=" + CHARSET; } return (contentType != null) ? contentType : "application/octet-stream"; }
/** {@inheritDoc} */ @Override public Optional<VFile> getActivityLogFile(final Long actityExecutionId) { Assertion.checkNotNull(actityExecutionId); // --- final Optional<OActivityLog> activityLog = activityLogDAO.getActivityLogByAceId(actityExecutionId); if (activityLog.isPresent()) { final byte[] stringByteArray = activityLog.get().getLog().getBytes(StandardCharsets.UTF_8); final InputStreamBuilder inputStreamBuilder = () -> new ByteArrayInputStream(stringByteArray); final String fileName = TECHNICAL_LOG_PREFIX + actityExecutionId + TECHNICAL_LOG_EXTENSION; final VFile file = fileManager.createFile(fileName, FileTypeMap.getDefaultFileTypeMap().getContentType(fileName), new Date(), stringByteArray.length, inputStreamBuilder); return Optional.<VFile> of(file); } return Optional.<VFile> empty(); }
@Override public ContentType getContentType(final String contentName) { ContentType ret = null; final int idx = contentName.lastIndexOf('.'); if (idx >= 0) { final String ext = contentName.substring(idx); // Shall not throw IndexOutOfBoundsException. final FileTypeMap map = FileTypeMap.getDefaultFileTypeMap(); final String contentType = map.getContentType(contentName); // Test the default map. if (!"application/octet-stream".equals(contentType)) { ret = new ContentType(new String[] { ext }, new String[] { contentType }, null, null); } } return ret; }
@Override public String getContentType(File filename) { if (!filename.exists()) { return null; } try { final MimeType type = getMimeType(filename.toURI().toURL()); if (type != null) { return type.toString(); } } catch (Exception e) { // } final FileTypeMap lparent = this.parent.get(); if (lparent != null) { return lparent.getContentType(filename); } return MimeName.MIME_OCTET_STREAM.getMimeConstant(); }
/** * Return the delegate FileTypeMap, compiled from the mappings in the mapping file * and the entries in the {@code mappings} property. * @see #setMappingLocation * @see #setMappings * @see #createFileTypeMap */ protected final FileTypeMap getFileTypeMap() { if (this.fileTypeMap == null) { try { this.fileTypeMap = createFileTypeMap(this.mappingLocation, this.mappings); } catch (IOException ex) { throw new IllegalStateException( "Could not load specified MIME type mapping file: " + this.mappingLocation, ex); } } return this.fileTypeMap; }
private static void initFileTypeMap() { fileTypeMap = new MimetypesFileTypeMap(); // Additional MIME types fileTypeMap.addMimeTypes("application/atom+xml atom"); fileTypeMap.addMimeTypes("application/msword doc dot"); fileTypeMap.addMimeTypes("application/mspowerpoint ppt pot"); fileTypeMap.addMimeTypes("application/msexcel xls"); fileTypeMap.addMimeTypes("application/pdf pdf"); fileTypeMap.addMimeTypes("application/rdf+xml rdf rss"); fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat docx docm dotx dotm"); fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat xlsx xlsm"); fileTypeMap.addMimeTypes("application/x-vnd.openxmlformat pptx pptm potx"); fileTypeMap.addMimeTypes("application/x-javascript js"); fileTypeMap.addMimeTypes("application/x-rar-compressed rar"); fileTypeMap.addMimeTypes("application/x-textedit bat cmd"); fileTypeMap.addMimeTypes("application/zip zip"); fileTypeMap.addMimeTypes("audio/mpeg mp3"); fileTypeMap.addMimeTypes("image/bmp bmp"); fileTypeMap.addMimeTypes("image/gif gif"); fileTypeMap.addMimeTypes("image/jpeg jpg jpeg jpe"); fileTypeMap.addMimeTypes("image/png png"); fileTypeMap.addMimeTypes("text/css css"); fileTypeMap.addMimeTypes("text/csv csv"); fileTypeMap.addMimeTypes("text/html htm html"); fileTypeMap.addMimeTypes("text/xml xml"); fileTypeMap.addMimeTypes("video/quicktime qt mov moov"); fileTypeMap.addMimeTypes("video/mpeg mpeg mpg mpe mpv vbs mpegv"); fileTypeMap.addMimeTypes("video/msvideo avi"); fileTypeMap.addMimeTypes("video/mp4 mp4"); fileTypeMap.addMimeTypes("video/ogg ogg"); FileTypeMap.setDefaultFileTypeMap(fileTypeMap); }
/** * Introduced to dispel claims in a thread on Stack Overflow: * <a href="http://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a> */ @Test public void getMimeTypeWithCustomConfiguredType() { FileTypeMap defaultFileTypeMap = FileTypeMap.getDefaultFileTypeMap(); assertThat(defaultFileTypeMap, instanceOf(MimetypesFileTypeMap.class)); MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) defaultFileTypeMap; mimetypesFileTypeMap.addMimeTypes("text/enigma enigma"); assertEquals("text/enigma", sc.getMimeType("filename.enigma")); }
public String getContentType() { if (typeMap == null) { return FileTypeMap.getDefaultFileTypeMap().getContentType(fileName); } else { return typeMap.getContentType(fileName); } }
public FileTypeMap getFileTypeMap() { if (fileTypeMap == null) { fileTypeMap = loadFileTypeMapFromContextSupportModule(); } return fileTypeMap; }
/** * Create a new SmartMimeMessage. * @param session the JavaMail Session to create the message for * @param defaultEncoding the default encoding, or {@code null} if none * @param defaultFileTypeMap the default FileTypeMap, or {@code null} if none */ public SmartMimeMessage(final Session session, final String defaultEncoding, final FileTypeMap defaultFileTypeMap) { super(session); _defaultEncoding = defaultEncoding; _defaultFileTypeMap = defaultFileTypeMap; }
public static String getContentType(String filename){ if(filetypeMap == null) { filetypeMap = FileTypeMap.getDefaultFileTypeMap(); } return filetypeMap.getContentType(filename); }
private void addAttachments( Enumeration<fileAttachment> _attach, Multipart _parent, boolean _isInline ) throws MessagingException{ while ( _attach.hasMoreElements() ){ fileAttachment nextFile = _attach.nextElement(); FileDataSource fds = new FileDataSource( nextFile.getFilepath() ); String mimeType = nextFile.getMimetype(); if (mimeType == null){ // if mime type not supplied then auto detect mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(nextFile.getFilepath()); }else{ // since mime type is not null then it the mime type has been set manually therefore // we need to ensure that any call to the underlying FileDataSource.getFileTypeMap() // returns a FileTypeMap that will map to this type fds.setFileTypeMap(new CustomFileTypeMap(mimeType)); } String filename = cleanName(fds.getName()); try { // encode the filename to ensure that it contains US-ASCII characters only filename = MimeUtility.encodeText( filename, "utf-8", "b" ); } catch (UnsupportedEncodingException e5) { // shouldn't occur } MimeBodyPart mimeAttach = new MimeBodyPart(); mimeAttach.setDataHandler( new DataHandler(fds) ); mimeAttach.setFileName( filename ); ContentType ct = new ContentType(mimeType); ct.setParameter("name", filename ); mimeAttach.setHeader("Content-Type", ct.toString() ); if ( _isInline ){ mimeAttach.setDisposition( "inline" ); mimeAttach.addHeader( "Content-id", "<" + nextFile.getContentid() + ">" ); } _parent.addBodyPart(mimeAttach); } }
@Override public MimeMessage createMimeMessage() { // We have to use reflection as SmartMimeMessage is not package-private if (ClassUtils.isPresent(SMART_MIME_MESSAGE_CLASS_NAME, ClassUtils.getDefaultClassLoader())) { Class<?> smartMimeMessage = ClassUtils.resolveClassName(SMART_MIME_MESSAGE_CLASS_NAME, ClassUtils.getDefaultClassLoader()); Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(smartMimeMessage, Session.class, String.class, FileTypeMap.class); if (constructor != null) { Object mimeMessage = BeanUtils.instantiateClass(constructor, getSession(), this.defaultEncoding, this.defaultFileTypeMap); return (MimeMessage) mimeMessage; } } return new MimeMessage(getSession()); }
@Test public void createMimeMessage_withCustomFileTypeMap_fileTypeMapIsAvailableInMailSender() throws Exception { // Arrange SimpleEmailServiceJavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(null); mailSender.setDefaultFileTypeMap(FileTypeMap.getDefaultFileTypeMap()); // Act MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); // Assert assertNotNull("ISO-8859-1", mimeMessageHelper.getFileTypeMap()); }
@Override public String getMimeType(String filePath) { String extension = StringUtils.getFilenameExtension(filePath); if (this.mimeTypes.containsKey(extension)) { return this.mimeTypes.get(extension).toString(); } else { return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath); } }
/** Be sure that the default FileTypeMap is a * {@link ContentFileTypeMap}. * * @return the default content type manager. */ public static ContentFileTypeMap ensureContentTypeManager() { FileTypeMap defaultMap = FileTypeMap.getDefaultFileTypeMap(); if (!(defaultMap instanceof ContentFileTypeMap)) { defaultMap = new ContentFileTypeMap(defaultMap); FileTypeMap.setDefaultFileTypeMap(defaultMap); } return (ContentFileTypeMap) defaultMap; }
private void updateMediaEntryAppLinks(final Entry entry, final String fileName, final boolean singleEntry) { // TODO: figure out why PNG is missing from Java MIME types final FileTypeMap map = FileTypeMap.getDefaultFileTypeMap(); if (map instanceof MimetypesFileTypeMap) { try { ((MimetypesFileTypeMap) map).addMimeTypes("image/png png PNG"); } catch (final Exception ignored) { } } entry.setId(getEntryMediaViewURI(fileName)); entry.setTitle(fileName); entry.setUpdated(new Date()); final List<Link> otherlinks = new ArrayList<Link>(); entry.setOtherLinks(otherlinks); final Link editlink = new Link(); editlink.setRel("edit"); editlink.setHref(getEntryEditURI(fileName, relativeURIs, singleEntry)); otherlinks.add(editlink); final Link editMedialink = new Link(); editMedialink.setRel("edit-media"); editMedialink.setHref(getEntryMediaEditURI(fileName, relativeURIs, singleEntry)); otherlinks.add(editMedialink); final Content content = entry.getContents().get(0); content.setSrc(getEntryMediaViewURI(fileName)); final List<Content> contents = new ArrayList<Content>(); contents.add(content); entry.setContents(contents); }
/** * Return the default FileTypeMap of this message, or {@code null} if none. */ public final FileTypeMap getDefaultFileTypeMap() { return this.defaultFileTypeMap; }
/** * Return the {@code FileTypeMap} used by this MimeMessageHelper. */ public FileTypeMap getFileTypeMap() { return this.fileTypeMap; }
/** * classPath의 정보를 파싱하여 데이터셋으로 반환 * * @작성자 : KYJ * @작성일 : 2015. 10. 26. * @param filePathName * @return * @throws Exception */ public static ClassPath parsingClassPath(String filePathName) throws Exception { DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = newInstance.newDocumentBuilder(); { FileTypeMap defaultFileTypeMap = MimetypesFileTypeMap.getDefaultFileTypeMap(); String contentType = defaultFileTypeMap.getContentType(filePathName); LOGGER.debug(String.format("File path Name : %s Content type : %s ", filePathName, contentType)); } Reader reader = new InputStreamReader(new FileInputStream(new File(filePathName)), ENCODING); InputSource is = new InputSource(reader); Document parse = builder.parse(is); ClassPath classPath = new ClassPath(); classPath.setFilePathName(filePathName); classPath.setApplyedEncoding(ENCODING); NodeList elementsByTagName2 = parse.getElementsByTagName("classpath"); int length = elementsByTagName2.getLength(); for (int i = 0; i < length; i++) { Node classPathNode = elementsByTagName2.item(i); NodeList childNodes = classPathNode.getChildNodes(); int classEntrySize = childNodes.getLength(); for (int e = 0; e < classEntrySize; e++) { Node classEntryNode = childNodes.item(e); NamedNodeMap attributes = classEntryNode.getAttributes(); if (attributes == null) continue; // 코드에서 필요로하는 부분만 XML을 파싱해서 데이터셋에 담는다. String kind = emptyThan(attributes.getNamedItem("kind")); String output = emptyThan(attributes.getNamedItem("output")); String path = emptyThan(attributes.getNamedItem("path")); ClassPathEntry classPathEntry = new ClassPathEntry(kind, output, path); classPath.addEntry(classPathEntry); } } return classPath; }
public static String getMimeType(String filePath) { return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath); }
private Resource(String resourceName) { this.content = Resources.asByteSource(Resources.getResource(VizTool.class, resourceName)); this.contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(resourceName); }
public void setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap) { mailSender.setDefaultFileTypeMap(defaultFileTypeMap); }
public FileTypeMap getDefaultFileTypeMap() { return mailSender.getDefaultFileTypeMap(); }
public void setFileTypeMap(FileTypeMap map) { typeMap = map; }
@Override public void init () throws ServletException { super.init (); this.fileTypeMap = FileTypeMap.getDefaultFileTypeMap (); }
private String getContentType(File file) { return FileTypeMap.getDefaultFileTypeMap().getContentType(file); }