Hello everyone,

so, I think I figured it out when it breaks and throws the 
TypeConversionException. I was trying to "save" the body "for later" (content 
of the file), because during the route the message would be changing and 
mutating. 

I created the route directly from the file component i.e. 
from("file:.......*.pdf") and forwarded it to the http endpoint (so that the 
body of the message will stay the content of the file) and it worked. Anytime I 
try and convert the body or save the body into an exchangeProperty, it breaks.

So, my workaround was to divide a bit more the routes I had for it. Still 
unsure why the other method did not work.

Best regards and have a great weekend!
Jaime

-----Ursprüngliche Nachricht-----
Von: Penagos Jaime <jaime.pena...@ub.uni-muenchen.de> 
Gesendet: Mittwoch, 27. November 2024 11:40
An: 'users@camel.apache.org' <users@camel.apache.org>
Betreff: AW: Forwarding PDF files to HTTP endpoint gives TypeConversionException

Hi everyone,


so, i checked some other program I did last year, where i forwarded files to an 
HTTP endpoint to ingest them through an API, and I didn't do  anything 
different than in this example with PDFs (back then was for a RESTful Service 
through Camel, tho). I am still quite confuse why I keep getting the 
Serialization / TypeConversion exception.


If you have any ideas I can check, please let me know. I appreciate all the 
feedback.


Best regards,

Jaime

________________________________
Von: Penagos Jaime <jaime.pena...@ub.uni-muenchen.de>
Gesendet: Dienstag, 26. November 2024 07:26:28
An: 'users@camel.apache.org'
Betreff: AW: Forwarding PDF files to HTTP endpoint gives TypeConversionException

Hello,

yeah, my bad. My actual code looks like this:

-----------------------------------------------------
        from("file:appdata"
                + "?"
                + "antInclude=*/*.pdf"
                + "&"
                + "maxDepth=2" // to explore the main directory and all 
subfolders
                + "&"
                + "recursive=true"
                + "&"
                + "noop=true"
                + "&"
                + "probeContentType=true"
        )
                .id("PDF-Ingest")
                .autoStartup("{{pdfIngest}}")
                .throttle(1)

                .setProperty("myFileName", simple("${file:onlyname.noext}"))
                .setProperty("myDataBody", body())

                .process(txLoadVariablesForIngest) // Variables we need will be 
loaded into the exchange properties
                .to("direct:repoCreateStructure") // create the structure on 
the internal repository

                .process((Exchange exchange) -> {
                        String myContentType = 
exchange.getMessage().getHeader(Exchange.FILE_CONTENT_TYPE, String.class);

                        exchange.setProperty("contentType", myContentType);
                        exchange.setProperty("myDataBody", (Object) 
exchange.getMessage().getBody());

                        int myFileSize = 
exchange.getMessage().getHeader(Exchange.FILE_LENGTH, Integer.class);
                        exchange.setProperty("fileSize", myFileSize);

                .to("direct:repoIngestFile");




        from("direct:ingestFILE")
                .id("repoFileIngest")
                .removeHeaders("*")
                .setHeader(Exchange.HTTP_URI,
                                simple("{{repo}}"
                                                + "${exchangeProperty.myURL}" 
// URL we loaded from the Processor
                                )
                )
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Slug", exchangeProperty("myFileName"))

                .choice()
                .when(exchangeProperty("contentType").isNotNull())
                .setHeader(Exchange.CONTENT_TYPE, 
exchangeProperty("contentType"))

                .otherwise()
                .setHeader(Exchange.CONTENT_TYPE, 
constant("application/octet-stream"))
                .end()

                .process((Exchange exchange)->{
                        Object myBodyObj = 
exchange.getProperty("myDataBody",Object.class);
                        exchange.getIn().setBody(myBodyObj);
                })

                .to("http://repo?";
                                + "authUsername="
                                + "{{user}}"
                                + "&"
                                + "authPassword="
                                + "{{pass}}"
                                + "&"
                                + "throwExceptionOnFailure=false"
                )
        .setProperty("responseCode", header(Exchange.HTTP_RESPONSE_CODE)) // 
i.e. 200, 404, 410
        .setProperty("responseText", header(Exchange.HTTP_RESPONSE_TEXT)) // 
i.e. OK
                .log("${exchangeProperty.responseCode};");


-----------------------------------------------------------------


From the first route I scan the PDFs, extract OCRs and create the structure on 
my repository (that's done in another route), so that is the reason why I 
"save" the body of the file on a exchangeProperty. Still a bit unsure why am I 
getting the exception, somehow I recall working with other filetypes and not 
having issues, but that was a while ago. Maybe is it some dependency I am 
missing? The serialization exception I am getting might be because Camel can't 
map the object to the Object class? I tried using byte[] as well, and also 
convertBodyTo() but it did not work...

Thanks again for your advice.

Best regards,
Jaime





-----Ursprüngliche Nachricht-----
Von: Jeremy Ross <jeremy.g.r...@gmail.com>
Gesendet: Dienstag, 26. November 2024 00:28
An: users@camel.apache.org
Betreff: Re: Forwarding PDF files to HTTP endpoint gives TypeConversionException

 I don’t see why that exception would be thrown, from the example you provided. 
But the example you provided is just from the docs. So, if you could share more 
of your actual route/processor, it would be easier to help.

On Nov 25, 2024 at 2:05:38 PM, Penagos Jaime < 
jaime.pena...@ub.uni-muenchen.de> wrote:

> Hey everyone,
>
> I've been working with the Apache Camel PDF Component and my idea is 
> to forward the content to another endpoint, that it is supposed to 
> send it to another API through the HTTP component.
>
> So, I've been trying to use a File component that sends the PDFs to 
> the other direct: route, and I am getting the following exception
>
>
>
> ***************************************
> org.apache.camel.TypeConversionException: Error during type conversion 
> from type: org.apache.camel.component.file.GenericFile to the required
> type: java.io.File with value GenericFile
> ****************************************
>
>
> and somehow I am confused, because I never ran to this problem. I 
> thought I could just use something like this:
>
> ********************************
>
> from("file://inputdir/").process(new Processor() {  public void 
> process(Exchange exchange) throws Exception {
>    Object body = exchange.getIn().getBody();
>    // do some business logic with the input body  }
>
> }).to(direct:otherendpoint);
>
> ***********************************
>
>
>
> But it hasnt worked so far. I would appreciate some ideas, on how can 
> I forward the content of a file somehow to the other route, not sure 
> how the serialization works here. I am also quite new with the concept 
> of message forwarding and serialization, so any advice is greatly appreciated.
>
>
> Thanks for your insights.
> Best regards,
> Jaime P
>

Reply via email to