<?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; binary</title>
	<atom:link href="http://www.dimuthu.org/tag/binary/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<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[adb]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[codegen]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[Tutorial/Guide]]></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>Send Binary With Web Services in PHP &#8211; 2 Minutes Introduction</title>
		<link>http://www.dimuthu.org/blog/2008/09/22/send-binary-with-web-services-in-php-2-minutes-introduction/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/22/send-binary-with-web-services-in-php-2-minutes-introduction/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 17:14:53 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[2 minutes guide]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[2 minutes]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[mtom]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SWA]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=358</guid>
		<description><![CDATA[In PHP you have several ways of sending binary data. It can be primarily categorized in to non-optimized method (send as base64 binary) and optimized method (send as MTOM or SWA). Here I m talking about how to send binaries in above mentioned methods starting from a WSDL. WSDL Think you have a WSDL with [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP you have several ways of sending binary data. It can be primarily categorized in to non-optimized method (send as base64 binary) and optimized method (send as MTOM or SWA). Here I m talking about how to send binaries in above mentioned methods starting from a WSDL.</p>
<p><strong>WSDL</strong></p>
<p>Think you have a WSDL with the following XML Schema.</p>
<pre class="xml">            <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- Here is my submitPerson method-&gt;</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;">"submitPerson"</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;">name</span>=<span style="color: #ff0000;">"name"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:string"</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;">name</span>=<span style="color: #ff0000;">"age"</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;">name</span>=<span style="color: #ff0000;">"photo"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:base64Binary"</span></span><span style="color: #009900;"><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>The submit Person method submit the name,age and a photo which is a type of base64Binary.</p>
<p><strong>Generated Class</strong></p>
<p>After generating php class for this piece of code using <a href="http://labs.wso2.org/wsf/php/wsdl2phptool.php">wsdl2php</a> you will have  the following class.</p>
<pre class="php"><span style="font-weight: bold; color: #000000;">class</span> submitPerson <span style="color: #66cc66;">{</span>

    <span style="font-style: italic; color: #808080;">/**
     * @var string
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$name</span>;

    <span style="font-style: italic; color: #808080;">/**
     * @var int
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$age</span>;

    <span style="font-style: italic; color: #808080;">// You need to set only one from the following two vars</span>

    <span style="font-style: italic; color: #808080;">/**
     * @var Plain Binary
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$photo</span>;

    <span style="font-style: italic; color: #808080;">/**
     * @var base64Binary
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$photo_encoded</span>;

<span style="color: #66cc66;">}</span></pre>
<p>So it is very easy to fill the class with your own data. Note that you only need to fill one of the &#8216;photo&#8217; or &#8216;photo_encoded&#8217; fields. If you have binary already converted to base64 then you can use the &#8216;photo_encoded&#8217; field where as if you only have the row binary just use the &#8216;photo&#8217; field. Here are my values for this particular example.</p>
<pre class="php"><span style="color: #0000ff;">$person</span> = <span style="font-weight: bold; color: #000000;">new</span> submitPerson<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">name</span> = <span style="color: #ff0000;">"xxxx yyy"</span>;
<span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">age</span> = <span style="color: #cc66cc;">35</span>;
<span style="color: #0000ff;">$person</span>-&gt;<span style="color: #006600;">photo</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"/photo/xxxyyy"</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Sending Binary as MTOM</strong></p>
<p>Here is the code you need to send the above structure as a MTOM message.</p>
<pre class="php"><span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>; <span style="font-style: italic; color: #808080;">// anyway useMTOM is default to TRUE for WSClient</span>
<span style="color: #0000ff;">$proxy</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">getProxy</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$response</span> = <span style="color: #0000ff;">$proxy</span>-&gt;<span style="color: #006600;">submitPerson</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$person</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Sending Binary as Base64 Encoded string<br />
</strong></p>
<p>You only need to change one option. That is setting &#8220;useMTOM&#8221; to FALSE will send the binary as Base64.</p>
<pre class="php"><span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">FALSE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$proxy</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">getProxy</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$response</span> = <span style="color: #0000ff;">$proxy</span>-&gt;<span style="color: #006600;">submitPerson</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$person</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>SWA (SOAP With Attachments) </strong><br />
You can send the binary data as SWA by setting the &#8220;useMTOM&#8221; option to &#8220;SWA&#8221;. SWA is also a binary optimized method of sending attachments, but unlike with MTOM you can&#8217;t integrate security or reliability with this approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/22/send-binary-with-web-services-in-php-2-minutes-introduction/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Hack Axis2 Codegen templates from an Axis2 Binary</title>
		<link>http://www.dimuthu.org/blog/2008/09/13/how-to-hack-axis2-codegen-templates-from-a-axis2-binary/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/13/how-to-hack-axis2-codegen-templates-from-a-axis2-binary/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 14:35:17 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[adb]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[codegen]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsdl2c]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[Axis2/Java]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[Custom templates]]></category>
		<category><![CDATA[Skeleton]]></category>
		<category><![CDATA[Stub]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=255</guid>
		<description><![CDATA[I have been working for Axis2/C codegen tool for sometime now and I found lot of users who want to edit the codegen templates for a more optimized code specific to their use cases. This is mainly because codegen tool generates a very general code (For an example lot of unused variables generated for some [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working for <a href="http://ws.apache.org/axis2/c/">Axis2/C</a> codegen tool for sometime now and I found lot of users who want to edit the codegen templates for a more optimized code specific to their use cases. This is mainly because codegen tool generates a very general code (For an example lot of unused variables generated for some WSDLs, but they are used in some other WSDLs) where as mostly &#8216;C&#8217; people prefer to be as optimized as possible. Axis2 Java templates are more stable and not needed to customized as much as C templates, but Java developers too may find it is really useful to edit them in a case they need some customizations.</p>
<p>Both Axis2 C and Java tools comes with <a href="http://ws.apache.org/axis2/">Axis2/Java project</a>. So you may already have downloaded the latest <a href="http://ws.apache.org/axis2/download.cgi">Axis2/Java release</a> or <a href="http://builder.wso2.org/browse/AXIS2-NIGHTLY">the Axis2/Java nightly build</a>. As per title you can do this customization not only in the source but also in the binary pack. So I assume you have the binary pack.</p>
<p>Codegen tool mainly comes within two jars inside the lib directory of your Axis2/Java pack. (Here xxxx should be replaced with the particular jar version, it can be 1.4, 1.4.1 or SNAPSHOT)</p>
<ul>
<li>axis2-codegen-xxxx.jar &#8211; The core classes of the codegen (This is where WSDL2C and WSDL2Java classes reside)</li>
<li>axis2-adb-codegen-xxxx.jar &#8211; This is the library containing the adb component of the codegen tool. ADB (Axis2 Data Binding) is the native databinding format of Axis2 which convert your xml schema to a more programmer friendly set of java classes or &#8216;C&#8217; structures. The XSD2Java class comes in this jar.</li>
</ul>
<p><strong>Editing the Stub and Skeleton templates</strong></p>
<p>When you unpack the axis2-codegen-xxxx.jar</p>
<pre>jar xf axis2-codegen-xxxx.jar</pre>
<p>you can find the template for Stub and Skel in org/apache/axis2/wsdl/template/c or org/apache/axis2/wsdl/template/java directories.</p>
<ul>
<li>Stub For C &#8211; org/apache/axis2/wsdl/template/c/StubSourceTemplate.xsl (source) and org/apache/axis2/wsdl/template/c/StubHeaderTemplate.xsl (header)</li>
<li>Skeleton for C &#8211; org/apache/axis2/wsdl/template/c/SkelSourceTemplate.xsl (source) and org/apache/axis2/wsdl/template/c/SkelHeaderTemplate.xsl (header)</li>
<li>Stub For Java &#8211; org/apache/axis2/wsdl/template/java/InterfaceImplementationTemplate.xsl</li>
<li>Skel For Java &#8211; org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl (Message Reciever) and org/apache/axis2/wsdl/template/java/SkeletonTemplate.xsl (skeleton)</li>
</ul>
<p>These templates are written is XSL. Even if you are an XSL expert, you may find little difficult to understand whole lot of codes in there. But if you really know what you need to change, you can track back the lines (may be using comment or some specific static set of code). So you don&#8217;t actually need to know any XSL to do these little customizations.</p>
<p>After you do the change simply pack the classes (which are in the &#8216;org&#8217; directory) as the jar with the same name.</p>
<pre>jar cf axis2-codegen-xxxx.jar org</pre>
<p><strong>Editing the ADB templates</strong></p>
<p>You can similarly observe the axis2-adb-codegen-xxxx.jar. All the templates are in the org/apache/axis2/schema/template/ directory.</p>
<ul>
<li>C ADB templates &#8211; org/apache/axis2/schema/template/CADBBeanTemplateHeader.xsl (Source) and org/apache/axis2/schema/template/CADBBeanTemplateHeader.xsl (Header)</li>
<li>Java ADB template &#8211; org/apache/axis2/schema/template/ADBBeanTemplate.xsl</li>
</ul>
<p>You will find the template for the Java classes and C structures there. In C code generation if you find some variables are not used throughout your WSDLs, you can remove directly if from the templates. And some constants which are defined to allocate memory can be changed to suit to your application.</p>
<p>Anyway after all these notes, I have to say it is not really recommended to hack a code specific to your application. If you found a change general to all the applications you better submit the patch to the Axis2 community, so others too can use it. Otherwise whenever you do a upgrade you have to do this again on your own. But I hope these tips may be useful specially when you are working with ADB to do your own experiments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/13/how-to-hack-axis2-codegen-templates-from-a-axis2-binary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Send Binary in SOAP with WSF/PHP 1.3.2</title>
		<link>http://www.dimuthu.org/blog/2008/08/19/send-binary-in-soap-with-wsfphp-132/</link>
		<comments>http://www.dimuthu.org/blog/2008/08/19/send-binary-in-soap-with-wsfphp-132/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 02:57:17 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[mtom]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=195</guid>
		<description><![CDATA[In a valid XML you can only have text and you can not have binary characters. SOAP which in fact an XML is also having this issue. So it is not staright forward to send binary data in SOAP. But with WSO2 WSF/PHP 1.3.2 It is really easy to send binaries. Check the online WSF/PHP [...]]]></description>
			<content:encoded><![CDATA[<p>In a valid XML you can only have text and you can not have binary characters. SOAP which in fact an XML is also having this issue. So it is not staright forward to send binary data in SOAP. But with <a title="WSO2, WSF/PHP" href="http://wso2.org/projects/wsf/php">WSO2 WSF/PHP 1.3.2 </a>It is really easy to send binaries. Check the <a href="http://labs.wso2.org/wsf/php/source_page_frame.php?src=samples/mtom">online WSF/PHP Mtom Sample sources</a> to understand the few lines that needed to send and serve binaries.</p>
<p>To understand what is happening inside WSF/PHP to send your message we will first take a simple demo. To understand the messages I will be sending text data rather than binary so you can clearly see how your data goes through the wire.</p>
<p>Here is my code to send the binary. (in fact a test in this percular case)</p>
<pre><span style="font-weight: bold; color: #000000;">&lt;?php</span>
<span style="color: #0000ff;">$requestPayloadString</span> = &lt;&lt;&lt;XML
&lt;ns1:upload xmlns:ns1=<span style="color: #ff0000;">"http://php.axis2.org/samples/mtom"</span>&gt;
  &lt;ns1:fileName&gt;test.txt&lt;/ns1:fileName&gt;
  &lt;ns1:image xmlmime:contentType=<span style="color: #ff0000;">"text/txt"</span> xmlns:xmlmime=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span>&gt;

    &lt;xop:<span style="color: #b1b100;">Include</span> xmlns:xop=<span style="color: #ff0000;">"http://www.w3.org/2004/08/xop/include"</span> href=<span style="color: #ff0000;">"cid:myid1"</span>&gt;&lt;/xop:Include&gt;
  &lt;/ns1:image&gt;
&lt;/ns1:upload&gt;
XML;

try <span style="color: #66cc66;">{</span>
    <span style="color: #0000ff;">$f</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"test.txt"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$requestMessage</span> = <span style="font-weight: bold; color: #000000;">new</span> WSMessage<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$requestPayloadString</span>,
        <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"http://localhost/my_mtom_service.php"</span>,
        <span style="color: #ff0000;">"attachments"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"myid1"</span> =&gt; <span style="color: #0000ff;">$f</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="color: #ff0000;">"FALSE"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$responseMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$requestMessage</span><span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$responseMessage</span>-&gt;<span style="color: #006600;">str</span>;

<span style="color: #66cc66;">}</span> catch <span style="color: #66cc66;">(</span>Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$e</span> instanceof WSFault<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Soap Fault: %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">Reason</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>

        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Message = %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>,<span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>

<span style="font-weight: bold; color: #000000;">?&gt;</span></pre>
<p>And test.txt has the following text</p>
<pre>TEXT as BINARY</pre>
<p>In here the &#8216;xop:include&#8217; element is where you going to send either the binary data or some reference to binary data.<br />
<strong>&#8220;useMTOM&#8221; =&gt; FALSE</strong><br />
Note that in there in the &#8220;useMTOM&#8221; option for the WSClient I have given the value FALSE. That mean don&#8217;t use the MTOM(<span>Message Transmission Optimization Mechanism) to serialize the message.There the generated soap message (over HTTP) will look like this,</span></p>
<pre>POST /samples/mtom/mtom_upload_service.php HTTP/1.1
User-Agent: Axis2C/1.5.0
Content-Length: 404
Content-Type: application/soap+xml;charset=UTF-8
Host: 127.0.0.1:8080

<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://www.w3.org/2003/05/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:Header</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;ns1:upload</span> xmlns:ns1=<span style="color: #ff0000;">"http://php.axis2.org/samples/mtom"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>test.jpg<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:image</span> <span style="color: #000066;">xmlmime:contentType</span>=<span style="color: #ff0000;">"text/txt"</span> <span style="color: #000066;">xmlns:xmlmime</span>=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span><span style="font-weight: bold; color: black;">&gt;</span></span>VEVYVCBhcyBCSU5BUlkK<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:image<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:upload<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>The value &#8220;<strong>VEVYVCBhcyBCSU5BUlkK</strong>&#8221; is base64 encoded value for the &#8220;TEXT as BINARY&#8221; text.</p>
<pre><span style="font-weight: bold; color: #000000;">&lt;?php</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <a href="http://www.php.net/base64_encode"><span style="color: #000066;">base64_encode</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"TEXT as BINARY"</span><span style="color: #66cc66;">)</span>;
<span style="font-weight: bold; color: #000000;">?&gt;</span></pre>
<p>In fact it is a standard that map each 6 bit of your binary file to an 8 bit (valid character to send through XML), so you can send the encoded value in the soap message. But no doubt you see the drawback. The encoded value is 4/3rd of the original data size. So this is not the most optimized way to send binary over SOAP.<br />
<strong>&#8220;useMTOM&#8221; =&gt; TRUE</strong><br />
Setting this option TRUE will generate the following soap message over http.</p>
<pre>POST /samples/mtom/mtom_upload_service.php HTTP/1.1
User-Agent: Axis2C/1.5.0
Transfer-Encoding: chunked
Content-Type: multipart/related; boundary=MIMEBoundary594038a6-6d95-1dd1-2b72-00197e732d4d; type="application/xop+xml"; start="<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">0</span></span>.594038e2-6d95-1dd1-2b73-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>"; start-info="application/soap+xml"; charset="UTF-8"
Host: 127.0.0.1:8080

32
--MIMEBoundary594038a6-6d95-1dd1-2b72-00197e732d4d
2

b2
content-transfer-encoding: binary
content-id: <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">0</span></span>.594038e2-6d95-1dd1-2b73-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>content-type: application/xop+xml;charset=UTF-8;type="application/soap+xml";21ff
   <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://www.w3.org/2003/05/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:Header</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;ns1:upload</span> xmlns:ns1=<span style="color: #ff0000;">"http://php.axis2.org/samples/mtom"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>test.jpg<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:image</span> <span style="color: #000066;">xmlmime:contentType</span>=<span style="color: #ff0000;">"text/txt"</span> <span style="color: #000066;">xmlns:xmlmime</span>=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
               <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xop:Include</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"cid:1.594037a2-6d95-1dd1-2b71-00197e732d4d@apache.org"</span> <span style="color: #000066;">xmlns:xop</span>=<span style="color: #ff0000;">"http://www.w3.org/2004/08/xop/include"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:image<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:upload<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>32--MIMEBoundary594038a6-6d95-1dd1-2b72-00197e732d4d27econtent-transfer-encoding: binarycontent-id:
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">1</span></span>.594037a2-6d95-1dd1-2b71-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>content-type: text/txtf<strong>TEXT as BINARY</strong>234--MIMEBoundary594038a6-6d95-1dd1-2b72-00197e732d4d--0</pre>
<p>Note that in the last line you see the data you send as it is. Here the binary is sent as an attachment and the element which suppose to keep the binary data refers to the attachment using the content id (check &#8220;href&#8221; attribute). It is obvious for a large binary data this is really optimized way of sending binaries.</p>
<p><strong>&#8220;useMTOM&#8221; =&gt; &#8220;SWA&#8221;</strong></p>
<p>WSF/PHP supports 1.3.2 supports SWA (Soap with Attachment) as well. you can enable this by setting &#8220;useMTOM&#8221; =&gt; &#8220;swa&#8221;. It is also send the binary as an attachment.  But it is not a recommended approach since you can&#8217;t secure the message when using SWA, because it can not be represented in the XML infoset. Here is the message generated enabling the &#8220;SWA&#8221;,</p>
<pre>POST /samples/mtom/mtom_upload_service.php HTTP/1.1
User-Agent: Axis2C/1.5.0
Transfer-Encoding: chunked
Content-Type: multipart/related; boundary=MIMEBoundarybc636bdc-6d96-1dd1-31c0-00197e732d4d; type="application/xop+xml"; start="<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">0</span></span>.bc636c40-6d96-1dd1-31c1-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>"; start-info="application/soap+xml"; charset="UTF-8"
Host: 127.0.0.1:8080

32
--MIMEBoundarybc636bdc-6d96-1dd1-31c0-00197e732d4d
2

b2
content-transfer-encoding: binary
content-id: <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">0</span></span>.bc636c40-6d96-1dd1-31c1-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>content-type: application/xop+xml;charset=UTF-8;type="application/soap+xml";21b3
   <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://www.w3.org/2003/05/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:Header</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;ns1:upload</span> xmlns:ns1=<span style="color: #ff0000;">"http://php.axis2.org/samples/mtom"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>test.jpg<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:fileName<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:image</span> <span style="color: #000066;">xmlmime:contentType</span>=<span style="color: #ff0000;">"text/txt"</span> <span style="color: #000066;">xmlns:xmlmime</span>=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span><span style="font-weight: bold; color: black;">&gt;</span></span>1.bc6368a8-6d96-1dd1-31bf-00197e732d4d@apache.org<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:image<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:upload<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>

32
--MIMEBoundarybc636bdc-6d96-1dd1-31c0-00197e732d4d
2

7e
content-transfer-encoding: binary
content-id:
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<span style="color: #cc66cc;">1</span></span>.bc6368a8-6d96-1dd1-31bf-00197e732d4d@apache.org<span style="font-weight: bold; color: black;">&gt;</span></span>content-type: text/txtfTEXT as BINARY234--MIMEBoundarybc636bdc-6d96-1dd1-31c0-00197e732d4d--0</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/08/19/send-binary-in-soap-with-wsfphp-132/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

