Example usage for javax.activation FileTypeMap getDefaultFileTypeMap

List of usage examples for javax.activation FileTypeMap getDefaultFileTypeMap

Introduction

In this page you can find the example usage for javax.activation FileTypeMap getDefaultFileTypeMap.

Prototype

public static synchronized FileTypeMap getDefaultFileTypeMap() 

Source Link

Document

Return the default FileTypeMap for the system.

Usage

From source file:MSBodyPart.java

/**
 * Process the "begin" line to extract the filename,
 * and from it determine the Content-Type.
 *//*from   www .j  a  v  a2 s.c  o  m*/
private void processBegin() {
    InputStream in = getContentStream();
    try {
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        String begin = r.readLine();
        // format is "begin 666 filename.txt"
        if (begin != null && begin.regionMatches(true, 0, "begin ", 0, 6)) {
            int i = begin.indexOf(' ', 6);
            if (i > 0) {
                filename = begin.substring(i + 1);
                FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
                type = map.getContentType(filename);
                if (type == null)
                    type = "application/octet-stream";
            }
        }
    } catch (IOException ex) {
        // ignore
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            // ignore it
        }
        if (filename == UNKNOWN)
            filename = null;
        if (type == UNKNOWN || type == null)
            type = "text/plain";
    }
}

From source file:eu.planets_project.services.datatypes.ImmutableContent.java

/**
 * @param reference The content reference, as a file.
 *///from w w w  .  j a  va  2s .  c  o m
ImmutableContent(final File reference) {
    if (reference == null)
        throw new IllegalArgumentException("File parameter must not be null!");
    FileDataSource ds = new FileDataSource(reference);
    ds.setFileTypeMap(FileTypeMap.getDefaultFileTypeMap());
    DataHandler dh = new DataHandler(ds);
    this.length = reference.length();
    this.dataHandler = dh;
    log.info("Created Content from file: " + reference.getAbsolutePath() + "': " + this.length
            + " bytes in length.");
}

From source file:gov.nih.nci.firebird.web.common.Struts2UploadedFileInfo.java

/**
 * @return file type according to the system.
 * @see FileTypeMap./*from   w ww .  jav  a 2s  .  c  o  m*/
 */
private String getSystemFileType() {
    return FileTypeMap.getDefaultFileTypeMap().getContentType(dataFileName);
}

From source file:com.thoughtworks.go.spark.mocks.MockServletContext.java

@Override
public String getMimeType(String filePath) {
    String extension = StringUtils.getFilenameExtension(filePath);
    if (this.mimeTypes.containsKey(extension)) {
        return this.mimeTypes.get(extension).toString();
    } else {/*from   w w w .j a  v a 2  s.c  om*/
        return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
    }
}

From source file:com.sun.syndication.propono.atom.server.impl.FileBasedCollection.java

private void updateMediaEntryAppLinks(Entry entry, String fileName, boolean singleEntry) {

    // TODO: figure out why PNG is missing from Java MIME types
    FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
    if (map instanceof MimetypesFileTypeMap) {
        try {//from   ww w .  j  a v  a  2  s.  co  m
            ((MimetypesFileTypeMap) map).addMimeTypes("image/png png PNG");
        } catch (Exception ignored) {
        }
    }
    String contentType = map.getContentType(fileName);

    entry.setId(getEntryMediaViewURI(fileName));
    entry.setTitle(fileName);
    entry.setUpdated(new Date());

    List otherlinks = new ArrayList();
    entry.setOtherLinks(otherlinks);

    Link editlink = new Link();
    editlink.setRel("edit");
    editlink.setHref(getEntryEditURI(fileName, relativeURIs, singleEntry));
    otherlinks.add(editlink);

    Link editMedialink = new Link();
    editMedialink.setRel("edit-media");
    editMedialink.setHref(getEntryMediaEditURI(fileName, relativeURIs, singleEntry));
    otherlinks.add(editMedialink);

    Content content = (Content) entry.getContents().get(0);
    content.setSrc(getEntryMediaViewURI(fileName));
    List contents = new ArrayList();
    contents.add(content);
    entry.setContents(contents);
}

From source file:org.opencastproject.caption.impl.CaptionServiceImpl.java

/**
 * Converts the captions and returns them in a new catalog.
 * //  w  ww .  jav  a 2 s  .co m
 * @return the converted catalog
 */
protected Catalog convert(Job job, Catalog input, String inputFormat, String outputFormat, String language)
        throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
    try {

        // check parameters
        if (input == null)
            throw new IllegalArgumentException("Input catalog can't be null");
        if (StringUtils.isBlank(inputFormat))
            throw new IllegalArgumentException("Input format is null");
        if (StringUtils.isBlank(outputFormat))
            throw new IllegalArgumentException("Output format is null");

        // get input file
        File captionsFile;
        try {
            captionsFile = workspace.get(input.getURI());
        } catch (NotFoundException e) {
            throw new CaptionConverterException(
                    "Requested media package element " + input + " could not be found.");
        } catch (IOException e) {
            throw new CaptionConverterException(
                    "Requested media package element " + input + "could not be accessed.");
        }

        logger.debug("Atempting to convert from {} to {}...", inputFormat, outputFormat);

        List<Caption> collection = null;
        try {
            collection = importCaptions(captionsFile, inputFormat, language);
            logger.debug("Parsing to collection succeeded.");
        } catch (UnsupportedCaptionFormatException e) {
            throw new UnsupportedCaptionFormatException(inputFormat);
        } catch (CaptionConverterException e) {
            throw e;
        }

        URI exported;
        try {
            exported = exportCaptions(collection,
                    job.getId() + "." + FilenameUtils.getExtension(captionsFile.getAbsolutePath()),
                    outputFormat, language);
            logger.debug("Exporting captions succeeding.");
        } catch (UnsupportedCaptionFormatException e) {
            throw new UnsupportedCaptionFormatException(outputFormat);
        } catch (IOException e) {
            throw new CaptionConverterException("Could not export caption collection.", e);
        }

        // create catalog and set properties
        MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance()
                .newElementBuilder();
        Catalog catalog = (Catalog) elementBuilder.elementFromURI(exported, Catalog.TYPE,
                new MediaPackageElementFlavor("captions", outputFormat));
        String[] mimetype = FileTypeMap.getDefaultFileTypeMap().getContentType(exported.getPath()).split("/");
        catalog.setMimeType(mimeType(mimetype[0], mimetype[1]));
        catalog.addTag("lang:" + language);

        return catalog;

    } catch (Exception e) {
        logger.warn("Error converting captions in " + input, e);
        if (e instanceof CaptionConverterException) {
            throw (CaptionConverterException) e;
        } else if (e instanceof UnsupportedCaptionFormatException) {
            throw (UnsupportedCaptionFormatException) e;
        } else {
            throw new CaptionConverterException(e);
        }
    }
}

From source file:org.wso2.carbon.apimgt.everywhere.startup.publisher.APIManagerStartupPublisher.java

private String getImageContentType(String imageAbsolutePath) {
    String fileName = new File(imageAbsolutePath).getName();
    return FileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
}