4 Replies Latest reply on Nov 19, 2010 12:13 PM by charlyjboss

    Check if header element exists in message Response.

    charlyjboss

      Hi everyone, i'm using this platform for the first time, so maybe my question is very simple because i don't understand how it works, but i can't find the right way to solve this problem.

       

      The situation:

      I need to check if a header element exist in the reponse message, if it doesn't, i need to add one.

      At first i try to solve this problem writing a xsl transformation with smooks, i check the entire message (selector="$document") and copy every element and adding a header if it wasn't present. It worked at first, but when i try to process messages with very large body, i keep geting timeouts because takes too much time to process.

      So basically my question is, what is the right way to do this (check if the header element exists and add one if it doesn't)? it seems to be a common situation, but i couldn't find the right answer anywhere.

       

      Thanks in advance.

       

      PS. Sorry for my English

        • 1. Re: Check if header element exists in message Response.
          charlyjboss

          I think this question is not going to be answered, what i need to know is the reason. Is it not well written or do i need to add more details?.

          To sum up, i need to know if given a message there is a way to check whether an element exists or not (with namespace included) and add it myself if it isn't included.

           

          Anything would help.

           

          Thanks.

          • 2. Re: Check if header element exists in message Response.
            tfennelly

            Hi Carlos... I got your private message... here's the answer...

             

            Sounds like you have 2 issues... handling a large/huge message and conditionally adding a header element.

             

            Handle the huge message by setting th filter type to SAX (default is DOM => can't handle big messages)  See User Guide.

             

            For conditionally adding the header... in the versions of Smooks in the ESB (easier in newer versions of Smooks)... easiest way is prob to do something like the following...

             

            <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"

                                                xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd">

             

                <jb:bean beanId="headerDetails" class="java.util.HashMap" createOnElement="order/header">

                   <jb:value property="secureId" data="order/header/secureId" />

                <jb:bean>

             

                <ftl:freemarker applyOnElement="order/header">

                    <condition>isdef headerDetails.secureId</condition>

                    <ftl:template><!-- <secureId>blah</secureId> --></ftl:template>

                    <ftl:use>

                        <ftl:inline directive="addto" />

                    </ftl:use>

                </ftl:freemarker>

             

            </smooks-resource-list>

             

            So.. capture details about the data in the header into a HasMap and then add a conditional on a template (having an "addto" directive) that's applied to the header fragment.

            • 3. Re: Check if header element exists in message Response.
              tfennelly

              Oh and... make sure to check the MVEL user docs re the use of "isdef" in the conditional... what I have there might not be 100% right, but it would be something like that.

              • 4. Re: Check if header element exists in message Response.
                charlyjboss

                Thanks Tom, i'm trying to use your solution, but that code works for an element inside the header right? In my case, the whole header may not be present (<header>...<header>) at all, for example, could be like this:

                 

                <Envelope>

                   <Body>

                   ...
                   </Body>

                </Envelope>

                 

                So, the result message after the transformation is:

                 

                <Envelope>

                   <Body>

                   ...
                    </Body>

                   <Header>

                   ...

                   </Header>

                </Envelope>

                 

                Because the "add" directive locates the element at the end of the selector (Envelope in this case)

                Is it possible to modify a little bit your solution to make it works for my particular scenario?

                 

                Thank you very much.