Transcript Document
Web Services
Topics
Web Service Standards
Creating Eclipse/JEE Web Services
What is a Web Service?
Method of communicating between two electronic devices over the web
Big Web services use XML messages that follow the SOAP standard
Popular with the traditional enterprises.
There is usually a machine-readable description of the operations offered by the service written in the Web
Services Description Language (WSDL)
WSDL is not a requirement of a SOAP endpoint, but it is a prerequisite for automated client-side code
generation in many Java and .NET SOAP frameworks.
Some industry organizations, such as the WS-I, mandate both SOAP and WSDL in their definition of a Web
service.
ReST (Representation state transfer)
Light weight alternative to "Big" Web Services
Relies on HTTP operations like GET, POST, PUT, DELETE
Compared to SOAP based service ReST doesn't have built in fault tolerance, has less transaction
support (database topic), less (none) built in security
Most of the time this isn't a major problem and many recommend using ReST unless you have a
specific need for SOAP based services.
http://en.wikipedia.org/wiki/Web_service
http://spf13.com/post/soap-vs-rest
Big Web Service Standards
XML - eXtensible Markup Language
SOAP – Simple Object Access Protocol
WSDL – Web Services Description Language
UDDI – Universal Description, Discovery and Integration
What is XML?
eXtensible Markup Language (XML)
is a markup language that uses tags and attributes to structure data.
Similar to HTML – except 'you' define the tags
Derived from SGML, the standard generalized markup language
SGML is complex, XML is designed to be simple
How XML began (1)
In the 1970’s, three guys at IBM (Charles Goldfarb, along with Ed
Mosher and Ray Lorie) invented GML
GML - a way of marking up technical documents with structural
tags. The initials stood for Goldfarb, Mosher and Lorie
According to Goldfarb, the term “mark-up language” was later
adopted in order to make better use of the initials, so it became
the Standard Generalized Markup Language and was adopted by
the ISO in 1986.
It is a little confusing, because SGML is not itself a markup
language, but rather a specification for defining markup
languages.
The application of SGML that became well known is HTML
(Hypertext Markup Language). HTML is an SGML application,
and defines a specific set of tags suitable for web pages.
How XML began (2)
HTML is where things went wrong.
The original thinking was to separate content from presentation.
For example, the <em> tag in a web page means “emphasize”. It was left up to
the user agent how to render that, say as bold text, or in a different color, or with a
different tone of voice in a speech reader.
This type of thing does not please page designers, who want to nail down the
exact appearance of a page. Therefore HTML got extended with things like <font>
tags which went against the initial concept.
Competition between Netscape and Microsoft led to fragmentation
of the HTML standard, which remains a huge problem.
Web pages began to be used for things that went beyond the
original concept, including multimedia, animation, online
applications, ecommerce and more.
Browsers tried to be tolerant of hastily written web pages that
committed crimes like using an opening tag without a corresponding
closing tag.
Tolerance is normally commendable, but the resulting lack of discipline became a
barrier to programmatic interpretation of web content, or the use of HTML for
structured data.
How XML began (3)
Summary
HTML is limited and terminally polluted (not really looks like it will be here forever)
while SGML itself is too complex for mortals to implement.
In the late 1990s a group of people including Jon Bosak, Tim Bray,
James Clark and others came up with XML, the eXtensible Markup
Language.
XML is not a markup language
It is a specification for defining markup languages.
The W3C (World Wide Web Committee) reshaped HTML as an XML
application
The result being XHTML (now dropped in favor of HTML5)
This was only a small part of what XML is all about.
The key point is with XML the industry can specify how to store
almost any kind of data, in a form that applications running on any
platform can easily import and process.
With XSD allows defining the semantics of data (data types, range limits, etc.)
This allows translating data from a data base into text based XML files
Aids cross platform compatibility
Xml Schema Definition (XSD)
http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29
Who is using XML?
1990s Sun Microsystems introduced Java
Java helped companies like IBM make sense of their diverse operating
systems
Having each system running Java greatly simplifies the business of creating
interoperable applications
Another example is Oracle, which uses Java stored procedures for its crossplatform database.
Partly in response to the prospect of a Java-centric computing universe,
Microsoft picked XML as an alternative (but different) approach to the
interoperability puzzle, and became XML’s greatest advocate.
Unlike Java, XML is in the hands of the independent W3C, a factor which
endeared it to Microsoft.
Microsoft has described its .Net initiative as “a platform for XML web services”.
Through XML, Microsoft’s applications can communicate with those running on
other platforms.
A Java application can employ the services of a DCOM object (DCOM being Microsoft’s distributed
component object model), and vice versa. Hence Microsoft has been busy creating XML interfaces to its
server products, such as SQL Server and Exchange.
Microsoft does not own XML
IBM is a big XML user
Listening to Sun (now Oracle) you would think it is a Java technology
The fact that XML is important to all three (IBM, Microsoft, Oracle/Sun)
companies says a lot for its bridge-building potential.
So, why use XML?
The "holy grail of computing?" solving the problem of universal data
interchange between dissimilar systems.”
Handy format for everything from configuration files to data and
documents of almost any type.
Through SOAP (Simple Object Access Protocol), you can also use it to
invoke methods on remote objects, to do true distributed computing
over the Internet.
So it is significant
But it is a hard subject to pin down
It describes a whole family of technologies and specifications
Most XML is programmatically generated
You do not need to learn all the specifications in order to benefit from XML
What matters is to understand what XML does, so that you can see how to
exploit it in your own projects.
Structure of an XML Document
An XML document is a tree of nested elements
One root element.
Each element has a starting and ending tag, marked by angle brackets, with content
in between:
<element>…content…</element>
The content can contain other elements, can consist entirely of other elements, or
might be empty.
Attributes are named values which are given in the starting tag, with the values
surrounded by single or double quotations:
<element attribute1="value1" attribute2="value2">
Here’s an example that will look familiar:
Each of which can have zero or more attributes
<html>
<body background= "logo.jpg">
<h1>HTML or XML?</h1>
</body>
</html>
HTML or XML? It is valid as either, and illustrates the close relationship between the
two (however, note that not all HTML documents are valid XML).
HTML – but not valid XHTML
<html>
<body background= "logo.jpg">
<img src="logo.jpg"><br>
<h1>HTML or XML?</h1><br>
Why isn't this valid XHTML?
</body>
</html>
Structure of an XML Document
There are other fundamentals in XML, such as processing
instructions and namespaces, but elements and attributes are
the heart of it.
Attributes are properties of elements
Nested elements are also properties of the parent element
It can be hard to decide what should be an element and what
should be an attribute. For example, instead of
<body background="logo.jpg">
why not have:
<body>
<background>logo.jpg</background>
</body>
This is mostly a matter of taste. Some XML is element-centric,
some attribute-centric and some uses both.
Hierarchical data structure
universitystudents
universitystudent=
Clifford Wall
universitystudent=
Dawna Voss
coursesection
sectionid = 1
sectionid = 10
universitystudent=
Ann Bochman
XML Elements and values
<universitystudents>
<universitystudent>
<studentfirstname>Clifford</studentfirstname>
<studentlastname>Wall</studentlastname>
<coursesection>
<sectionid>1</sectionid>
<sectionid>10</sectionid>
</coursesection>
</universitystudent>
<universitystudent>
<studentfirstname>Dawna</studentfirstname>
<studentlastname>Voss</studentlastname>
</universitystudent>
<universitystudent>
<studentfirstname>Ann</studentfirstname>
<studentlastname>Bochman</studentlastname>
</universitystudent>
</universitystudents>
Root element
Child elements
of universitystudents
of universitystudent
of coursesection
XML Elements and values
Alternative approach
<universitystudents>
<universitystudent studentfirstname="Clifford" studentlastname="Wall">
<coursesection>
<sectionid>1</sectionid>
Represented as attributes
<sectionid>10</sectionid>
instead of child elements
</coursesection>
</universitystudent>
<universitystudent studentfirstname="Dawna" studentlastname="Voss">
</universitystudent>
<universitystudent studentfirstname="Ann" studentlastname'Bochman">
</universitystudent>
</universitystudents>
What is SOAP
Simple Object Access Protocol
Communication protocol
Format for sending messages
Communicates via Internet
Platform independent
Language independent
Based on XML
Allows getting around firewalls by using http protocols
Between applications
Port 80 left open by most organizations
A W3C recommendation
Web Services Everywhere?
Do web services replace DCOM, CORBA, or Java RMI?
- NO
The reason is very simple. Each of the above standards - while difficult to
integrate with other standards -- work well in the environments they were
designed for and are highly optimized for those environments.
If all your apps are in DCOM - it makes sense to integrate them with DCOM
But if you need to collaborate with your neighbor next door and you don't
know what he uses - go with an open-standard such as Web Services for
that integration.
SOAP Message
An ordinary XML document containing the following
elements:
An Envelope element that identifies the XML document as a SOAP
message
An optional Header element that contains header information
A Body element that contains call and response information
An optional Fault element containing errors and status information
Very simple SOAP message:
<soap:Envelope xmlns="http://www.w3.org/2001/12/soap-envelope">
<Body>
<helloWorld/>
</Body>
</soap:Envelope>
Improved message:
<soap:Envelope xmlns ="http://www.w3.org/2001/12/soap-envelope">
<Body>
<helloWorld xmlns="http://www.soapware.org/"/>
</Body>
</soap:Envelope>
Namespaces specify which helloWorld procedure is desired.
Skeleton SOAP Message
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
The xmlns:soap Namespace
Notice the xmlns:soap namespace in the example above. It should have the value of:
"http://www.w3.org/2001/12/soap-envelope".
The encodingStyle Attribute
The encodingStyle attribute is used to define the data types used in the document. This attribute may
appear on any SOAP element, and it will apply to that element's contents and all child elements.
A SOAP message has no default encoding. Any URI can be used.
http://www.w3schools.com/webservices/ws_soap_syntax.asp
SOAP Message is Embedded in HTTP Request
The following is an HTTP POST header followed by a more complex soap message
POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<m:GetLastTradePrice xmlns:m="Some-URI">
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</soap:Body>
</soap:Envelope>
WSDL
Web Services Description Language
Describes a Web Service
How the service can be called
What parameters it expects
What data structures it returns
Service described as a collection of endpoints (or ports)
Objects in WSDL 1.1 / 2.0
The next three slides show WSDL documents generated by ASP.NET, Eclipse
and Netbeans. They all have similar – but slightly different syntax.
http://en.wikipedia.org/wiki/Web_Services_Description_Language
ASP.NET WSDL File:
Eclipse Axis2 generated WSDL file
Netbeans generated WSDL File:
http://netbeans.org/kb/docs/websvc/intro-ws.html
UDDI
Universal Description, Discovery and Integration
UDDI is a directory for storing information about web services
UDDI is a directory of web service interfaces described by WSDL
UDDI communicates via SOAP
UDDI is built into the Microsoft .NET platform
If you know the URL of a Web service, you don't need UDDI
UDDI doesn't seem to be used much anymore (was big 10 years ago)
Creating an Axis2/Eclipse Web Service
Tell Eclipse where to find Axis2 libraries
Axis2 is built on the JAX-WS specification
JAX-WS was introduced with Java 6
JAX-WS implementations:
Totally redesigned from Axis1 (much improved)
Axis2
Metro
Apache CXF
Or use JAX-WS by itself
All of the JAX-WS implementations have good performance
None are inherently superior to the others
Axis2 can be used as a standalone application server for Web
services (we won't do this)
it has a "lot" of library files that come with it.
http://victor-jan.blogspot.com/2010/01/there-are-multiple-apis-for-java.html
http://predic8.com/axis2-cxf-jax-ws-comparison.htm
Creating an Axis2 Web Service
Tell Eclipse where to find Axis2 libraries
Click Window (top menu bar)
Click Preferences
Open the Web Services entry
Select: Axis2 Preferences
For this semester's CS268 class you will then set the
runtime location to:
W:\c s\Stevenson\CS268\_ClassFiles\Labs\lab8\axis2-1.6.0
These libraries are copied into your workspace
when you set up an Axis2 project
They are not "run" from the above W: drive location
Creating an Axis2 Web Service
Create a new Dynamic Web Project with either Tomcat 6
or 7, an older 2.4 (or 2.5 will also work) version of the
Dynamic web module (to be compatible with Axis2
libraries) and click the Modify... button:
Creating an Axis2 Web Service
After clicking the Modify... button (previous
slide) check the Axis2 Web Services box
Then click OK and Finish to copy the Axis2
libraries into your newly created project.
Creating an Axis2 Web Service
After adding the Axis2 libraries
Create a standard Java class with whatever
methods and additional libraries are needed (the
mysql jar file for example)
Use the class to generate the web service
Creating a Web Service Client
Create a new Dynamic Web Project as before
Restart the Service you previously made by right clicking it in the
Project Explorer and selecting Run As/Run on Server
2.4 (or 2.5) web module version
Axis2 libraries added to it
Click the Services link on the displayed web page
Click the link named for the class you used to generate the service
(previous slides)
Copy the url displayed in the browser address bar to the wsdl. It will look
something like this:
Right click the client project name in the Project Explorer
Select New and then Other
Open Web Services and select Web Service Client
Click the Web service runtime: link and select Apache
Axis2 – click OK, click Next and click Finish
Creating a Web Service Client
If all went well in the previous slide you will see something like this
in the project explorer:
The package name is derived
from the package name used
when the service was previously
created – but converted to
lower case.
The Handler and Stub files have code that is used by classes you
create calling methods from the remote service
Using the Stub code to connect to the remote service
Create a new class to hold the code connecting to the service
Import classes defined within the Stub class for the methods in
the service
One will be named identically to your method(s)
import servicepkg.PremeireUtilitiesStub.GetParts;
Another (for each method) has Response added to the name
import servicepkg.PremeireUtilitiesStub.GetPartsResponse;
Create a method using the Stub class to do "whatever"
Create a new object from the Stub class
PremeireUtilitiesStub stub = new PremeireUtilitiesStub();
Create objects for the imported classes
GetParts g = new GetParts();
GetPartsResponse res = stub.getParts(g);
Finally call the method and if it returns data store the data for later use
PremeireUtilitiesStub.Part[] data = res.get_return();
search for "public static class" to find the classes created within PremeireUtilitiesStub
What can be done by a service?
Anything done with any java method
If data related:
retrieve data
Modify data
As single values
As arrays if multiple data items
Array can contain objects with getters for "row" data for each item
in the array
Inserts, updates and deletes
Anything you previously did in the "model"
Deploying a Java based service
Axis2 provides a server to host Axis2 services
I haven't checked this out
Tomcat can host web services
Any java capable web server can host a java based web
service (same as hosting any web site)
Theoretically a java client can connect to a web service
hosted from "any" web server
I've made a few unsuccessful attempts at connecting a Java
client to a Microsoft IIS service
I'm sure it is possible
I succeeded connecting a C# client to our Lab8 leela java based
service.
Deploying to a Tomcat server (1)
Needed:
privileges to copy files to a folder accessible to the Tomcat server
privileges to stop and restart the server
Make a war file of the service project
This stores everything needed in a zipped file (with a .war file
extension) in the correct folder structure needed by Tomcat
Deploying to a Tomcat server (2)
Copy the .war file to the Tomcat server's webapps folder
If auto deploy is enabled on Tomcat (the default) a process will
monitor for new war files – find it – and extract the files into a
subfolder of webapps named the same as the war file. At this
time the service "should" be accessible
Deploying to a Tomcat server (3)
Rename the .war file with a .zip file extension and extract
the files in a folder accessible to the web server
Create a context in Tomcat's server.xml file pointing to
the folder with the service
For lab 8 I named this context "268"
Shutdown and restart the Tomcat server
Access the service with this sort of url:
http://yoda.cs.uwec.edu:8080/268/
* Click Services then the link to
the desired service to see its
WSDL
* The link to the WSDL can then be used to generate a client
Testing Web Service with a Browser
Enter this URL manually
REST
Representation State Transfer
Lightweight alternative to "Big" Web services
Relies on a stateless, client-server, cacheable
communications protocol
Uses HTTP requests to post data (create and/or
update), read data (e.g., make queries), and
delete data.
REST is not a "standard". There will never be a
W3C recommendataion for REST
REST
All you need are HTTP commands
The idea is that, rather than using complex
mechanisms such as CORBA, RPC or SOAP
to connect between machines, simple HTTP
is used to make calls between machines.
Connecting a .NET app to a java service
I thought I should document this for your benefit – and to
make sure I don't forget how I did this!
I'll go over how to create a .NET client using VS 2010
First step of course is having access to Visual Studio
2012 (about the same if you have VS 2010 or 2008)
Connecting a .NET app to a java service
Start Visual Studio 2012
Select File/New/Project
Open Visual C# in left pane
Select Windows
Select Console Application
I'm keeping this simple – I'll show how to connect a .NET Web
Site page to a service after this
Results:
Connecting a .NET app to a java service
Add a service reference
Copy and paste in the address to
the remote service's WSDL
Click Go and then OK
Connecting a .NET app to a java service
Modify Program.cs as shown here
(substitute whatever you named your project for Spr12NetConsoleServiceClient)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SR = Spr12NETConsoleServiceClient.ServiceReference1;
namespace Spr12NETConsoleServiceClient
{
class Program
{
static void Main(string[] args)
{
SR.SportOrdersPortTypeClient sc = new SR.SportOrdersPortTypeClient();
SR.Orders[] data = sc.getOrders();
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine(data[i].customerlastname + " \t" +
data[i].orderdate + " \t" + data[i].inventorydescription);
}
Console.Read();
}
}
}
Previous code errors out
I set a breakpoint
and watched what happened
Solution?
Delete these sections
(Keep the others).
Delete one of the "endpoints" from the generated app.config file
Results
The Console.Read(); line of code (three slides ago) keeps this console window
open until you press any key – necessary or you won't see the console output!
Connecting an ASP.NET app to a java service
Make a new project for C# and select Web and ASP.NET Empty
Web Application.
Add a Service Reference (as before)
Add a Web Form – Select Project from top menu then Add New
Item:
Connecting an ASP.NET app to a java service
Very similar to what you just read through
ASP.NET uses web.config instead of app.config
You will need to delete the extra "custom" endpoint from web.config instead of
app.config
ASP.NET pages only seem to work with and older style of .NET service
connection
Here are the steps:
File/New/Project/ASP.NET Empty Web Application
Choose Visual C# in left pane, open Web, select:
ASP.NET Empty Web Application
Right project name in Solution Explorer
Add a service reference "almost" identically to how you did it previously
Open web.config and remove the "<basicHttpBinding>...</basicHttpBinding> entry and remove the
"...Soap11..." entry
Remove this
entry (the opposite
of what you removed
previously)
After editing web.config, select BUILD from the top menu and Build Solution
Adding a GridView Control
Drag a GridView from the Toolbox into the
page as shown:
Drop it here in the page
Connecting an ASP.NET app to a java service
Switch to Design view
Open the task dialog
(select the GridView and click the small box with arrow visible at the upper right corner)
Connecting an ASP.NET app to a java service
Select <New data source...>
Select Object
Enter ServiceReference1 for the ID (if you named your service something
else use "your" name) and click OK
Connecting an ASP.NET app to a java service
Choose the SportOrdersPortTypeClient for the business object
if connecting to a different service select the "Client" option for that one.
Click Next
Choose the getOrders method (or if a different service whatever method
you need)
Click Finish
Run the web page
Click the Start Debugging button
if grayed out click the
Stop Debugging button
first
Results: