<?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>Dimuthu's Blog &#187; C</title>
	<atom:link href="http://www.dimuthu.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 07 Jul 2010 12:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Hello World To Web Services With C And C++</title>
		<link>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 04:39:27 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[axiom]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/c++]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[wsf/cpp]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=903</guid>
		<description><![CDATA[Once you have a web service, you can write clients to invoke that service from any language, mostly with the help of a framework written in to that particular language. When it comes to C, the most popular choice is Apache Axis2/C framework. When you are using Axis2/C to write web service clients, you need [...]]]></description>
			<content:encoded><![CDATA[<p>Once you have a web service, you can write clients to invoke that service from any language, mostly with the help of a framework written in to that particular language. When it comes to C, the most popular choice is <a href="http://ws.apache.org/axis2/c/">Apache Axis2/C</a> framework. When you are using Axis2/C to write web service clients, you need to learn about AXIOM which is a easy to use high performing XML model and the service client API which can be used to actually invoke the service. Lets look at the code.</p>
<pre class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;axiom.h&gt;</span>
<span style="color: #339933;">#include &lt;axis2_util.h&gt;</span>
<span style="color: #339933;">#include &lt;axiom_soap.h&gt;</span>
<span style="color: #339933;">#include &lt;axis2_client.h&gt;</span>

axiom_node_t *build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>
    <span style="color: #993333;">const</span> axutil_env_t * env<span style="color: #66cc66;">)</span>;

<span style="color: #993333;">int</span>
main<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="color: #993333;">const</span> axutil_env_t *env = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #993333;">const</span> axis2_char_t *address = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_endpoint_ref_t *endpoint_ref = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_options_t *options = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #993333;">const</span> axis2_char_t *client_home = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_svc_client_t *svc_client = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *payload = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *ret_node = <span style="color: #000000; font-weight: bold;">NULL</span>;

    <span style="color: #808080; font-style: italic;">/* Set up the environment */</span>
    env = axutil_env_create_all<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"helloworld.log"</span>, AXIS2_LOG_LEVEL_TRACE<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Set end point reference of helloworld service */</span>
    address = <span style="color: #ff0000;">"http://localhost:9090/axis2/services/helloworld"</span>;

    <span style="color: #808080; font-style: italic;">/* Create EPR with given address */</span>
    endpoint_ref = axis2_endpoint_ref_create<span style="color: #66cc66;">(</span>env, address<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Setup options */</span>
    options = axis2_options_create<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;
    axis2_options_set_to<span style="color: #66cc66;">(</span>options, env, endpoint_ref<span style="color: #66cc66;">)</span>;
    axis2_options_set_action<span style="color: #66cc66;">(</span>options, env,
                             <span style="color: #ff0000;">"http://ws.apache.org/axis2/c/samples/helloworldString"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Set up deploy folder. It is from the deploy folder, the configuration is picked up
     * using the axis2.xml file. You need to set the AXIS2C_HOME variable to the axis2/c
     * installed dir.
     */</span>
    client_home = AXIS2_GETENV<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"AXIS2C_HOME"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!client_home || !strcmp<span style="color: #66cc66;">(</span>client_home, <span style="color: #ff0000;">""</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
        client_home = <span style="color: #ff0000;">"../.."</span>;

    <span style="color: #808080; font-style: italic;">/* Create service client */</span>
    svc_client = axis2_svc_client_create<span style="color: #66cc66;">(</span>env, client_home<span style="color: #66cc66;">)</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!svc_client<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* reporting the error */</span>
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a>
            <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Error creating service client, Please check AXIS2C_HOME again<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;
        AXIS2_LOG_ERROR<span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                        <span style="color: #ff0000;">"Stub invoke FAILED: Error code:"</span> <span style="color: #ff0000;">" %d :: %s"</span>,
                        env-&gt;error-&gt;error_number,
                        AXIS2_ERROR_GET_MESSAGE<span style="color: #66cc66;">(</span>env-&gt;error<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">-1</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #808080; font-style: italic;">/* Set service client options */</span>
    axis2_svc_client_set_options<span style="color: #66cc66;">(</span>svc_client, env, options<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Build the SOAP request message payload using OM API. */</span>
    payload = build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Send request */</span>
    ret_node = axis2_svc_client_send_receive<span style="color: #66cc66;">(</span>svc_client, env, payload<span style="color: #66cc66;">)</span>;

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>ret_node<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* extracting out the content from the response */</span>
        axis2_char_t *om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;
        om_str = axiom_node_to_string<span style="color: #66cc66;">(</span>ret_node, env<span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>om_str<span style="color: #66cc66;">)</span>
            <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nReceived OM : %s<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, om_str<span style="color: #66cc66;">)</span>;
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nhelloworld client invoke SUCCESSFUL!<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;

        AXIS2_FREE<span style="color: #66cc66;">(</span>env-&gt;allocator, om_str<span style="color: #66cc66;">)</span>;
        ret_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">{</span>
        AXIS2_LOG_ERROR<span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                        <span style="color: #ff0000;">"Stub invoke FAILED: Error code:"</span> <span style="color: #ff0000;">" %d :: %s"</span>,
                        env-&gt;error-&gt;error_number,
                        AXIS2_ERROR_GET_MESSAGE<span style="color: #66cc66;">(</span>env-&gt;error<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"helloworld client invoke FAILED!<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #808080; font-style: italic;">/* freeing the allocated memory */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>svc_client<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axis2_svc_client_free<span style="color: #66cc66;">(</span>svc_client, env<span style="color: #66cc66;">)</span>;
        svc_client = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axutil_env_free<span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span>axutil_env_t *<span style="color: #66cc66;">)</span> env<span style="color: #66cc66;">)</span>;
        env = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">}</span></pre>
<p>Here is the implementation of the &#8220;build_om_payload_for_helloworld_svc&#8221; function that build the request SOAP message using Axiom/C. Note that axiom_element and axiom_node has one to one association. We use node to to navigate the XML, where as axiom_element to store the data.</p>
<pre class="c"><span style="color: #808080; font-style: italic;">/* build SOAP request message content using OM
           &lt;ns1:greet xmlns:ns1="http://ws.apache.org/axis2/services/helloworld"&gt;
                &lt;text&gt;Hello World&lt;/text&gt;
           &lt;/ns1:greet&gt;
*/</span>
axiom_node_t *
build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>
    <span style="color: #993333;">const</span> axutil_env_t * env<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    axiom_node_t *helloworld_om_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_element_t *helloworld_om_ele = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *text_om_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_element_t *text_om_ele = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_namespace_t *ns1 = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_char_t *om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;

    ns1 =
       axiom_namespace_create<span style="color: #66cc66;">(</span>env, <span style="color: #ff0000;">"http://ws.apache.org/axis2/services/helloworld"</span>,
                               <span style="color: #ff0000;">"ns1"</span><span style="color: #66cc66;">)</span>;
    helloworld_om_ele =
        axiom_element_create<span style="color: #66cc66;">(</span>env, <span style="color: #000000; font-weight: bold;">NULL</span>, <span style="color: #ff0000;">"greet"</span>, ns1, &amp;helloworld_om_node<span style="color: #66cc66;">)</span>;
    text_om_ele =
        axiom_element_create<span style="color: #66cc66;">(</span>env, helloworld_om_node, <span style="color: #ff0000;">"text"</span>, <span style="color: #000000; font-weight: bold;">NULL</span>, &amp;text_om_node<span style="color: #66cc66;">)</span>;
    axiom_element_set_text<span style="color: #66cc66;">(</span>text_om_ele, env, <span style="color: #ff0000;">"Hello World!"</span>, text_om_node<span style="color: #66cc66;">)</span>;
    om_str = axiom_node_to_string<span style="color: #66cc66;">(</span>helloworld_om_node, env<span style="color: #66cc66;">)</span>;

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>om_str<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nSending OM : %s<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, om_str<span style="color: #66cc66;">)</span>;
        AXIS2_FREE<span style="color: #66cc66;">(</span>env-&gt;allocator, om_str<span style="color: #66cc66;">)</span>;
        om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #b1b100;">return</span> helloworld_om_node;
<span style="color: #66cc66;">}</span></pre>
<p>So lets see how the same thing is done with C++. For C++ we use <a href="http://wso2.org/projects/wsf/cpp">WSO2 WSF/C++</a></p>
<pre class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;WSSOAPClient.h&gt;</span>
<span style="color: #339900;">#include &lt;OMElement.h&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;AxisFault.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> wso2wsf;

OMElement build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>;

<span style="color: #0000ff;">int</span> main<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
    WSSOAPClient * sc = <span style="color: #0000dd;">new</span> WSSOAPClient<span style="color: #000000;">(</span><span style="color: #666666;">"http://localhost:9090/axis2/services/helloworld"</span><span style="color: #000000;">)</span>;
    sc-&gt;initializeClient<span style="color: #000000;">(</span><span style="color: #666666;">"helloworld_blocking.log"</span>, AXIS2_LOG_LEVEL_TRACE<span style="color: #000000;">)</span>;
    <span style="color: #000000;">{</span>
        <span style="color: #ff0000; font-style: italic;">/* generating the payload */</span>
        OMElement * payload = build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>;

        OMElement * response;
        <span style="color: #0000ff;">try</span>
        <span style="color: #000000;">{</span>
            <span style="color: #ff0000; font-style: italic;">/* invoking the web service */</span>
            response = sc-&gt;request<span style="color: #000000;">(</span>payload, <span style="color: #666666;">"http://ws.apache.org/axis2/c/samples/helloworldString"</span><span style="color: #000000;">)</span>;

            <span style="color: #ff0000; font-style: italic;">/* printing the response */</span>
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">(</span>response<span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; response &lt;&lt; endl;
            <span style="color: #000000;">}</span>
        <span style="color: #000000;">}</span>

        <span style="color: #ff0000; font-style: italic;">/* handling the fault */</span>
        <span style="color: #0000ff;">catch</span> <span style="color: #000000;">(</span>AxisFault &amp; e<span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">(</span>sc-&gt;getLastSOAPFault<span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; sc-&gt;getLastSOAPFault<span style="color: #000000;">(</span><span style="color: #000000;">)</span> &lt;&lt; endl;
            <span style="color: #000000;">}</span>
            <span style="color: #0000ff;">else</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; e &lt;&lt; endl;
            <span style="color: #000000;">}</span>
        <span style="color: #000000;">}</span>
        <span style="color: #0000dd;">delete</span> payload;
    <span style="color: #000000;">}</span>
    <span style="color: #0000dd;">delete</span> sc;
