Archive for October, 2009

Use XForms to send and receive Web services messages

This tip assumes a basic familiarity with XForms and with Web services. It uses the FormsPlayer plug-in for Internet Explorer (see Resources to download it).

The Web service

In this tip, I’m going to show you how to create an XForms form that sends a request to a Web service and then displays the results. To keep things simple, I’m going to use the ubiquitous weather example, sending a ZIP code and receiving the current temperature. The actual Web service is provided by XMethods.net (see Resources).

The initial request is just a SOAP message, such as:

Listing 1. Initial SOAP request

<SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
       xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <SOAP-ENV:Body>
      <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature"
           SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <zipcode xsi:type="xsd:string">02134</zipcode>

      </ns1:getTemp>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

You’ll use the XForms form to provide a value for the zipcode element. When you submit it to the service, you’ll get a response such as:

Listing 2. Response

<SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
       xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <SOAP-ENV:Body>
      <ns1:getTempResponse xmlns:ns1="urn:xmethods-Temperature"
           SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <return xsi:type="xsd:int">86</return>

      </ns1:getTempResponse>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

You’ll also use the form to retrieve the return value and display it.


The basic XForms form

Start by creating the basic form. An XForms form consists of a model and controls. The model includes information such as the instance, which represents the data to be displayed and submitted, and the submission, which includes information on where and how to send the data:

Listing 3. A basic XForms form

<html
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xforms="http://www.w3.org/2002/xforms"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
   xmlns:ev="http://www.w3.org/2001/xml-events"
   xmlns:ns1="urn:xmethods-Temperature"
   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
>
   <head>

      <title>XForms and Web Services</title>
   </head>
   <body>
      <object id="FormsPlayer"
              classid="CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58">
          <b>FormsPlayer has not loaded. Please check your installation.</b>

      </object>
      <?import namespace="xforms" implementation="#FormsPlayer" ?>

      <xforms:model id="WeatherService">

         <xforms:instance id="messages">

           <SOAP-ENV:Envelope
                xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/1999/XMLSchema">
              <SOAP-ENV:Body>
                 <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature"
                    SOAP-ENV:encodingStyle=
                           "http://schemas.xmlsoap.org/soap/encoding/">
                    <zipcode xsi:type="xsd:string">02134</zipcode>
                 </ns1:getTemp>
              </SOAP-ENV:Body>
           </SOAP-ENV:Envelope> 

         </xforms:instance>

         <xforms:submission id="getweather"
             method="text-xml-post"
             replace="instance"
             action="http://services.xmethods.net:80/soap/servlet/rpcrouter"
             />

      </xforms:model>

</body>
</html>

First, tell Internet Explorer to load the FormsPlayer object. If it’s not loaded, the browser displays the contents of the tag, telling the user to download and install it.

Next you have the model, which includes the instance and the submission. The instance is not just the data to be displayed; it’s also the data that will eventually be submitted, so it’s identical to the SOAP request presented earlier in Listing 1.

You’ll get a closer look at the submission itself when you actually submit the data.

In the meantime, you need to actually create the form:

Listing 4. Create the form

...
   <xforms:model id="WeatherService">

     <xforms:instance id="messages">

       <SOAP-ENV:Envelope
            xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/1999/XMLSchema">
          <SOAP-ENV:Body>
             <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature"
                SOAP-ENV:encodingStyle=
                       "http://schemas.xmlsoap.org/soap/encoding/">
                <zipcode xsi:type="xsd:string">02134</zipcode>
             </ns1:getTemp>
          </SOAP-ENV:Body>
       </SOAP-ENV:Envelope> 

     </xforms:instance>

     <xforms:submission id="getweather"
           method="text-xml-post"
           replace="instance"
           action="http://services.xmethods.net:80/soap/servlet/rpcrouter"
           />

   </xforms:model>

   <xforms:input ref="instance('messages').//zipcode">
   <xforms:label>Zip code: </xforms:label>

   <xforms:hint>Enter a zip code and submit the form for the current
         temperature in that area.</xforms:hint>
  </xforms:input>

</body>
</html>

Figure 1: The simple text control

The simple text control

