<?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; wsdl2c</title>
	<atom:link href="http://www.dimuthu.org/tag/wsdl2c/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>Code Generate Custom SOAP Faults For Axis2/C</title>
		<link>http://www.dimuthu.org/blog/2009/02/09/code-generate-custom-soap-faults-for-axis2c/</link>
		<comments>http://www.dimuthu.org/blog/2009/02/09/code-generate-custom-soap-faults-for-axis2c/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 06:04:41 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[2 minutes guide]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[codegen]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl2c]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[faults]]></category>
		<category><![CDATA[handling]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[throwing]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=916</guid>
		<description><![CDATA[Web services use SOAP faults to report fault cases back to clients. The faults can be generated from the SOAP framework in a case of invalid SOAP messages, invalid security tokens or they can be generated from the service business logic itself. The fault messages may contain simply a string indicating the error, or it [...]]]></description>
			<content:encoded><![CDATA[<p>Web services use SOAP faults to report fault cases back to clients. The faults can be generated from the SOAP framework in a case of invalid SOAP messages, invalid security tokens or they can be generated from the service business logic itself. The fault messages may contain simply a string indicating the error, or it may contain lot of details which could be useful to the clients find the problem. In fact the format of the SOAP fault is a standard. But services can send custom details within the details element in a SOAP fault.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soapenv:Envelope</span> <span style="color: #000066;">xmlns:soapenv</span>=<span style="color: #ff0000;">"http://schemas.xmlsoap.org/soap/envelope/"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soapenv:Body<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soapenv:Fault<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;faultcode<span style="font-weight: bold; color: black;">&gt;</span></span></span>soapenv:Sender<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/faultcode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;faultstring<span style="font-weight: bold; color: black;">&gt;</span></span></span>..<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/faultstring<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;detail<span style="font-weight: bold; color: black;">&gt;</span></span></span> You Custom Message <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/detail<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/soapenv:Fault<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/soapenv:Body<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/soapenv:Envelope<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>And you can define the schema of your custom fault message in a WSDL, so the clients can prepare to handle fault scenarios. In facts tools like <a href="http://ws.apache.org/axis2/">Apache Axis2</a> WSDL2C, WSDL2Java tool will help you to work with custom faults when they are defined in the WSDL.  Here is an example section of a WSDL with an operation which can throw two faults &#8220;MyFirstException&#8221;, &#8220;MySecondException&#8221;.</p>
<pre class="xml"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- fault schemas --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;types<span style="font-weight: bold; color: black;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;schema</span> ..<span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MyFirstException"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xsd:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- fault element --&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MySecondException"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"number"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xsd:int"</span><span style="color: #ff0000;"></span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/schema<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/types<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- the fault messages --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MySecondExceptionFault"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
 <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"fault"</span> <span style="color: #000066;">element</span>=<span style="color: #ff0000;">"ns1:MySecondException"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/message<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MyFirstExceptionFault"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
 <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"fault"</span> <span style="color: #000066;">element</span>=<span style="color: #ff0000;">"ns1:MyFirstException"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/message<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- operation to throw fault --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;portType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MyType"</span><span style="font-weight: bold; color: black;">&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;">"myOperation"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;input</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"tns:myOperationRequest"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;output</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"tns:myOperationResponse"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fault</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MySecondException"</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"tns:MySecondExceptionFault"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fault</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"MyFirstException"</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"tns:MyFirstExceptionFault"</span><span style="font-weight: bold; color: black;">/&gt;</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;/portType<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Note that the operation &#8220;myOperation&#8221; is throwing faults &#8220;MyFirstException&#8221;, &#8220;MySecondExcpetion&#8221;. If you generate the Java code for this operation, it would be simple as this,</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> MyOperationRequest
MyOperation<span style="color: #66cc66;">(</span>MyOperationRequest<span style="color: #66cc66;">)</span> <span style="color: #000000; font-weight: bold;">throws</span>
          MyFirstExcpetion,
          MySecondExcpetion <span style="color: #66cc66;">{</span>
  <span style="color: #808080; font-style: italic;">// here is your business logic</span>
<span style="color: #66cc66;">}</span></pre>
<p>Anyway we are going to write codes in &#8216;C&#8217; language, which doesn&#8217;t have similar exception mechanism. Let see how we can do it, starting with writing the service and then writing a client.</p>
<p><strong>Writing Services With Custom SOAP Faults</strong></p>
<p>Once you generate the &#8216;C&#8217; codes for the WSDL using WSDL2C tool, you should first have a look at the skeleton header file.</p>
<pre class="c">    <span style="color: #808080; font-style: italic;">/**
     * the generated fault union for operation "myOperation|urn:myuri:1.0",
     * in a case you want to return a fault, put the appropriate adb object for
     * the union variable pointer comes as the last parameter of the method
     */</span>
    <span style="color: #993333;">typedef</span> <span style="color: #993333;">union</span>
    <span style="color: #66cc66;">{</span>
        adb_MyFirstException_t* MyFirstException;
        adb_MySecondException_t* MySecondException;

    <span style="color: #66cc66;">}</span> axis2_skel_MyService_myOperation_fault;

    <span style="color: #808080; font-style: italic;">/**
     * auto generated function declaration
     * for "myOperation|urn:myuri:1.0" operation.
     * @param env environment ( mandatory)
     * @param _myOperation of the adb_myOperation_t*
     *
     * @return adb_myOperationResponse_t*
     */</span>
    adb_myOperationResponse_t* axis2_skel_MyService_myOperation<span style="color: #66cc66;">(</span><span style="color: #993333;">const</span> axutil_env_t *env,
                                      adb_myOperation_t* _myOperation,
                                      axis2_skel_MyService_myOperation_fault *fault<span style="color: #66cc66;">)</span>;</pre>
<p>And at the very end of the header file, you will see an enumeration of constants corresponding to each fault is generated.</p>
<pre class="c">    <span style="color: #993333;">typedef</span> <span style="color: #000000; font-weight: bold;">enum</span>
    <span style="color: #66cc66;">{</span>
        AXIS2_SKEL_MYSERVICE_ERROR_NONE = AXIS2_SKEL_MYSERVICE_ERROR_CODES_START,

        AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
        AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,

        AXIS2_SKEL_MYSERVICE_ERROR_LAST

    <span style="color: #66cc66;">}</span> axis2_skel_MyService_error_codes;</pre>
<p>That&#8217;s all you need to aware of. The plan is whenever you need to report the fault, you have to do three things inside the business logic.</p>
<ol>
<li>Create the adb object for the fault, in this case either &#8220;adb_MyFirstException_t&#8221; or &#8220;adb_MySecondException_t&#8221; and set it to the fault pointer variable.</li>
<li>Set the constant corresponding to the fault using &#8220;AXIS2_ERROR_SET&#8221; function</li>
<li>return NULL</li>
</ol>
<p>Here is an example how you do it inside the actual business logic code.</p>
<pre class="c">    adb_myOperationResponse_t* axis2_skel_MyService_myOperation<span style="color: #66cc66;">(</span><span style="color: #993333;">const</span> axutil_env_t *env,
                                      adb_myOperation_t* myOperation,
                                      axis2_skel_MyService_myOperation_fault *fault <span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* the buisness logic */</span>

        ....

        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #808080; font-style: italic;">/* checking some condition to throw the "MyFirstException" fault */</span><span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
          <span style="color: #808080; font-style: italic;">/* 1. Creating the adb object and set it to the fault pointer variable */</span>
          adb_MyFirstException_t *exp = <span style="color: #000000; font-weight: bold;">NULL</span>;
          exp = adb_MyFirstException_create<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;
          adb_MyFirstException_set_text<span style="color: #66cc66;">(</span>exp, env, <span style="color: #ff0000;">"this is the exception 1"</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">/* custom value */</span>

          fault-&gt;MyFirstException = exp;

          <span style="color: #808080; font-style: italic;">/* 2. Setting the error constant corrosponding to the fault */</span>
          AXIS2_ERROR_SET<span style="color: #66cc66;">(</span>env-&gt;error,
                      AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
                      AXIS2_FAILURE<span style="color: #66cc66;">)</span>;

          <span style="color: #808080; font-style: italic;">/* 3. Returning NULL */</span>
          <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #808080; font-style: italic;">/* checking some condition to throw the "MySecondException" fault */</span><span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
          <span style="color: #808080; font-style: italic;">/* 1. Creating the adb object and set it to the fault pointer variable */</span>
          adb_MySecondException_t *exp = <span style="color: #000000; font-weight: bold;">NULL</span>;
          exp = adb_MySecondException_create<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;
          adb_MySecondException_set_number<span style="color: #66cc66;">(</span>exp, env, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">)</span>;<span style="color: #808080; font-style: italic;">/* custom value */</span>

          fault-&gt;MySecondException = exp;

          <span style="color: #808080; font-style: italic;">/* 2. Setting the error constant corrosponding to the fault */</span>
          AXIS2_ERROR_SET<span style="color: #66cc66;">(</span>env-&gt;error,
                      AXIS2_SKEL_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,
                      AXIS2_FAILURE<span style="color: #66cc66;">)</span>;
          <span style="color: #808080; font-style: italic;">/* 3. Returning NULL */</span>
          <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        <span style="color: #808080; font-style: italic;">/* return the response in no fault scenario */</span>
        <span style="color: #b1b100;">return</span> response;
    <span style="color: #66cc66;">}</span></pre>
<p>That&#8217;s all you have to do, Axis2/C will make sure to build the fault and put your custom message inside the details element.</p>
<p><strong>Writing Clients  to Handle custom SOAP Faults</strong></p>
<p>After generating the code for clients using WSDL2C tool, this time you should look at the generated stub header file first. It is just similar to the skeleton header files may be except all the &#8220;skel&#8221; prefixes are renamed to &#8220;stub&#8221; and additional parameter &#8220;stub&#8221; for the operation.</p>
<pre class="c">    <span style="color: #808080; font-style: italic;">/**
     * the generated fault union for operation "myOperation|urn:myuri:1.0",
     * in a case the server return a fault, the corresponding adb object will be loaded for
     * the union variable pointer comes as the last parameter of the method
     */</span>
    <span style="color: #993333;">typedef</span> <span style="color: #993333;">union</span>
    <span style="color: #66cc66;">{</span>
        adb_MyFirstException_t* MyFirstException;
        adb_MySecondException_t* MySecondException;

    <span style="color: #66cc66;">}</span> axis2_stub_MyService_myOperation_fault;

    <span style="color: #808080; font-style: italic;">/**
     * auto generated function declaration
     * for "myOperation|urn:myuri:1.0" operation.
     * @param env environment ( mandatory)
     * @param _myOperation of the adb_myOperation_t*
     *
     * @return adb_myOperationResponse_t*
     */</span>
    adb_myOperationResponse_t* axis2_stub_MyService_myOperation<span style="color: #66cc66;">(</span>axis2_stub_t* stub, <span style="color: #993333;">const</span> axutil_env_t *env,
                                      adb_myOperation_t* _myOperation,
                                      axis2_stub_MyService_myOperation_fault *fault<span style="color: #66cc66;">)</span>;

    <span style="color: #993333;">typedef</span> <span style="color: #000000; font-weight: bold;">enum</span>
    <span style="color: #66cc66;">{</span>
        AXIS2_STUB_MYSERVICE_ERROR_NONE = AXIS2_STUB_MYSERVICE_ERROR_CODES_START,

        AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION,
        AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION,

        AXIS2_STUB_MYSERVICE_ERROR_LAST

    <span style="color: #66cc66;">}</span> axis2_stub_MyService_error_codes;</pre>
<p>Looking at this, you may have got the idea how to differentiate what fault is being thrown by the server and how to extract the parameters of the custom fault. Here is an example client code correctly handling exceptions.</p>
<pre class="c">    <span style="color: #808080; font-style: italic;">/* the structure to keep the fault */</span>
    axis2_stub_MyService_myOperation_fault fault;

    ..... <span style="color: #808080; font-style: italic;">/* the part preparing the request is ignored here */</span>

    <span style="color: #808080; font-style: italic;">/* invoking the "myOperation" operation */</span>
    response = axis2_stub_op_MyService_myOperation<span style="color: #66cc66;">(</span>stub, env, op, &amp;fault<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* checking the response == NULL implies fault is sent  */</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span>response == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* getting the error number to distinguish the fault */</span>
        error_code = env-&gt;error-&gt;error_number;

        <span style="color: #808080; font-style: italic;">/* compare error code with constants of each faults */</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span>error_code == AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYFIRSTEXCEPTION<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

            <span style="color: #808080; font-style: italic;">/* extracting out the adb objects */</span>
            axis2_char_t *text = adb_MyFirstException_get_text<span style="color: #66cc66;">(</span>fault.<span style="color: #202020;">MyFirstException</span>, env<span style="color: #66cc66;">)</span>;

            <span style="color: #808080; font-style: italic;">/* do a printf of the message */</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;">"My First Exception called: with param %s<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, text<span style="color: #66cc66;">)</span>;

        <span style="color: #66cc66;">}</span>
        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span>error_code == AXIS2_STUB_MYSERVICE_MYOPERATION_FAULT_MYSECONDEXCEPTION<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
            <span style="color: #808080; font-style: italic;">/* extracting out the adb objects */</span>
            <span style="color: #993333;">int</span> number = adb_MySecondException_get_number<span style="color: #66cc66;">(</span>fault.<span style="color: #202020;">MySecondException</span>, env<span style="color: #66cc66;">)</span>;

            <span style="color: #808080; font-style: italic;">/* do a printf of the message */</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;">"My Second Exception called: with param %d<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, number<span style="color: #66cc66;">)</span>;

        <span style="color: #66cc66;">}</span>

    <span style="color: #66cc66;">}</span></pre>
<p>Note that this feature is available only in the very latest WSDL2C tool. Try to get latest build from <a href="http://ws.apache.org/axis2">Axis2/Java</a> to use this up to date tool.</p>
<p>You can download the WSDL and codes used in this example from here, <a href="https://issues.apache.org/jira/secure/attachment/12399724/case45.zip">https://issues.apache.org/jira/secure/attachment/12399724/case45.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/02/09/code-generate-custom-soap-faults-for-axis2c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Sending Binary with Apache Axis2/C ADB</title>
		<link>http://www.dimuthu.org/blog/2008/11/24/sending-binary-with-apache-axis2c-adb/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/24/sending-binary-with-apache-axis2c-adb/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 18:21:22 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[codegen]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl2c]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[base64 binary]]></category>
		<category><![CDATA[binary]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=716</guid>
		<description><![CDATA[Axis2/C ADB is a C language binding to the XML schema. ADB object model represents an XML specific to a given schema in a WSDL. You can use the Axis2 codegen tool to generate ADB codes for your WSDL and use that to build and parse your XMLs. The idea is, if you use ADB [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ws.apache.org/axis2/c/">Axis2/C</a> ADB  is a C language binding to the XML schema. ADB object model represents an  XML specific to a given schema in a WSDL. You can use the Axis2 codegen tool to generate ADB codes for your WSDL and use that to build and parse your XMLs. The idea is, if you use ADB to build and parse your xmls, it will be really easy to do that  and you don&#8217;t need to know or understand anything about the schema or the wsdl.</p>
<p>Apache Axis2/C to can be used to send and receive binaries as MTOM, SWA or base64 encoded. But ADB generated code still support to send and receive base64 encoded binaries only. So if you use contract first approach  with Axis2/C (i.e start with the WSDL, then write the service based on that), you have to use base64-encoded (non-optimized) as the binary transferring method. Note that you can use the other methods like MTOM or SWA, if you write the code to build and parse the xmls from AXIOM which is a general model for XML like DOM.</p>
<p>Say you have an element with base64Binary type in your request XML. So the schema for that element would be,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Person"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"image"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:base64Binary"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
        ... <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- some more elements --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>After you code generated, you will get the adb_person.h and adb_person.c files with the following function prototypes and the implementations,</p>
<pre class="c">        <span style="font-style: italic; color: #808080;">/**
         * Constructor for creating adb_Person_t
         * @param env pointer to environment struct
         * @return newly created adb_Person_t object
         */</span>
        adb_Person_t* AXIS2_CALL
        adb_Person_create<span style="color: #66cc66;">(</span>
            <span style="color: #993333;">const</span> axutil_env_t *env <span style="color: #66cc66;">)</span>;

        <span style="font-style: italic; color: #808080;">/**
         * Free adb_Person_t object
         * @param  _Person adb_Person_t object to free
         * @param env pointer to environment struct
         * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE
         */</span>
        axis2_status_t AXIS2_CALL
        adb_Person_free <span style="color: #66cc66;">(</span>
            adb_Person_t* _Person,
            <span style="color: #993333;">const</span> axutil_env_t *env<span style="color: #66cc66;">)</span>;

        <span style="font-style: italic; color: #808080;">/**
         * Getter for image.
         * @param  _Person adb_Person_t object
         * @param env pointer to environment struct
         * @return axutil_base64_binary_t*
         */</span>
        axutil_base64_binary_t* AXIS2_CALL
        adb_Person_get_image<span style="color: #66cc66;">(</span>
            adb_Person_t* _Person,
            <span style="color: #993333;">const</span> axutil_env_t *env<span style="color: #66cc66;">)</span>;

        <span style="font-style: italic; color: #808080;">/**
         * Setter for image.
         * @param  _Person adb_Person_t object
         * @param env pointer to environment struct
         * @param arg_image axutil_base64_binary_t*
         * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE
         */</span>
        axis2_status_t AXIS2_CALL
        adb_Person_set_image<span style="color: #66cc66;">(</span>
            adb_Person_t* _Person,
            <span style="color: #993333;">const</span> axutil_env_t *env,
            axutil_base64_binary_t*  arg_image<span style="color: #66cc66;">)</span>;</pre>
<p>So you can manipulate the person element in c language using the create, get, set and free function.</p>
<pre class="c">    <span style="font-style: italic; color: #808080;">/* here the env is the axutil_env_t* instance - the axis2/c environment */</span>
    FILE *f = fopen<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"./images/person.png"</span>, <span style="color: #ff0000;">"r+"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #993333;">int</span> binary_count;
    <span style="font-style: italic; color: #808080;">/* binary read a function you may write to read the binary data to the
     variable binary and the count to the variable binary_count */</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> *binary = binary_read<span style="color: #66cc66;">(</span>f, env, &amp;binary_count<span style="color: #66cc66;">)</span>;
    axutil_base64_binary_t *base64 = axutil_base64_binary_create_with_plain_binary<span style="color: #66cc66;">(</span>
                                        env, binary, binary_count<span style="color: #66cc66;">)</span>;

    adb_Person_t *person = adb_Person_create<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;
    adb_Person_set_image<span style="color: #66cc66;">(</span>person, env, base64<span style="color: #66cc66;">)</span>;</pre>
<p>You can set this adb person directly to your request or to a setter of another adb instance to complete the ADB tree. So this way you can send binaries (as base64 encoded) using the Axis2/C ADB generated code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/24/sending-binary-with-apache-axis2c-adb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Axis2/C WSDL2C With Unwrapped Mode for Simpler Code</title>
		<link>http://www.dimuthu.org/blog/2008/09/15/use-axis2c-wsdl2c-with-unwrapped-mode-for-simpler-code/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/15/use-axis2c-wsdl2c-with-unwrapped-mode-for-simpler-code/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 15:10:37 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[codegen]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl2c]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[Code generation]]></category>
		<category><![CDATA[Easier]]></category>
		<category><![CDATA[Simple]]></category>
		<category><![CDATA[unwrapped]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=263</guid>
		<description><![CDATA[WSDL2Java and WSDL2C tools shipped with the Axis2/Java project are used to generate Stubs(client side) and Skeletons(Server Side) respectively for Axis2/Java and Axis2/C codes. This make the development of web services and consumers more quicker and easier. The tools come with many options allowing the user to customize the code generation. One of the such [...]]]></description>
			<content:encoded><![CDATA[<p>WSDL2Java and WSDL2C tools shipped with the <a href="http://ws.apache.org/axis2">Axis2/Java project</a> are used to generate Stubs(client side) and Skeletons(Server Side) respectively for Axis2/Java and Axis2/C codes. This make the development of web services and consumers more quicker and easier.</p>
<p>The tools come with many options allowing the user to customize the code generation. One of the such option is &#8216;-uw&#8217; which used to turn on the &#8216;unwrapping&#8217; mode. WSDL2Java project had this option for a long time, But it is just Today I was able to enable that option in the WSDL2C tool. So lets see how it make the code easier.</p>
<p>Say you have the &#8216;simpleAdd&#8217; operation with the following schema,</p>
<pre>            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"simpleAdd"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"param0"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"param1"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"simpleAddResponse"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"return"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>When you generate the code for server side with Java using the following command, (note the option -uw which stands for &#8216;unwrapping&#8217;)</p>
<pre>wsdl2java.sh -uri xxx.wsdl -u -ss -sd -uw</pre>
<p>it gives you the following service function.</p>
<pre><span style="font-weight: bold; color: #000000;">public</span> <span style="color: #993333;">int</span> simpleAdd<span style="color: #66cc66;">(</span><span style="color: #993333;">int</span> param0, <span style="color: #993333;">int</span> param1<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">//TODO : fill this with the necessary business logic</span>
<span style="color: #66cc66;">}</span></pre>
<p>Note the input and output parameters of the function are simple types. If you don&#8217;t give the &#8216;-uw&#8217; option, You will instead get a function with input and output variables as class names wrapping these simple types which is not this much straight forward.</p>
<p>So now the same is available with C. You can use  WSDl2C tool with the same option set to generate a very simple service API.</p>
<pre>wsdl2c.sh -uri xxx.wsdl -u -ss -sd -uw</pre>
<p>And here is the service logic you may write, Can you think of an API simpler than this,</p>
<pre>        <span style="font-style: italic; color: #808080;">/**
         * auto generated function definition signature
         * for "simpleAdd|http://ws.apache.org/axis2" operation.
         * @param env environment ( mandatory)
         * @param _param0 of the int
         * @param _param1 of the int
         *
         * @return int
         */</span>
        <span style="color: #993333;">int</span> axis2_skel_UnwrappedAdder_simpleAdd<span style="color: #66cc66;">(</span><span style="color: #993333;">const</span> axutil_env_t *env,
                                              <span style="color: #993333;">int</span> _param0,
                                              <span style="color: #993333;">int</span> _param1 <span style="color: #66cc66;">)</span>

        <span style="color: #66cc66;">{</span>
          <span style="color: #b1b100;">return</span> _param0 + _param1;
        <span style="color: #66cc66;">}</span></pre>
<p>You can find the code for complete case (wsdl, skel and stub) in the <a href="https://issues.apache.org/jira/secure/attachment/12390112/unwrapped.zip"> Axis2/C Jira Attachment space for the &#8216;unwrapping&#8217; issue</a>. Note that this update is only available in the nightly build and not in the release. You can download the nightly build from <a href="http://builder.wso2.org/browse/AXIS2-NIGHTLY">wso2&#8242;s axis2/java nightly build page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/15/use-axis2c-wsdl2c-with-unwrapped-mode-for-simpler-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
