camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ronny Aerts <ronny.ae...@intris.onmicrosoft.com>
Subject RE: looking for a way to run javascript in camel and set message headers
Date Sat, 11 Jan 2014 12:14:00 GMT
Hello camel community,

I found my soluation in the language component (http://camel.apache.org/language.html) after
doing some more research.

My routes is now:
    <route id="test-javascript">
        <from uri="timer://runOnce?repeatCount=1&amp;delay=1000"/>
        <log message="test bericht van de ronny" loggingLevel="INFO"/>
        <setBody><constant>dit is een test bericht van Ronny Aerts</constant></setBody>
        <to uri="language://javascript:classpath:test-javascript.js?transform=false" />
        <to uri="log:javascript?showAll=true"/>
    </route>

And the javascript content is:
var content = request.getBody().toString().toUpperCase();
if (content.match("^UN[AB][+]{1}.*") != null) {
request.setHeader("datatype", "edifact");
} else {
request.setHeader("datatype", "xml");
}
java.lang.System.out.println ("Dit is output vanuit de javascript");
java.lang.System.out.println ("Komt de 2de lijn na de eerste?");

var logger = org.slf4j.LoggerFactory.getLogger(exchange.getFromRouteId());
logger.info ("test logging");
logger.info ("Camel Name {}", camelContext.getName());
logger.info("Camel Status {}", camelContext.getStatus().toString());
logger.info ("Camel Uptime {}", camelContext.getUptime());
logger.info ("Camel Version {}", camelContext.getVersion());
logger.info ("FromRouteId {}", exchange.getFromRouteId());
logger.info ("ExchangeId {}", exchange.getExchangeId());
logger.info ("FromEndpointUri {}", exchange.getFromEndpoint().getEndpointUri());

context.getWriter().println("output from script context");

properties = exchange.getProperties();
if (properties.size() > 0) {
logger.info("there are {} properties in the exchange", properties.size());
propertyNames = properties.keySet().iterator();
while (propertyNames.hasNext()) {
  propertyName = propertyNames.next();
  propertyValue = properties.get(propertyName);

  logger.info ("exchange property '{}' has value '{}' of type '{}'", propertyName, propertyValue,
propertyValue.getClass().getName());
}
} else {
logger.warn("the exchange does not have any properties!");
}

headers = request.getHeaders();
if (headers.size() > 0) {
logger.info("there are {} headers in the message", headers.size());
headerNames = headers.keySet().iterator();
while (headerNames.hasNext()) {
  headerName = headerNames.next();
  headerValue = headers.get(headerName);

  logger.info ("msg header '{}' has value '{}' of type '{}'", headerName, headerValue, headerValue.getClass().getName());
}
} else {
logger.warn("the message does not have any headers!");
}

This proceduces:
2014-01-11 13:12:25.226 [INFO ] [org.apache.camel.spring.SpringCamelContext] Total 1 routes,
of which 1 is started.
2014-01-11 13:12:25.228 [INFO ] [org.apache.camel.spring.SpringCamelContext] Apache Camel
2.12.2 (CamelContext: TrisESB-intris-test-rrts2) started in 0.366 seconds
2014-01-11 13:12:26.224 [INFO ] [test-javascript] test bericht van de ronny
Dit is output vanuit de javascript
Komt de 2de lijn na de eerste?
2014-01-11 13:12:26.282 [INFO ] [test-javascript] test logging
2014-01-11 13:12:26.286 [INFO ] [test-javascript] Camel Name TrisESB-intris-test-rrts2
2014-01-11 13:12:26.287 [INFO ] [test-javascript] Camel Status Started
2014-01-11 13:12:26.288 [INFO ] [test-javascript] Camel Uptime 1.427 seconds
2014-01-11 13:12:26.288 [INFO ] [test-javascript] Camel Version 2.12.2
2014-01-11 13:12:26.288 [INFO ] [test-javascript] FromRouteId test-javascript
2014-01-11 13:12:26.288 [INFO ] [test-javascript] ExchangeId ID-INTRISLPT48-56623-1389442344536-0-2
2014-01-11 13:12:26.289 [INFO ] [test-javascript] FromEndpointUri timer://runOnce?delay=1000&repeatCount=1
output from script context
2014-01-11 13:12:26.290 [INFO ] [test-javascript] there are 7.0 properties in the exchange
2014-01-11 13:12:26.292 [INFO ] [test-javascript] exchange property 'CamelToEndpoint' has
value 'language://javascript:classpath:test-javascript.js?transform=false' of type 'java.lang.String'
2014-01-11 13:12:26.294 [INFO ] [test-javascript] exchange property 'CamelMessageHistory'
has value '[DefaultMessageHistory[routeId=test-javascript, node=log1], DefaultMessageHistory[routeId=test-javascript,
node=setBody1], DefaultMessageHistory[routeId=test-javascript, node=to1]]' of type 'java.util.ArrayList'
2014-01-11 13:12:26.295 [INFO ] [test-javascript] exchange property 'CamelCreatedTimestamp'
has value 'Sat Jan 11 13:12:26 CET 2014' of type 'java.util.Date'
2014-01-11 13:12:26.295 [INFO ] [test-javascript] exchange property 'CamelTimerFiredTime'
has value 'Sat Jan 11 13:12:26 CET 2014' of type 'java.util.Date'
2014-01-11 13:12:26.295 [INFO ] [test-javascript] exchange property 'CamelTimerPeriod' has
value '1000' of type 'java.lang.Long'
2014-01-11 13:12:26.296 [INFO ] [test-javascript] exchange property 'CamelTimerName' has value
'runOnce' of type 'java.lang.String'
2014-01-11 13:12:26.296 [INFO ] [test-javascript] exchange property 'CamelTimerCounter' has
value '1' of type 'java.lang.Long'
2014-01-11 13:12:26.296 [INFO ] [test-javascript] there are 3.0 headers in the message
2014-01-11 13:12:26.297 [INFO ] [test-javascript] msg header 'breadcrumbid' has value 'ID-INTRISLPT48-56623-1389442344536-0-1'
of type 'java.lang.String'
2014-01-11 13:12:26.297 [INFO ] [test-javascript] msg header 'firedtime' has value 'Sat Jan
11 13:12:26 CET 2014' of type 'java.util.Date'
2014-01-11 13:12:26.297 [INFO ] [test-javascript] msg header 'datatype' has value 'xml' of
type 'java.lang.String'
2014-01-11 13:12:26.300 [INFO ] [javascript] Exchange[Id: ID-INTRISLPT48-56623-1389442344536-0-2,
ExchangePattern: InOnly, Properties: {CamelCreatedTimestamp=Sat Jan 11 13:12:26 CET 2014,
CamelMessageHistory=[DefaultMessageHistory[routeId=test-javascript, node=log1], DefaultMessageHistory[routeId=test-javascript,
node=setBody1], DefaultMessageHistory[routeId=test-javascript, node=to1], DefaultMessageHistory[routeId=test-javascript,
node=to2]], CamelTimerCounter=1, CamelTimerFiredTime=Sat Jan 11 13:12:26 CET 2014, CamelTimerName=runOnce,
CamelTimerPeriod=1000, CamelToEndpoint=log://javascript?showAll=true}, Headers: {breadcrumbId=ID-INTRISLPT48-56623-1389442344536-0-1,
datatype=xml, firedTime=Sat Jan 11 13:12:26 CET 2014}, BodyType: String, Body: dit is een
test bericht van Ronny Aerts, Out: null: ]

