Search in sources :

Example 1 with TemporaryFile

use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.

the class TempResourceActionTest method createEmptyFile.

/**
 * Creates an empty file at the specified path.
 *
 * @param path the file path
 * @throws IOException if creating the empty file fails
 */
private void createEmptyFile(String path) throws IOException {
    File emptyFile = new TemporaryFile(new File(base, path));
    emptyFile.getParentFile().mkdirs();
    emptyFile.createNewFile();
}
Also used : TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) File(java.io.File)

Example 2 with TemporaryFile

use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.

the class TempResourceActionTest method createFile.

/**
 * Creates a file at the specified path, with the specified content.
 *
 * @param path the file path
 * @throws IOException if creating the empty file fails
 */
private void createFile(String path, String content) throws IOException {
    File file = new TemporaryFile(new File(base, path));
    file.getParentFile().mkdirs();
    file.createNewFile();
    FileUtils.write(file, content);
}
Also used : TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) File(java.io.File)

Example 3 with TemporaryFile

use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.

the class MailSenderPlugin method createAttachmentBodyPart.

/**
 * Add attachments to a multipart message
 *
 * @param attachment the attachment to create the body part for.
 * @param context the XWiki context.
 * @return the body part for the given attachment.
 */
public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException, IOException, MessagingException {
    String name = attachment.getFilename();
    byte[] stream = attachment.getContent();
    File temp = new TemporaryFile(File.createTempFile("tmpfile", ".tmp"));
    FileOutputStream fos = new FileOutputStream(temp);
    fos.write(stream);
    fos.close();
    DataSource source = new FileDataSource(temp);
    MimeBodyPart part = new MimeBodyPart();
    String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
    part.setDataHandler(new DataHandler(source));
    part.setHeader("Content-Type", mimeType);
    part.setFileName(name);
    part.setContentID("<" + name + ">");
    part.setDisposition("inline");
    return part;
}
Also used : FileOutputStream(java.io.FileOutputStream) FileDataSource(javax.activation.FileDataSource) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 4 with TemporaryFile

use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.

the class AttachmentMimeBodyPartFactory method createTemporaryAttachmentDataSource.

private DataSource createTemporaryAttachmentDataSource(Attachment attachment) throws MessagingException {
    File temporaryAttachmentFile;
    FileOutputStream fos = null;
    try {
        temporaryAttachmentFile = new TemporaryFile(File.createTempFile("attachment", ".tmp", this.temporaryDirectory));
        fos = new FileOutputStream(temporaryAttachmentFile);
        fos.write(attachment.getContent());
    } catch (Exception e) {
        throw new MessagingException(String.format("Failed to save attachment [%s] to the file system", attachment.getFilename()), e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            // Only an error at closing, we continue
            this.logger.warn("Failed to close the temporary file attachment when sending an email. " + "Root reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
        }
    }
    return new FileDataSource(temporaryAttachmentFile);
}
Also used : MessagingException(javax.mail.MessagingException) FileOutputStream(java.io.FileOutputStream) FileDataSource(javax.activation.FileDataSource) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) IOException(java.io.IOException) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) File(java.io.File) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) InitializationException(org.xwiki.component.phase.InitializationException)

Aggregations

TemporaryFile (com.xpn.xwiki.internal.file.TemporaryFile)4 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)2 FileDataSource (javax.activation.FileDataSource)2 IOException (java.io.IOException)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 InitializationException (org.xwiki.component.phase.InitializationException)1