1 package edu.rice.rubis.beans.servlets; 2 3 import java.io.IOException ; 4 import java.net.URLEncoder ; 5 6 import javax.jms.MapMessage ; 7 import javax.jms.Session ; 8 import javax.jms.TextMessage ; 9 import javax.jms.Topic ; 10 import javax.jms.TopicConnection ; 11 import javax.jms.TopicConnectionFactory ; 12 import javax.jms.TopicRequestor ; 13 import javax.jms.TopicSession ; 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServlet ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 21 31 32 public class SearchItemsByCategory extends HttpServlet 33 { 34 35 private void printError(String errorMsg, ServletPrinter sp) 36 { 37 sp.printHTMLheader("RUBiS ERROR: Search Items By Category"); 38 sp.printHTML("<h2>We cannot process your request due to the following error :</h2><br>"); 39 sp.printHTML(errorMsg); 40 } 41 42 private void itemList(Integer categoryId, int page, int nbOfItems, ServletPrinter sp, Context initialContext, String categoryName) 43 { 44 TopicConnectionFactory topicFactory = null; 45 TopicConnection connection = null; 46 TopicSession session = null; 47 Topic topic = null; 48 String list; 49 try 50 { 51 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 53 connection = topicFactory.createTopicConnection(); 55 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicSearchItemsByCategory"); 57 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 60 catch (Exception e) 61 { 62 sp.printHTML("Cannot connect to message bean MDB_SearchItemsByCategory : " +e+"<br>"); 63 return ; 64 } 65 try 66 { 67 TopicRequestor requestor = new TopicRequestor (session, topic); 69 MapMessage message = session.createMapMessage(); 71 message.setInt("categoryId", categoryId.intValue()); 73 message.setInt("page", page); 74 message.setInt("nbItems", nbOfItems); 75 message.setJMSCorrelationID("searchByCategory"); 76 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 79 connection.stop(); 80 list = reply.getText(); 82 requestor.close(); connection.close(); 85 } 86 catch (Exception e) 87 { 88 sp.printHTML("Cannot get the list of items: " +e+"<br>"); 89 return ; 90 } 91 try 92 { 93 if ((list != null) && (!list.equals(""))) 94 { 95 sp.printItemHeader(); 96 sp.printHTML(list); 97 sp.printItemFooter(); 98 } 99 else 100 { 101 if (page == 0) 102 sp.printHTML("<h2>Sorry, but there are no items available in this category !</h2>"); 103 else 104 { 105 sp.printHTML("<h2>Sorry, but there are no more items available in this category !</h2>"); 106 sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+categoryId+ 108 "&categoryName="+URLEncoder.encode(categoryName)+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>", ""); 109 } 110 return ; 111 } 112 if (page == 0) 113 sp.printItemFooter("", "<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+categoryId+ 114 "&categoryName="+URLEncoder.encode(categoryName)+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>"); 115 else 116 sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+categoryId+ 117 "&categoryName="+URLEncoder.encode(categoryName)+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>", 118 "<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+categoryId+ 119 "&categoryName="+URLEncoder.encode(categoryName)+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>"); 120 } 121 catch (Exception e) 122 { 123 printError("Exception getting item list: " + e +"<br>", sp); 124 } 125 } 126 127 128 136 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 137 { 138 ServletPrinter sp = null; 139 Context initialContext = null; 140 String categoryName; 141 String value; 142 Integer categoryId; 143 Integer page; 144 Integer nbOfItems; 145 146 categoryName = request.getParameter("categoryName"); 147 sp = new ServletPrinter(response, "SearchItemsByCategory"); 148 149 value = request.getParameter("category"); 150 if ((value == null) || (value.equals(""))) 151 { 152 printError("You must provide a category identifier!<br>", sp); 153 return ; 154 } 155 else 156 categoryId = new Integer (value); 157 158 value = request.getParameter("page"); 159 if ((value == null) || (value.equals(""))) 160 page = new Integer (0); 161 else 162 page = new Integer (value); 163 164 value = request.getParameter("nbOfItems"); 165 if ((value == null) || (value.equals(""))) 166 nbOfItems = new Integer (25); 167 else 168 nbOfItems = new Integer (value); 169 170 try 171 { 172 initialContext = new InitialContext (); 173 } 174 catch (Exception e) 175 { 176 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 177 return ; 178 } 179 180 if (categoryName == null) 181 { 182 sp.printHTMLheader("RUBiS: Missing category name"); 183 sp.printHTML("<h2>Items in this category</h2><br><br>"); 184 } 185 else 186 { 187 sp.printHTMLheader("RUBiS: Items in category "+categoryName); 188 sp.printHTML("<h2>Items in category "+categoryName+"</h2><br><br>"); 189 } 190 191 itemList(categoryId, page.intValue(), nbOfItems.intValue(), sp, initialContext, categoryName); 192 193 sp.printHTMLfooter(); 194 } 195 196 197 205 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 206 { 207 doGet(request, response); 208 } 209 } 210 | Popular Tags |