<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Engwar &#187; wsf/perl</title>
	<atom:link href="http://engwar.com/tags/wsfperl/feed" rel="self" type="application/rss+xml" />
	<link>http://engwar.com</link>
	<description>Chintana Wilamuna&#039;s weblog</description>
	<lastBuildDate>Sat, 03 Jul 2010 08:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Pissing people off</title>
		<link>http://engwar.com/post/50</link>
		<comments>http://engwar.com/post/50#comments</comments>
		<pubDate>Tue, 26 Aug 2008 18:02:49 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[wsf/perl]]></category>
		<category><![CDATA[wsf/ruby]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=50</guid>
		<description><![CDATA[From time to time, if you feel the need to piss someone off for their own good, let me assure you that it&#8217;s a perfectly natural thing. If you see someone living their lives without getting pissed off even for few minutes, do them a favour by pissing them off, at least once. Just like [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, if you feel the need to piss someone off for their own good, let me assure you that it&#8217;s a perfectly natural thing. If you see someone living their lives without getting pissed off even for few minutes, do them a favour by pissing them off, at least once. Just like <a href="http://en.wikipedia.org/wiki/Eustress">eustress</a> people need eupiss for a long healthy life. Besides, it shakes things a little bit which helps people to bring some variety to their monotonous existence. So, if the conventional wisdom has shown you that pissing people off is bad, well, it&#8217;s because it&#8217;s conventional. Make a note on your todo lists, or your <a href="http://en.wikipedia.org/wiki/Getting_Things_Done">GTD</a> system and make it an items that you could do in less than 2 minutes. Then do it. After all it&#8217;s for their own good.</p>
<p>Now, let me show you a very easy method to piss someone off. This works extremely well if the pissee is a tech guy. More specifically a programmer. It&#8217;s a well tested and trustworthy method which people used in mailing lists from time to time to start holy wars. This method is most successful if the pissee uses a <a href="http://java.sun.com/">big stupid</a> language and actually LIKE it. All you have to do is utter &#8220;my language is better than you one&#8221; phrase in a slightly offensive tone and throw an example on their face. It&#8217;s that simple. Lemme give you one example.</p>
<p>Artificial examples on blogs sucks. So let me take some code which I was playing with today and explain.</p>
<p>I was writing some Ruby code this afternoon with <a href="http://wso2.org/projects/wsf/ruby">WSF/Ruby</a>. In order to talk to an SSL endpoint you need to supply the CA cert to the client as an option. <a href="http://ws.apache.org/rampart/c/">Rampart</a> expects you give the cert with the certificate delimiters removed (first and the last line of a certificate). <a href="http://wso2.org/projects/wsf/perl">WSF/Perl</a> will read the certificate for you and do the right thing when you pass the filename. But not WSF/Ruby (note to self: make this part of the lib).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">client = WSClient.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> 
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;to&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/your/end/point&quot;</span>,
    <span style="color:#996600;">&quot;ca_cert&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;cert content as a string&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, logfile <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The beauty of the language you&#8217;re using spring up in situations like this. Without further ado, here&#8217;s the one liner to get the cert content as a string with certificate delimiters removed,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'server.cert'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">readlines</span><span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\n<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>You got to appreciate the beauty of the above line. Although I&#8217;m quite fond of it I prefer the Python version over it. Here&#8217;s one way to do it,</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;server.cert&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:-<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Yes, the list comprehension beats it all. It&#8217;s simple and elegant. Most importantly beautiful and pleasing to look at. My first exposure to list comprehension has been via <a href="http://erlang.org/">Erlang</a>. Purely due to the fact that I started reading about it before Python.</p>
<p>Anyhoo, all you have to do now is find someone who uses a big stupid language and throw an example like above to their face and tell them to beat it. See if they can write it more elegantly using their language. The beauty of the trick is that there&#8217;s no way in this world that&#8217;s gonna happen. So, you&#8217;ve already won. One word of caution though. When taking an example take something slightly more complex than a single function call. Like opening a file. In both Ruby and Python it&#8217;s one function call. DON&#8217;T take this type of examples. Why? Because there&#8217;s a very high probability that the other person, yes, the one who&#8217;s using a big stupid language, will get so angry and beat you up. You don&#8217;t want this. So, avoid at all costs.</p>
<p>There you go. Do some good to the world by pissing off few people with this technique. It&#8217;s good for your karma too. Happy pissing!</p>
<p><strong>Update:</strong> Ok, I admit that I got a bit carried away with that example which made it artificial, hence sucked. When you write an SSL client using WSF/Ruby you only have to give the filename of the certificate, none of the certificate delimiter removal plus newline removal is necessary. But, I&#8217;ll keep the examples to keep the central theme in alignment. BTW, here&#8217;s the correct SSL client example with WSF/Ruby,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">client = WSClient.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;to&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>  <span style="color:#996600;">&quot;/your/end/point&quot;</span>,
    <span style="color:#996600;">&quot;ca_cert&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;server.cert&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, logfile <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>Update 2</strong>: Before it gets a bit out of hand, let me reiterate if it was not evident from the first reading. This whole thing was meant to be a joke. Don&#8217;t take it too seriously, have a laugh and return to you editors, grow some sense of humour for chrissake. Oh, one more thing, I&#8217;m no Erlang wiz. I&#8217;m just started to grok the Erlang landscape.</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/50/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Perl embedding woes</title>
		<link>http://engwar.com/post/40</link>
		<comments>http://engwar.com/post/40#comments</comments>
		<pubDate>Thu, 03 Jul 2008 14:33:55 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[mod_perl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/c]]></category>
		<category><![CDATA[wsf/perl]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=40</guid>
		<description><![CDATA[While developing server side components that enables providing Web services using Perl bumped into a brick wall. First the requirement is when a client request comes, need to extract the payload and pass it to WSF/C which acts as the underlying Web services engine. So WSF/C knows how to process the incoming XML payload and [...]]]></description>
			<content:encoded><![CDATA[<p>While developing server side components that enables providing Web services using Perl bumped into a brick wall.  First the requirement is when a client request comes, need to extract the payload and pass it to WSF/C which acts as the underlying Web services engine.  So WSF/C knows how to process the incoming XML payload and do what it does and return another XML payload which is then passed back to the client who send the request.  And the business logic is written using Perl.  Simple, I know.</p>
<p>We used SWIG to generate some wrapper functions (plus some custom written C code) which belongs to WSF/C and wrote a Perl module encapsulating the lower level stuff.  So when you have to write a Web service in Perl all you have to do is <code>use WSO2::WSF::Service;</code> in your script and call a couple of functions.  As the deployment scenario we choose Apache with ModPerl.  That combination seemd logical and some of the heavy trafficked sites are using that.</p>
<p>It gets interesting when you test the whole thing.  Apache is configured to run ModPerl on a folder named <code>/perl</code> so requests matching <code>/perl/something</code> will go through ModPerl.  <code>ResponseHandler</code> is <code>ModPerl::Registry</code> and also <code>GlobalRequest</code> has been enabled to get the global request via <code>Apache::RequestUtil->request()</code>.</p>
<p>Say, we have a service script named <code>echo_service.pl</code> which simply return back whatever it gets.  Here&#8217;s the code,</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Apache2<span style="color: #339933;">::</span><span style="color: #006600;">RequestUtil</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$payload</span> <span style="color: #339933;">=</span><span style="color: #cc0000; font-style: italic;">&lt;&lt;E;
&lt;ns1:echoString xmlns:ns1=&quot;http://perl.axis2.org/samples&quot;&gt;
  &lt;text&gt;plurk u can haz&lt;/text&gt;
&lt;/ns1:echoString&gt;
E</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> echoFunction <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$arg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSMessage</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'payload'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$arg</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%operations</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">'echoString'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'echoFunction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$ENV</span><span style="color: #009900;">&#123;</span>MOD_PERL<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> Apache2<span style="color: #339933;">::</span><span style="color: #006600;">RequestUtil</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSService</span><span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'operations'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\%operations</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'testAction'</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #0000ff;">$service</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">reply</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$r</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When it goes to the reply method it&#8217;ll call up some C routines.  Inside one of those C functions, after determining the function name to call by looking at the request payload, we need to call it.  From a C function.  How can you do that?  By embedding the Perl interpreter.  AFAIK, you cannot call a Perl function from C without embedding the interpreter (please correct me if I&#8217;m wrong).  In WSF/Ruby server side there was no such issue because the entire Ruby module (<code>WSService</code> class and methods) is written in C.</p>
<p>Let me show you the code that embeds the interpreter and calls the function after looking at the XML payload in the request,</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> axiom_node_t <span style="color: #339933;">*</span>
wsf_xml_msg_recv_invoke_other<span style="color: #009900;">&#40;</span>axis2_msg_recv_t<span style="color: #339933;">*</span> msg_recv<span style="color: #339933;">,</span>
        <span style="color: #993333;">const</span> axutil_env_t<span style="color: #339933;">*</span> env<span style="color: #339933;">,</span>
        wsf_svc_info_t<span style="color: #339933;">*</span> svc_info<span style="color: #339933;">,</span>
        axis2_msg_ctx_t<span style="color: #339933;">*</span> in_msg_ctx<span style="color: #339933;">,</span>
        axis2_msg_ctx_t<span style="color: #339933;">*</span> out_msg_ctx<span style="color: #339933;">,</span>
        axis2_char_t<span style="color: #339933;">*</span> function_name<span style="color: #339933;">,</span>
        axis2_char_t<span style="color: #339933;">*</span> class_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> svc_info<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> in_msg_ctx<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> out_msg_ctx<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    axiom_node_t <span style="color: #339933;">*</span>node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_node_t <span style="color: #339933;">*</span>om_node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_soap_envelope_t <span style="color: #339933;">*</span>envelope <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_soap_body_t <span style="color: #339933;">*</span>body <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>retstr <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_node_t <span style="color: #339933;">*</span>soap_body_node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* extracting payload from the soap message */</span>
    envelope <span style="color: #339933;">=</span> axis2_msg_ctx_get_soap_envelope<span style="color: #009900;">&#40;</span>in_msg_ctx<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>envelope<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap envelope not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    body <span style="color: #339933;">=</span> axiom_soap_envelope_get_body<span style="color: #009900;">&#40;</span>envelope<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>body<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap body not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    soap_body_node <span style="color: #339933;">=</span> axiom_soap_body_get_base_node<span style="color: #009900;">&#40;</span>body<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>soap_body_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap body base node not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    om_node <span style="color: #339933;">=</span> axiom_node_get_first_child<span style="color: #009900;">&#40;</span>soap_body_node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>om_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    axis2_char_t <span style="color: #339933;">*</span>embedding<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;-M'WSO2::WSF::C; WSO2::WSF::Service;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>svc_info<span style="color: #339933;">-&gt;</span>script_filename<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span>
                <span style="color: #ff0000;">&quot;perl function invocation failed, script_file name not found for &quot;</span>
                <span style="color: #ff0000;">&quot;service %s&quot;</span><span style="color: #339933;">,</span> svc_info<span style="color: #339933;">-&gt;</span>svc_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #808080; font-style: italic;">/* passing script real path into perl interpreter. */</span>
    embedding<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> svc_info<span style="color: #339933;">-&gt;</span>script_filename<span style="color: #339933;">;</span>
&nbsp;
    my_perl <span style="color: #339933;">=</span> perl_alloc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PL_perl_destruct_level <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/*    PL_use_safe_putenv = 1; */</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_construct<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PL_origalen <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>perl_parse<span style="color: #009900;">&#40;</span>my_perl<span style="color: #339933;">,</span> xs_init<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> embedding<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;perl_parse method failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    PL_exit_flags <span style="color: #339933;">|=</span> PERL_EXIT_DESTRUCT_END<span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/* perl_run(my_perl); */</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>SvTRUE<span style="color: #009900;">&#40;</span>ERRSV<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;invoke perl function failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    retstr <span style="color: #339933;">=</span> invoke_perl_function<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">,</span> om_node<span style="color: #339933;">,</span> function_name<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>retstr<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        node <span style="color: #339933;">=</span> wsf_util_deserialize_buffer<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">,</span> retstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_destruct<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_free<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PERL_SYS_TERM<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> axiom_node_to_string<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> node<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> axis2_char_t <span style="color: #339933;">*</span>
invoke_perl_function<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> axutil_env_t <span style="color: #339933;">*</span>env<span style="color: #339933;">,</span> axiom_node_t <span style="color: #339933;">*</span>om_node<span style="color: #339933;">,</span>
        axis2_char_t <span style="color: #339933;">*</span>operation<span style="color: #339933;">,</span> axis2_char_t <span style="color: #339933;">*</span>class_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>inmsg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    SV <span style="color: #339933;">**</span>wsmsg_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* hold the value for the key 'payload' in WSMessage */</span>
    SV <span style="color: #339933;">*</span>wsmsg_ref <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* reference to a WSMessage object */</span>
    HV <span style="color: #339933;">*</span>wsmsg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* WSMessage object */</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>tmp_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>res_payload_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>operation<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span>
                <span style="color: #ff0000;">&quot;invoking perl function failed, operation not available&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>om_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        inmsg <span style="color: #339933;">=</span> axiom_node_to_string<span style="color: #009900;">&#40;</span>om_node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* declare and init a local copy of the Perl stack pointer */</span>
    dSP<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* mortal SVs for the stack */</span>
    ENTER<span style="color: #339933;">;</span>
    SAVETMPS<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* &quot;record&quot; the current stack pointer */</span>
    PUSHMARK<span style="color: #009900;">&#40;</span>SP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* push parameters to the stack */</span>
    XPUSHs<span style="color: #009900;">&#40;</span>sv_2mortal<span style="color: #009900;">&#40;</span>newSVpv<span style="color: #009900;">&#40;</span>inmsg<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* make the global copy of the stack pointer same as the local copy */</span>
    PUTBACK<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* call the Perl function, expecting a scalar to be returned */</span>
    count <span style="color: #339933;">=</span> call_pv<span style="color: #009900;">&#40;</span>operation<span style="color: #339933;">,</span> G_SCALAR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* refreshing the local copy of the stack pointer, call_pv might have reallocated it */</span>
    SPAGAIN<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">!=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        croak<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;perl function invocation failed&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;perl function %s invocation failed&quot;</span><span style="color: #339933;">,</span> operation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* get the scalar reference from the stack */</span>
    wsmsg_ref <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SV <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> POPs<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* getting the object from the reference */</span>
    wsmsg <span style="color: #339933;">=</span> SvRV<span style="color: #009900;">&#40;</span>wsmsg_ref<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* fetching the value of the member variable 'payload' from WSMessage */</span>
    wsmsg_str <span style="color: #339933;">=</span> hv_fetch<span style="color: #009900;">&#40;</span>wsmsg<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;payload&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> FALSE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* perl scalar to c string */</span>
    tmp_str <span style="color: #339933;">=</span> SvPVutf8_nolen<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>wsmsg_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    res_payload_str <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>axis2_char_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> savepvn<span style="color: #009900;">&#40;</span>tmp_str<span style="color: #339933;">,</span> strlen<span style="color: #009900;">&#40;</span>tmp_str<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PUTBACK<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* cleaning up mortal SVs */</span>
    FREETMPS<span style="color: #339933;">;</span>
    LEAVE<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> res_payload_str<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="http://wso2.org/repos/wso2/trunk/wsf/perl/service/wsf_xml_msg_recv.c">Full source is here</a> (note that the actual source might be different from what you see here since it&#8217;s being modified at the time of this writing).  Also <code>my_perl</code> is declared as a static variable.   The problem is when my_perl gets freed it looks like ModPerl is also affected.  A valgrind report shows that it tries to read from a pointer after <code>perl_free()</code> gets called.  As the following valgrind exerpt shows,</p>
<pre>
==16101== Invalid read of size 4
==16101==    at 0x62AD118: _wrap_wsf_worker_process_request (wsf_wrap.c:4167)
==16101==    by 0x5476D68: Perl_pp_entersub (pp_hot.c:2847)
==16101==    by 0x5438092: Perl_runops_debug (dump.c:1931)
==16101==    by 0x546FF97: Perl_call_sv (perl.c:2646)
==16101==    by 0x4DC2AF3: modperl_callback (modperl_callback.c:101)
==16101==    by 0x4DC388B: modperl_callback_run_handlers (modperl_callback.c:262)
==16101==    by 0x4DC4289: modperl_callback_per_dir (modperl_callback.c:369)
==16101==    by 0x4DBB53E: modperl_response_handler_run (mod_perl.c:1004)
==16101==    by 0x4DBB704: modperl_response_handler_cgi (mod_perl.c:1099)
==16101==    by 0x25A6C: ap_run_handler (config.c:158)
==16101==    by 0x295BE: ap_invoke_handler (config.c:372)
==16101==    by 0x35D80: ap_process_request (http_request.c:258)
==16101==  Address 0x649051c is 12 bytes inside a block of size 1,692 free'd
==16101==    at 0x480590A: free (vg_replace_malloc.c:323)
==16101==    by 0x546AED4: perl_free (perl.c:1394)
==16101==    by 0x62BD599: wsf_xml_msg_recv_invoke_business_logic_sync (wsf_xml_msg_recv.c:452)
==16101==    by 0x613D503: axis2_msg_recv_invoke_business_logic (msg_recv.c:392)
==16101==    by 0x613DB34: axis2_msg_recv_receive_impl (msg_recv.c:319)
==16101==    by 0x613D583: axis2_msg_recv_receive (msg_recv.c:431)
==16101==    by 0x61327D4: axis2_engine_receive (engine.c:315)
==16101==    by 0x6167177: axis2_http_transport_utils_process_http_post_request (http_transport_utils.c:595)
==16101==    by 0x62BE9B3: wsf_worker_process_request (wsf_worker.c:301)
==16101==    by 0x62AD0F7: _wrap_wsf_worker_process_request (wsf_wrap.c:4166)
==16101==    by 0x5476D68: Perl_pp_entersub (pp_hot.c:2847)
==16101==    by 0x5438092: Perl_runops_debug (dump.c:1931)
</pre>
<p>Still haven&#8217;t been able to pin down what exactly happens when <code>perl_free()</code> gets called <img src='http://engwar.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/40/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WSF/Perl 1.1</title>
		<link>http://engwar.com/post/39</link>
		<comments>http://engwar.com/post/39#comments</comments>
		<pubDate>Thu, 29 May 2008 04:15:13 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[webservices]]></category>
		<category><![CDATA[wsf/perl]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=39</guid>
		<description><![CDATA[Just uploaded WSF/Perl 1.1 to CPAN. Doesn&#8217;t show up there yet but will be available within a few hours. This has many things added since the last release (which was ages ago). You could now do WS-ReliableMessaging, WS-Security, WS-SecurityPolicy. The release became ridiculously late due to couple of bugs which took me REALLY LONG to [...]]]></description>
			<content:encoded><![CDATA[<p>Just uploaded WSF/Perl 1.1 to CPAN.  Doesn&#8217;t show up there yet but will be available within a few hours.  This has many things added since the last release (which was ages ago).  You could now do WS-ReliableMessaging, WS-Security, WS-SecurityPolicy.  The release became ridiculously late due to couple of bugs which took me REALLY LONG to debug and fix.  Ouch.  Download and give it a shot.  It&#8217;ll take couple of seconds to compile.  If you&#8217;re in a generous mood I recommend <a href="http://www.everytopicintheuniverseexceptchickens.com/">contributing to a good cause</a> in those few seconds.</p>
<p>This release again has only support only on the client side, if you&#8217;re looking forward to write/host services with WSF/Perl you still have to wait a bit more.  <a href="http://nethu.org/">Dinesh</a> and Danushka has done some excellent progress on that front and will be finishing it soon.  If you wanna know how it&#8217;s taking shape, then <a href="http://wso2.org/repos/wso2/trunk/wsf/perl/service/">by all means</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/39/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SWIG binary string caveat</title>
		<link>http://engwar.com/post/31</link>
		<comments>http://engwar.com/post/31#comments</comments>
		<pubDate>Thu, 24 Apr 2008 11:48:37 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[mtom]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[swig]]></category>
		<category><![CDATA[wsf/perl]]></category>

		<guid isPermaLink="false">http://engwar.com/post/31</guid>
		<description><![CDATA[If you have been working with Web services you might have heard the term MTOM. I was getting MTOM support to work on WSF/Perl and the image that was not getting saved properly. Only the first few bytes was written to the file. The magic numbers that were getting saved was enough for the file [...]]]></description>
			<content:encoded><![CDATA[<p>If you have been working with Web services you might have heard the term <a href="http://en.wikipedia.org/wiki/MTOM">MTOM</a>.  I was getting MTOM support to work on <a href="http://wso2.org/projects/wsf/perl">WSF/Perl</a> and the image that was not getting saved properly.  Only the first few bytes was written to the file.  The magic numbers that were getting saved was enough for the file command to say it&#8217;s a JPEG image.  From the C layer it was reading image data to a character pointer and returning it to the scripting language.</p>
<p>The C function reading the image had a <code>char *</code> return type.  The code that was generated by SWIG was returning the pointer after running it through the following function,</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">SWIG_FromCharPtr<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cptr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> SWIG_FromCharPtrAndSize<span style="color: #009900;">&#40;</span>cptr<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>cptr <span style="color: #339933;">?</span> strlen<span style="color: #009900;">&#40;</span>cptr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The rough English translation of the above is, &#8220;you&#8217;re screwed&#8221;.  Why?  The <code>strlen</code> doesn&#8217;t particularly like to go through binary strings.  It&#8217;ll choke and die when it see a null byte.  So, only the content before the null byte was returned.</p>
<p>The solution was to create a string using the API provided by the particular scripting language.  For Perl it&#8217;s <code>newSVpv</code> and for Ruby it&#8217;s <code>rb_str_new</code>.  Wrapped the functions inside an ifdef (thanks to the nice SWIG&lt;lang&gt; defines) and all is set for MTOM!</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/31/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Custom SOAP headers using WSF/Perl</title>
		<link>http://engwar.com/post/30</link>
		<comments>http://engwar.com/post/30#comments</comments>
		<pubDate>Fri, 04 Apr 2008 13:05:00 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[webservices]]></category>
		<category><![CDATA[wsf/perl]]></category>

		<guid isPermaLink="false">http://engwar.com/post/30</guid>
		<description><![CDATA[SOAP headers are used to convey additional control information that complements the information in the payload. For example WS-Security is one such thing that use SOAP headers to say how integrity and confidentiality should be achieved. Apart from the specs that define standard SOAP headers you could also define your own custom headers which could [...]]]></description>
			<content:encoded><![CDATA[<p>SOAP headers are used to convey additional control information that complements the information in the payload.  For example WS-Security is one such thing that use SOAP headers to say how integrity and confidentiality should be achieved.  Apart from the specs that define standard SOAP headers you could also define your own custom headers which could then be processed as you wish from your application.  Let&#8217;s see how we can generate custom SOAP headers using WSF/Perl.  You&#8217;ll need to get the SVN head revision of <a href="http://wso2.org/projects/wsf/perl">WSF/Perl</a> in order to get this working (as of this writing).</p>
<p>First, let&#8217;s see an example from &#8220;Processing XML with Java&#8221; (<a href="http://www.amazon.com/Processing-XML-Java-TM-Guide/dp/0201771861/">Amazon page</a>) also <a href="http://www.cafeconleche.org/books/xmljava">available online here</a>.  I&#8217;m using <a href="http://www.cafeconleche.org/books/xmljava/chapters/ch02s06.html#bid_with_credit_card.xml">the SOAP header example</a> to show you how to generate the exact thing.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Payment</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://namespaces.cafeconleche.org/xmljava/ch2/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Elliotte Harold<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Issuer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>VISA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Issuer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5125456787651230<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Expires<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2005-12<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Expires<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Payment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here&#8217;s how you can generate the above headers using WSF/Perl,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">my <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Elliotte Harold'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$issuer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Issuer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'VISA'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Number'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'5125456787651230'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$expires</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Expires'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2005-12'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$payment</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Payment'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'ns'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://namespaces.cafeconleche.org/xmljava/ch2/'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$issuer</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number</span><span style="color: #339933;">,</span> <span style="color: #000088;">$expires</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSClient</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #339933;">...,</span>
     <span style="color: #0000ff;">'inputHeaders'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000088;">$payment</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the <a href="http://www.cafeconleche.org/books/xmljava/chapters/ch02s06.html#signed_bid_with_payment.xml">next example</a> it shows the header with the <code>mustUnderstand</code> attribute set, this is how you set it,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">my <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSClient</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #339933;">...,</span>
     <span style="color: #0000ff;">'mustUnderstand'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'inputHeaders'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000088;">$payment</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, I&#8217;ll take an example from a <a href="http://www.ibm.com/developerworks/webservices/library/ws-soapheaders/index.html?ca=drs">developerWorks article that shows you how to manipulate SOAP headers in WebSphere</a>.  Consider the following header contents (Listing 4 in the article)</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MyPrefix:CallingApplication</span> <span style="color: #000066;">xmlns:MyPrefix</span>=<span style="color: #ff0000;">&quot;http://SOAPHeaderDemonstration&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ServiceRequester1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;timeRequestMade<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>13:20:00<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/timeRequestMade<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MyPrefix:CallingApplication<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The main difference in this is that it contains a custom prefix for the element <code>CallingApplication</code>.  Here&#8217;s the WSF/Perl code to generate this header,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">my <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ServiceRequester1'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$version</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'version'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1.0'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$timereq</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'timeRequestMade'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'13:20:00'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$capp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSHeader</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'CallingApplication'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ns'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://SOAPHeaderDemonstration'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'nsprefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'MyPrefix'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timereq</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
my <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #004000;">WSF</span><span style="color: #339933;">::</span><span style="color: #004000;">WSClient</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #009900;">&#123;</span><span style="color: #339933;">...,</span>
     <span style="color: #0000ff;">'inputHeaders'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000088;">$capp</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you saw in the examples, <code>WSHeader</code> class facilitate creating custom SOAP headers with WSF/Perl.</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/30/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
