You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by gramanero <gr...@gmail.com> on 2012/09/18 00:12:09 UTC

String replacement via Spring

Is it possible to replace characters in a string via Spring DSL? I have a
file path and want to send that path to a resful service in the body;
however slashes in the path are preventing the packet from making it to the
restful service. If I escape the slashes, then the call works just fine. I
need a way to do this via Spring DSL if at all possible. I could try doing
this via Java, but at this point have very little Java experience and
everything we have done has been done in Spring DSL.

Is there anyway that anyone knows how to manipulate strings in Spring DSL?

Thank you.




--
View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: String replacement via Spring

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

I digged into this and have logged a ticket to get this enhanced
https://issues.apache.org/jira/browse/CAMEL-5642



On Wed, Sep 19, 2012 at 9:43 PM, gramanero <gr...@gmail.com> wrote:
> Thank you everyone for helping me work through this issue. I finally figured
> out how to use groovy to solve for it.
>
> This seems to be working for me.
>
>       <transform>
>         <groovy>request.body.replace("\\","\\\\")</groovy>
>       </transform>
>
> Thanks again!
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719623.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: String replacement via Spring

Posted by gramanero <gr...@gmail.com>.
Thank you everyone for helping me work through this issue. I finally figured
out how to use groovy to solve for it.

This seems to be working for me.

      <transform>
        <groovy>request.body.replace("\\","\\\\")</groovy>
      </transform>

Thanks again!




--
View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719623.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: String replacement via Spring

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Sep 19, 2012 at 5:05 PM, gramanero <gr...@gmail.com> wrote:
> Yes, I started to realize that when I received an Ambiguous method call
> exception in Camel. This is what I was trying to refer to when I said that
> there are probably some limitations to the Simple language in that I am
> unable to essentially add a cast to show my intent. For example I am
> assuming something like this is not possible:
>
> transform>
>   <simple>${body.replace((char)'\\',(char)'-')}</simple>
> </transform>
>

The simple language supports specifying types to pin-point a method
when its ambiguous.
As it is in your case. But we haven't yet implement support for also
passing in parameters at the same time.

You can see some details here:
http://camel.apache.org/bean-binding.html

The scripting language such as groovy, mvel is better suited for this



> This would be an attempt to indicate to the Simple language parser that I
> want the char method of the replace API and not the CharSequence method,
> thus trying to eliminate the Ambiguous call exception.
>
> Any ideas why the .replaceAll won't work even though I got a simple java
> code sample working? For example, the following works fine, but in the
> Simple language it throws a java.util.regex.PatternSyntaxException: Illegal
> repetition exception.
>
> class DoubleTheBackslash {
>    public static void main(String[] args) {
>         String path = "E:\\root\\dira\\dirb";
>         System.out.println(path);
>         System.out.println(path.replaceAll("foo","bar"));
>    }
> }
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719603.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: String replacement via Spring

Posted by gramanero <gr...@gmail.com>.
Yes, I started to realize that when I received an Ambiguous method call
exception in Camel. This is what I was trying to refer to when I said that
there are probably some limitations to the Simple language in that I am
unable to essentially add a cast to show my intent. For example I am
assuming something like this is not possible:

transform> 
  <simple>${body.replace((char)'\\',(char)'-')}</simple> 
</transform> 

This would be an attempt to indicate to the Simple language parser that I
want the char method of the replace API and not the CharSequence method,
thus trying to eliminate the Ambiguous call exception.

Any ideas why the .replaceAll won't work even though I got a simple java
code sample working? For example, the following works fine, but in the
Simple language it throws a java.util.regex.PatternSyntaxException: Illegal
repetition exception.

class DoubleTheBackslash {
   public static void main(String[] args) {
        String path = "E:\\root\\dira\\dirb";
        System.out.println(path);
        System.out.println(path.replaceAll("foo","bar"));
   }    
}





--
View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719603.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: String replacement via Spring

Posted by James Carman <ja...@carmanconsulting.com>.
There is a new method:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(java.lang.CharSequence,
java.lang.CharSequence)

