Java Reference
In-Depth Information
A library fielddeclarationfollowsthe wsContext declaration,where library
isdeclaredtobeoftype Map<String, Book> .Thisvariablestoresbooksinamap,
whereabook'sISBNservesasamapentry'skey,andthebook'sinformationisrecorded
in a Book object that serves as the map entry's value.
Library next declares a noargument constructor whose job is to initialize lib-
rary . The constructor first attempts to deserialize library.ser 's contents to a
java.util.HashMap instancebycallingthe deserialize() method(explained
later), and assign the instance's reference to library . If this file does not exist,
java.io.IOException isthrownandanempty HashMap instanceiscreatedand
assigned to library —note the use of Java 7's diamond operator to avoid having to
respecify the map's java.lang.String and Book actual type arguments.
The invoke() method is now declared. Its first task is to verify that dependency
injection succeeded by testing wsContext to determine if it contains the null ref-
erence. If so, dependency injection failed and an instance of the
java.lang.RuntimeException class is created with a suitable message and
thrown.
Continuing, invoke() calls WebServiceContext 's MessageContext
getMessageContext() methodtoreturnaninstanceofaclassthatimplementsthe
javax.xml.ws.handler.MessageContext interface. This instance abstracts
the message context for the request being served at the time this method is called.
MessageContext extends Map<String, Object> , making MessageCon-
text a special kind of map. This interface declares various constants that are used
withtheinherited Object get(String key) methodtoobtaininformationabout
therequest.Forexample, get(MessageContext.HTTP_REQUEST_METHOD) re-
turns a String object identifying the HTTP operation that the RESTful client wants
performed; for example, POST .
At this point, you might want to convert the string's contents to uppercase and trim
offanyleadingortrailingwhitespace.Idon'tperformthesetasksbecausetheclientthat
IpresentlaterwillnotallowanHTTPverbtobespecifiedthatisn'tentirelyuppercase
and/or is preceded/followed by whitespace.
Java 7's switch-on-string language feature is used to simplify the logic for invoking
the method that corresponds to the HTTP verb. The first argument passed to each
of the doDelete() , doGet() , doPost() , and doPut() helper methods is the
MessageContext instance (assigned to msgContext ). Although not used by
doPost() and doPut() ,thisinstanceispassedtothesemethodsforconsistency—I
might want to access the message context from doPost() and doPut() in the fu-
Search WWH ::




Custom Search