Camel Exchange Body URL

I need help with this.

I am using the camel-http component as shown here , but I am having problems because the body that I am sending has unescaped ampersands. This results in the query string on the receiving server splitting the message into several message parameters.

I know that I can create compiled routes in java, but I have to use the spring xml dialect so that new routes can be created / modified in configuration files without recompilation.

So, in short, I would like to URL Encode the $ {body} property on my route using the spring dialect, as shown below (obviously invalid) pseudo-code below.

<setBody inheritErrorHandler="true" id="setBody2"> <simple>name=<urlencode>${body}</urlencode></simple> </setBody> 
+4
source share
2 answers

Ok, I bit the bullet. I created java POJO

 package com.wufoo.camel; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.log4j.Logger; public class PayloadEncoder { public String getEncodedBody(String body) throws UnsupportedEncodingException { Logger mylogger = Logger.getLogger("log4j.logger.org.apache.camel"); mylogger.info("Appending payload and URL Encoding"); String encodedBody = new StringBuffer() .append("payload=") .append(URLEncoder.encode(body, "UTF-8")).toString(); return encodedBody; } } 

Then introduced it into context

 <bean id="payloadEncoder" class="com.wufoo.camel.PayloadEncoder" /> 

And finally, a transform is used to encode the body

 <transform> <method bean="payloadEncoder" method="getEncodedBody"/> </transform> 

It works. If someone tells me what is wrong with this approach, please let me know.

+2
source

You can also use groovy language, for example:

 <?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="file:camel/input"/> <log message="Moving ${file:name} to the output directory"/> <setBody> <groovy> "name=" + URLEncoder.encode(request.getBody(String.class)); </groovy> </setBody> <to uri="file:camel/output"/> </route> </camelContext> </blueprint> 
+2
source

Source: https://habr.com/ru/post/1334974/


All Articles