1 18 package org.apache.activemq.web.controller; 19 20 import org.apache.activemq.command.ActiveMQDestination; 21 import org.apache.activemq.web.BrokerFacade; 22 import org.apache.activemq.web.DestinationFacade; 23 import org.apache.activemq.web.WebClient; 24 import org.springframework.web.servlet.ModelAndView; 25 import org.springframework.web.servlet.mvc.Controller; 26 27 import javax.jms.JMSException ; 28 import javax.jms.Message ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 39 public class SendMessage extends DestinationFacade implements Controller { 40 41 private String JMSText; 42 private boolean JMSPersistent; 43 private int JMSPriority; 44 private int JMSTimeToLive = -1; 45 private String JMSCorrelationID; 46 private String JMSReplyTo; 47 private String JMSType; 48 private int JMSMessageCount = 1; 49 private String JMSMessageCountHeader = "JMSXMessageNumber"; 50 private boolean redirectToBrowse; 51 52 public SendMessage(BrokerFacade brokerFacade) { 53 super(brokerFacade); 54 } 55 56 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 57 WebClient client = WebClient.getWebClient(request); 58 ActiveMQDestination dest = createDestination(); 59 60 sendMessages(request, client, dest); 61 if (redirectToBrowse) { 62 if (isQueue()) { 63 return new ModelAndView("redirect:browse.jsp?destination=" + getJMSDestination()); 64 } 65 } 66 return redirectToBrowseView(); 67 } 68 69 protected void sendMessages(HttpServletRequest request, WebClient client, ActiveMQDestination dest) throws JMSException { 70 if (JMSMessageCount <= 1) { 71 JMSMessageCount = 1; 72 } 73 for (int i = 0; i < JMSMessageCount; i++) { 74 Message message = createMessage(client, request); 75 appendHeaders(message, request); 76 if (JMSMessageCount > 1) { 77 message.setIntProperty(JMSMessageCountHeader, i + 1); 78 } 79 80 client.send(dest, message, JMSPersistent, JMSPriority, JMSTimeToLive); 81 82 System.out.println("Sent message: " + message); 83 } 84 } 85 86 89 public String getJMSCorrelationID() { 90 return JMSCorrelationID; 91 } 92 93 public void setJMSCorrelationID(String correlationID) { 94 JMSCorrelationID = correlationID; 95 } 96 97 public String getJMSReplyTo() { 98 return JMSReplyTo; 99 } 100 101 public void setJMSReplyTo(String replyTo) { 102 JMSReplyTo = replyTo; 103 } 104 105 public String getJMSType() { 106 return JMSType; 107 } 108 109 public void setJMSType(String type) { 110 JMSType = type; 111 } 112 113 public boolean isJMSPersistent() { 114 return JMSPersistent; 115 } 116 117 public void setJMSPersistent(boolean persistent) { 118 this.JMSPersistent = persistent; 119 } 120 121 public int getJMSPriority() { 122 return JMSPriority; 123 } 124 125 public void setJMSPriority(int priority) { 126 this.JMSPriority = priority; 127 } 128 129 public String getJMSText() { 130 return JMSText; 131 } 132 133 public void setJMSText(String text) { 134 this.JMSText = text; 135 } 136 137 public int getJMSTimeToLive() { 138 return JMSTimeToLive; 139 } 140 141 public void setJMSTimeToLive(int timeToLive) { 142 this.JMSTimeToLive = timeToLive; 143 } 144 145 public int getJMSMessageCount() { 146 return JMSMessageCount; 147 } 148 149 public void setJMSMessageCount(int copies) { 150 JMSMessageCount = copies; 151 } 152 153 public String getJMSMessageCountHeader() { 154 return JMSMessageCountHeader; 155 } 156 157 public void setJMSMessageCountHeader(String messageCountHeader) { 158 JMSMessageCountHeader = messageCountHeader; 159 } 160 161 protected Message createMessage(WebClient client, HttpServletRequest request) throws JMSException { 164 if (JMSText != null) { 165 return client.getSession().createTextMessage(JMSText); 166 } 167 return client.getSession().createMessage(); 169 } 170 171 protected void appendHeaders(Message message, HttpServletRequest request) throws JMSException { 172 message.setJMSCorrelationID(JMSCorrelationID); 173 if (JMSReplyTo != null && JMSReplyTo.trim().length() > 0) { 174 message.setJMSReplyTo(ActiveMQDestination.createDestination(JMSReplyTo, ActiveMQDestination.QUEUE_TYPE)); 175 } 176 message.setJMSType(JMSType); 177 178 Map map = request.getParameterMap(); 180 if (map != null) { 181 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 182 Map.Entry entry = (Map.Entry ) iter.next(); 183 String name = (String ) entry.getKey(); 184 Object value = entry.getValue(); 185 if (isValidPropertyName(name)) { 186 if (value instanceof String []) { 187 String [] array = (String []) value; 188 if (array.length > 0) { 189 value = array[0]; 190 } 191 else { 192 value = null; 193 } 194 } 195 if (value instanceof String ) { 196 String text = value.toString().trim(); 197 if (text.length() == 0) { 198 value = null; 199 } 200 else { 201 value = text; 202 } 203 } 204 if (value != null) { 205 message.setObjectProperty(name, value); 206 } 207 } 208 } 209 } 210 } 211 212 protected boolean isValidPropertyName(String name) { 213 return name.startsWith("JMSX") || !name.startsWith("JMS"); 215 } 216 } 217 | Popular Tags |