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    import javax.jms.JMSException;
022    import javax.jms.Message;
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    import org.apache.activemq.command.ActiveMQDestination;
026    import org.apache.activemq.web.BrokerFacade;
027    import org.apache.activemq.web.DestinationFacade;
028    import org.apache.activemq.web.WebClient;
029    import org.springframework.web.servlet.ModelAndView;
030    import org.springframework.web.servlet.mvc.Controller;
031    
032    /**
033     * Sends a message
034     */
035    public class SendMessage extends DestinationFacade implements Controller {
036    
037        private String jmsText;
038        private boolean jmsPersistent;
039        private int jmsPriority;
040        private int jmsTimeToLive = -1;
041        private String jmsCorrelationID;
042        private String jmsReplyTo;
043        private String jmsType;
044        private int jmsMessageCount = 1;
045        private String jmsMessageCountHeader = "JMSXMessageNumber";
046        private boolean redirectToBrowse;
047    
048        public SendMessage(BrokerFacade brokerFacade) {
049            super(brokerFacade);
050        }
051    
052        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
053            WebClient client = WebClient.getWebClient(request);
054            ActiveMQDestination dest = createDestination();
055    
056            sendMessages(request, client, dest);
057            if (redirectToBrowse) {
058                if (isQueue()) {
059                    return new ModelAndView("redirect:browse.jsp?destination=" + getJMSDestination());
060                }
061            }
062            return redirectToBrowseView();
063        }
064    
065        protected void sendMessages(HttpServletRequest request, WebClient client, ActiveMQDestination dest)
066                throws JMSException {
067            if (jmsMessageCount <= 1) {
068                jmsMessageCount = 1;
069            }
070            for (int i = 0; i < jmsMessageCount; i++) {
071                Message message = createMessage(client, request);
072                appendHeaders(message, request);
073                if (jmsMessageCount > 1) {
074                    message.setIntProperty(jmsMessageCountHeader, i + 1);
075                }
076    
077                client.send(dest, message, jmsPersistent, jmsPriority, jmsTimeToLive);
078            }
079        }
080    
081        // Properties
082        // -------------------------------------------------------------------------
083    
084        public String getJMSCorrelationID() {
085            return jmsCorrelationID;
086        }
087    
088        public void setJMSCorrelationID(String correlationID) {
089            if (correlationID != null) {
090                correlationID = correlationID.trim();
091            }
092            jmsCorrelationID = correlationID;
093        }
094    
095        public String getJMSReplyTo() {
096            return jmsReplyTo;
097        }
098    
099        public void setJMSReplyTo(String replyTo) {
100            if (replyTo != null) {
101                replyTo = replyTo.trim();
102            }
103            jmsReplyTo = replyTo;
104        }
105    
106        public String getJMSType() {
107            return jmsType;
108        }
109    
110        public void setJMSType(String type) {
111            if (type != null) {
112                type = type.trim();
113            }
114            jmsType = type;
115        }
116    
117        public boolean isJMSPersistent() {
118            return jmsPersistent;
119        }
120    
121        public void setJMSPersistent(boolean persistent) {
122            this.jmsPersistent = persistent;
123        }
124    
125        public int getJMSPriority() {
126            return jmsPriority;
127        }
128    
129        public void setJMSPriority(int priority) {
130            this.jmsPriority = priority;
131        }
132    
133        public String getJMSText() {
134            return jmsText;
135        }
136    
137        public void setJMSText(String text) {
138            this.jmsText = text;
139        }
140    
141        public int getJMSTimeToLive() {
142            return jmsTimeToLive;
143        }
144    
145        public void setJMSTimeToLive(int timeToLive) {
146            this.jmsTimeToLive = timeToLive;
147        }
148    
149        public int getJMSMessageCount() {
150            return jmsMessageCount;
151        }
152    
153        public void setJMSMessageCount(int copies) {
154            jmsMessageCount = copies;
155        }
156    
157        public String getJMSMessageCountHeader() {
158            return jmsMessageCountHeader;
159        }
160    
161        public void setJMSMessageCountHeader(String messageCountHeader) {
162            if (messageCountHeader != null) {
163                messageCountHeader = messageCountHeader.trim();
164            }
165            jmsMessageCountHeader = messageCountHeader;
166        }
167    
168        // Implementation methods
169        // -------------------------------------------------------------------------
170        protected Message createMessage(WebClient client, HttpServletRequest request) throws JMSException {
171            if (jmsText != null) {
172                return client.getSession().createTextMessage(jmsText);
173            }
174            // TODO create Bytes message from request body...
175            return client.getSession().createMessage();
176        }
177    
178        @SuppressWarnings("rawtypes")
179        protected void appendHeaders(Message message, HttpServletRequest request) throws JMSException {
180            message.setJMSCorrelationID(jmsCorrelationID);
181            if (jmsReplyTo != null && jmsReplyTo.trim().length() > 0) {
182                message.setJMSReplyTo(ActiveMQDestination.createDestination(jmsReplyTo, ActiveMQDestination.QUEUE_TYPE));
183            }
184            message.setJMSType(jmsType);
185    
186            // now lets add all of the parameters
187            Map map = request.getParameterMap();
188            if (map != null) {
189                for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
190                    Map.Entry entry = (Map.Entry) iter.next();
191                    String name = (String) entry.getKey();
192                    if (name.equals("secret")) {
193                        continue;
194                    }
195                    Object value = entry.getValue();
196                    if (isValidPropertyName(name)) {
197                        if (value instanceof String[]) {
198                            String[] array = (String[]) value;
199                            if (array.length > 0) {
200                                value = array[0];
201                            } else {
202                                value = null;
203                            }
204                        }
205                        if ((name.equals("AMQ_SCHEDULED_DELAY") || name.equals("AMQ_SCHEDULED_PERIOD"))) {
206                            if (value != null) {
207                                String str = value.toString().trim();
208                                if (str.length() > 0) {
209                                    message.setLongProperty(name, Long.parseLong(str));
210                                }
211                            }
212                        } else if (name.equals("AMQ_SCHEDULED_REPEAT")) {
213                            if (value != null) {
214                                String str = value.toString().trim();
215                                if (str.length() > 0) {
216                                    message.setIntProperty(name, Integer.parseInt(str));
217                                }
218                            }
219                        } else if (name.equals("AMQ_SCHEDULED_CRON")) {
220                            if (value != null) {
221                                String str = value.toString().trim();
222                                if (str.length() > 0) {
223                                    message.setStringProperty(name, str);
224                                }
225                            }
226                        } else {
227                            if (value instanceof String) {
228                                String text = value.toString().trim();
229                                if (text.length() == 0) {
230                                    value = null;
231                                } else {
232                                    value = text;
233                                }
234                            }
235                            if (value != null) {
236                                message.setObjectProperty(name, value);
237                            }
238                        }
239                    }
240                }
241            }
242        }
243        protected boolean isValidPropertyName(String name) {
244            // allow JMSX extensions or non JMS properties
245            return name.startsWith("JMSX") || !name.startsWith("JMS");
246        }
247    
248        public String[] getSupportedHttpMethods() {
249            return new String[]{"POST"};
250        }
251    }