When the user changes the value in this control, it changes the corresponding node of the XML contained in the instance. So if you were to change the value in that field to, say, 10314, the instance document would look like this in memory:

Listing 5. Instance document

<SOAP-ENV:Envelope
      xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <SOAP-ENV:Body>
       <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature"
           SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

         <zipcode xsi:type="xsd:string">10314</zipcode>
       </ns1:getTemp>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

It is this document that gets submitted to the service. Before you get that far, however, you need to look at the state of the form.


Changing states

When you submit the form, you will actually replace the instance document with a second instance document that has a different structure, so in order to display information from it, you will need controls based on that structure instead of the original one. You can’t, however, include both of these controls on the page at the same time, because one of them will always refer to a nonexistent node, causing an error. To get around this problem, you need to create a situation in which only one set appears at a time. You can do that with a switch:

Listing 6. Add a switch

...
   </xforms:model>

   <xforms:switch id="switch1">

      <xforms:case id="requestGUI">

         <xforms:input ref=".//zipcode">
            <xforms:label>Zip code: </xforms:label>
            <xforms:hint>Enter a zip code and submit the form for the current
                  temperature in that area.</xforms:hint>

         </xforms:input>

         <xforms:trigger style="display:block">
            <xforms:label>Change to results case</xforms:label>
            <xforms:action ev:event="DOMActivate">
               <xforms:toggle case="responseGUI" />

            </xforms:action>
         </xforms:trigger>

      </xforms:case>

      <xforms:case id="responseGUI">

         Results go here.

         <xforms:trigger>

            <xforms:label>Change to request case</xforms:label>
            <xforms:action ev:event="DOMActivate">
               <xforms:toggle case="requestGUI" />
            </xforms:action>
        </xforms:trigger>

     </xforms:case>

 </xforms:switch>

</body>
</html>

Here there are two cases. The first is displayed by default. To this case you add a trigger, or button, that activates when you click it, as in Figure 2.

Figure 2. Add a trigger

Add a trigger

When it does, it executes everything in the action element, which in this case is to toggle the case to the responseGUI state. This tells the browser to display only that case, as in Figure 3:

Figure 3. The results case

The results case

Using this form, the user can toggle back and forth by clicking the buttons. Now you need to add the form submission.


Submitting the form

Of course, the form isn’t much good to you this way. What you ultimately want is to switch to the results case after you submit the form. You can do that by adding the submission to the action:

Listing 7. Add the submission to the action

...
         <xforms:trigger style="display:block">
            <xforms:label>Get current temperature</xforms:label>
            <xforms:action ev:event="DOMActivate">
               <xforms:send submission="getweather" />
               <xforms:toggle case="responseGUI" />

            </xforms:action>
         </xforms:trigger>
...

This simply tells the browser to execute the submission getweather when the user clicks the button. Now, when you created the submission, you added some information to it:

Listing 8. Specify the submission as an XML post

...
      <xforms:submission id="getweather"
           method="text-xml-post"
           replace="instance"
           action="http://services.xmethods.net:80/soap/servlet/rpcrouter"
               />
...

This submission specifies that you’re submitting it as an XML post, which is perfect for a SOAP message, and that you want to replace the instance. This way, when it comes back, the instance looks something like Listing 9:

Listing 9. Returned instance

<SOAP-ENV:Envelope
       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
       xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <SOAP-ENV:Body>
      <ns1:getTempResponse xmlns:ns1="urn:xmethods-Temperature"
          SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <return xsi:type="xsd:int">86</return>
      </ns1:getTempResponse>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

To see the results, you need to retrieve them from within the resultGUI case:

Listing 10. Retrieve the results

  <xforms:case id="responseGUI">

    <xforms:output ref="instance('messages')//return"
            style="width:40ex;display:block;">
      <xforms:label>Current Temperature:</xforms:label>
    </xforms:output>

  </xforms:case>
...

This way, when you submit the form, you get the resulting temperature, as in Figure 4.

Figure 4. The resulting page

The resulting page


Summary

Because an XForms form sends and receives XML documents, it makes an excellent Web services client. In this tip, I showed you the basic process of using an XForms form to request information from a Web service. The process looked like this:

  1. The browser displays information from the instance document, which is structured as a SOAP request.
  2. The browser sends the instance document (including the ZIP code) to the Web service.
  3. The Web service sends a response (including the temperature) to the browser, which uses it to replace the original instance document.
  4. The browser switches to a second set of controls, which display the information returned by the Web service.

