Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use a function processor with javascript #1035

Closed
hguerrero opened this issue Oct 30, 2019 · 7 comments
Closed

can't use a function processor with javascript #1035

hguerrero opened this issue Oct 30, 2019 · 7 comments

Comments

@hguerrero
Copy link
Contributor

When trying to call .process(function) in javascript i'm getting:

[19] Exception in thread "main" TypeError: invokeMember on JavaObject[Route[From[direct:notify] -> [Log[Inventory Notified ${body}]]] (org.apache.camel.model.RouteDefinition)] failed due to: Multiple applicable overloads found for method name process (candidates: [Method[public org.apache.camel.model.ProcessorDefinition org.apache.camel.model.ProcessorDefinition.process(java.util.function.Supplier)], Method[public org.apache.camel.model.ProcessorDefinition org.apache.camel.model.ProcessorDefinition.process(org.apache.camel.Processor)]], arguments: [DynamicObject<JSFunction>@719843e5 (DynamicObjectBasic)])

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Oct 30, 2019

Can you add the full javascript code ? there are some know limitation of the javascript interpreter that need to be workaround.

i.e. you may need to do something like: https://github.com/apache/camel-k-runtime/blob/master/camel-k-loader-js/src/test/resources/routes-with-processors.js

@hguerrero
Copy link
Contributor Author

hguerrero commented Oct 30, 2019

script

c = restConfiguration();
c.setComponent('undertow');
c.setPort('8080');

rest('/')
    .post('/notify/order')
    .to('direct:notify');

from('direct:notify')
    .log('Inventory Notified ${body}')
    .process(processInventory)
    .to('log:info');

function processInventory(e) {
    return JSON.stringify({
        inventoryId: '12345'
    })
}

@lburgazzoli
Copy link
Contributor

you need to wrap it with one of the options shown here

@hguerrero
Copy link
Contributor Author

changed to wrapper, no crash but now looks like function is not updating the body

const Processor = Java.type("org.apache.camel.Processor");
const p = Java.extend(Processor);
const proc = new p(processInventory);

c = restConfiguration();
c.setComponent('undertow');
c.setPort('8080');

rest('/')
    .post('/notify/order')
    .to('direct:notify');

from('direct:notify')
    .log('Inventory Notified ${body}')
    .process(proc)
    .to('log:info');

function processInventory(e) {
    return JSON.stringify({
        inventoryId: '12345'
    })
}

called curl -X POST http://inventory-myproject.192.168.99.106.nip.io/notify/order -v

returned:

*   Trying 192.168.99.106...
* TCP_NODELAY set
* Connected to inventory-myproject.192.168.99.106.nip.io (192.168.99.106) port 80 (#0)
> POST /notify/order HTTP/1.1
> Host: inventory-myproject.192.168.99.106.nip.io
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept: */*
< User-Agent: curl/7.54.0
< Forwarded: for=192.168.99.1;host=inventory-myproject.192.168.99.106.nip.io;proto=http;proto-version=
< Date: Wed, 30 Oct 2019 23:42:25 GMT
< X-Forwarded-Proto: http
< X-Forwarded-Port: 80
< X-Forwarded-For: 192.168.99.1
< Content-Length: 0
< X-Forwarded-Host: inventory-myproject.192.168.99.106.nip.io
< Set-Cookie: e782efb2f1a658a2dcb1288019f96928=c01d8c86fb21c5bb7e6aaad972fb79a4; path=/; HttpOnly
< 
* Connection #0 to host inventory-myproject.192.168.99.106.nip.io left intact

expected: {"inventoryId":"12345"}

@lburgazzoli
Copy link
Contributor

a processor is supposed to change the content of the exchange whereas here your wrapped function does jot alter the exchange but return something that is ignored

@hguerrero
Copy link
Contributor Author

hguerrero commented Oct 30, 2019

Ok, refactored to modify body using:

function processInventory(e) {
    e.getMessage().setBody(
        JSON.stringify({
            flavor: e.getMessage().getBody(),
            inventoryId: '12345'
        })
    )
}

It returns expected value but body is in different format: {"flavor":[104,101,121],"inventoryId":"12345"}

What is the correct way to retrieve the body as String?

@hguerrero
Copy link
Contributor Author

String.fromCharCode.apply(String, e.getMessage().getBody());

does the trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants