1 /** 2 * ServingXML 3 * 4 * Copyright (C) 2006 Daniel Parker 5 * daniel.parker@servingxml.com 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 **/ 20 21 package samples.books; 22 23 import java.util.logging.Level; 24 import java.util.ArrayList; 25 import java.util.Iterator; 26 27 import com.servingxml.components.content.dynamic.DynamicContentHandler; 28 import com.servingxml.components.content.Cacheable; 29 import com.servingxml.app.ServiceContext; 30 import com.servingxml.components.content.dynamic.AttributeSet; 31 import com.servingxml.components.content.dynamic.ContentWriter; 32 33 /** 34 * 35 * @author Daniel A. Parker (daniel.parker@servingxml.com) 36 */ 37 38 public class BookCatalog implements DynamicContentHandler, Cacheable { 39 private static final String sourceClass = BookCatalog.class.getName(); 40 41 static class BookEntry { 42 private final String bookId; 43 private final String categoryCode; 44 private final String title; 45 private final String publisher; 46 private final String author; 47 private final double price; 48 private final int quantity; 49 private final double tax; 50 BookEntry(String bookId, String categoryCode, String title, String publisher, String author, double price, int quantity, double tax)51 public BookEntry(String bookId, String categoryCode, String title, String publisher, 52 String author, double price, int quantity, double tax) { 53 this.bookId = bookId; 54 this.categoryCode = categoryCode; 55 this.title = title; 56 this.publisher = publisher; 57 this.author = author; 58 this.price = price; 59 this.quantity = quantity; 60 this.tax = tax; 61 } 62 getBookId()63 public String getBookId() { 64 return bookId; 65 } 66 getCategoryCode()67 public String getCategoryCode() { 68 return categoryCode; 69 } 70 getTitle()71 public String getTitle() { 72 return title; 73 } 74 getPublisher()75 public String getPublisher() { 76 return publisher; 77 } 78 getAuthor()79 public String getAuthor() { 80 return author; 81 } 82 getPrice()83 public double getPrice() { 84 return price; 85 } 86 getQuantity()87 public int getQuantity() { 88 return quantity; 89 } 90 getTax()91 public double getTax() { 92 return tax; 93 } 94 } 95 96 // All data members in a DynamicContentHandler should be declared as final 97 // and initialized in the constructor 98 // The onRequest method must be reentrant 99 private final String namespace = ""; 100 private final ArrayList bookList; 101 BookCatalog()102 public BookCatalog() { 103 bookList = new ArrayList(); 104 bookList.add(new BookEntry("1", "S","Number, the Language of Science", null, "Danzig", 5.95, 3, 0.0)); 105 bookList.add(new BookEntry("2", "F","Fierce Invalids Home From Hot Climates", "Bantam BookEntrys", "Robbins, Tom", 19.95, 5, 0.0)); 106 bookList.add(new BookEntry("3", "F","Pulp", "Black Sparrow Press", "Bukowski, Charles", 17.95, 5, 0.0)); 107 bookList.add(new BookEntry("4", "S","Language & the Science of Number", null, "Danzig", 8.95, 5, 0.0)); 108 bookList.add(new BookEntry("5", "S","Evolution of Complexity in Animal Culture", null, "Bonner", 5.95, 2, 0.0)); 109 bookList.add(new BookEntry("6", "CHILD","Harry Potter and Philosopher's Stone", "Raincoast BookEntrys", "J.K. Rowling", 31.50, 0, 0.0)); 110 bookList.add(new BookEntry("7", "CHILD","Harry Potter and the Chamber of Secrets", "Raincoast BookEntrys", "J.K. Rowling", 31.50, 0, 0.0)); 111 bookList.add(new BookEntry("8", "CHILD","Harry Potter and the Prisoner of Azkaban", "Raincoast BookEntrys", "J.K. Rowling", 31.50, 0, 0.0)); 112 bookList.add(new BookEntry("9", "CHILD","Harry Potter and the Goblet of Fire", "Raincoast BookEntrys", "J.K. Rowling", 31.50, 0, 0.0)); 113 bookList.add(new BookEntry("10", "F","When We Were Very Young", null, "Milne, A. A.", 12.50, 1, 7.5)); 114 bookList.add(new BookEntry("11", "C","Java Servlet Programming", "O'Reilly", "Hunter, Jason", 65.95, 12, 0.0)); 115 bookList.add(new BookEntry("12", "C","Design Patterns", "Addison Wesley", "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides", 49.95, 2, 12.5)); 116 bookList.add(new BookEntry("13", "C","creative html design", "New Riders", "Linda Weinman, William Weinman", 39.99, 2, 12.5)); 117 bookList.add(new BookEntry("14", "P","The Book of Nothing", "Johnathan Cape", "Barrow, John D.", 33.25, 2, 12.5)); 118 bookList.add(new BookEntry("15", "R","The Jesus Mysteries","Harmony BookEntrys", "Freke, Timothy, Peter Gandy", 33.25, 2, 12.5)); 119 } 120 onRequest(ServiceContext context, Book parameters, ContentWriter contentWriter)121 public void onRequest(ServiceContext context, Book parameters, 122 ContentWriter contentWriter) { 123 final String sourceMethod = "onRequest"; 124 125 context.trace(sourceClass,sourceMethod,"parameters = " + parameters.getCategory(),Level.FINEST); 126 127 String categoryCode = parameters.getCategory(); 128 if (categoryCode == null) { 129 categoryCode = ""; 130 } 131 AttributeSet attributes = contentWriter.newAttributeSet(); 132 attributes.addAttribute(namespace,"cat",categoryCode); 133 contentWriter.startElement(namespace,"books",attributes); 134 135 context.trace(sourceClass,sourceMethod,"category = " + categoryCode,Level.FINEST); 136 if (categoryCode.length() > 0) { 137 Iterator iter = bookList.iterator(); 138 while (iter.hasNext()) { 139 BookEntry bookEntry = (BookEntry)iter.next(); 140 if (bookEntry.getCategoryCode().equals(categoryCode)) { 141 context.trace(sourceClass,sourceMethod,"processing = " + categoryCode,Level.FINEST); 142 writeBook(bookEntry,contentWriter); 143 } 144 } 145 } 146 147 contentWriter.endElement(namespace,"books"); 148 } 149 writeBook(BookEntry bookEntry, ContentWriter contentWriter)150 private void writeBook(BookEntry bookEntry, ContentWriter contentWriter) { 151 152 AttributeSet itemAttributes = contentWriter.newAttributeSet(); 153 itemAttributes.addAttribute(namespace,"bookId",bookEntry.getBookId()); 154 itemAttributes.addAttribute(namespace,"cat",bookEntry.getCategoryCode()); 155 contentWriter.startElement(namespace,"book",itemAttributes); 156 contentWriter.insertElement(namespace,"title", bookEntry.getTitle()); 157 contentWriter.insertElement(namespace,"author", bookEntry.getAuthor()); 158 if (bookEntry.getPublisher() != null) { 159 contentWriter.insertElement(namespace,"publisher", bookEntry.getPublisher()); 160 } 161 contentWriter.insertElement(namespace,"price", bookEntry.getPrice()); 162 contentWriter.insertElement(namespace,"quantity", bookEntry.getQuantity()); 163 contentWriter.endElement(namespace,"book"); 164 } 165 getLastModified(Book key, long timestamp, long elapsed)166 public long getLastModified(Book key, long timestamp, long elapsed) { 167 168 // changed if older than 10 seconds 169 return elapsed > 10000 ? timestamp : timestamp + elapsed; 170 } 171 } 172 173