SAAJ: No strings attached
Tutorial Details:
SAAJ: No strings attached
SAAJ: No strings attached
By: By Frank Sommers
Send and receive binary Web services content using SAAJ 1.2
t the time of this writing, most Web services consist of simple message exchanges: A client contacts a Web service and sends a message to that service. The Web service, in turn, processes that request and then sends back a reply to the client. That simple request/response pattern models the way the HTTP protocol facilitates client/Web server interactions. As with HTTP, Web service message exchanges often must include binary content, such as images, documents, or sound clips. This article introduces sending and receiving binary Web service content using SOAP (Simple Object Access Protocol) with Attachments API for Java (SAAJ) 1.2.
Before diving into the intricacies of transferring binary Web service content, it's worth pointing out that a simple request/response-style Web service contrasts with services that fashion client/server interaction as remote procedure calls, or RPCs. In an RPC, a server exposes an interface that resembles an API. In turn, a client invokes such a service by making remote calls on the service's API, passing the required parameters, and receiving the values the call produces.
XML-based RPC resembles the way you invoke objects in an object-oriented (OO) system. Indeed, when working with the Java API for XML-based RPC (JAX-RPC), you seldom become aware you are working with XML documents, not Java objects. JAX-RPC lets you think of Web services as remote objects, much as you would with Java RMI (Remote Method Invocation). The JAX-RPC runtime translates the high-level, OO method calls to the XML documents expected by the remote Web service. While RPC-style Web services often provide a more convenient programming model, RPC calls must also rely on a lower-level messaging layer to exchange the XML messages that make up the remote call.
For some Web services, it is often useful to directly program to that lower-level messaging layer. For instance, if you wish to invoke a Web service that consumes a purchase order document and returns a receipt, you can easily model that document exchange as a single request/response message exchange. Instead of making remote method invocations, you would construct XML messages, send those messages directly to a Web service, and process the service's XML response, if any exists. Since SOAP defines the common message format for Web service messages, you would need to construct SOAP-conformant messages, and, once the service responds, parse those SOAP response messages back into a format your program understands.
SAAJ provides a convenient library to construct and read SOAP messages, and also lets you send and receive SOAP messages across the network. SAAJ defines the namespace javax.xml.soap . The classes that reside in that package initially formed part of the Java API for XML Messaging (JAXM), but were recently separated into their own API. JAXM relies on SAAJ for SOAP message construction and manipulation, and adds message reliability and other features specific to XML messaging. Whereas SAAJ is a required component of J2EE (Java 2 Platform, Enterprise Edition) 1.4, JAXM is not. This article focuses on one of SAAJ's most useful aspects: the ability to attach binary content to a SOAP message.
The benefits of attachments
While SOAP's design center focuses on encapsulating XML documents in a message, SOAP's attachment feature extends a SOAP message to include, in addition to the regular SOAP part, zero or more attachments, as Figure 1 shows. Each attachment is defined by a MIME type and can assume any content represented as a byte stream.
Figure 1. A SOAP message with attachments
SOAP's attachment feature proves most useful when a client wishes to transmit binary data, such as an image or audio data, to a Web service. Without SOAP attachments, sending a piece of binary data would prove more difficult. For instance, a client's SOAP message could convey the binary file's URL address. The client would then have to operate an HTTP server to let the Web service retrieve that file. That would represent an undue burden on any Web service client, especially on clients running on limited-resource devices such as digital cameras or scanners. SOAP's attachment capability lets any Web service client able to transmit SOAP messages embed binary files directly in a SOAP message.
SOAP attachments, for instance, prove handy when interacting with portal Websites. Consider a real estate agency network that needs to distribute descriptions and photographs of homes for sale to a centralized real estate search portal. If the portal operates a servlet allowing the posting of SOAP messages with attachments, a real estate agency could update its listings with a few SOAP messages, including photos of those homes. The SOAP message body might embed the property description, and SOAP attachments could carry the image files. Under that scenario, when a portal operator's servlet receives such a message, it would return an acknowledgment document, indicating the post's availability on the portal. Figure 2 illustrates such a Web service.
Figure 2. A real estate Web service, using SOAP with attachments
The anatomy of SOAP with attachments message
The SOAP Messages with Attachments W3C (World Wide Web Consortium) Note (see Resources ) does not add new features to SOAP. Rather, it defines how to take advantage of MIME types in a SOAP message to define attachments, and how to reference those attachments from within the SOAP body.
The MIME type multipart/related defines documents consisting of multiple related parts. SOAP messages with attachments must follow the multipart/related MIME type. The example below shows a multipart/related SOAP message, bound to the HTTP protocol, with two attachments:
POST /propertyListing HTTP/1.1
Host: www.realproperties.com
Content-Type: Multipart/Related; boundary=MIME_boundary; type=text/xml; start=""
Content-Length: NNNN
--MIME_boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID:
xmlns:realProperty="http://schemas.realhouses.com/listingSubmission">
Really Nice Homes, Inc.
Add
1234 Main St
Pleasantville
CA
94323
250000
--MIME_boundary
Content-Type: image/jpeg
Content-ID:
....JPEG DATA .....
--MIME_boundary
Content-Type: image/jpeg
Content-ID:
....JPEG DATA .....
--MIME_boundary--
The above multipart message comprises a series of MIME-headers and related data. At the root of the document is the SOAP body. Because the SOAP body contains only XML data, the MIME type of the entire message is text/xml . Following the SOAP envelope are two attachments, each corresponding to an image file sent along with the message.
A content ID identifies each attachment. The W3C Note lets either a content ID or a content location reference the attachments, but it gives preference to the former. Such content IDs act as Uniform Resource Identifier (URI) references to attachments; the SOAP 1.1 encoding rules define how to reference a resource in a SOAP message via a URI that can reference any content, not just XML (see Section 5 of SOAP 1.1 in Resources ). A SOAP processor resolves those URI references as it processes the message. Based on the above example, the SOAP processor associates the element frontImage with the data section with Content ID property1234_front.jpeg@realhouses.com in the SOAP message.
Create and send a SOAP message with attachments
SAAJ lets you create and edit any part of a SOAP message, including attachments. Most of SAAJ is based on abstract classes and interfaces such that each provider can implement SAAJ in its own products. Sun Microsystems' reference implementation comes with the Java Web Services Developer Pack (JWSDP).
Since SOAP messages represent but a special form of XML documents, JAAS builds on the Document Object Model (DOM) API for XML processing. Most SOAP message components descend from the javax.xml.soap.Node interface, which, in turn, is a org.w3c.dom.Node subclass. SAAJ subclasses Node to add SOAP-specific constructs. For instance, a special Node , SOAPElement , represents a SOAP message element.
A direct result of SAAJ's reliance on interfaces and abstract classes is that you accomplish most SOAP-related tasks via factory methods. To connect your application with the SAAJ API, you first create a SOAPConnection from a SOAPConnectionFactory . For creating and editing SOAP messages, you can also initialize a MessageFactory and a SOAPFactory . MessageFactory lets you create SOAP messages, and SOAPFactory provides the methods to create individual parts of a SOAP message:
SOAPConnectionFactory spConFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = spConFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
With these tools in place, you can create a SOAP message a client from a real estate agency would use to send a listing update to a portal Website.
SAAJ offers several ways to create a new SOAP message. The following example shows the simplest method that creates an empty SOAP message with an envelope, and header and body in that envelope. Since you don't need a SOAP header in this message, you can remove that element from the message:
SOAPMessage message = factory.createMessage();
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: SAAJ: No strings attached
View Tutorial: SAAJ: No strings attached
Related
Tutorials:
|
Displaying 1 - 50 of about 241 Related Tutorials.
|
Converting Strings to Numbers
Java: Converting Strings to Numbers
Java: Converting Strings to Numbers
To convert a string value to a number
(for example, to convert the String value in a text field to an int |
Converting Numbers to Strings
Java: Converting Numbers to Strings
Java: Converting Numbers to Strings
Vanilla Java: Converting numbers to strings is easy.
You can do almost everything with concatenation, but can |
String Comparison
Java NotesString Comparison
Strings can not be compared with the usual <... the characters in the strings.
Comparing objects vs. primitive types
[to be supplied]
Comparing Strings: ==, .equals(), .compareTo(), ...
To compare Strings |
Converting Anything to String
to strings is easy.
You can do almost everything with concatenation, but can get more control using
some of the alternatives.
Converting numbers to strings - See Converting Numbers to Strings
There are many specialized issues with regard |
String Overview
Java NotesString Overview
Strings are sequences of Unicode characters. In many programming languages strings are
are stored in arrays of characters.
However, in Java strings are a separate object type, String.
The "+" operator is used |
FontMetrics
Java: FontMetrics
Java NotesFontMetrics
The java.awt.FontMetrics class is used to determine the measurements of
characters and strings. These measurements can be used |
Combine String example
Java Concatenate Strings, Concatenate Two String, Add Two String in Java...;
In this section, you will learn how to combine or merge
two strings in java. The java.lang package provides the method
that helps you to combine two strings |
Example - Array to String
, program to concatenate all
of the strings in an array, each separated by a specifed...()
// Convert an array of strings to one string.
// Put the 'separator' string between each....
//-------------------------------------------------- arrayToString2()
// Convert an array of strings to one string.
// Put |
Features and Functions of GPS Vehicle Tracking System
handing over the vehicle to the children, there is always a rule book attached... can set the speed limitation in the device attached. If somebody drives the car |
String Length
;
In java, Strings are objects that belong to
class java.lang.String. |
BufferedReader/Writer
Strings from/to text files.
Assume
BufferedReader br;
BufferedWriter bw |
StringBuffer and StringBuilder
to store character strings that will be
changed (String objects cannot be changed....
For example, consider a method which duplicates strings the requested number |
Create a Sash in SWT
be attached to it. We have used the class FormData to define
the attachments |
Convert Object To XML
into xml file with the help of an example. We are taking ten strings from console.
Store these strings into an array of objects and pass these string objects |
Character Comparison Example
provides a method for comparing
two case sensitive strings. The compareTo()
method compares two strings on the basis of Unicode of each character of the given
strings. This method returns integer type value. If it will return '0' it
means |
Regular Expressions
programming technique to
search for patterns in
strings, extracting, and replacing... for parsing input
strings than, eg, StringTokenizer.
Programming language. Regular |
Java: Unicode
Java: Unicode
Java: Unicode
Unicode is a system of encoding characters. All characters
and Strings in Java use the Unicode encoding, which allows truly international
programming |
Regex Exercises 2
. Regular expressions do not have to be written as Java strings
(eg, no Java string |
Character Cases Ignoring case sensitiveness in java
to compare two strings
ignoring case sensitiveness Java provides.... The compareToIgnoreCase()
method compares two strings and ignores its cases. This method....
Description of program:
The following program compares two strings and ignore |
Java String Examples
;
Comparing Strings (==
operator |
Java String Examples
;
Comparing Strings (==
operator |
Parsing The XML File Using JDOM Parser in JSP
existing XML documents from files, network
sockets, strings, or from reader |
Handling Mouse Clicks in Java
class. In which, buttons
are set on the frame and event listeners are attached... to compare
strings. It returns boolean value either true or false.
Here |
FreeNAS 0.65 has been released
).
About the FreeNAS
FreeNAS is a free NAS
(Network-Attached Storage...
are based on M0n0wall.
Network-attached storage (NAS)
1. A term used...
be
attached to any type of computer network. When |
Compare string example
String Compare,Java Compare Strings,Compare Two String in Java,String Comparing Example
Compare strings example...;
In this section, you will learn how to compare two
strings in java |
Java Compare String (== operator)
while comparing strings with this operator is that it compares
the references the two strings are pointing to rather than comparing the
content... strings. If both strings are equal, it will display a message "The given strings |
String Concat()
.
Description of the code:
As shown in the example we have taken two strings i.e. str1
and str2. Then we have concatenated both the strings with the help of
"+" operator. Concatenation means to add the two or more strings. After |
The Dialog boxes
for JOptionPane.
Dialogs are attached to window (frame)
Every dialog is attached |
String intern()
.
Description of the code:
As shown in the example we have created three Strings
in three... Strings are equal. Basically an intern string is the one
that has an entry... of strings privately, which is empty initially.
For any two strings s1 and
s2 |
Watch Out For Spyware Programs That Slows Down Your Computer System
the internet. Spyware software is usually attached to some kind of free software... and terms of use that are attached to freebies on the web. This is where you... are attached to your free download.
Other time, it is clear that there are extra |
Types of LBS
hotel. Here you can use your GPS attached mobile phone that can calculate... have a GPS attached device or a mobile phone with GPS receiver.
E911 &ndash... chips or antennas that can be attached to any other object even living beings |
Watch Out For Spyware Programs That Slows Down Your Computer System
the internet. Spyware software is usually attached to some kind of free software... and terms of use that are attached to freebies on the web. This is where you... are attached to your free download.
Other time, it is clear that there are extra |
Lemo Cobol Development Tool
and others.
The IDE is not attached to certain COBOL compiler. You
can use |
GPS Tracking Systems
asset to which it is attached. The device records the position of the data... via Internet.
Mechanism: The receiver or tracking unit attached inside |
Summary: I/O
.
In all of these prototypes, i and j are int,
s and t are Strings,
and c... and java.io.BufferedWriter are
used to read/write Strings from/to text files... from one file and writes to another.
// Assume inFile and outFile are Strings |
Summary - String
: Summary - String
String Concatenation
The + operator joins two strings... and concatenated with it. This is
a common way to convert numbers to Strings.
If a non... building up a string from many strings.
Character has many useful static |
Wi-Fi as a part of LBS
the attached Wi-Fi device again converts it into binary code for the respective... equipments like laptops or even motor vehicle attached with Wi-Fi receiver.
Comparison |
Tutorials - Java Server Pages Technology
appear
quoted. The special strings ' and " can be used... attached with existing
server-side applications.
  |
Tutorials - Java Server Pages Technology
appear
quoted. The special strings ' and " can be used... attached with existing
server-side applications.
  |
Classes in Java
.
Regular expression
The set of strings which are based on common characteristics... of strings.
This regular expression as a Java string, becomes "\\\\".
That is 4 backslashes to match a single one. As in literal Java strings |
Request Headers in JSP
-Length: It is mostly used for POST messages,
it tells how much data is attached |
Specific Request Headers in JSP
-Length: It is mostly used for POST messages,
it tells how much data is attached.
5 |
Request Headers In EL
used for POST messages,
it tells how much data is attached.
5) Cookie: It is one |
GIS and GPS will craft municipal work well-organized
management of civic work. GPS vehicle tracking devices will be attached in vehicle |
Eclipse Tidy
, download the
zip file and follow the notes attached to it.
Bugs and Feature Requests |
What is BIOS?
are attached in the right way and all the necessary
functionality parts |
What is VRRP?
router situated
next to it. As many routers are attached in the series, as many |
java.util.StringTokenizer
The java.util.StringTokenizer class is used
to break strings into tokens (words, numbers... strings because it has to scan the string once just to get this number.
Using |
Introduction
strings in a case
sensitive way then they are considered different if they differ... String.compareTo,
If you
compare two Strings in a case insensitive way |
Show Image Reader and Image Writer by image format
() of class ImageIO returns an
array of strings consisting of all the image format... getWriterFormatNames() returns
an array of strings consisting of all the image format |
|
|
|