Leave a comment »

Images of Beautiful Cleveland

Cleveland

Cleveland

Cleveland

Cleveland

Cleveland

Leave a comment »

Adultery Hoax on Facebook ends in Divorce

People get tricked, scammed and embarrassed on Facebook all the time, so it takes a really funny/depressing story to get my attention. Taken from Banned In Hollywood, this hilarious case of adultery-gone-wrong has everything: revenge, travel, deception and, of course, soccer.

Last November, 39-year-old Stuart Slann, resident of South Yorkshire, England and loyal Manchester United fan, was vacationing in Cancun when he met two rabid Liverpool supporters (Liverpool and Manchester United are bitter rivals). The two men grew tired of Slann’s boasting (Manchester currently holds the English Premier League and Champions League titles) and decided to throw him in the pool for being an arrogant wanker. Slann, however, had yet to see the full extent of the Liverpudians’ retribution.

Upon returning to Liverpool, the two men (who are also cage fighters) decided to set up a fake Facebook profile under the name “Emma” in an attempt to lure Slann into a virtual love affair. The devious plan worked perfectly and to devastating effect.
Slann drove 500 miles from his home to a remote location in North Scotland, where he thought Emma was eagerly awaiting his arrival. When he finally reached the address “Emma” had given him, there was nothing but an old, deserted farm. Soon after arriving, he received a text from “Emma,” saying that she was still at work and that he would have to wait a while. After waiting for three hours in his car, a worried Slann called the number he had been texting and was shocked when a man picked up the phone. “Hello Stuart,” the man said, “do you remember us? It’s them Scouse lads who threw you in the pool. You’ve been framed.”

Slann, who was also tricked into taking a “rude” photo of himself with his camera phone on the drive north, has since been divorced by his wife. While he does think the trick was cruel, he said, “I’ll hold my hands up and say they really wound me up.”

Brutal. Awesomely brutal. [From: BannedinHollywood]

Leave a comment »

Old News is So Exciting!

Old News

Leave a comment »

Military studies virtual reality as therapy for post traumatic stress disorder

ptsd

A new, high-tech system designed to treat military veterans suffering from Post-Traumatic Stress Disorder — or PTSD — may be familiar to fans of a squad-based combat video game.

Using components from the popular game Full Spectrum Warrior, psychologist Skip Rizzo and his colleagues have fashioned a “virtual” world that simulates the sources of combat stress.

The project is a joint venture between the Institute for Creative Technologies — a cutting-edge research lab at the University of Southern California — and the Office of Naval Research. The object is to help veterans come to terms with what they’ve experienced in places like Iraq and Afghanistan by immersing vets in the sights and sounds of those theaters of battle.

The soldier being treated wears VR goggles and headphones. Using a tablet-based interface, a therapist can activate or remove the sounds of gunshots or the sight of smoke, depending on a patient’s reaction. The idea is to re-introduce the patients to the experiences that triggered the trauma, gradually, until the memory no longer incapacitates them.

Eventually, Rizzo believes the therapy will include other stimuli, such as vibrations to simulate the impact of bombs or rumbling of tanks, and even the smells of war — the body odor, garbage and spices of urban combat, for example.

Early results from trials suggest virtual reality therapy is uniquely suited to a generation raised on video games played on Cheap Game Consoles. The gaming aspect of the treatment also helps to lessen the stigma associated with getting therapy.

Leave a comment »

Beethoven Klaviersonaten

The Klangbogen Wien 2000 summer festival includes performances of Beethoven’s complete 32 piano sonatas by nine young pianists in the Beethoven-Saal of the Baroque Palais Palffy. Representing the true origins of the solo piano genre, the sonata season kicks off with the first three Sonatas performed by Paul Gulda, son of the famous classical and jazz pianist Friedrich Gulda who died earlier this year. Other recommended concerts include that of Austrian Roland Batik who takes on Sonatas 18-22. A fine opportunity to hear Beethoven’s most touching pieces in an incomparable historic setting.

Comments off