And that is exactly what I need.

--
vriendelijke groeten,
Ronny Aerts - Intris nv - Wapenstilstandlaan 47, 2600 Berchem, Belgiƫ
R&D Integration Architect
Prince II certified
Tel: +32-3-326.50.75

-----Original Message-----
From: Ronny Aerts [mailto:ronny.aerts@intris.be]
Sent: vrijdag 10 januari 2014 20:05
To: users@camel.apache.org
Subject: looking for a way to run javascript in camel and set message headers

Hello camel community,

I'm trying to figure it a way to run a real javascript procedures in camel as an alternative
to use java code out of a bean.

My company is using camel routes from a spring dsl xml file and we would like to minimize
our java coding effort. I know that javascript can be used in expressions, filters, ... but
I can't figure out to more with it like:
<route>
                <javaScript>
                               var content = request.getBody().toString().toUpperCase();
if (match("^UN[AB][+]{1}.*") != null) {
                request.headers.set("datatype", "edifact"); } else { request.headers.set("datatype",
"xml"); }
                </javaScript>
</route>

Please don't comment on the content of the javascript because this is just a very simple example.
Things that I surely want to do is setting message (and attachment) headers.

The fact that javascript can be loading from an external file (since camel 2.11) is already
of much help.

--
kind regards,
Ronny Aerts - Belgium

Intris nv
Wapenstilstandlaan 47
B-2600 Berchem
        Tel.  +32 3 326 50 75
Fax  +32 3 326 42 23
www.intris.be<http://www.intris.be/>

DISCLAIMER
This is an e-mail from Intris. The information contained in this communication is intended
solely for use by the individual or entity to whom it is addressed.
Use of this communication by others is prohibited. If the e-mail message was sent to you by
mistake, please notify support@intris.be<mailto:support@intris.be>, destroy it without
reading, using, copying or disclosing its contents to any other person.
We accept no liability for damage related to data and/or documents which are communicated
by electronic mail.
Intris nv
Wapenstilstandlaan 47
B-2600 Berchem
        Tel.  +32 3 326 50 75
Fax  +32 3 326 42 23
www.intris.be<http://www.intris.be/>

DISCLAIMER
This is an e-mail from Intris. The information contained in this communication is intended
solely for use by the individual or entity to whom it is addressed.
Use of this communication by others is prohibited. If the e-mail message was sent to you by
mistake, please notify support@intris.be<mailto:support@intris.be>, destroy it without
reading, using, copying or disclosing its contents to any other person.
We accept no liability for damage related to data and/or documents which are communicated
by electronic mail.

Mime
View raw message