<span style="color: #000000;">}</span></pre>
<p>You can see lines of code is reduced a lot. And you can see it from the code to build the request XML as well.</p>
<pre class="cpp"><span style="color: #ff0000; font-style: italic;">/* building the request soap message
   &lt;ns1:greet xmlns:ns1="http://ws.apache.org/axis2/services/helloworld"&gt;
        &lt;text&gt;Hello World&lt;/text&gt;
   &lt;/ns1:greet&gt;
 */</span>
OMElement build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
    OMNamespace * ns = <span style="color: #0000dd;">new</span> OMNamespace<span style="color: #000000;">(</span><span style="color: #666666;">"http://ws.apache.org/axis2/services/helloworld"</span>, <span style="color: #666666;">"ns1"</span><span style="color: #000000;">)</span>;
    OMElement * payload = <span style="color: #0000dd;">new</span> OMElement<span style="color: #000000;">(</span><span style="color: #0000ff;">NULL</span>,<span style="color: #666666;">"greet"</span>, ns<span style="color: #000000;">)</span>;
    OMElement * child = <span style="color: #0000dd;">new</span> OMElement<span style="color: #000000;">(</span>payload,<span style="color: #666666;">"text"</span>, <span style="color: #0000ff;">NULL</span><span style="color: #000000;">)</span>;
    child-&gt;setText<span style="color: #000000;">(</span><span style="color: #666666;">"Hello World!"</span><span style="color: #000000;">)</span>;

    <span style="color: #0000ff;">return</span> payload;
