1 17 package org.apache.activemq.web.view; 18 19 import javax.jms.JMSException ; 20 import javax.jms.Message ; 21 import javax.jms.QueueBrowser ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Enumeration ; 29 30 36 public class SimpleMessageRenderer implements MessageRenderer { 37 38 private String contentType = "text/xml"; 39 private int maxMessages = 0; 40 41 public void renderMessages(HttpServletRequest request, HttpServletResponse response, QueueBrowser browser) 42 throws IOException , JMSException , ServletException { 43 response.setContentType(getContentType()); 45 PrintWriter writer = response.getWriter(); 46 printHeader(writer, browser, request); 47 48 Enumeration iter = browser.getEnumeration(); 49 for (int counter = 0; iter.hasMoreElements() && (maxMessages <= 0 || counter < maxMessages); counter++) { 50 Message message = (Message ) iter.nextElement(); 51 renderMessage(writer, request, response, browser, message); 52 } 53 54 printFooter(writer, browser, request); 55 } 56 57 public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, 58 QueueBrowser browser, Message message) throws JMSException , ServletException { 59 writer.print("<message id='"); 61 writer.print(message.getJMSMessageID()); 62 writer.println("'/>"); 63 } 64 65 public int getMaxMessages() { 68 return maxMessages; 69 } 70 71 public void setMaxMessages(int maxMessages) { 72 this.maxMessages = maxMessages; 73 } 74 75 public String getContentType() { 76 return contentType; 77 } 78 79 public void setContentType(String contentType) { 80 this.contentType = contentType; 81 } 82 83 86 protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) 87 throws IOException , JMSException , ServletException { 88 writer.println(""); 89 writer.print("<messages queue='"); 90 writer.print(browser.getQueue()); 91 writer.print("'"); 92 String selector = browser.getMessageSelector(); 93 if (selector != null) { 94 writer.print(" selector='"); 95 writer.print(selector); 96 writer.print("'"); 97 } 98 writer.println(">"); 99 } 100 101 protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) 102 throws IOException , JMSException , ServletException { 103 writer.println("</messages>"); 104 } 105 106 } 107 | Popular Tags |