Search in sources :

Example 1 with CisConversionResult

use of nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult in project iaf by ibissource.

the class CisConversionServiceImpl method createFailureResult.

private CisConversionResult createFailureResult(String filename, ConversionOption conversionOption, MediaType mediaType, String message) {
    CisConversionResult result;
    StringBuilder msg = new StringBuilder();
    if (filename != null) {
        msg.append(filename);
    }
    msg.append(" " + message);
    LOGGER.warn("Conversion not supported: " + msg.toString());
    result = CisConversionResult.createFailureResult(conversionOption, mediaType, filename, msg.toString());
    return result;
}
Also used : CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)

Example 2 with CisConversionResult

use of nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult in project iaf by ibissource.

the class AbstractConvertor method convertToPdf.

/**
 * Should not be overloaded by the concrete classes.
 */
@Override
public final CisConversionResult convertToPdf(MediaType mediaType, String filename, Message message, ConversionOption conversionOption, String charset) {
    checkForSupportedMediaType(mediaType);
    CisConversionResult result = new CisConversionResult();
    File resultFile = null;
    try {
        resultFile = UniqueFileGenerator.getUniqueFile(pdfOutputlocation, this.getClass().getSimpleName(), "pdf");
        result.setConversionOption(conversionOption);
        result.setMediaType(mediaType);
        result.setDocumentName(ConvertorUtil.createTidyNameWithoutExtension(filename));
        result.setPdfResultFile(resultFile);
        result.setResultFilePath(resultFile.getAbsolutePath());
        LOGGER.debug("Convert to file... " + filename);
        convert(mediaType, message, result, charset);
        LOGGER.debug("Convert to file finished. " + filename);
    } catch (Exception e) {
        if (isPasswordException(e)) {
            result = CisConversionResult.createPasswordFailureResult(filename, conversionOption, mediaType);
        } else {
            result.setFailureReason(createTechnishefoutMsg(e));
        }
        // Clear the file to state that the conversion has failed.
        result.setPdfResultFile(null);
    }
    return result;
}
Also used : CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult) File(java.io.File) IOException(java.io.IOException)

Example 3 with CisConversionResult

use of nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult in project iaf by ibissource.

the class MailConvertor method convert.

@Override
public void convert(MediaType mediaType, Message message, CisConversionResult result, String charset) throws Exception {
    MailMessage eml = null;
    try (InputStream inputStream = message.asInputStream(charset)) {
        eml = MailMessage.load(inputStream, MEDIA_TYPE_LOAD_FORMAT_MAPPING.get(mediaType));
        AttachmentCollection attachments = eml.getAttachments();
        LOGGER.debug("cc : " + toString(eml.getCC()));
        LOGGER.debug("bcc : " + toString(eml.getBcc()));
        LOGGER.debug("sender : " + toString(eml.getSender()));
        LOGGER.debug("from : " + toString(eml.getFrom()));
        LOGGER.debug("to : " + toString(eml.getTo()));
        LOGGER.debug("reversePath : " + toString(eml.getReversePath()));
        LOGGER.debug("subject : " + eml.getSubject());
        MhtSaveOptions options = MhtSaveOptions.getDefaultMhtml();
        options.setMhtFormatOptions(MhtFormatOptions.HideExtraPrintHeader | MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteBccEmailAddress | MhtFormatOptions.WriteCompleteCcEmailAddress | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.WriteCompleteFromEmailAddress | MhtFormatOptions.WriteCompleteToEmailAddress);
        options.setPreserveOriginalDate(true);
        // Overrules the default documentname.
        result.setDocumentName(ConvertorUtil.createTidyNameWithoutExtension(eml.getSubject()));
        File tempMHtmlFile = UniqueFileGenerator.getUniqueFile(getPdfOutputlocation(), this.getClass().getSimpleName(), null);
        eml.getHeaders().set_Item("Date", new SimpleDateFormat(eMailHeaderDateFormat).format(eml.getDate()));
        eml.save(tempMHtmlFile.getAbsolutePath(), options);
        // Load the stream in Word document
        HtmlLoadOptions loadOptions = new HtmlLoadOptions();
        loadOptions.setLoadFormat(LoadFormat.MHTML);
        loadOptions.setWebRequestTimeout(0);
        Long startTime = new Date().getTime();
        try (FileInputStream fis = new FileInputStream(tempMHtmlFile)) {
            Document doc = new Document(fis, loadOptions);
            new Fontsetter(cisConversionService.getFontsDirectory()).setFontSettings(doc);
            resizeInlineImages(doc);
            doc.save(result.getPdfResultFile().getAbsolutePath(), SaveFormat.PDF);
            result.setNumberOfPages(getNumberOfPages(result.getPdfResultFile()));
            Long endTime = new Date().getTime();
            LOGGER.info("Conversion completed in " + (endTime - startTime) + "ms");
        } finally {
            Files.delete(tempMHtmlFile.toPath());
        }
        // Convert and (optional add) any attachment of the mail.
        for (int index = 0; index < attachments.size(); index++) {
            // Initialize Attachment object and Get the indexed Attachment reference
            Attachment attachment = attachments.get_Item(index);
            // Convert the attachment.
            CisConversionResult cisConversionResultAttachment = convertAttachmentInPdf(attachment, result.getConversionOption());
            // If it is an singlepdf add the file to the the current pdf.
            if (ConversionOption.SINGLEPDF.equals(result.getConversionOption()) && cisConversionResultAttachment.isConversionSuccessfull()) {
                try {
                    PdfAttachmentUtil pdfAttachmentUtil = new PdfAttachmentUtil(cisConversionResultAttachment, result.getPdfResultFile());
                    pdfAttachmentUtil.addAttachmentInSinglePdf();
                } finally {
                    deleteFile(cisConversionResultAttachment.getPdfResultFile());
                    // Clear the file because it is now incorporated in the file it self.
                    cisConversionResultAttachment.setPdfResultFile(null);
                    cisConversionResultAttachment.setResultFilePath(null);
                }
            }
            result.addAttachment(cisConversionResultAttachment);
        }
    }
}
Also used : MailMessage(com.aspose.email.MailMessage) MhtSaveOptions(com.aspose.email.MhtSaveOptions) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Attachment(com.aspose.email.Attachment) AttachmentCollection(com.aspose.email.AttachmentCollection) Document(com.aspose.words.Document) Date(java.util.Date) FileInputStream(java.io.FileInputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) HtmlLoadOptions(com.aspose.words.HtmlLoadOptions) CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)