<span style="color: #000000;">}</span></pre>
<p>WSF/C++ is build on top of Axis2/C. You can see the WSF/C++ API is designed very carefully to make it easy to use without breaking the flexibility provided in the C API. So C++ developers can straightaway use WSF/C++ to develop their web service consumers. Anyway Axis2/C API still has the power of embedding easily in to scripting languages  (Like it is done in WSF/PHP, WSF/Ruby) and probably deploy in legacy systems that doesn&#8217;t support C++ compiled binaries. So you have the options to select the most sutiable one for your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache Axis2/C RESTful URL Mapping Algorithm</title>
		<link>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 10:26:27 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[time complexity]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=664</guid>
		<description><![CDATA[Few weeks back I wrote a blog post about Writing RESTful Services in C which explain the use of Axis2/C REST API. Basically when you provide a HTTP Method (GET, POST, PUT or DELETE) and a HTTP URL, it is matched with a given HTTP method and a URL pattern in order to identify the [...]]]></description>
			<content:encoded><![CDATA[<p>Few weeks back I wrote a blog post about <a href="http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/">Writing RESTful Services in C</a> which explain the use of <a href="http://ws.apache.org/axis2/c">Axis2/C</a> REST API. Basically when you provide a HTTP Method (GET, POST, PUT or DELETE) and a HTTP URL, it is matched with a given HTTP method and a URL pattern in order to identify the operation and extract out the request parameters. For the example mentioned in the above <a href="http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/">blog</a>, we can summarize the URL mapping like this.</p>
<table border="1">
<tbody>
<tr style="background:#dddddd">
<td>Operation</td>
<td>HTTP Method</td>
<td>URL Pattern</td>
<td>Example Requests</td>
</tr>
<tr>
<td>getSubjects</td>
<td>GET</td>
<td>subjects</td>
<td>GET subjects</td>
</tr>
<tr>
<td>getSubjectInfoPerName</td>
<td>GET</td>
<td>subjects/{name}</td>
<td>GET subjects/maths</td>
</tr>
<tr>
<td>getStudnets</td>
<td>GET</td>
<td>students</td>
<td>GET students</td>
</tr>
<tr>
<td>getStudnetsInfoPerName</td>
<td>GET</td>
<td>students/{name}</td>
<td>GET students/john</td>
</tr>
<tr>
<td>getMarksPerSubjectPerStudent</td>
<td>GET</td>
<td>students/{student}/marks/{subject}</td>
<td>GET students/john/marks/maths</td>
</tr>
</tbody>
</table>
<p>You can watch <a href="http://labs.wso2.org/wsf/php/resource_view.php?url=RESTfulSchool">an application with this URL mapping in live</a>, written using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> which in fact run Axis2/C algorithms underneath.</p>
<p>Last week I updated this REST mapping algorithm and started a <a href="http://www.nabble.com/REST-URL-Mapping---The-changes-done-in-pattern-matching-Algorithm-to20594898.html#a20594898">discussion about the changes on Axis2/C Dev list</a>. I thought it would be better explain the idea on by blog too.</p>
<p>What the early algorithm (before my changes) did was, it search each pattern in the order it was declared, and returns when a match is found. Sequential searching for a matching pattern can reduce the performance as the number of operations grows. So my solutions was to keep the url pattern in a multi level (recursive) structure and match the url from one level to another.</p>
<p>Here is the structure of the &#8216;c struct&#8217;. (defined in src/core/util/core_utils.c)</p>
<pre class="c"><span style="font-style: italic; color: #808080;">/* internal structure to keep the rest map in a multi level hash */</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">/* the structure will keep as many as following fields */</span>

    <span style="font-style: italic; color: #808080;">/* if the mapped value is directly the operation */</span>
    axis2_op_t *op_desc;

    <span style="font-style: italic; color: #808080;">/* if the mapped value is a constant, this keeps a hash map of
    possible constants =&gt; corrosponding map_internal structure */</span>
    axutil_hash_t *consts_map;

    <span style="font-style: italic; color: #808080;">/* if the mapped value is a param, this keeps a hash map of
    possible param_values =&gt; corrosponding_map_internal structre */</span>
    axutil_hash_t *params_map;

<span style="color: #66cc66;">}</span> axutil_core_utils_map_internal_t;</pre>
<p>Here is how it will looks like when the above URL pattern set (shown in the above table) is kept inside this multi-level (recursive) structure.</p>
<pre>svc-&gt;op_rest_map  (hash)
                |
            "GET:students" --------- axutil_core_utils_map_internal_t (instance)
                |                                            |
                |                                        op_desc (axis2_op_t* for "GET students" op)
                |                                            |
                |                                        consts_map (empty hash)
                |                                            |
                |                                        params_map (hash)
                |                                                         |
                |                                                      "{student_id}" ------------- axutil_core_utils_map_internal_t (instance)
                |                                                                                            |
                |                                                                                op_desc (axis2_op_t* for "GET students/{student_id}" op)
                |                                                                                            |
                |                                                                                parms_map (empty hash)
                |                                                                                            |
                |                                                                                 const_map (hash)
                |                                                                                            |
                |                                                                                        "marks" ------------------- axutil_core_utils_map_internal_t (instance)
                |                                                                                                                            |
                |                                                                                                                    op_desc (NULL)
                |                                                                                                                            |
                |                                                                                                                   consts_map (empty hash)
                |                                                                                                                            |
                |                                                                                                                   params_map (hash)
                |                                                                                                                            |
                |                                                                                                                      "{subject_id}" ----------- axutil_core_utils_map_internal_t (instance)
                |                                                                                                                                                                               |
                |                                                                                                                                       op_desc (axis2_op_t* for "GET students/{student_id}/marks/{subject_id}" op)
                |                                                                                                                                                                               |
                |                                                                                                                                                                 consts_map / params_map (Both NULL)
                |
            "GET:students" --------- axutil_core_utils_map_internal_t (instance)
                                                            |
                                                        op_desc (axis2_op_t* for "GET students" op)
                                                            |
                                                        consts_map (empty hash)
                                                            |
                                                        params_map (hash)
                                                            |
                                                      "{student_id}" ------------- axutil_core_utils_map_internal_t (instance)
                                                                                                          |
                                                                                  op_desc (axis2_op_t* for "GET students/{student_id}" op)
                                                                                                          |
                                                                                             consts_map / params_map (Both NULL)</pre>
<p>This structure is build at the time the server initialize the services. (from the &#8220;axis2_svc_get_rest_op_list_with_method_and_location&#8221; function in src/core/description/svc.c)</p>
<p>As each request hit the service, the request HTTP method and the URL is matched (which we call &#8216;rest dispatching&#8217;) with the above structure using the following algorithm. (defined in the &#8220;axis2_rest_disp_find_op&#8221; function in src/core/engine/rest_disp.c). Note that here we are extracting out the user REST parameters as well, but it is not shown in here.</p>
<ol>
<li>The request URL is spitted in to URL components from &#8216;/&#8217; character. Retrive the instance of axutil_core_utils_map_internal_t  from the svc-&gt;rest_map to the varaible &#8216;mapping_struct&#8217;.</li>
<li>Check the existance of URL components, count(URL components) &gt; 0.</li>
<li>If it doesn&#8217;t exist any URL components, get the value of mapping_struct-&gt;op_desc where the mapping_struct is the current mapping instance of axutil_core_utils_map_internal_t. if the mapping_struct-&gt;op_desc is not NULL, we found the operation. If it is NULL just exit returning NULL.</li>
<li>Else If some URL component(s) exist, check the most former URL component in the mapping_struct-&gt;const_map hash. If mapping_struct-&gt;const_map['former_url_component'] is not NULL, assign the mapping struct-&gt;const_map['former_url_component'] value to mapping_struct and follow the step 2 with the remaining URL components. (note that here hash['key'] syntax is used to take the value for the key from the hash &#8216;hash&#8217;. If that returns TRUE, we found the opeartion, if not countine to step5.</li>
<li>if mapping_struct-&gt;const_map['former_url_component'] is NULL, match the former url component with each key (which is a URL component pattern) in mapping_struct-&gt;param_map hash. (We use the function <span> &#8220;axis2_core_utils_match_url_component_with_pattern&#8221; in src/core/util/core_utils.c to</span> map URL component with the URL component pattern<span>). If matching pattern found assign the mapping_struct-&gt;param_map['key'] to mapping struct and </span>follow the step 2 with the remaining URL components. If that returns TRUE for some key it will be the matching operation.</li>
</ol>
<p>Where as the earlier algorithm can be simplified to,</p>
<ol>
<li>Match the request URL with URL patterns of each operation. This will be like calling the function &#8220;<span>axis2_core_utils_match_url_component_with_pattern&#8221; (mentioned in step5 of the above algorithm) for the complete URL rather than for a URL component<br />
</span></li>
<li>If the pattern is matched, matching operation is the selected operation for the request.</li>
</ol>
<p>I approximately calculated the time complexity of both of these algorithm.</p>
<p>Here is the time complexity of the later described algorithm.</p>
<table border="1">
<tbody>
<tr>
<td>Average time complexity of iterating &#8216;n&#8217; number of operations</td>
<td>n/2 = O(n)</td>
</tr>
<tr>
<td>Time complexity of matching pattern with a URL with the length &#8216;p&#8217; (complexity of the &#8216;axis2_core_utils_match_url_component_with_pattern&#8217; function)</td>
<td>O(p^2)</td>
</tr>
<tr style="background:#eeeeee">
<td>Complete time complexity of the algorithm</td>
<td>O(n*p^2)</td>
</tr>
</tbody>
</table>
<p>Time complexity of the formerly described algorithm. (which is currently in the SVN).</p>
<table border="1">
<tbody>
<tr>
<td>Time Complexity of a Hash Search</td>
<td>O(1)</td>
</tr>
<tr>
<td>Average Number of has searches required. This is the average number of levels in the tree of recursive structures drawn above</td>
<td>long(n)/2 = O(log(n))</td>
</tr>
<tr>
<td>Time complexity of matching pattern with a URL component with the average length &#8216;d&#8217;, d &lt; p (p = the length of the complete URL)</td>
<td>O(d^2)</td>
</tr>
<tr>
<td>Number of time pattern matching is required = number of param components in the URL = k, k &lt; p/d (p = the length of the url, d = average length of the URL component)/</td>
<td>k = O(k)</td>
</tr>
<tr style="background:#eeeeee">
<td>Complete time complexity of the algorithm</td>
<td>O(log(n)*d^2*k)</td>
</tr>
</tbody>
</table>
<p>Considering the facts, O(logn) &lt; O(n),d &lt; p and k &lt; p/d we can safely conclude</p>
<p>O(long(n)*d^2*k) &lt; O(n*p^2)  =&gt; The newer algorithm has better (low) time complexity.</p>
<p>However the time complexity is valid only it takes high values for the parameters. For low values  the actual time taken by the newer algorithm can have high values, considering the constant overhead of the recursions and the hash search. So in order to judge the performance of the algorithm, we have to run some test cases and measure the actual times. Possibly a task for the weekend <img src='http://www.dimuthu.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/21/apache-axis2c-restful-url-mapping-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Write RESTful Services in C</title>
		<link>http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/</link>
		<comments>http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 17:40:14 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[services.xml]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=497</guid>
		<description><![CDATA[You can write REST as well as SOAP web services using Apache Axis2/C web services framework. There you can make existing Axis2/C web services RESTful just by providing the URL patterns and the HTTP methods to each operation in  the services.xml which act as a simple descriptor for an Axis2/C service. So if we rewrite [...]]]></description>
			<content:encoded><![CDATA[<p>You can write REST as well as SOAP web services using <a href="http://www.dimuthu.org/">Apache Axis2/C</a> web services framework. There you can make existing Axis2/C web services RESTful just by providing the URL patterns and the HTTP methods to each operation in  the services.xml which act as a simple descriptor for an Axis2/C service.</p>
<p>So if we rewrite the <a href="http://labs.wso2.org/wsf/php/demo.php?name=RESTfulSchool&amp;demo=RESTFulSchool/demo_client.php&amp;src=RESTFulSchool">RESTful Demo</a> (Written in PHP) using Axis2/C, the services.xml would be something like following.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTfulSchool"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- mentioning the service library--&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"ServiceClass"</span> <span style="color: #000066;">locked</span>=<span style="color: #ff0000;">"xsd:false"</span><span style="font-weight: bold; color: black;">&gt;</span></span>RESTfulSchool<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>

    <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- some description </span></span><span><span><span style="color: #009900;"><span style="font-style: italic; color: #808080;">-</span></span></span></span><span style="color: #009900;"><span style="font-style: italic; color: #808080;">-&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        The RESTful School demo
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span>

    <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- list of operations --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getSubjects"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTMethod"</span><span style="font-weight: bold; color: black;">&gt;</span></span>GET<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTLocation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>subjects<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getSubjectInfoPerName"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTMethod"</span><span style="font-weight: bold; color: black;">&gt;</span></span>GET<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTLocation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>subjects/{name}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getStudents"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTMethod"</span><span style="font-weight: bold; color: black;">&gt;</span></span>GET<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTLocation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>students<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getStudentInfoPerName"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTMethod"</span><span style="font-weight: bold; color: black;">&gt;</span></span>GET<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTLocation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>students/{name}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getMarksPerSubjectPerStudent"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTMethod"</span><span style="font-weight: bold; color: black;">&gt;</span></span>GET<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"RESTLocation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>students/{student}/marks/{subject}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/parameter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/service<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>We will check how to write the service logic for a operation like &#8220;getMarksPerSubjectPerStudent&#8221;.</p>
<pre class="c">axiom_node_t *
RESTfulSchool_getMarksPerSubjectPerStudent<span style="color: #66cc66;">(</span>
    <span style="color: #993333;">const</span> axutil_env_t * env,
    axiom_node_t * request_payload<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    axiom_node_t *student_node = <span style="font-weight: bold; color: #000000;">NULL</span>;
    axiom_node_t *subject_node = <span style="font-weight: bold; color: #000000;">NULL</span>;

    <span style="font-style: italic; color: #808080;">/* Extracting out the child nodes from the request */</span>
    student_node = axiom_node_get_first_child<span style="color: #66cc66;">(</span>request_payload, env<span style="color: #66cc66;">)</span>;
    subject_node = axiom_node_get_next_sibling<span style="color: #66cc66;">(</span>student_node, env<span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">/* now we can write the logic to retrieve the marks
       for the given student and subject and build and
       return the response payload */</span>

    <span style="color: #b1b100;">return</span> response_payload;
<span style="color: #66cc66;">}</span></pre>
<p>As you can see the variables {student} and {subject} given in the services.xml can be easily accessed from your business logic, so we can build the response accordingly.</p>
<p>This way you can build a RESTful web services easily using C language.</p>
<p><a style="cursor: pointer; color: green;"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/10/18/write-restful-services-in-c/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
