1
votes

I'm working in an apache camel project that download any attachment in the inbox, to achieve that I followed the information in the Consuming mails with attachment sample from http://camel.apache.org/mail.html. That works pretty well except when the attachment has a filename with special characters or spaces. For example if the attachment has a filename like "voucher.pdf" the program works and downloads the file, but if it has a filename like "Pase de abordar en línea (1).pdf" I get this error:

Message History --------------------------------------------------------------------------------------------------------------------------------------- RouteId ProcessorId Processor
Elapsed (ms) [route1 ] [route1 ] [imaps://imap.gmail.com:993?consumer.delay=60000&contentType=multipart%2FMIXED&] [ 2576] [route1 ] [convertBodyTo1 ] [convertBodyTo[java.lang.String]
] [ 642] [route1 ] [process1 ] [Processor@0x13e344d
] [ 1903] [route1 ] [to1 ] [file:C:/outputFolder/?flatten=true&fileName=${header.nombref}
] [ 8]

Stacktrace --------------------------------------------------------------------------------------------------------------------------------------- org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:344) at org.apache.camel.component.file.GenericFileProducer.writeFile(GenericFileProducer.java:277) at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:165) at org.apache.camel.component.file.GenericFileProducer.process(GenericFileProducer.java:79) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) at org.apache.camel.component.mail.MailConsumer.processExchange(MailConsumer.java:392) at org.apache.camel.component.mail.MailConsumer.processBatch(MailConsumer.java:221) at org.apache.camel.component.mail.MailConsumer.poll(MailConsumer.java:128) at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175) at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.FileNotFoundException: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= (El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:162) at org.apache.camel.component.file.FileOperations.prepareOutputFileChannel(FileOperations.java:505) at org.apache.camel.component.file.FileOperations.writeFileByStream(FileOperations.java:444) at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:326) ... 24 more 7209 [Camel (camel-1) thread #0 - imaps://imap.gmail.com:993] DEBUG org.apache.camel.processor.Pipeline - Message exchange has failed: so breaking out of pipeline for exchange: Exchange[ID-desarrollo01-59804-1524233458774-0-2] Exception: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= 7209 [Camel (camel-1) thread #0 - imaps://imap.gmail.com:993] WARN org.apache.camel.component.mail.MailConsumer - Exchange failed, so rolling back message status: Exchange[ID-desarrollo01-59804-1524233458774-0-2] org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:344) at org.apache.camel.component.file.GenericFileProducer.writeFile(GenericFileProducer.java:277) at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:165) at org.apache.camel.component.file.GenericFileProducer.process(GenericFileProducer.java:79) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) at org.apache.camel.component.mail.MailConsumer.processExchange(MailConsumer.java:392) at org.apache.camel.component.mail.MailConsumer.processBatch(MailConsumer.java:221) at org.apache.camel.component.mail.MailConsumer.poll(MailConsumer.java:128) at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175) at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.FileNotFoundException: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= (El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:162) at org.apache.camel.component.file.FileOperations.prepareOutputFileChannel(FileOperations.java:505) at org.apache.camel.component.file.FileOperations.writeFileByStream(FileOperations.java:444) at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:326) ... 24 more

This is my main method:

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("C://Program Files (x86)//Java//jre1.8.0_161//lib//security");
        ksp.setPassword("changeit");

        KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp);
        kmp.setKeyPassword("changeit");

        SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);        


        BasicConfigurator.configure();
        RutaMail routeBuilder = new RutaMail();

        SimpleRegistry jndi= new SimpleRegistry();
        jndi.put("keystore", scp);              

        CamelContext ctx = new DefaultCamelContext(jndi);





        try {

            ctx.addRoutes(routeBuilder);
            ctx.start();
            Thread.sleep(30000);
            ctx.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

This is the camel route:

public void configure() throws Exception {

        from("imaps://imap.gmail.com:[email protected]&password=password"
                + "&delete=false&unseen=true&consumer.delay=60000&contentType=multipart/MIXED")
                        .convertBodyTo(String.class, "UTF-8")
                        .process(new MailProcessor())

                        .to("file:C:/outputFolder/?flatten=true&fileName=${header.nombref}")
                        .log("el nombre del archivo es  ${header.nombref}").end();
    }

}

This is the processor:

public class MailProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
                String nombre="";


        // the API is a bit clunky so we need to loop
        Map<String, DataHandler> attachments = exchange.getIn().getAttachments();

        String file="";

        if (attachments.size() > 0) {
            for (String name : attachments.keySet()) {                


                DataHandler dh = attachments.get(name);

                String filename =dh.getName();
                nombre =filename;


                byte[] data = exchange.getContext().getTypeConverter()
                                  .convertTo(byte[].class, dh.getInputStream());                                             

                exchange.getIn().setBody( data);
                exchange.getIn().setHeader("nombref", filename);
            }          


        }
   }

}

Thanks for your help.

2
Make sure that the user has permission to write to the directorySheetal Mohan Sharma
What version of Camel do you use. And have you tried with a newer version.Claus Ibsen
"Caused by: java.io.FileNotFoundException: C:\outputFolder\=?UTF-8?Q?Pase_de_abordar_en_l=C3=ADnea_(1).pdf?= (El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos)" suggests you (or maybe JavaMail) didn't properly decode the filename before using it.Mark Rotteveel
Hi, @SheetalMohanSharma the user has permission, the code works well if the filename doesn't have special characters in it.juan jose perez hernandez
@ClausIbsen I'm using version 2.18.1.redhat-000015,I haven't try any other vesion.juan jose perez hernandez

2 Answers

0
votes

It seems that your OS doesn't allow to save file with such name.

0
votes

I believe you are using the 'file' component incorrectly. You need to add // after URI name. Try with file://C:/outputFolder/?flatten=true&fileName=${header.nombref and let me know.