Example usage for javax.activation MimetypesFileTypeMap getContentType

List of usage examples for javax.activation MimetypesFileTypeMap getContentType

Introduction

In this page you can find the example usage for javax.activation MimetypesFileTypeMap getContentType.

Prototype

public synchronized String getContentType(String filename) 

Source Link

Document

Return the MIME type based on the specified file name.

Usage

From source file:org.bonitasoft.connectors.bonita.AddDocuments.java

/**
 * get file mimeType//from w w w. j  a  va2  s  . c  om
 * 
 * @param file
 * @return String
 */
private String getType(File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String mimeType = mimeTypesMap.getContentType(file);
    return mimeType;
}

From source file:org.craftercms.studio.impl.v1.util.spring.mvc.BinaryView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    //Writer output = response.getWriter();
    OutputStream out = response.getOutputStream();
    Map<String, Object> responseModelMap = (Map<String, Object>) model
            .get(RestScriptsController.DEFAULT_RESPONSE_BODY_MODEL_ATTR_NAME);
    InputStream contentStream = (InputStream) responseModelMap.get(DEFAULT_CONTENT_STREAM_MODEL_ATTR_NAME);
    String contentPath = (String) responseModelMap.get(DEFAULT_CONTENT_PATH_MODEL_ATTR_NAME);

    MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
    String contentType = mimetypesFileTypeMap.getContentType(contentPath);
    response.setContentType(contentType);
    if (contentStream != null) {
        IOUtils.write(IOUtils.toByteArray(contentStream), out);
    }//from  w w w  .java2 s .  com
    out.flush();
    IOUtils.closeQuietly(contentStream);
    IOUtils.closeQuietly(out);
}

From source file:org.craftercms.cstudio.publishing.processor.SearchAttachmentProcessor.java

private void update(String siteId, String root, List<String> fileList, boolean isDelete) throws IOException {
    for (String fileName : fileList) {
        String mimeType = null;/* www  . ja  v  a 2s . co m*/
        File file = new File(root + fileName);
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        mimeType = mimeTypesMap.getContentType(fileName);
        if (supportedMimeTypes.contains(mimeType) && !isDelete) {
            searchService.updateDocument(siteId, fileName, file);
        } else if (isDelete) {
            searchService.delete(siteId, fileName);
        }
    }
}

From source file:org.craftercms.studio.impl.v1.content.pipeline.CheckImageSizeProcessor.java

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String name = content.getProperty(DmConstants.KEY_FILE_NAME);
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String mimetype = mimeTypesMap.getContentType(name);
    //String isImage = content.getProperty(WcmConstants.KEY_IS_IMAGE);
    boolean process = (StringUtils.isEmpty(mimetype)) ? false
            : mimetype.startsWith("image/") && !StringUtils.equalsIgnoreCase(mimetype, "image/svg+xml");
    if (process) {
        String allowLessSize = content.getProperty(DmConstants.KEY_ALLOW_LESS_SIZE);
        boolean lessSize = ContentFormatUtils.getBooleanValue(allowLessSize);
        String allowedWidth = content.getProperty(DmConstants.KEY_ALLOWED_WIDTH);
        String allowedHeight = content.getProperty(DmConstants.KEY_ALLOWED_HEIGHT);
        int width = (StringUtils.isEmpty(allowedWidth)) ? -1 : ContentFormatUtils.getIntValue(allowedWidth);
        int height = (StringUtils.isEmpty(allowedHeight)) ? -1 : ContentFormatUtils.getIntValue(allowedHeight);
        InputStream in = content.getContentStream();
        ContentAssetInfoTO assetInfo = (result.getItem() == null) ? new ContentAssetInfoTO()
                : (ContentAssetInfoTO) result.getItem();
        in = checkForImageSize(in, width, height, lessSize, assetInfo);
        content.getProperties().put(DmConstants.KEY_WIDTH, String.valueOf(assetInfo.getWidth()));
        content.getProperties().put(DmConstants.KEY_HEIGHT, String.valueOf(assetInfo.getHeight()));
        assetInfo.getWidth();/*from   w  w  w. ja v a 2s . c om*/
        result.setItem(assetInfo);
        content.setContentStream(in);
    }
}

From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java

public String getZipEntryType(String entry) {
    MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap();
    String type = mimetypes.getContentType(entry);
    if (type == null || type.length() == 0) {
        return "application/octet-stream";
    }//from   ww w .ja  v a  2  s .c  o m
    return type;
}

From source file:ddf.catalog.data.BinaryContentImplTest.java

@Before
public void setUp() {
    content = new File("src/test/resources/data/i4ce.png");
    MimetypesFileTypeMap mimeMapper = new MimetypesFileTypeMap();
    try {/*www.  ja va2 s  .c o  m*/
        mimeType = new MimeType(mimeMapper.getContentType(content));
    } catch (MimeTypeParseException e) {
        LOGGER.error("Mime parser Failure", e);
        new Failure(null, e);
    }
}