On Wed, Sep 19, 2012 at 9:49 AM, Claus Ibsen <cl...@gmail.com> wrote:
> The replace method on java.lang.String accepts only char as
> parameters, and not another string.
> Hence why it wont work.
>
> eg "foo" is not a char.
>
>
> On Wed, Sep 19, 2012 at 3:30 PM, gramanero <gr...@gmail.com> wrote:
>> Hi Henryk,
>>
>> I apologize for not providing more information. I guess I thought there was
>> enough context within the thread that one could understand what I was
>> referring to. Sounds like that was not the case.
>>
>> The two attempts I made using Spring are as follows:
>>
>> What Claus suggested:
>> <transform>
>>   <simple>${body.replaceAll("foo", "bar")}</simple>
>> </transform>
>>
>> Same thing as what Claus suggested with the exception of using .replace
>> instead of .replaceAll.
>> <transform>
>>   <simple>${body.replace("foo", "bar")}</simple>
>> </transform>
>>
>> The replacement strings that I am using in the example above are not really
>> what I want in the end, but I wanted to just get anything working at this
>> point. The point of my last response is that neither solution appears to be
>> behaving as I would expect, but again I am wondering if this just isn't
>> something that the Simple language can handle. I would like to know if the
>> errors I posted earlier are expected or if there is some other underlying
>> problem with the Simple language parser.
>>
>> The real transform that I am trying to get to is something like this:
>> <transform>
>>   <simple>${body.replace("\\", "\\\\")}</simple>
>> </transform>
>>
>> I just need to replace all single backslashes in a windows-based fully
>> qualified file path with double backslashes such that I can send the JSON
>> packet to a restful service. Right now, the request is failing to send to
>> the .NET restful service due to the backslashes in the file path. If I
>> manually change the single backslashes to double backslashes, the JSON
>> packet is accepted and processed by the restful service. I also thought
>> about using headers to send the information, but this doesn't seem like a
>> "restful"way of doing things.
>>
>> Another approach would be to URL encode the file path, but I can't seem to
>> find a way to do that in Spring either. I am very new to Camel, so I am just
>> hoping that I am missing something simple.
>>
>> Thank you for the response and sorry for not being clear enough in my
>> previous post.
>>
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719590.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen

Re: String replacement via Spring

Posted by Claus Ibsen <cl...@gmail.com>.
The replace method on java.lang.String accepts only char as
parameters, and not another string.
Hence why it wont work.

eg "foo" is not a char.


On Wed, Sep 19, 2012 at 3:30 PM, gramanero <gr...@gmail.com> wrote:
> Hi Henryk,
>
> I apologize for not providing more information. I guess I thought there was
> enough context within the thread that one could understand what I was
> referring to. Sounds like that was not the case.
>
> The two attempts I made using Spring are as follows:
>
> What Claus suggested:
> <transform>
>   <simple>${body.replaceAll("foo", "bar")}</simple>
> </transform>
>
> Same thing as what Claus suggested with the exception of using .replace
> instead of .replaceAll.
> <transform>
>   <simple>${body.replace("foo", "bar")}</simple>
> </transform>
>
> The replacement strings that I am using in the example above are not really
> what I want in the end, but I wanted to just get anything working at this
> point. The point of my last response is that neither solution appears to be
> behaving as I would expect, but again I am wondering if this just isn't
> something that the Simple language can handle. I would like to know if the
> errors I posted earlier are expected or if there is some other underlying
> problem with the Simple language parser.
>
> The real transform that I am trying to get to is something like this:
> <transform>
>   <simple>${body.replace("\\", "\\\\")}</simple>
> </transform>
>
> I just need to replace all single backslashes in a windows-based fully
> qualified file path with double backslashes such that I can send the JSON
> packet to a restful service. Right now, the request is failing to send to
> the .NET restful service due to the backslashes in the file path. If I
> manually change the single backslashes to double backslashes, the JSON
> packet is accepted and processed by the restful service. I also thought
> about using headers to send the information, but this doesn't seem like a
> "restful"way of doing things.
>
> Another approach would be to URL encode the file path, but I can't seem to
> find a way to do that in Spring either. I am very new to Camel, so I am just
> hoping that I am missing something simple.
>
> Thank you for the response and sorry for not being clear enough in my
> previous post.
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719590.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: String replacement via Spring

Posted by gramanero <gr...@gmail.com>.
Hi Henryk,

I apologize for not providing more information. I guess I thought there was
enough context within the thread that one could understand what I was
referring to. Sounds like that was not the case.

The two attempts I made using Spring are as follows:

What Claus suggested:
<transform> 
  <simple>${body.replaceAll("foo", "bar")}</simple> 
</transform> 

Same thing as what Claus suggested with the exception of using .replace
instead of .replaceAll.
<transform> 
  <simple>${body.replace("foo", "bar")}</simple> 
</transform> 

The replacement strings that I am using in the example above are not really
what I want in the end, but I wanted to just get anything working at this
point. The point of my last response is that neither solution appears to be
behaving as I would expect, but again I am wondering if this just isn't
something that the Simple language can handle. I would like to know if the
errors I posted earlier are expected or if there is some other underlying
problem with the Simple language parser.

