how can I use uuid
<bean id="uuid" class="org.apache.camel.impl.SimpleUuidGenerator" />
on the move option on Camel File route ? in order to add a uuid string on the file name when moving it
The ExchangeId is a UUID, so you can simply reference the '${exchangeId}' from Simple language without having to mess with setting up the bean.
If you really want to use your own Uuid generator, reference the bean name in the parameter:
${bean:uuid.generateid}
Where 'uuid' is your bean id and generateid is the method name example here: File language
Related
I am trying to add the new column and row/content to the existing CSV data but unable to achieve this in Apache Camel
I have used camel-csv component in the code and below is the snippet for the same.
<unmarshal>
<csv delimiter="|" useMaps="true" lazyLoad="true" />
</unmarshal>
When unmarshalling, getting "org.apache.camel.dataformat.csv.CsvUnmarshaller$CsvIterator" as class name but unable to get the exchange or cast to any type to this class as this is abstract class.
Let me know if we can use bean component and solution to add the column and content to the existing CSV data.
I can suggest an alternative solution. You can use BeanIO Data Format.
E.g :
BeanIODataFormat dataFormat = new BeanIODataFormat("classpath:beanio/mappings.xml", "ContactsCSV");
from("direct:convert-to-csv")
.marshal(dataFormat)
.to("file:xxxx")
.end();
You can find check data format details in docs. There is an examples file in there as well.
I have a problem reading different fileName from Camel file component.
from("file:/in?fileName={{property.name}}")
.to(file:/out)
I used fileName={{property.name}} from application.yml, but I need to use it from String.
Is there any way to use it like:
String name = "blabla.xml";
from("file:/in?fileName=${name}")
.to(file:/out)
Camel don't support it. String concatenation can solve your problem:
from("file:/in?fileName="+name)
or you can set a property and then read it:
String name="name";
from("direct:start")
.setProperty("name",constant(name))
.to("file:/in?fileName=${exchangeProperty.name}");
Tried to find a solution, but I got always stuck a the docs or at answers include other bundles. In the documentation of the dynamic router you can find the hint:
"Of course you can also have several parameters, as with normal Symfony routes. The semantics and rules for patterns, defaults and requirements are exactly the same as in core routes."
Thats it.
...
/foo/{id}/bar
I tried (seems not) everything to get it done.
Same for all tries:
I tried it to apply a variable pattern and a child route.
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route as PhpcrRoute;
$dm = $this->get('cmf_routing.route_provider');
$route = new PhpcrRoute();
$route->setPosition( $dm->find( null, '/cms/routes' ), 'foo' );
$route->setVariablePattern('/{id}');
$dm->persist( $route );
$child = new PhpcrRoute();
$child->setPosition( $route, 'bar' );
$dm->persist( $child );
$dm->flush();
With or without default value and requirement only '/foo/bar' and '/foo/*' return matches, but '/foo/1/bar' prompts me with a 'No route found for "GET /foo/1/bar"'.
...
Just now I nearly got it done.
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route as PhpcrRoute;
$dm = $this->get('cmf_routing.route_provider');
$route = new PhpcrRoute();
$route->setPosition( $dm->find( null, '/cms/routes' ), 'example_route' );
$dm->persist( $route );
$route->setPrefix( '/cms/routes/example_route' );
$route->setPath( '/foo/{id}/bar' );
$dm->flush();
If prefix is '/cms/routes' and name is 'foo' everything works fine. But now that I got this far, assigning a speaking name would round it up.
Thanks in advice!
You got quite close to the solution, actually!
When using PHPCR-ODM, the route document id is its path in the repository. PHPCR stores all content in a tree, so every document needs to be in a specific place in the tree. We then use the prefix to get a URL to match. If the prefix is configured as /cms/routes and the request is for /foo, the router looks in /cms/routes/foo. To allow parameters, you can use setVariablePattern as you correctly assumed. For the use case of /foo/{id}/bar, you need to do setVariablePattern('/{id}/bar'). You could also have setVariablePattern('/{context}/{id}') (this is what the doc paragraph you quoted meant - i will look into adding an example there as its indeed not helpful to say "you can do this" but not explain how to).
Calling setPath is not recommended as its just less explicit - but as you noticed, it would get the job done. See the phpdoc and implementation of Model\Route::setPattern:
/**
* It is recommended to use setVariablePattern to just set the part after
* the static part. If you use this method, it will ensure that the
* static part is not changed and only change the variable part.
*
* When using PHPCR-ODM, make sure to persist the route before calling this
* to have the id field initialized.
*/
public function setPath($pattern)
{
$len = strlen($this->getStaticPrefix());
if (strncmp($this->getStaticPrefix(), $pattern, $len)) {
throw new \InvalidArgumentException('You can not set a pattern for the route that does not start with its current static prefix. First update the static prefix or directly use setVariablePattern.');
}
return $this->setVariablePattern(substr($pattern, $len));
}
About explicit names: The repository path is also the name of the route, in the example /cms/routes/foo. But it is not a good idea to use a route name of a dynamic route in your code, as those routes are supposed to be editable (and deletable) by an admin. If you have a route that exists for sure and is at a specific path, use the configured symfony routes (the routing.yml file). If its dynamic routes, have a look at the CMF Resource Bundle. It allows to define a role for a document and a way to look up documents by role. If you have a route with a specific role that you want to link to from your controller / template, this is the way to go. If you have a content document that is linked with a route document and have that content document available, your third and best option is to generate the URL from the content document. The CMF dynamic router can do that, just use the content object where you normally specify the route name.
If in Yii 1.1.x there is some extension, to create valid file system(linux) name from object title with unique name, like a slug, but with unique name in given directory ?
I mean I want to save file on disk with name based on title of object.
Thanks!
Actually I would like a bit more :
1) In title of object all illigel chars(for Linux/windows OS) and space must be converted to '-'
2) if there is such file add valid counter for it , like "_45"
Maybe that not yii extension, but some php function.
The simplest way - you can add object id to prefix of file. Then your file names always be uniqueness. For example:
1-title-first
2-title-first
3-title-second
you can use php own uniqueid() to generate unique ids!
$title = uniqid('wp_'); // have wp_ prefixed for title
Is it possible to set multiple exchange headers in camel route using single call to setHeader.
Something like this
<from uri="file://inputdir/?delete=true" />
<!-- need to set multiple headers at once(as a comma separated list)-->
<setHeader headerName="headername">
<constant>headerval</constant>
</setHeader>
<to uri="mock:end"/>
Or should I create a custom processor for this?
No you cant set multiple headers at once as far as I am aware. In your case a custom processor will probably be more effective.
You might want to look at the simple expression language here here is that you can OGNL notation in your camel route xml file. OGNL will allow you to specify a chain of methods in the expression.
For example suppose you have a message that contains an Employee object that has a getSalaryGrade() method you could set a header to this value by using the following syntax:
<setHeader headerName="SalaryGrade">
<simple>${body.getSalaryGrade()}</simple>
</setHeader>
You could for example create a simple class that returns a list and store the list in the header and then access the list via simple in the route. The following code is untested but should give you a idea.
public class ListCity {
public List<String> ListCities()
{
ArrayList< String> list = new ArrayList<String>();
list.add("New York");
list.add("JOhannesburg");
list.add("HoChiMinh");
return list;
}
}
Declare the list city bean in your xml. You can then set this list into a header by using something like this:
<setHeader headerName="CityList">
<simple>${listCity.ListCities()}</simple>
</setHeader>