<?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>James Ward &#187; SOAP</title>
	<atom:link href="http://www.jamesward.com/category/soap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamesward.com</link>
	<description>Heroku &#124; Java &#124; Scala &#124; Cloud &#124; Open Source &#124; Linux</description>
	<lastBuildDate>Fri, 03 Feb 2012 19:29:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Using an Embedded WSDL with Flex&#8217;s WebService API</title>
		<link>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-an-embedded-wsdl-with-flexs-webservice-api</link>
		<comments>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 14:42:53 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[flexorg]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2255</guid>
		<description><![CDATA[Recently I was helping a customer figure out how to use an embedded WSDL with Flex&#8217;s WebService API. One scenario in which this is needed is when the actual WSDL is not available at runtime. In this case the application must contain the WSDL instead of request it at runtime. The Flex WebService API today [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was helping a customer figure out how to use an embedded WSDL with Flex&#8217;s WebService API.  One scenario in which this is needed is when the actual WSDL is not available at runtime.  In this case the application must contain the WSDL instead of request it at runtime.  The Flex WebService API today only supports loading the WSDL over the network at runtime.  Beginning in Flash Builder 4 the Service wizard generate code that internally use the WebService API.  So no matter how you integrate with a SOAP Web Service in Flex, you need the WSDL accessible via a URL at runtime.  This wasn&#8217;t possible for the customer I was working with so we figured out a way to actually embed the WSDL into the application.  Here is what we did&#8230;</p>
<p>I used my <a href="http://www.jamesward.com/census2">Census</a> SOAP Service as a simple service to test this with.  In this case, my WSDL is publicly available at: <a href="http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl">http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl</a></p>
<p>First, I created a new Flex project and saved the WSDL into the src dir of the project.  I built a simple test program using the WebService API directly:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span> <span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:WebService</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ws&quot;</span> <span style="color: #000066;">wsdl</span>=<span style="color: #ff0000;">&quot;http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		ws.getElements(0, 50);
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:DataGrid</span> <span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{ws.getElements.lastResult}&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then I ran the application in Chrome with the Network view open in the Chrome Developer Tools panel.  This indicated that, just as expected, the WSDL is used at runtime:<br />
<img src="http://www.jamesward.com/wp/uploads/2011/04/flex_wsdl_webservice.png" title="WSDL Loaded with the Flex WebService API" width="806" height="511" class="alignnone size-full wp-image-2259" /></p>
<p>Next I had a look around the <em>WebService</em> source code (available in &lt;FLEX_SDK&gt;/frameworks/projects/rpc/src/mx/rpc/soap) and discovered there is a <em>loadWSDL</em> method that can be overwritten to handle the embedded loading of the WSDL instead of the default network loading of the WSDL.  So I created a new class that extends the base <em>WebService</em> class, embeds the WSDL file, and overrides the <em>loadWSDL</em> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">ByteArrayAsset</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">mx_internal</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">WSDLLoadEvent</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">soap</span>.<span style="color: #006600;">mxml</span>.<span style="color: #006600;">WebService</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">wsdl</span>.<span style="color: #006600;">WSDL</span>;
&nbsp;
	use namespace mx_internal;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">dynamic</span> <span style="color: #000000; font-weight: bold;">class</span> MyWebService <span style="color: #0066CC;">extends</span> WebService
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;CensusSOAPService.xml&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const WSDL_CLASS:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyWebService<span style="color: #66cc66;">&#40;</span>destination:<span style="color: #0066CC;">String</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>destination<span style="color: #66cc66;">&#41;</span>;
			loadWSDL<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadWSDL<span style="color: #66cc66;">&#40;</span>uri:<span style="color: #0066CC;">String</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> thing:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> WSDL_CLASS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> baa:ByteArrayAsset = <span style="color: #66cc66;">&#40;</span>thing as ByteArrayAsset<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">xml</span>:<span style="color: #0066CC;">String</span> = baa.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#40;</span>baa.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			deriveHTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> wsdlLoadEvent:WSDLLoadEvent = WSDLLoadEvent.<span style="color: #006600;">createEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WSDL<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">xml</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			wsdlHandler<span style="color: #66cc66;">&#40;</span>wsdlLoadEvent<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In the regular <em>WebService</em> class, setting the <em>wsdl</em> property will trigger the <em>loadWSDL</em> method, but since we are embedding the WSDL, we must manually call the <em>loadWSDL</em> method.  I do that in the constructor.</p>
<p>Embedded assets become a class, so first an instance of that class must be instantiated as a <em>ByteArrayAsset</em> (the default type for files embedded with the application/octet-stream mimeType).  Then the instance is read into a string.  Then the <em>deriveHTTPService</em> method is called to set up the underlying <em>HTTPService</em>&#8216;s channel information.  Finally, we create a <em>WSDL</em> object from the <em>XML</em> that was read from the <em>ByteArrayAsset</em>, create a new <em>WSDLLoadEvent</em> with the <em>WSDL</em>, and call the <em>wsdlHandler</em> method, passing it the <em>wsdlLoadEvent</em>.  This essentially is doing the same thing as the original <em>WebService</em> class, but now doing it without the network request.</p>
<p>I can now switch my test application to use my extension to WebService instead of the original one:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MyWebService</span> <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ws&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now when I run the application and monitor the network activity I no longer see the WSDL being requested at runtime!  So everything works when using the <em>WebService</em> API directly.  However, the customer I was working with was using the Service wizard in Flash Builder.  So we needed to figure out how to get the generated code to use the new <em>WebService</em> extension instead of the original <em>WebService</em> class.  I went through the Data Wizards and had it generate the client-side stubs for my Census SOAP Service.  This created a <em>CensusSOAPService</em> class that extends the generated <em>_Super_CensusSOAPService</em> class.  The <em>CensusSOAPService</em> is intended to give us a place to make modifications to the generated stuff, while the <em>_Super_CensusSOAPService</em> class is not supposed to be modified because it will be overwritten if we refresh the service.  Looking in the <em>_Super_CensusSOAPService</em> class I discovered that the <em>WebService</em> instance is being created directly in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _Super_CensusSOAPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// initialize service control</span>
        _serviceControl = <span style="color: #000000; font-weight: bold;">new</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">soap</span>.<span style="color: #006600;">mxml</span>.<span style="color: #006600;">WebService</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// rest of method omitted</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>These are the kinds of things that really make you wish the Flex framework used dependency injection because we need to set <em>_serviceControl</em> from the <em>CensusSOAPService</em> class.  So we thought&#8230;  Alright, this is not ideal but we can just copy the contents of the <em>_Super_CensusSOAPService</em>&#8216;s constructor into <em>CensusSOAPService</em>&#8216;s constructor, replace the line that instantiates the <em>WebService</em>, have it instantiate <em>MyWebService</em> instead, and then just not call <em>super()</em>.  We gave it a try and for some reason kept getting <em>_serviceControl</em> set as a <em>WebService</em> not <em>MyWebService</em>.  WTF?  It made no sense until we found this little gem in the <a href="http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf67eed-7fff.html">Flex docs</a>:<br />
<em>&#8220;If you define the constructor, but omit the call to super(), Flex automatically calls super() at the beginning of your constructor.&#8221;</em><br />
Now it all made sense!  Since we didn&#8217;t call <em>super()</em>, Flex conveniently inserted a <em>super()</em> call for us!  Fun.  So we had to figure out a way to convince the Flex compiler that we were going to call <em>super()</em>, but then not call it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Voila!  Now the <em>CensusSOAPService</em>&#8216;s constructor sets <em>_serviceControl</em> to a new instance of <em>MyWebService</em> and <em>_Super_CensusSOAPService</em> doesn&#8217;t get the chance to mess that up.</p>
<p>We tested the new <em>CensusSOAPService</em> and everything worked perfectly!</p>
<p>I hope that helps some of you who are using the Flex WebService API.  Let me know if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Video &#8211; Connecting Flex to a SOAP Webservice</title>
		<link>http://www.jamesward.com/2009/04/20/video-connecting-flex-to-a-soap-webservice/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=video-connecting-flex-to-a-soap-webservice</link>
		<comments>http://www.jamesward.com/2009/04/20/video-connecting-flex-to-a-soap-webservice/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 16:39:17 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=939</guid>
		<description><![CDATA[Adobe TV has published a video I did recently about connecting Flex to a SOAP Webservice. Check it out and let me know what you think.]]></description>
			<content:encoded><![CDATA[<p><a href="http://tv.adobe.com">Adobe TV</a> has published a video I did recently about connecting Flex to a SOAP Webservice.  Check it out and let me know what you think.</p>
<p><embed src="http://tv.adobe.com/Embed.swf" quality="high" bgcolor="#000000" width="600" height="385" name="AdobeTVPlayer" play="true" loop="false" quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" flashVars="v=~b64~aHR0cDovL2Fkb2JlLmVkZ2Vib3NzLm5ldC9mbGFzaC9hZG9iZS9hZG9iZXR2Mi9hZGNfcHJlc2VudHMvNjRfYWRjXzExNC5mbHY/cnNzX2ZlZWRpZD0xNDcyJnhtbHZlcnM9Mg==&#038;w=600&#038;t=http://tv.adobe.com/vi+f1472v1078&#038;h=385"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2009/04/20/video-connecting-flex-to-a-soap-webservice/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

