Scripting the Element - Java Script

Once again,the differences in how browsers access Web services are so vast that you can’t create a crossbrowser method using the current browser functionality.But because Web services are nothing more than standard HTTP requests with a special format,it’s possible to create a cross-browser approach using the XML HTTP request object.For this to work,you use the browser detection script and the script creating a XMLHttpRequest constructor for Internet Explorer.

The WebService object

Each Web service is unique,so how can you possibly develop a standard way to use them? The answer is to create a generalized template object definition that can be overridden to make it more specific.This template object is called WebService and is defined this way:

Once again,the differences in how browsers access Web services are so vast that you can’t create a crossbrowser method using the current browser functionality.But because Web services are nothing more than standard HTTP requests with a special format,it’s possible to create a cross-browser approach using the XML HTTP request object.For this to work,you use the browser detection script and the script creating a XMLHttpRequest constructor for Internet Explorer.

The WebService object

Each Web service is unique,so how can you possibly develop a standard way to use them? The answer is to create a generalized template object definition that can be overridden to make it more specific.This template object is called WebService and is defined this way:

The WebService object is the basis of cross-browser Web service support.It has two properties: the URL for the SOAP request (url) and the SOAP action (action).Both properties are initialized to null; subclasses fill these in as needed.The buildRequest() method is intended to build the SOAP message string; but in this case, it just returns an empty string. The handleResponse() method receives the SOAP message from the response and returns the appropriate value.

The send() method does the heavy lifting for this functionality.First,if the browser is Mozilla, you must request the UniversalBrowserRead privilege. Then,an XMLHttpRequest object is created (remember, this code uses the wrapper for IE), and the SOAP request is built up. This request includes opening a request to the specified URL, setting the content type to “text/xml” and setting the SOAP action.Then, the buildRequest() method is called to get the SOAP message and send it using the XMLHttpRequest’s send() method.

Lastly,if the request returns a status of 200, the text returned is interpreted by the handleResponse() and that value is returned.Otherwise, an error is thrown.The intent is to allow you to use a WebService object like this:

This object, of course, doesn’t have enough information to be used on its own. However, it can be used to create a wrapper for any other Web service.

The Temperature Service

Once again, it’s time to take a look at the Temperature Service.To do this, the TemperatureService object inherits from WebService and defines both the url and action properties:

Remember that the SOAP action for the Temperature Service is actually an empty string, so the default value (inherited from WebService) is fine. Anew property, zipcode, is used to store the zip code to check.

