001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.activemq.web.controller; 018 019 import java.util.Iterator; 020 import java.util.Map; 021 022 import javax.jms.JMSException; 023 import javax.jms.Message; 024 import javax.servlet.http.HttpServletRequest; 025 import javax.servlet.http.HttpServletResponse; 026 027 import org.apache.activemq.command.ActiveMQDestination; 028 import org.apache.activemq.web.BrokerFacade; 029 import org.apache.activemq.web.DestinationFacade; 030 import org.apache.activemq.web.WebClient; 031 import org.springframework.web.servlet.ModelAndView; 032 import org.springframework.web.servlet.mvc.Controller; 033 034 /** 035 * Sends a message 036 * 037 * @version $Revision: 915387 $ 038 */ 039 public class SendMessage extends DestinationFacade implements Controller { 040 041 private String jmsText; 042 private boolean jmsPersistent; 043 private int jmsPriority; 044 private int jmsTimeToLive = -1; 045 private String jmsCorrelationID; 046 private String jmsReplyTo; 047 private String jmsType; 048 private int jmsMessageCount = 1; 049 private String jmsMessageCountHeader = "JMSXMessageNumber"; 050 private boolean redirectToBrowse; 051 052 public SendMessage(BrokerFacade brokerFacade) { 053 super(brokerFacade); 054 } 055 056 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 057 WebClient client = WebClient.getWebClient(request); 058 ActiveMQDestination dest = createDestination(); 059 060 sendMessages(request, client, dest); 061 if (redirectToBrowse) { 062 if (isQueue()) { 063 return new ModelAndView("redirect:browse.jsp?destination=" + getJMSDestination()); 064 } 065 } 066 return redirectToBrowseView(); 067 } 068 069 protected void sendMessages(HttpServletRequest request, WebClient client, ActiveMQDestination dest) throws JMSException { 070 if (jmsMessageCount <= 1) { 071 jmsMessageCount = 1; 072 } 073 for (int i = 0; i < jmsMessageCount; i++) { 074 Message message = createMessage(client, request); 075 appendHeaders(message, request); 076 if (jmsMessageCount > 1) { 077 message.setIntProperty(jmsMessageCountHeader, i + 1); 078 } 079 080 client.send(dest, message, jmsPersistent, jmsPriority, jmsTimeToLive); 081 } 082 } 083 084 // Properties 085 // ------------------------------------------------------------------------- 086 087 public String getJMSCorrelationID() { 088 return jmsCorrelationID; 089 } 090 091 public void setJMSCorrelationID(String correlationID) { 092 jmsCorrelationID = correlationID; 093 } 094 095 public String getJMSReplyTo() { 096 return jmsReplyTo; 097 } 098 099 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 }