The real transform that I am trying to get to is something like this:
<transform> 
  <simple>${body.replace("\\", "\\\\")}</simple> 
</transform>

I just need to replace all single backslashes in a windows-based fully
qualified file path with double backslashes such that I can send the JSON
packet to a restful service. Right now, the request is failing to send to
the .NET restful service due to the backslashes in the file path. If I
manually change the single backslashes to double backslashes, the JSON
packet is accepted and processed by the restful service. I also thought
about using headers to send the information, but this doesn't seem like a
"restful"way of doing things. 

Another approach would be to URL encode the file path, but I can't seem to
find a way to do that in Spring either. I am very new to Camel, so I am just
hoping that I am missing something simple.

Thank you for the response and sorry for not being clear enough in my
previous post.




--
View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719590.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: String replacement via Spring

Posted by Henryk Konsek <he...@gmail.com>.
> I am guessing that I am hitting some limitations of the Simple language?
> Both .replaceAll and .replace seems to work fine when put into a simple Java
> program.

Please next time provide us the minimal example demonstrating the issue.

For example here is the minimal example demonstrating that Simple
language can handle String#replaceAll . :)

        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:test").
                        setBody(simple("body.replaceAll(\"replace
me\",\"replaced\")")).
                        to("mock:test");
            }
        });
        camelContext.start();
        camelContext.createProducerTemplate().sendBody("direct:test",
"replace me");
        MockEndpoint mockEndpoint =
camelContext.getEndpoint("mock:test", MockEndpoint.class);
        String replacedMessage =
mockEndpoint.getExchanges().get(0).getIn().getBody(String.class);
        assertEquals("replaced", replacedMessage);

Regards.

-- 
Henryk Konsek
http://henryk-konsek.blogspot.com

Re: String replacement via Spring

Posted by gramanero <gr...@gmail.com>.
Thank you for the response Claus. Cool that this is possible. I have been
trying to work with the Simple language in order to not have to dive into
the other scripting languages. I have had mixed results, none of which have
actually worked yet.

If I just take your example verbatim, then I getting the following error:
/Failed delivery for exchangeId: ID-WMORMANS-63776-1347901580693-114-1.
Exhausted after delivery attempt: 1 caught:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to
invoke method: .replaceAll("foo","bar") on null due to:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to
invoke method: replaceAll("foo","bar") on null due to:
java.util.regex.PatternSyntaxException: Illegal repetition

{"FilePath":"E:\Publishing\NewDataReceived\ReferenceData\Ford","FileName":"TestFile.txt","FileSize":"14"}/

I also tried using the .replace() method, but that throws this error:
/Failed delivery for exchangeId: ID-WMORMANS-63776-1347901580693-116-1.
Exhausted after delivery attempt: 1 caught:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to
invoke method: .replace("foo","bar") on null due to:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to
invoke method: replace("foo","bar") on null due to:
org.apache.camel.component.bean.AmbiguousMethodCallException: Ambiguous
method invocations possible: [public java.lang.String
java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence),
public java.lang.String java.lang.String.replace(char,char)].
Exchange[Message:
{"FilePath":"E:\Publishing\NewDataReceived\ReferenceData\Ford","FileName":"TestFile.txt","FileSize":"14"}]/

I am guessing that I am hitting some limitations of the Simple language?
Both .replaceAll and .replace seems to work fine when put into a simple Java
program.




--
View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498p5719540.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: String replacement via Spring

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You can do a message transformation, and use some of the scripting
languages to invoke methods that can do string replacement etc.
http://camel.apache.org/languages

For example groovy etc.

The simple language may also be able to do that if the message body is
already a String, using the replaceAll method from String itself. But
its using regexp for the replacement, so you can try at first in an
unit test or a java app to make the replaceAll code as you want.

<transform>
  <simple>${body.replaceAll("foo", "bar")}</simple>
</transform>


On Tue, Sep 18, 2012 at 12:12 AM, gramanero <gr...@gmail.com> wrote:
> Is it possible to replace characters in a string via Spring DSL? I have a
> file path and want to send that path to a resful service in the body;
> however slashes in the path are preventing the packet from making it to the
> restful service. If I escape the slashes, then the call works just fine. I
> need a way to do this via Spring DSL if at all possible. I could try doing
> this via Java, but at this point have very little Java experience and
> everything we have done has been done in Spring DSL.
>
> Is there anyway that anyone knows how to manipulate strings in Spring DSL?
>
> Thank you.
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/String-replacement-via-Spring-tp5719498.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen