Java Reference
In-Depth Information
ture. In contrast,
invoke()
's
request
argument is passed only to
doPost()
and
doPut()
sothatthesemethodscanaccesstherequest'ssourceofbytes,whichconsist
of the XML for the topic to be inserted or updated.
IfanyotherHTTPverb(suchas
HEAD
)shouldbepassedastherequestmethod,
in-
voke()
respondsbythrowinganinstanceofthe
HTTPException
classwitha405
response code (request method not allowed).
The
doDelete()
method first obtains the query string that identifies the topic
to delete via its ISBN (as in
?isbn=9781430234135
). It does so by calling
get(MessageContext.QUERY_STRING)
onthe
msgContext
argumentpassed
to this method.
If the null reference returns, there is no query string and
doDelete()
deletes
all entries in the map by executing
library.clear()
. This method then calls the
serialize()
methodtopersistthelibrarymapto
library.ser
,sothatthenext
invocation of this web service will find an empty library.
If a query string was passed, it will be returned in the form
key1
=
value1
&
key2
=
value2
&
….
doDelete()
assumesthatonlyasingle
key
=
value
pair
is passed, and splits this pair into an array with two entries.
doDelete()
firstvalidatesthekeyasoneof
isbn
,
ISBN
,oranyotheruppercase/
lowercase mix of these letters. When this key is any other combination of characters,
doDelete()
throws
HTTPException
witha400responsecodeindicatingabadre-
quest.Thisvalidationisn'tessentialwhereasinglekeyisconcerned,butifmultiplekey/
valuepairswerepassed,youwouldneedtoperformvalidationtodifferentiatebetween
keys.
After extracting the ISBN value,
doDelete()
passes this value to
lib-
rary.remove()
, which removes the ISBN
String
object key/
Book
object value
entry from the
library
map. It then calls
serialize()
to persist the new map
to
library.ser
, and creates an XML response message that is sent back to the
client. The message is returned from
invoke()
as a
String
object encapsulated
in a
java.io.StringReader
instance that's encapsulated in a
javax.xml.transform.stream.StreamSource
object.
If
doDelete()
encounters a problem, it throws an
HTTPException
instance
with response code 500 indicating an internal error.
The
doGet()
methodissimilarto
doDelete()
.However,itrespondstotheab-
senceorpresenceofaquerystringbyreturninganXMLdocumentcontainingalistof
all ISBNs, or an XML document containing book information for a specific ISBN.