1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.web.controller; 18 19 import java.util.Iterator; 20 import java.util.Map; 21 22 import javax.jms.JMSException; 23 import javax.jms.Message; 24 import javax.servlet.http.HttpServletRequest; 25 import javax.servlet.http.HttpServletResponse; 26 27 import org.apache.activemq.command.ActiveMQDestination; 28 import org.apache.activemq.web.BrokerFacade; 29 import org.apache.activemq.web.DestinationFacade; 30 import org.apache.activemq.web.WebClient; 31 import org.springframework.web.servlet.ModelAndView; 32 import org.springframework.web.servlet.mvc.Controller; 33 34 /** 35 * Sends a message 36 * 37 * @version $Revision: 915387 $ 38 */ 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 } 83 84 // Properties 85 // ------------------------------------------------------------------------- 86 87 public String getJMSCorrelationID() { 88 return jmsCorrelationID; 89 } 90 91 public void setJMSCorrelationID(String correlationID) { 92 jmsCorrelationID = correlationID; 93 } 94 95 public String getJMSReplyTo() { 96 return jmsReplyTo; 97 } 98 99 public void setJMSReplyTo(String replyTo) { 100 jmsReplyTo = replyTo; 101 } 102 103 public String getJMSType() { 104 return jmsType; 105 } 106 107 public void setJMSType(String type) { 108 jmsType = type; 109 } 110 111 public boolean isJMSPersistent() { 112 return jmsPersistent; 113 } 114 115 public void setJMSPersistent(boolean persistent) { 116 this.jmsPersistent = persistent; 117 } 118 119 public int getJMSPriority() { 120 return jmsPriority; 121 } 122 123 public void setJMSPriority(int priority) { 124 this.jmsPriority = priority; 125 } 126 127 public String getJMSText() { 128 return jmsText; 129 } 130 131 public void setJMSText(String text) { 132 this.jmsText = text; 133 } 134 135 public int getJMSTimeToLive() { 136 return jmsTimeToLive; 137 } 138 139 public void setJMSTimeToLive(int timeToLive) { 140 this.jmsTimeToLive = timeToLive; 141 } 142 143 public int getJMSMessageCount() { 144 return jmsMessageCount; 145 } 146 147 public void setJMSMessageCount(int copies) { 148 jmsMessageCount = copies; 149 } 150 151 public String getJMSMessageCountHeader() { 152 return jmsMessageCountHeader; 153 } 154 155 public void setJMSMessageCountHeader(String messageCountHeader) { 156 jmsMessageCountHeader = messageCountHeader; 157 } 158 159 // Implementation methods 160 // ------------------------------------------------------------------------- 161 protected Message createMessage(WebClient client, HttpServletRequest request) throws JMSException { 162 if (jmsText != null) { 163 return client.getSession().createTextMessage(jmsText); 164 } 165 // TODO create Bytes message from request body... 166 return client.getSession().createMessage(); 167 } 168 169 protected void appendHeaders(Message message, HttpServletRequest request) throws JMSException { 170 message.setJMSCorrelationID(jmsCorrelationID); 171 if (jmsReplyTo != null && jmsReplyTo.trim().length() > 0) { 172 message.setJMSReplyTo(ActiveMQDestination.createDestination(jmsReplyTo, ActiveMQDestination.QUEUE_TYPE)); 173 } 174 message.setJMSType(jmsType); 175 176 // now lets add all of the parameters 177 Map map = request.getParameterMap(); 178 if (map != null) { 179 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 180 Map.Entry entry = (Map.Entry)iter.next(); 181 String name = (String)entry.getKey(); 182 Object value = entry.getValue(); 183 if (isValidPropertyName(name)) { 184 if (value instanceof String[]) { 185 String[] array = (String[])value; 186 if (array.length > 0) { 187 value = array[0]; 188 } else { 189 value = null; 190 } 191 } 192 if (value instanceof String) { 193 String text = value.toString().trim(); 194 if (text.length() == 0) { 195 value = null; 196 } else { 197 value = text; 198 } 199 } 200 if (value != null) { 201 message.setObjectProperty(name, value); 202 } 203 } 204 } 205 } 206 } 207 208 protected boolean isValidPropertyName(String name) { 209 // allow JMSX extensions or non JMS properties 210 return name.startsWith("JMSX") || !name.startsWith("JMS"); 211 } 212 213 public String[] getSupportedHttpMethods() { 214 return new String[]{"POST"}; 215 } 216 }