KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > controller > SendMessage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Sends a message
36  *
37  * @version $Revision: 504235 $
38  */

39 public class SendMessage extends DestinationFacade implements Controller {
40
41     private String JavaDoc JMSText;
42     private boolean JMSPersistent;
43     private int JMSPriority;
44     private int JMSTimeToLive = -1;
45     private String JavaDoc JMSCorrelationID;
46     private String JavaDoc JMSReplyTo;
47     private String JavaDoc JMSType;
48     private int JMSMessageCount = 1;
49     private String JavaDoc JMSMessageCountHeader = "JMSXMessageNumber";
50     private boolean redirectToBrowse;
51
52     public SendMessage(BrokerFacade brokerFacade) {
53         super(brokerFacade);
54     }
55
56     public ModelAndView handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
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 JavaDoc request, WebClient client, ActiveMQDestination dest) throws JMSException JavaDoc {
70         if (JMSMessageCount <= 1) {
71             JMSMessageCount = 1;
72         }
73         for (int i = 0; i < JMSMessageCount; i++) {
74             Message JavaDoc 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     // Properties
87
// -------------------------------------------------------------------------
88

89     public String JavaDoc getJMSCorrelationID() {
90         return JMSCorrelationID;
91     }
92
93     public void setJMSCorrelationID(String JavaDoc correlationID) {
94         JMSCorrelationID = correlationID;
95     }
96
97     public String JavaDoc getJMSReplyTo() {
98         return JMSReplyTo;
99     }
100
101     public void setJMSReplyTo(String JavaDoc replyTo) {
102         JMSReplyTo = replyTo;
103     }
104
105     public String JavaDoc getJMSType() {
106         return JMSType;
107     }
108
109     public void setJMSType(String JavaDoc 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 JavaDoc getJMSText() {
130         return JMSText;
131     }
132
133     public void setJMSText(String JavaDoc 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 JavaDoc getJMSMessageCountHeader() {
154         return JMSMessageCountHeader;
155     }
156
157     public void setJMSMessageCountHeader(String JavaDoc messageCountHeader) {
158         JMSMessageCountHeader = messageCountHeader;
159     }
160
161     // Implementation methods
162
// -------------------------------------------------------------------------
163
protected Message JavaDoc createMessage(WebClient client, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
164         if (JMSText != null) {
165             return client.getSession().createTextMessage(JMSText);
166         }
167         // TODO create Bytes message from request body...
168
return client.getSession().createMessage();
169     }
170
171     protected void appendHeaders(Message JavaDoc message, HttpServletRequest JavaDoc request) throws JMSException JavaDoc {
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         // now lets add all of the parameters
179
Map JavaDoc map = request.getParameterMap();
180         if (map != null) {
181             for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
182                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
183                 String JavaDoc name = (String JavaDoc) entry.getKey();
184                 Object JavaDoc value = entry.getValue();
185                 if (isValidPropertyName(name)) {
186                     if (value instanceof String JavaDoc[]) {
187                         String JavaDoc[] array = (String JavaDoc[]) value;
188                         if (array.length > 0) {
189                             value = array[0];
190                         }
191                         else {
192                             value = null;
193                         }
194                     }
195                     if (value instanceof String JavaDoc) {
196                         String JavaDoc 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 JavaDoc name) {
213         // allow JMSX extensions or non JMS properties
214
return name.startsWith("JMSX") || !name.startsWith("JMS");
215     }
216 }
217
Popular Tags