Example 4 with CisConversionResult

use of nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult in project iaf by ibissource.

the class PdfPipe method doPipe.

@Override
public PipeRunResult doPipe(Message input, PipeLineSession session) throws PipeRunException {
    // message should always be available.
    if (Message.isEmpty(input)) {
        throw new IllegalArgumentException("message == null");
    }
    try {
        switch(getAction()) {
            case COMBINE:
                // Get main document to attach attachments
                Message mainPdf = session.getMessage(getMainDocumentSessionKey());
                // Get file name of attachment
                String fileNameToAttach = session.getMessage(getFilenameToAttachSessionKey()).asString();
                Message result = PdfAttachmentUtil.combineFiles(mainPdf, input, fileNameToAttach + ".pdf", getCharset());
                session.put("CONVERSION_OPTION", ConversionOption.SINGLEPDF);
                session.put(getMainDocumentSessionKey(), result);
                return new PipeRunResult(getSuccessForward(), result);
            case CONVERT:
                String filename = session.getMessage(FILENAME_SESSION_KEY).asString();
                CisConversionResult cisConversionResult = cisConversionService.convertToPdf(input, filename, isSaveSeparate() ? ConversionOption.SEPARATEPDF : ConversionOption.SINGLEPDF);
                XmlBuilder main = new XmlBuilder("main");
                cisConversionResult.buildXmlFromResult(main, true);
                session.put("documents", main.toXML());
                return new PipeRunResult(getSuccessForward(), main.toXML());
            default:
                throw new PipeRunException(this, "action attribute must be one of the followings: " + EnumUtils.getEnumList(DocumentAction.class));
        }
    } catch (IOException e) {
        throw new PipeRunException(this, getLogPrefix(session) + "cannot convert to stream", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) PipeRunException(nl.nn.adapterframework.core.PipeRunException) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) IOException(java.io.IOException) CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)

Example 5 with CisConversionResult

use of nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult in project iaf by ibissource.

the class CisConversionServiceImpl method convertToPdf.

@Override
public CisConversionResult convertToPdf(Message message, String filename, ConversionOption conversionOption) throws IOException {
    CisConversionResult result = null;
    MediaType mediaType = getMediaType(message, filename);
    if (isPasswordProtected(mediaType)) {
        result = CisConversionResult.createPasswordFailureResult(filename, conversionOption, mediaType);
    } else {
        // Get the convertor for the given mediatype.
        Convertor convertor = convertorFactory.getConvertor(mediaType);
        if (convertor == null) {
            // Conversion not supported.
            String errorMessage = "Omzetten naar PDF mislukt! Reden: bestandstype wordt niet ondersteund (mediaType: " + mediaType + ")";
            result = createFailureResult(filename, conversionOption, mediaType, errorMessage);
        } else {
            long startTime = System.currentTimeMillis();
            // Convertor found, convert the file
            result = convertor.convertToPdf(mediaType, filename, message, conversionOption, charset);
            if (LOGGER.isDebugEnabled())
                LOGGER.debug(String.format("Convert (in %d msec): mediatype: %s, filename: %s, attachmentoptions: %s", System.currentTimeMillis() - startTime, mediaType, filename, conversionOption));
        }
    }
    return result;
}
Also used : MediaType(org.apache.tika.mime.MediaType) CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult) Convertor(nl.nn.adapterframework.extensions.aspose.services.conv.impl.convertors.Convertor)

Aggregations

CisConversionResult (nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)5 File (java.io.File)2 IOException (java.io.IOException)2 Attachment (com.aspose.email.Attachment)1 AttachmentCollection (com.aspose.email.AttachmentCollection)1 MailMessage (com.aspose.email.MailMessage)1 MhtSaveOptions (com.aspose.email.MhtSaveOptions)1 Document (com.aspose.words.Document)1 HtmlLoadOptions (com.aspose.words.HtmlLoadOptions)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 PipeRunException (nl.nn.adapterframework.core.PipeRunException)1 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)1 Convertor (nl.nn.adapterframework.extensions.aspose.services.conv.impl.convertors.Convertor)1 Message (nl.nn.adapterframework.stream.Message)1 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)1 MediaType (org.apache.tika.mime.MediaType)1