/** * Returns a mime type for the provided file. * <p> * This method tries various ways to extract mime type information from the files name or its contents. * <p> * If no mime type can be derived from either the file name or its contents, a <code>UnknownFileTypeException</code> * is thrown. * * @param url * the file * @return the corresponding mime type * @throws UnknownFileTypeException * if the mime type cannot be derived from the file */ public static MimeType fromURL(URL url) throws UnknownFileTypeException { if (url == null) throw new IllegalArgumentException("Argument 'url' is null"); return fromString(url.getPath()); }
/** * Returns a mime type for the provided file. * <p> * This method tries various ways to extract mime type information from the files name or its contents. * <p> * If no mime type can be derived from either the file name or its contents, a <code>UnknownFileTypeException</code> * is thrown. * * @param uri * the file * @return the corresponding mime type * @throws UnknownFileTypeException * if the mime type cannot be derived from the file */ public static MimeType fromURI(URI uri) throws UnknownFileTypeException { if (uri == null) throw new IllegalArgumentException("Argument 'uri' is null"); return fromString(uri.getPath()); }
/** * {@inheritDoc} * * @see org.osgi.service.http.HttpContext#getMimeType(java.lang.String) */ @Override public String getMimeType(String name) { try { return MimeTypes.fromString(name).toString(); } catch (UnknownFileTypeException e) { return null; } }
/** * Convenience method to get a mime type as String from a filename extension * * @param name * the filename * @return the corresponding mime type or DEFAULT_TYPE if no match */ public static String getMimeType(String name) { try { return MimeTypes.fromString(name).toString(); } catch (UnknownFileTypeException e) { return DEFAULT_TYPE; } }
@Override public Opt<MimeType> apply(String name) { try { return Opt.some(fromString(name)); } catch (Exception e) { return Opt.none(); } } };
private Option<String> getMimeType(final String filename) { Option<String> mimeType; try { mimeType = Option.some(MimeTypes.fromString(filename).toString()); } catch (Exception e) { logger.warn("Unable to detect the mime type of file {}", filename); mimeType = Option.<String> none(); } return mimeType; }
File file = getFile(mediaPackageID, mediaPackageElementID); try { contentType = MimeTypes.fromString(file.getPath()).toString(); } catch (UnknownFileTypeException e) { contentType = "application/octet-stream";