Re: Parse xml or yaml snippet into a Processor

2023-09-17 Thread Fyodor Kravchenko

Hello,

Camel internally parses the route files and instantiates route steps as 
Processes. I would like to call this function in my program.


Currently, with xml and Camel 2 I'm doing it with Jaxb, but I would like 
to employ the existing Camel functions that can do it for both xml and 
yaml.


To parse an xml I am wrapping it with some root element and then 
unmarshal it's children with Jaxb.


So we have a String that contains this:


    


The code that creates a Processor from this is this:

    JAXBContext jc = JAXBContext.newInstance(
    "org.apache.camel.model:"
    + "org.apache.camel.model.language:"
    + "org.apache.camel.model.dataformat:"
    + "org.apache.camel.model.rest:"
    + "org.apache.camel.model.transformer:"
    + "org.apache.camel.model.validator:"
    + "org.apache.camel.model.config:"
    + "org.apache.camel.model.loadbalancer:"
    + "org.apache.camel.model.cloud:"
    + "org.apache.camel.model.loadbalancer");
    _unmarshal = jc.createUnmarshaller();

...

    RouteContext rc = new DefaultRouteContext(_context);
    rc.getRoute().setId(.);


    NodeList nl = _convertStringToNodeList(processorsDefinition);
    ArrayList ret = new ArrayList<>();
    for (int i = 0; i < nl.getLength(); i++) {
    if (nl.item(i) instanceof Element) {
    Object def = _unmarshal.unmarshal(nl.item(i));
    if (def instanceof ProcessorDefinition) {
    ProcessorDefinition pd = (ProcessorDefinition) def;
    Processor processor = pd.createProcessor(rc);
    //_context.addService(processor, true);
    ret.add(processor);
    }
    }
    }

    return ret;

.

    private static NodeList _convertStringToNodeList(String xmlStr) { 
// TODO: optimize when all other problems are solved
    String xml = "" + xmlStr 
+ "";
    DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    try {
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new 
StringReader(xml)));
    return 
doc.getElementsByTagName("root").item(0).getChildNodes();

    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }


On 17.09.2023 12:36, Claus Ibsen wrote:

Hi

I dont think we understand your question. Can you try to explain this in
more detail?

On Thu, Sep 7, 2023 at 10:31 AM Fyodor Kravchenko  wrote:


Hello,

what is the reliable way in a Java program to create a Processor from a
String containing a definition of a processor that usually are parts of
the route? Camel 4. Like



  ${body[0]}



or


- unmarshal:
  csv:
delimiter: ";"
useMaps: "true"


Thank you!




Re: InterceptSendToEndpoint and access the intercepted endpoint details?

2023-09-17 Thread Claus Ibsen
Hi

Yes a header with key Exchange.INTERCEPTED_ENDPOINT



On Wed, Sep 6, 2023 at 12:03 PM Mikael Koskinen  wrote:

> Hi,
>
> If I'm intercepting (using InterceptSendToEndpoint) the exchange before it
> is sent to an endpoint, can I somehow access the details of the endpoint
> which I intercepted?
>
> For example if I have Log-component in my route and I use
> InterceptSendToEndpoint to intercept the request, can I somehow check the
> details of the intercepted Log-component (like parameters etc.)?
>
> Thank you in advance.
>
> Best regards,
> Mikael
>


-- 
Claus Ibsen
-
@davsclaus
Camel in Action 2: https://www.manning.com/ibsen2


Re: Parse xml or yaml snippet into a Processor

2023-09-17 Thread Claus Ibsen
Hi

I dont think we understand your question. Can you try to explain this in
more detail?

On Thu, Sep 7, 2023 at 10:31 AM Fyodor Kravchenko  wrote:

> Hello,
>
> what is the reliable way in a Java program to create a Processor from a
> String containing a definition of a processor that usually are parts of
> the route? Camel 4. Like
>
>
> 
>  ${body[0]}
> 
>
>
> or
>
>
> - unmarshal:
>  csv:
>delimiter: ";"
>useMaps: "true"
>
>
> Thank you!
>
>

-- 
Claus Ibsen
-
@davsclaus
Camel in Action 2: https://www.manning.com/ibsen2