1 18 19 package org.apache.activemq.web; 20 21 import org.apache.activemq.command.ActiveMQDestination; 22 import org.apache.activemq.command.ActiveMQQueue; 23 import org.apache.activemq.command.ActiveMQTopic; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import javax.jms.Destination ; 28 import javax.jms.JMSException ; 29 import javax.jms.TextMessage ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import java.io.BufferedReader ; 35 import java.io.IOException ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 52 public abstract class MessageServletSupport extends HttpServlet { 53 54 private static final transient Log log = LogFactory.getLog(MessageServletSupport.class); 55 56 private boolean defaultTopicFlag = true; 57 private Destination defaultDestination; 58 private String destinationParameter = "destination"; 59 private String typeParameter = "type"; 60 private String bodyParameter = "body"; 61 private boolean defaultMessagePersistent = true; 62 private int defaultMessagePriority = 5; 63 private long defaultMessageTimeToLive = 0; 64 private String destinationOptions; 65 66 public void init(ServletConfig servletConfig) throws ServletException { 67 super.init(servletConfig); 68 69 destinationOptions = servletConfig.getInitParameter("destinationOptions"); 70 71 String name = servletConfig.getInitParameter("topic"); 72 if (name != null) { 73 defaultTopicFlag = asBoolean(name); 74 } 75 76 if (log.isDebugEnabled()) { 77 log.debug("Defaulting to use topics: " + defaultTopicFlag); 78 } 79 80 name = servletConfig.getInitParameter("destination"); 81 if (name != null) { 82 if (defaultTopicFlag) { 83 defaultDestination = new ActiveMQTopic(name); 84 } 85 else { 86 defaultDestination = new ActiveMQQueue(name); 87 } 88 } 89 90 WebClient.initContext(getServletContext()); 92 } 93 94 public static boolean asBoolean(String param) { 95 return asBoolean(param, false); 96 } 97 98 public static boolean asBoolean(String param, boolean defaultValue) { 99 if (param == null) { 100 return defaultValue; 101 } 102 else { 103 return param.equalsIgnoreCase("true"); 104 } 105 } 106 107 108 protected void appendParametersToMessage(HttpServletRequest request, TextMessage message) throws JMSException { 109 Map parameterMap = request.getParameterMap(); 110 if (parameterMap == null) { 111 return; 112 } 113 Map parameters = new HashMap (parameterMap); 114 String correlationID = asString(parameters.remove("JMSCorrelationID")); 115 if (correlationID != null) { 116 message.setJMSCorrelationID(correlationID); 117 } 118 Long expiration = asLong(parameters.remove("JMSExpiration")); 119 if (expiration != null) { 120 message.setJMSExpiration(expiration.longValue()); 121 } 122 Integer priority = asInteger(parameters.remove("JMSPriority")); 123 if (expiration != null) { 124 message.setJMSPriority(priority.intValue()); 125 } 126 Destination replyTo = asDestination(parameters.remove("JMSReplyTo")); 127 if (replyTo != null) { 128 message.setJMSReplyTo(replyTo); 129 } 130 String type = (String ) asString(parameters.remove("JMSType")); 131 if (correlationID != null) { 132 message.setJMSType(type); 133 } 134 135 for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) { 136 Map.Entry entry = (Map.Entry ) iter.next(); 137 String name = (String ) entry.getKey(); 138 if (!destinationParameter.equals(name) && !typeParameter.equals(name) && !bodyParameter.equals(name)) { 139 Object value = entry.getValue(); 140 if (value instanceof Object []) { 141 Object [] array = (Object []) value; 142 if (array.length == 1) { 143 value = array[0]; 144 } 145 else { 146 log.warn("Can't use property: " + name + " which is of type: " + value.getClass().getName() + " value"); 147 value = null; 148 for (int i = 0, size = array.length; i < size; i++) { 149 log.debug("value[" + i + "] = " + array[i]); 150 } 151 } 152 } 153 if (value != null) { 154 message.setObjectProperty(name, value); 155 } 156 } 157 } 158 } 159 160 protected long getSendTimeToLive(HttpServletRequest request) { 161 String text = request.getParameter("JMSTimeToLive"); 162 if (text != null) { 163 return asLong(text); 164 } 165 return defaultMessageTimeToLive; 166 } 167 168 protected int getSendPriority(HttpServletRequest request) { 169 String text = request.getParameter("JMSPriority"); 170 if (text != null) { 171 return asInt(text); 172 } 173 return defaultMessagePriority; 174 } 175 176 protected boolean isSendPersistent(HttpServletRequest request) { 177 return defaultMessagePersistent; 178 } 179 180 protected Destination asDestination(Object value) { 181 if (value instanceof Destination) { 182 return (Destination) value; 183 } 184 if (value instanceof String ) { 185 String text = (String ) value; 186 return ActiveMQDestination.createDestination(text, ActiveMQDestination.QUEUE_TYPE); 187 } 188 if (value instanceof String []) { 189 String text = ((String []) value)[0]; 190 if (text == null) { 191 return null; 192 } 193 return ActiveMQDestination.createDestination(text, ActiveMQDestination.QUEUE_TYPE); 194 } 195 return null; 196 } 197 198 protected Integer asInteger(Object value) { 199 if (value instanceof Integer ) { 200 return (Integer ) value; 201 } 202 if (value instanceof String ) { 203 return Integer.valueOf((String ) value); 204 } 205 if (value instanceof String []) { 206 return Integer.valueOf(((String []) value)[0]); 207 } 208 return null; 209 } 210 211 protected Long asLong(Object value) { 212 if (value instanceof Long ) { 213 return (Long ) value; 214 } 215 if (value instanceof String ) { 216 return Long.valueOf((String ) value); 217 } 218 if (value instanceof String []) { 219 return Long.valueOf(((String []) value)[0]); 220 } 221 return null; 222 } 223 224 protected long asLong(String name) { 225 return Long.parseLong(name); 226 } 227 228 protected int asInt(String name) { 229 return Integer.parseInt(name); 230 } 231 232 protected String asString(Object value) { 233 if (value instanceof String []) { 234 return ((String [])value)[0]; 235 } 236 237 if (value != null) { 238 return value.toString(); 239 } 240 241 return null; 242 } 243 244 247 protected Destination getDestination(WebClient client, HttpServletRequest request) throws JMSException { 248 String destinationName = request.getParameter(destinationParameter); 249 if (destinationName == null) { 250 if (defaultDestination == null) { 251 return getDestinationFromURI(client, request); 252 } 253 else { 254 return defaultDestination; 255 } 256 } 257 258 return getDestination(client, request, destinationName); 259 } 260 261 265 protected Destination getDestinationFromURI(WebClient client, HttpServletRequest request) throws JMSException { 266 String uri = request.getPathInfo(); 267 if (uri == null) 268 return null; 269 270 if (uri.startsWith("/")) { 272 uri = uri.substring(1); 273 if (uri.length()==0) 274 return null; 275 } 276 277 uri = uri.replace('/', '.'); 278 System.err.println("destination uri="+uri); 279 return getDestination(client, request, uri); 280 } 281 282 285 protected Destination getDestination(WebClient client, HttpServletRequest request, String destinationName) throws JMSException { 286 287 289 boolean is_topic=defaultTopicFlag; 290 if (destinationName.startsWith("topic://")) 291 { 292 is_topic=true; 293 destinationName=destinationName.substring(8); 294 } 295 else if (destinationName.startsWith("channel://")) 296 { 297 is_topic=true; 298 destinationName=destinationName.substring(10); 299 } 300 else 301 is_topic=isTopic(request); 302 303 if( destinationOptions!=null ) { 304 destinationName += "?" + destinationOptions; 305 } 306 307 if (is_topic) { 308 return client.getSession().createTopic(destinationName); 309 } 310 else { 311 return client.getSession().createQueue(destinationName); 312 } 313 } 314 315 318 protected boolean isTopic(HttpServletRequest request) { 319 String typeText = request.getParameter(typeParameter); 320 if (typeText == null) { 321 return defaultTopicFlag; 322 } 323 return typeText.equalsIgnoreCase("topic"); 324 } 325 326 330 protected String getPostedMessageBody(HttpServletRequest request) throws IOException { 331 String answer = request.getParameter(bodyParameter); 332 if (answer == null && "text/xml".equals(request.getContentType())) { 333 BufferedReader reader = request.getReader(); 335 StringBuffer buffer = new StringBuffer (); 336 while (true) { 337 String line = reader.readLine(); 338 if (line == null) { 339 break; 340 } 341 buffer.append(line); 342 buffer.append("\n"); 343 } 344 return buffer.toString(); 345 } 346 return answer; 347 } 348 } 349 | Popular Tags |