1 package edu.rice.rubis.beans.servlets; 2 3 import java.io.IOException ; 4 5 import javax.jms.MapMessage ; 6 import javax.jms.Session ; 7 import javax.jms.TextMessage ; 8 import javax.jms.Topic ; 9 import javax.jms.TopicConnection ; 10 import javax.jms.TopicConnectionFactory ; 11 import javax.jms.TopicRequestor ; 12 import javax.jms.TopicSession ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 25 public class SearchItemsByRegion extends HttpServlet 26 { 27 28 private void printError(String errorMsg, ServletPrinter sp) 29 { 30 sp.printHTMLheader("RUBiS ERROR: SearchItemsByRegion"); 31 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 32 sp.printHTML(errorMsg); 33 sp.printHTMLfooter(); 34 } 35 36 37 38 private void itemList(Integer categoryId, Integer regionId, int page, int nbOfItems, ServletPrinter sp, Context initialContext) 39 { 40 TopicConnectionFactory topicFactory = null; 41 TopicConnection connection = null; 42 TopicSession session = null; 43 Topic topic = null; 44 String list; 45 try 46 { 47 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 49 connection = topicFactory.createTopicConnection(); 51 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicSearchItemsByRegion"); 53 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 56 catch (Exception e) 57 { 58 sp.printHTML("Cannot connect to message bean MDB_SearchItemsByRegion : " +e+"<br>"); 59 return ; 60 } 61 try 62 { 63 TopicRequestor requestor = new TopicRequestor (session, topic); 65 MapMessage message = session.createMapMessage(); 67 message.setInt("categoryId", categoryId.intValue()); 69 message.setInt("regionId", regionId.intValue()); 70 message.setInt("page", page); 71 message.setInt("nbItems", nbOfItems); 72 message.setJMSCorrelationID("searchByRegion"); 73 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 76 connection.stop(); 77 list = reply.getText(); 79 requestor.close(); connection.close(); 82 } 83 catch (Exception e) 84 { 85 sp.printHTML("Cannot get the list of items: " +e+"<br>"); 86 return ; 87 } 88 try 89 { 90 if ((list != null) && (!list.equals(""))) 91 { 92 sp.printItemHeader(); 93 sp.printHTML(list); 94 sp.printItemFooter(); 95 } 96 else 97 { 98 if (page == 0) 99 sp.printHTML("<h3>Sorry, but there is no items in this category for this region.</h3><br>"); 100 else 101 { 102 sp.printHTML("<h3>Sorry, but there is no more items in this category for this region.</h3><br>"); 103 sp.printItemHeader(); 104 sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+ 105 "®ion="+regionId+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>", ""); 106 } 107 return ; 108 } 109 if (page == 0) 110 sp.printItemFooter("", "<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+ 111 "®ion="+regionId+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>"); 112 else 113 sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+ 114 "®ion="+regionId+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>", 115 "<a HREF=\"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+ 116 "®ion="+regionId+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>"); 117 } 118 catch (Exception e) 119 { 120 sp.printHTML("Exception getting item list: " + e +"<br>"); 121 } 122 } 123 124 133 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 134 { 135 ServletPrinter sp = null; 136 Context initialContext = null; 137 138 Integer categoryId, regionId; 139 Integer page; 140 Integer nbOfItems; 141 142 sp = new ServletPrinter(response, "SearchItemsByRegion"); 143 144 try 145 { 146 initialContext = new InitialContext (); 147 } 148 catch (Exception e) 149 { 150 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 151 return ; 152 } 153 154 String value = request.getParameter("category"); 155 if ((value == null) || (value.equals(""))) 156 { 157 printError("You must provide a category!<br>", sp); 158 return ; 159 } 160 else 161 categoryId = new Integer (value); 162 163 value = request.getParameter("region"); 164 if ((value == null) || (value.equals(""))) 165 { 166 printError("You must provide a region!<br>", sp); 167 return ; 168 } 169 else 170 regionId = new Integer (value); 171 172 value = request.getParameter("page"); 173 if ((value == null) || (value.equals(""))) 174 page = new Integer (0); 175 else 176 page = new Integer (value); 177 178 value = request.getParameter("nbOfItems"); 179 if ((value == null) || (value.equals(""))) 180 nbOfItems = new Integer (25); 181 else 182 nbOfItems = new Integer (value); 183 184 sp.printHTMLheader("RUBiS: Search items by region"); 185 sp.printHTML("<h2>Items in this region</h2><br><br>"); 186 187 itemList(categoryId, regionId, page.intValue(), nbOfItems.intValue(), sp, initialContext); 188 189 sp.printHTMLfooter(); 190 } 191 192 193 201 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 202 { 203 doGet(request, response); 204 } 205 } 206 | Popular Tags |