From source file:com.nemesis.admin.UploadServlet.java

private String getMimeType(File file) {
    String mimetype = "";
    if (file.exists()) {
        if (getSuffix(file.getName()).equalsIgnoreCase("png")) {
            mimetype = "image/png";
        } else if (getSuffix(file.getName()).equalsIgnoreCase("jpg")) {
            mimetype = "image/jpg";
        } else if (getSuffix(file.getName()).equalsIgnoreCase("jpeg")) {
            mimetype = "image/jpeg";
        } else if (getSuffix(file.getName()).equalsIgnoreCase("gif")) {
            mimetype = "image/gif";
        } else {// ww w.j a  v  a 2 s  . co m
            javax.activation.MimetypesFileTypeMap mtMap = new javax.activation.MimetypesFileTypeMap();
            mimetype = mtMap.getContentType(file);
        }
    }
    return mimetype;
}

From source file:org.talend.components.service.rest.impl.PropertiesControllerImpl.java

@Override
public ResponseEntity<InputStreamResource> getIcon(String definitionName, DefinitionImageType imageType) {
    notNull(definitionName, "Definition name cannot be null.");
    notNull(imageType, "Definition image type cannot be null.");
    final Definition<?> definition = propertiesHelpers.getDefinition(definitionName);
    notNull(definition, "Could not find definition of name %s", definitionName);

    // Undefined and missing icon resources are simply 404.
    String imagePath = definition.getImagePath(imageType);
    if (imagePath == null) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }//from   w  w  w. ja  v a2  s. c  o  m
    InputStream is = definition.getClass().getResourceAsStream(imagePath);
    if (is == null) {
        log.info("The image type %s should exist for %s at %s, but is missing. "
                + "The component should provide this resource.", imageType, definitionName, imagePath);
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // At this point, we have enough information for a correct response.
    ResponseEntity.BodyBuilder response = ResponseEntity.ok();

    // Add the content type if it can be determined.
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String contentType = mimeTypesMap.getContentType(imagePath);
    if (contentType != null) {
        response = response.contentType(MediaType.parseMediaType(contentType));
    }

    return response.body(new InputStreamResource(is));
}

From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java

public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException {
    final FileObject file = this;
    final XMLEventFactory ef = XMLEventFactory.newInstance();
    final byte[] buf = new byte[1024];
    final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream());
    return new XMLEventReaderBase() {
        private boolean started;
        private boolean ended;

        public void close() throws XMLStreamException {
            try {
                zip.close();//from ww w . j  av  a 2 s. c  om
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }

        protected boolean more() throws XMLStreamException {
            try {
                ZipArchiveEntry entry;
                if (!started) {
                    Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI());
                    add(ef.createStartDocument());
                    add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator()));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(file.getName()));
                    add(ef.createEndElement(TITLE, null));
                    add(ef.createStartElement(ID, null, null));
                    add(ef.createCharacters(id));
                    add(ef.createEndElement(ID, null));
                    Attribute href = ef.createAttribute("href", file.toUri().toASCIIString());
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip"));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    add(ef.createStartElement(UPDATED, null, null));
                    add(ef.createCharacters(format(new Date(file.getLastModified()))));
                    add(ef.createEndElement(UPDATED, null));
                    started = true;
                    return true;
                } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) {
                    String name = entry.getName();
                    String link = entryPattern.replace("{entry}", PercentCodec.encode(name));
                    MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap();
                    String type = mimetypes.getContentType(name);
                    if (type == null || type.length() == 0) {
                        type = "application/octet-stream";
                    }
                    add(ef.createStartElement(ENTRY, null, null));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(name));
                    add(ef.createEndElement(TITLE, null));
                    Attribute href = ef.createAttribute("href", link);
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    long size = entry.getSize();
                    if (size > 0) {
                        zip.skip(size);
                    } else {
                        while (zip.read(buf, 0, buf.length) >= 0)
                            ;
                    }
                    add(ef.createEndElement(ENTRY, null));
                    return true;
                } else if (!ended) {
                    add(ef.createEndElement(FEED, null));
                    add(ef.createEndDocument());
                    ended = true;
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }
    };
}

From source file:de.jakusys.jackhammer.cli.command.UploadingFileAlternationListener.java

private void uploadFile(File file) {

    MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();

    try {/*from  w w w.  ja  v a 2 s  .  c  om*/
        String relPath = PathRelativizer.relativize(root, file.getParentFile());
        Node parent = getNode(relPath);
        String contentType = mimetypesFileTypeMap.getContentType(file);
        JcrUtils.putFile(parent, file.getName(), contentType, new FileInputStream(file));

        session.save();
    } catch (RepositoryException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (FileNotFoundException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
}