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