Search in sources :

Example 56 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project projectforge by micromata.

the class SendMail method createMailAttachmentContent.

private MimeMultipart createMailAttachmentContent(MimeMessage message, final Mail composedMessage, final String icalContent, final Collection<? extends MailAttachment> attachments, final SendMailConfig sendMailConfig) throws MessagingException {
    // create and fill the first message part
    final MimeBodyPart mbp1 = new MimeBodyPart();
    String type = "text/";
    if (StringUtils.isNotBlank(composedMessage.getContentType())) {
        type += composedMessage.getContentType();
        type += "; charset=";
        type += composedMessage.getCharset();
    } else {
        type = "text/html; charset=";
        type += sendMailConfig.getCharset();
    }
    mbp1.setContent(composedMessage.getContent(), type);
    mbp1.setHeader("Content-Transfer-Encoding", "8bit");
    // create the Multipart and its parts to it
    final MimeMultipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);
    if (StringUtils.isNotBlank(icalContent)) {
        message.addHeaderLine("method=REQUEST");
        message.addHeaderLine("charset=UTF-8");
        message.addHeaderLine("component=VEVENT");
        final MimeBodyPart icalBodyPart = new MimeBodyPart();
        icalBodyPart.setHeader("Content-Class", "urn:content-  classes:calendarmessage");
        icalBodyPart.setHeader("Content-ID", "calendar_message");
        icalBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(icalContent.getBytes(), "text/calendar")));
        final String s = Integer.toString(random.nextInt(Integer.MAX_VALUE));
        icalBodyPart.setFileName("ICal-" + s + ".ics");
        mp.addBodyPart(icalBodyPart);
    }
    if (attachments != null && !attachments.isEmpty()) {
        // create an Array of message parts for Attachments
        final MimeBodyPart[] mbp = new MimeBodyPart[attachments.size()];
        // remember you can extend this functionality with META-INF/mime.types
        // See http://docs.oracle.com/javaee/5/api/javax/activation/MimetypesFileTypeMap.html
        final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        int i = 0;
        for (final MailAttachment attachment : attachments) {
            // create the next message part
            mbp[i] = new MimeBodyPart();
            // only by file name
            String mimeType = mimeTypesMap.getContentType(attachment.getFilename());
            if (StringUtils.isBlank(mimeType)) {
                mimeType = "application/octet-stream";
            }
            // attach the file to the message
            final DataSource ds = new ByteArrayDataSource(attachment.getContent(), mimeType);
            mbp[i].setDataHandler(new DataHandler(ds));
            mbp[i].setFileName(attachment.getFilename());
            mp.addBodyPart(mbp[i]);
            i++;
        }
    }
    return mp;
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) MimeMultipart(javax.mail.internet.MimeMultipart) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 57 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project Artemis by ls1intum.

the class FileResource method buildFileResponse.

/**
 * Builds the response with headers, body and content type for specified path and file name
 *
 * @param path to the file
 * @param filename the name of the file
 * @return response entity
 */
private ResponseEntity<byte[]> buildFileResponse(String path, String filename) {
    try {
        var actualPath = Paths.get(path, filename).toString();
        var file = fileService.getFileForPath(actualPath);
        if (file == null) {
            return ResponseEntity.notFound().build();
        }
        HttpHeaders headers = new HttpHeaders();
        // attachment will force the user to download the file
        String contentType = filename.endsWith("htm") || filename.endsWith("html") || filename.endsWith("svg") || filename.endsWith("svgz") ? "attachment" : "inline";
        headers.setContentDisposition(ContentDisposition.builder(contentType).filename(filename).build());
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(filename);
        // if it also can't determine mime type
        if (mimeType == null) {
            MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
            mimeType = fileTypeMap.getContentType(filename);
        }
        return ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType(mimeType)).header("filename", filename).body(file);
    } catch (IOException ex) {
        log.error("Failed to download file: " + filename + "on path: " + path, ex);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) IOException(java.io.IOException) FileNameMap(java.net.FileNameMap)

Example 58 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project mercury by yellow013.

the class HttpStaticFileServerHandler method setContentTypeHeader.

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap)

Example 59 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project clearth by exactpro.

the class LogsBean method getLogsZip.

public StreamedContent getLogsZip() {
    if (selectedLogsList.size() == 0)
        return null;
    if (!logsDir.exists())
        return null;
    try {
        if (!outputDir.exists())
            outputDir.mkdir();
        List<File> filesToZip = new ArrayList<File>();
        for (String logName : selectedLogsList) filesToZip.add(new File(logsDir, logName));
        File result = new File(outputDir, UserInfoUtils.getUserName() + "_logs.zip");
        FileOperationUtils.zipFiles(result, filesToZip);
        result.deleteOnExit();
        StreamedContent file = new DefaultStreamedContent(new FileInputStream(result), new MimetypesFileTypeMap().getContentType(result), "logs.zip");
        // selectedLogsList.clear();
        return file;
    } catch (Exception e) {
        String msg = "Could not download logs";
        getLogger().error(msg, e);
        MessageUtils.addErrorMessage(msg, ExceptionUtils.getDetailedMessage(e));
        return null;
    }
}
Also used : DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) StreamedContent(org.primefaces.model.StreamedContent) DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 60 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project clearth by exactpro.

the class ConfigMakerToolBean method makeConfigAndDownload.

public StreamedContent makeConfigAndDownload() {
    if (file == null) {
        MessageUtils.addWarningMessage("No file selected", "Please select a script file (matrix)!");
        return null;
    }
    try {
        File storedUploadedFile = storeUploadedFile();
        if (storedUploadedFile == null)
            return null;
        File configFile = configMakerTool.makeConfig(storedUploadedFile, destDir, storedUploadedFile.getName());
        return new DefaultStreamedContent(new FileInputStream(configFile), new MimetypesFileTypeMap().getContentType(configFile), configFile.getName());
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}
Also used : DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) File(java.io.File) UploadedFile(org.primefaces.model.UploadedFile) FileInputStream(java.io.FileInputStream) ClearThException(com.exactprosystems.clearth.utils.ClearThException)

Aggregations

MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)63 File (java.io.File)19 IOException (java.io.IOException)15 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileNameMap (java.net.FileNameMap)3 HttpURLConnection (java.net.HttpURLConnection)3 MimeType (javax.activation.MimeType)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Failure (org.junit.runner.notification.Failure)3 InvalidKeyException (java.security.InvalidKeyException)2