Example usage for org.springframework.mail.javamail ConfigurableMimeFileTypeMap getContentType

List of usage examples for org.springframework.mail.javamail ConfigurableMimeFileTypeMap getContentType

Introduction

In this page you can find the example usage for org.springframework.mail.javamail ConfigurableMimeFileTypeMap getContentType.

Prototype

@Override
public String getContentType(String fileName) 

Source Link

Document

Delegates to the underlying FileTypeMap.

Usage

From source file:com.netsteadfast.greenstep.util.FSUtils.java

public static String getMimeType(String filename) throws Exception {
    ConfigurableMimeFileTypeMap mfm = new ConfigurableMimeFileTypeMap();
    return mfm.getContentType(filename);
}

From source file:org.sventon.web.ctrl.template.ShowFileControllerTest.java

@Test
public void testConfigurableMimeFileTypeMap() {
    final ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap();
    fileTypeMap.afterPropertiesSet();//from  ww w.j a  v  a  2  s  . c o m

    assertEquals("application/octet-stream", fileTypeMap.getContentType("file.abc"));
    assertEquals("application/zip", fileTypeMap.getContentType("file.zip"));
    assertEquals("image/jpeg", fileTypeMap.getContentType("file.jpg"));
    assertEquals("image/jpeg", fileTypeMap.getContentType("file.jpe"));
    assertEquals("image/jpeg", fileTypeMap.getContentType("file.jpeg"));
    assertEquals("image/gif", fileTypeMap.getContentType("file.gif"));
    assertEquals("image/x-png", fileTypeMap.getContentType("file.png"));
    assertEquals("application/octet-stream", fileTypeMap.getContentType("filenamejpg"));
    assertEquals("image/gif", fileTypeMap.getContentType("/dir/file.gif"));
}

From source file:com.wavemaker.runtime.FileController.java

protected void setContentType(HttpServletResponse response, File file) {
    ConfigurableMimeFileTypeMap mimeFileTypeMap = new ConfigurableMimeFileTypeMap();
    mimeFileTypeMap.setMappings(new String[] { "text/css css CSS", "application/json json JSON smd SMD" });

    response.setContentType(mimeFileTypeMap.getContentType(file));
}

From source file:eionet.webq.web.controller.FileDownloadController.java

/**
 * Writes project file to response.//from  w  w  w . ja v a2s  .co  m
 *
 * @param name        file name
 * @param projectFile project file object
 * @param response    http response
 * @param disposition inline or attachment
 */
private void writeProjectFileToResponse(String name, ProjectFile projectFile, HttpServletResponse response,
        String disposition, String format) {

    ConfigurableMimeFileTypeMap mimeTypesMap = new ConfigurableMimeFileTypeMap();
    String contentType = mimeTypesMap.getContentType(name);

    // check if default
    if (mimeTypesMap.getContentType("").equals(contentType)) {
        if (name.endsWith(".xhtml")) {
            contentType = MediaType.APPLICATION_XHTML_XML_VALUE;
        } else if (name.endsWith(".js")) {
            contentType = "application/javascript";
        } else if (name.endsWith(".json")) {
            contentType = MediaType.APPLICATION_JSON_VALUE;
        } else {
            contentType = MediaType.APPLICATION_XML_VALUE;
        }
        // TODO check if there are more missing mime types
    }

    byte[] fileContent = projectFile.getFileContent();

    if ("json".equals(format)) {
        fileContent = jsonXMLConverter.convertXmlToJson(projectFile.getFileContent());
        contentType = MediaType.APPLICATION_JSON_VALUE;
        disposition = "inline";
    }

    if (contentType.startsWith("text") || contentType.startsWith("application")) {
        contentType += ";charset=UTF-8";
    }
    response.setContentType(contentType);
    setContentDisposition(response, disposition + ";filename=" + name);
    if (projectFile.getUpdated() != null) {
        response.setDateHeader("Last-Modified", projectFile.getUpdated().getTime());
    } else if (projectFile.getCreated() != null) {
        response.setDateHeader("Last-Modified", projectFile.getCreated().getTime());
    }
    writeToResponse(response, fileContent);
}

From source file:org.betaconceptframework.astroboa.console.jsf.edit.SimpleCmsPropertyWrapper.java

