BookImageServletpublic class BookImageServlet extends ora.jwsnut.saaj.SAAJServlet A servlet that uses SAAJ attachments to
serve images to a client. |
Fields Summary |
---|
private static final String | XMLSCHEMA_URI | private static final String | XMLSCHEMA_INSTANCE_URI | private static final String | XMLSCHEMA_PREFIX | private static final String | XMLSCHEMA_INSTANCE_PREFIX | private static final String | SOAP_ENC_PREFIX | private static final String | SERVICE_URI | private static final String | SERVICE_PREFIX | private static MessageFactory | messageFactory | private static SOAPFactory | soapFactory | private static Name | BOOK_LIST_NAME | private static Name | BOOK_TITLES_NAME | private static Name | BOOK_IMAGE_REQUEST_NAME | private static Name | BOOK_IMAGES_NAME | private static Name | IMAGE_TYPE_ATTRIBUTE | private static Name | HREF_ATTRIBUTE |
Methods Summary |
---|
private void | createFault(javax.xml.soap.SOAPBody replyBody, java.lang.String faultCode, java.lang.String faultString, java.lang.String faultActor, java.lang.String detailString)Creates a fault in the reply body.
SOAPFault fault = replyBody.addFault();
fault.setFaultCode(faultCode);
fault.setFaultString(faultString);
fault.setFaultActor(faultActor);
if (detailString != null) {
Name detailName = soapFactory.createName("BookFaultDetail", SERVICE_PREFIX, SERVICE_URI);
Detail detail = fault.addDetail();
DetailEntry detailEntry = detail.addDetailEntry(detailName);
detailEntry.addTextNode(detailString);
}
| private void | handleBookImageRequest(javax.xml.soap.SOAPElement element, javax.xml.soap.SOAPMessage reply)Handles a request for the images for a given list of book.
// The request element contains an attribute that holds the
// type of image requested and a nested string for each title.
// The reply body has a BookImages element and a nested item with
// a reference to the image which is sent as an attachment
SOAPBody replyBody = reply.getSOAPPart().getEnvelope().getBody();
// Determine whether to use JPEG or GIF images
String imageType = element.getAttributeValue(IMAGE_TYPE_ATTRIBUTE);
boolean gif = imageType.equalsIgnoreCase("image/gif");
// Build the BookImages element containing all of the replies
SOAPBodyElement bodyElement = replyBody.addBodyElement(BOOK_IMAGES_NAME);
bodyElement.addAttribute(
soapFactory.createName("type", XMLSCHEMA_INSTANCE_PREFIX, XMLSCHEMA_INSTANCE_URI),
SOAP_ENC_PREFIX + ":Array");
bodyElement.addAttribute(
soapFactory.createName("arrayType", SOAP_ENC_PREFIX, SOAPConstants.URI_NS_SOAP_ENCODING),
XMLSCHEMA_PREFIX + ":anyType[]");
// Index of the next attachment to use
int index = 0;
// Handle each nested element.
Iterator iter = element.getChildElements();
while (iter.hasNext()) {
// Get the next child element from the request message
SOAPElement childElement = (SOAPElement)iter.next();
// Get the book title
String title = childElement.getValue();
// Get the image data
byte[] imageData = BookImageServletData.getBookImage(title, gif);
if (imageData != null) {
// Got the data - attach it.
AttachmentPart attach = reply.createAttachmentPart();
attach.setDataHandler(new DataHandler(
new ByteArrayDataSource("Image Data",
imageData,
gif ? "image/gif" : "image/jpeg")));
attach.setContentId("ID" + index);
reply.addAttachmentPart(attach);
// Add an element in the reply pointing to the attachment
bodyElement.addChildElement("item").addAttribute(HREF_ATTRIBUTE, "cid:ID" + index);
// Increment the index
index++;
} else {
// No data - this is a fault.
// Clear the reply and install the fault
reply.removeAllAttachments();
bodyElement.detachNode();
createFault(replyBody, "soap-env:Client.Title", "Unknown title",
SERVICE_URI, title);
return;
}
}
| private void | handleBookListRequest(javax.xml.soap.SOAPBody replyBody)Handles a request for list of book names.
// Create a BookTitles element containing an entry
// for each book title.
SOAPBodyElement bodyElement = replyBody.addBodyElement(BOOK_TITLES_NAME);
// Add 'xsi:type = "SOAP-ENC:Array"'
bodyElement.addAttribute(
soapFactory.createName("type", XMLSCHEMA_INSTANCE_PREFIX, XMLSCHEMA_INSTANCE_URI),
SOAP_ENC_PREFIX + ":Array");
// Add 'SOAP-ENC:arrayType = "xsd:string[]"
bodyElement.addAttribute(
soapFactory.createName("arrayType", SOAP_ENC_PREFIX, SOAPConstants.URI_NS_SOAP_ENCODING),
XMLSCHEMA_PREFIX + ":string[]");
// Add an array entry for each book
String[] titles = BookImageServletData.getBookTitles();
for (int i = 0; i < titles.length; i++) {
SOAPElement titleElement = bodyElement.addChildElement("item");
titleElement.addTextNode(titles[i]);
}
| public javax.xml.soap.SOAPMessage | onMessage(javax.xml.soap.SOAPMessage message)Handles a received SOAP message.
if (messageFactory == null) {
// Create all static data on first call
messageFactory = MessageFactory.newInstance();
soapFactory = SOAPFactory.newInstance();
BOOK_LIST_NAME = soapFactory.createName("BookList", SERVICE_PREFIX, SERVICE_URI);
BOOK_TITLES_NAME = soapFactory.createName("BookTitles", SERVICE_PREFIX, SERVICE_URI);
BOOK_IMAGE_REQUEST_NAME =
soapFactory.createName("BookImageRequest", SERVICE_PREFIX, SERVICE_URI);
BOOK_IMAGES_NAME = soapFactory.createName("BookImages", SERVICE_PREFIX, SERVICE_URI);
IMAGE_TYPE_ATTRIBUTE = soapFactory.createName("imageType", SERVICE_PREFIX, SERVICE_URI);
HREF_ATTRIBUTE = soapFactory.createName("href");
}
// Create the reply message and define the namespace
// and encoding for the elements used in the reply.
SOAPMessage reply = messageFactory.createMessage();
SOAPEnvelope replyEnvelope = reply.getSOAPPart().getEnvelope();
replyEnvelope.getHeader().detachNode();
replyEnvelope.addNamespaceDeclaration(SERVICE_PREFIX, SERVICE_URI);
replyEnvelope.addNamespaceDeclaration(SOAP_ENC_PREFIX,
SOAPConstants.URI_NS_SOAP_ENCODING);
replyEnvelope.addNamespaceDeclaration(XMLSCHEMA_PREFIX, XMLSCHEMA_URI);
replyEnvelope.addNamespaceDeclaration(XMLSCHEMA_INSTANCE_PREFIX,
XMLSCHEMA_INSTANCE_URI);
replyEnvelope.setEncodingStyle(SOAPConstants.URI_NS_SOAP_ENCODING);
SOAPBody replyBody = reply.getSOAPPart().getEnvelope().getBody();
// There are two requests - one for the list of
// book titles, the other for the image for a book.
SOAPBody requestBody = message.getSOAPPart().getEnvelope().getBody();
Iterator iter = requestBody.getChildElements();
if (iter.hasNext()) {
// The child element contains the request
SOAPElement element = (SOAPElement)iter.next();
Name elementName = element.getElementName();
if (elementName.equals(BOOK_LIST_NAME)) {
handleBookListRequest(replyBody);
} else if (elementName.equals(BOOK_IMAGE_REQUEST_NAME)) {
handleBookImageRequest(element, reply);
} else {
// Unrecognized request - this is a fault.
createFault(replyBody, "soap-env:Client.UnknownRequest", "Unrecognized request",
SERVICE_URI, elementName.getLocalName());
}
} else {
// No request - this is a fault
createFault(replyBody, "soap-env:Client.MissingRequest", "Missing request",
SERVICE_URI, "No request found");
}
return reply;
|
|