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;
}
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();
}
}
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()));
}
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;
}
}
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;
}
}
Aggregations