private void processUploadedFile(UploadItem uploadItem) throws IOException {

    String filename = null;/*from w ww  . jav  a  2  s. c  om*/
    byte[] filedata = null;
    GregorianCalendar lastModified;
    String mimeType = null;

    ConfigurableMimeFileTypeMap mimeTypesMap = (ConfigurableMimeFileTypeMap) JSFUtilities
            .getBeanFromSpringContext("mimeTypesMap");

    // add the wrapper index to the list of wrappers that should be updated by the UI
    complexCmsPropertyEdit.setWrapperIndexesToUpdate(Collections.singleton(wrapperIndex));

    SimpleCmsPropertyValueWrapper simpleCmsPropertyValueWrapper;

    // We should check if we add a new binary or updating an existing one
    if (indexOfPropertyValueToBeProcessed == -1) {
        // add a new value wrapper if property is defined to get multiple values or it is defined to get a single value and no value
        // exists yet
        if (isMultiple() || (!isMultiple() && getSimpleCmsPropertyValueWrappers().isEmpty())) {
            addNewSimpleCmsPropertyValueWrapper();
        }
        simpleCmsPropertyValueWrapper = simpleCmsPropertyValueWrappers
                .get(simpleCmsPropertyValueWrappers.size() - 1);
    } else {
        simpleCmsPropertyValueWrapper = simpleCmsPropertyValueWrappers.get(indexOfPropertyValueToBeProcessed);
    }

    if (uploadItem.isTempFile()) { // if upload was set to use temp files
        filename = FilenameUtils.getName(uploadItem.getFileName());
        filedata = FileUtils.readFileToByteArray(uploadItem.getFile());
        lastModified = new GregorianCalendar(JSFUtilities.getLocale());
        lastModified.setTimeInMillis(uploadItem.getFile().lastModified());
        mimeType = mimeTypesMap.getContentType(filename);
        logger.debug("file uploaded " + filename);
    } else if (uploadItem.getData() != null) { // if upload was done in memory
        filename = uploadItem.getFileName();
        filedata = uploadItem.getData();
        lastModified = (GregorianCalendar) GregorianCalendar.getInstance(JSFUtilities.getLocale());
        mimeType = mimeTypesMap.getContentType(filename);
        logger.debug("file uploaded " + filename);
    }

    if (StringUtils.isBlank(mimeType)) {
        mimeType = "application/octet-stream";
    }

    if (filedata == null || filename == null) {
        JSFUtilities.addMessage(null, "object.edit.action.uploadFile.uploadFailed", null,
                FacesMessage.SEVERITY_WARN);
        return;
    }

    //Check if this property is the 'thumbnail' property
    try {
        byte[] thumbnailContent = null;
        if (simpleCmsPropertyValueWrapper.isThumbnailPropertyValueWrapper()) {
            //Generate thumbnail
            if (mimeType != null && (mimeType.equals("image/jpeg") || mimeType.equals("image/gif")
                    || mimeType.equals("image/png") || mimeType.equals("image/x-png"))) {
                thumbnailContent = ImageUtils.changeAspectRatioAndResize(filedata, "image/png", 128, 0, 1.0,
                        "TOPLEFT");
            } else {
                JSFUtilities.addMessage(null,
                        "object.edit.action.uploadFile.thumbnailCanBeCreatedOnlyFromJPGorPNGorGIFFiles", null,
                        FacesMessage.SEVERITY_WARN);
                return;
            }
        }

        BinaryChannel binaryChannelValue = simpleCmsPropertyValueWrapper.getNewBinaryChannelValue();

        //Copy byte[] to a new byte[]
        byte[] newContent = null;
        if (thumbnailContent == null) {
            newContent = new byte[filedata.length];
            System.arraycopy(filedata, 0, newContent, 0, filedata.length);
        } else {
            newContent = new byte[thumbnailContent.length];
            System.arraycopy(thumbnailContent, 0, newContent, 0, thumbnailContent.length);
        }

        binaryChannelValue.setContent(newContent);
        binaryChannelValue.setMimeType(new String(mimeType));
        binaryChannelValue.setSize(filedata.length);
        binaryChannelValue.setSourceFilename(new String(filename));
        binaryChannelValue.setModified(GregorianCalendar.getInstance(JSFUtilities.getLocale()));

        simpleCmsPropertyValueWrapper.setValue(binaryChannelValue);

    } catch (Exception e) {
        JSFUtilities.addMessage(null, "object.edit.action.uploadFile.uploadFailed", null,
                FacesMessage.SEVERITY_ERROR);
        logger.error("File upload failed", e);
    } finally {
        simpleCmsPropertyValueWrapper.setMimeTypeIconFilePath(null);
        indexOfPropertyValueToBeProcessed = -1;

    }
}