Working with an existing web service
Learning objectives
By completing this module, you will:
Learn how WSDL definitions are reflected in NexJ messages.
Learn how to extend a class model to include a web service.
Reinforce your knowledge about creating a channel.
Reinforce or introduce some Presentation layer knowledge.
Prerequisite: The SOA service created in the previous lesson.
Key concepts
- WSDL is a standard XML format for describing XML web services that define the set of operations and messages that can be sent to and received from a given web service.
In this learning module, we add an attribute that gets its value from a web service call to the Person
class. An input value of City
is passed to the service, and a response message is returned with the temperature. This can then be optionally displayed in the user interface, demonstrating how you can quickly integrate with external non-relational data sources.
Importing the WSDL definition
To simplify interacting with the web service, we create message definitions by importing the WSDL definition.
Learning activity
To import the WDSL definition:
- From the File menu, select Import.
- In the Import dialog, select NexJ Studio > XML Schema/WSDL and click Next.
- In the File paths or URLs field, enter
http://localhost:7080/training/soa/training:weather:1.0/soap?wsdl
and click Next. - In the Message Prefix field, enter
Weather_
. - Leave all checkboxes unchecked, and click Finish.
In the Integration layer, select the Messages tab.
A number of new messages with names that start with "Weather_" have been created. To see only these new messages, type
Weather
in the Search field.Double-click the
Weather_getWeather
message to open it and review the source. Note that the expected input is acityName
.- Open the
Weather_getWeatherResponse
message and review the outputs that come back as a response.
Creating a new channel for integration
You are now ready to set up for accessing the web service through a channel. You'll need to create a new channel for the web service and add it to the environment.
Learning activity
To create a new channel:
- In the Integration layer, select the Channels tab.
- Right-click and select New Channel.
- Specify the following details:
- Name:
training:Weather
- Channel Type:
HTTP
- Click Finish.
- Name:
In the Source tab, enter the following code:
XML<HTTP contentType="text/xml" get="true" url="http://localhost:7080/training/soa/training:weather:1.0/soap"/>
In the new channel, specify http://localhost:7080/training/soa/training:weather:1.0/soap
as the URL.
- Set the content type as
text/xml
. - Enable the GET verb.
- Save your work.
To add the channel to the environment:
- In the Deployment layer, select the Environments tab.
- Double-click the Development environment to open it for editing.
- In the Channel Connections tab, click the Select Channel Connections button . The Select Channel Connections dialog opens.
- Add the
TrnWeather
channel. - Set authentication to
none
and the user and password tonexjsa
andnexj
respectively. - Click OK.
Save your work.
The channel should look similar to the following:XML<HTTPConnection authentication="none" channel="training:WeatherChannel" password="text:nexj" user="nexjsa"/>
- Set up anonymous access to your server environment by going to the Security tab of your Development environment and enabling Anonymous RPC.
Extending the class model
Learning activity
Add an event to the Entity
class that requests the information through the new channel.
- In the Business Model layer, select the Classes tab.
- Right-click the
Entity
class and select Customize. - Double-click the
Entity
class to open for editing. - In the Events tab, add a new event called
fetchCurrentWeather
. - In the Actions tab for the event, add a
main
action. In the
main
action's Script tab, add the following code:CODE(let* ( (myCity (ifnull (@ defaultAddress city) "NA")) (msg (message (: :class "WeathergetWeather") (: cityName myCity))) (req (message (: :class "HTTP") (: url "http://localhost:7080/training/anon/soa/training:weather:1.0/soap") (: headers (message (: content-type "text/xml"))) (: principal "nexjsa") (: password "nexj") (: body (format-message msg)) ) ) (resp ()) (parsed ()) (ret "No default city found") ) (begin (set! resp (integration-send-receive req "training:WeatherChannel" ())) (set! parsed (parse-message (resp'body) "WeathergetWeatherResponse")) (if (= myCity "NA") (set! ret "No location set") (set! ret (format "Local Temperature: {0}" ((parsed'getWeatherResult)'tempurature))) ) ) ret )
The code sends a message to the web service that includes the ZIP code for the entity's default address. The response is formatted when it is received.
- In the
Entity
class, go to the Attributes tab of the class editor and add a new attribute. This is a calculated attribute that contains the formatted response from the web service.- Name:
currentWeather
- Type:
string
- Name:
- In the Value tab for this attribute, set the value to
(this'fetchCurrentWeather)
. - Save your work.
Testing the web service integration
Write a Scheme script that creates a Person
with an address, and then fetches the person's current weather through the web service.
Learning activity
To test the web service integration:
Use an existing scratchpad or create a new one, and add the following code:
SCHEME(define FIRST "Tim") (define LAST "Lamont") ; Read the person (define thePerson (read-instance Person '() (quasiquote (and (= firstName ,FIRST) (= lastName ,LAST))) #f)) ; If the person doesn't exist, create it (if (null? thePerson) (begin (set! thePerson (Person'createEntity "Contact" (: firstName FIRST) (: lastName LAST))) (Address'createFactory thePerson (read-instance AddressType '() '(= name "Home") #f) (: address1 "Lamont Tower") (: address2 "1997 Orphan Road") (: city "Toronto") (: state "ON") (: country "CA") (: zip "M4A E3T") ) (commit) ) ) ; Fetch the person's weather (thePerson'currentWeather)
- Save the scratchpad.
Run the Server Console.
Execute each statement of the scratchpad in the Server Console and review the results.
Stop the Console.
Testing the updated application
Learning activity
To test the updated application:
- Start your server.
- Load
http://localhost:7080/training/ui/portal
in your browser. - Navigate to the Contacts workspace.
The weather information should appear in the Contact Navigator.