For the buildRequest() method, you first determine the format for the SOAP request.This can easily be done for any Web service by using the WSDL analyzer tool available at XMethods (http://www.xmethods.net/ve2/Tools.po). These methods enable you to see the request and response formats for any Web service with a WSDL file.

The SOAP request for the Temperature Service looks like this:

A whole lot of information is contained in this simple SOAP request, but the important line (which is highlighted) is where the zip code should be entered.The buildRequest() method must create this SOAP string with the zip code inserted:

Because the SOAP request string is so long, this method uses the StringBuffer() object created earlierto build up the string. Note that the zipcode property is used here to insert the zip code in question instead of passing it in as an argument. If the zip code is passed in as an argument, you must rewrite the send() method to take this into account; this way, the send() method can be used as-is.

The handleResponse() method expects to receive a SOAP response string as its only argument. Once again,this format can be determined by using the WSDL analyzer tool from XMethods:

Despite all the extra code, the only part of interest is what is contained within the <return/> element. The easiest way to extract that data from the string is to use a regular expression; there’s no need for expensive DOM parsing and operations in this case (for more complicated Web services, however, that may be an option). After the value is extracted, the parseFloat() function can be used to get the floating-point value:

The only thing left to do is modify the send() method to accept an argument (the zip code) because the zipcode property must be assigned for the method to function properly (remember, the send() method calls buildRequest(), which uses the zipcode property to create the SOAP string).

To accomplish this, you first must create a pointer to the original send() method:

TemperatureService.prototype.webServiceSend = TemperatureService.prototype.send;

This line of code creates a property called webServiceSend that points to the send() function. This procedure makes it possible to redefine send() without losing the original functionality:

The first thing the new send() method does is assign the zipcode property. Then, it returns the result of the webServiceSend() method (which is a call to the original send() method). The TemperatureService object can be used like this:

Using the TemperatureService object

To recreate the temperature example one more time, the code is much simpler:

As you can see, the callWebService() function is greatly simplified in this example. The zip code is obtained from the text box; then the TemperatureService object is created, and the zip code is passed into the send() method, which returns the temperature in degrees. An alert is then displayed indicating the temperature

The WebService object is the basis of cross-browser Web service support.It has two properties: the URL for the SOAP request (url) and the SOAP action (action).Both properties are initialized to null; subclasses fill these in as needed.The buildRequest() method is intended to build the SOAP message string; but in this case, it just returns an empty string. The handleResponse() method receives the SOAP message from the response and returns the appropriate value.

The send() method does the heavy lifting for this functionality.First,if the browser is Mozilla, you must request the UniversalBrowserRead privilege. Then,an XMLHttpRequest object is created (remember, this code uses the wrapper for IE), and the SOAP request is built up. This request includes opening a request to the specified URL, setting the content type to “text/xml” and setting the SOAP action.Then, the buildRequest() method is called to get the SOAP message and send it using the XMLHttpRequest’s send() method.

Lastly,if the request returns a status of 200, the text returned is interpreted by the handleResponse() and that value is returned. Otherwise, an error is thrown.The intent is to allow you to use a WebService object like this:

This object, of course, doesn’t have enough information to be used on its own. However, it can be used to create a wrapper for any other Web service.

The Temperature Service

Once again, it’s time to take a look at the Temperature Service.To do this, the TemperatureService object inherits from WebService and defines both the url and action properties:

Remember that the SOAP action for the Temperature Service is actually an empty string, so the default value (inherited from WebService) is fine. Anew property, zipcode, is used to store the zip code to check.

For the buildRequest() method, you first determine the format for the SOAP request.This can easily be done for any Web service by using the WSDL analyzer tool available at XMethods (http://www.xmethods.net/ve2/Tools.po). These methods enable you to see the request and response formats for any Web service with a WSDL file.

The SOAP request for the Temperature Service looks like this:

A whole lot of information is contained in this simple SOAP request, but the important line (which is highlighted) is where the zip code should be entered.The buildRequest() method must create this SOAP string with the zip code inserted:

Because the SOAP request string is so long, this method uses the StringBuffer() object created earlierto build up the string. Note that the zipcode property is used here to insert the zip code in question instead of passing it in as an argument. If the zip code is passed in as an argument, you must rewrite the send() method to take this into account; this way, the send() method can be used as-is.

The handleResponse() method expects to receive a SOAP response string as its only argument. Once again,this format can be determined by using the WSDL analyzer tool from XMethods:

Despite all the extra code, the only part of interest is what is contained within the <return/> element. The easiest way to extract that data from the string is to use a regular expression; there’s no need for expensive DOM parsing and operations in this case (for more complicated Web services, however, that may be an option). After the value is extracted, the parseFloat() function can be used to get the floating-point value:

The only thing left to do is modify the send() method to accept an argument (the zip code) because the zipcode property must be assigned for the method to function properly (remember, the send() method calls buildRequest(), which uses the zipcode property to create the SOAP string).

To accomplish this, you first must create a pointer to the original send() method:

TemperatureService.prototype.webServiceSend = TemperatureService.prototype.send;

This line of code creates a property called webServiceSend that points to the send() function. This procedure makes it possible to redefine send() without losing the original functionality:

The first thing the new send() method does is assign the zipcode property. Then, it returns the result of the webServiceSend() method (which is a call to the original send() method). The TemperatureService object can be used like this:

Using the TemperatureService object

To recreate the temperature example one more time, the code is much simpler:

As you can see, the callWebService() function is greatly simplified in this example. The zip code is obtained from the text box; then the TemperatureService object is created, and the zip code is passed into the send() method, which returns the temperature in degrees. An alert is then displayed indicating the temperature


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics