Coverage Report - org.mule.transport.jms.JmsMessageUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsMessageUtils
0%
0/179
0%
0/126
0
 
 1  
 /*
 2  
  * $Id: JmsMessageUtils.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.jms;
 12  
 
 13  
 import org.mule.api.transport.OutputHandler;
 14  
 import org.mule.util.ArrayUtils;
 15  
 import org.mule.util.ClassUtils;
 16  
 import org.mule.util.IOUtils;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.ObjectOutputStream;
 22  
 import java.io.Serializable;
 23  
 import java.text.MessageFormat;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Enumeration;
 26  
 import java.util.HashMap;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 import javax.jms.BytesMessage;
 32  
 import javax.jms.Destination;
 33  
 import javax.jms.JMSException;
 34  
 import javax.jms.MapMessage;
 35  
 import javax.jms.Message;
 36  
 import javax.jms.MessageEOFException;
 37  
 import javax.jms.MessageFormatException;
 38  
 import javax.jms.ObjectMessage;
 39  
 import javax.jms.Queue;
 40  
 import javax.jms.Session;
 41  
 import javax.jms.StreamMessage;
 42  
 import javax.jms.TextMessage;
 43  
 import javax.jms.Topic;
 44  
 
 45  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 46  
 import org.apache.commons.logging.Log;
 47  
 import org.apache.commons.logging.LogFactory;
 48  
 
 49  
 /**
 50  
  * <code>JmsMessageUtils</code> contains helper method for dealing with JMS
 51  
  * messages in Mule.
 52  
  */
 53  0
 public class JmsMessageUtils
 54  
 {
 55  
     public static final char REPLACEMENT_CHAR = '_';
 56  
 
 57  0
     private static final Log logger = LogFactory.getLog(JmsMessageUtils.class);
 58  
 
 59  
     /**
 60  
      * Encode a String so that is is a valid JMS header name
 61  
      *
 62  
      * @param name the String to encode
 63  
      * @return a valid JMS header name
 64  
      */
 65  
     public static String encodeHeader(String name)
 66  
     {
 67  
         // check against JMS 1.1 spec, sections 3.5.1 (3.8.1.1)
 68  0
         boolean nonCompliant = false;
 69  
 
 70  0
         if (StringUtils.isEmpty(name))
 71  
         {
 72  0
             throw new IllegalArgumentException("Header name to encode must not be null or empty");
 73  
         }
 74  
 
 75  0
         int i = 0, length = name.length();
 76  0
         while (i < length && Character.isJavaIdentifierPart(name.charAt(i)))
 77  
         {
 78  
             // zip through
 79  0
             i++;
 80  
         }
 81  
 
 82  0
         if (i == length)
 83  
         {
 84  
             // String is already valid
 85  0
             return name;
 86  
         }
 87  
         else
 88  
         {
 89  
             // make a copy, fix up remaining characters
 90  0
             StringBuffer sb = new StringBuffer(name);
 91  0
             for (int j = i; j < length; j++)
 92  
             {
 93  0
                 if (!Character.isJavaIdentifierPart(sb.charAt(j)))
 94  
                 {
 95  0
                     sb.setCharAt(j, REPLACEMENT_CHAR);
 96  0
                     nonCompliant = true;
 97  
                 }
 98  
             }
 99  
 
 100  0
             if (nonCompliant)
 101  
             {
 102  0
                 logger.warn(MessageFormat.format(
 103  
                         "Header: {0} is not compliant with JMS specification (sec. 3.5.1, 3.8.1.1). It will cause " +
 104  
                                 "problems in your and other applications. Please update your application code to correct this. " +
 105  
                                 "Mule renamed it to {1}", name, sb.toString()));
 106  
             }
 107  
 
 108  0
             return sb.toString();
 109  
         }
 110  
     }
 111  
 
 112  
     public static Message toMessage(Object object, Session session) throws JMSException
 113  
     {
 114  0
         if (object instanceof Message)
 115  
         {
 116  0
             return (Message) object;
 117  
         }
 118  0
         else if (object instanceof String)
 119  
         {
 120  0
             return stringToMessage((String) object, session);
 121  
         }
 122  0
         else if (object instanceof Map<?, ?> && validateMapMessageType((Map<?, ?>)object))
 123  
         {
 124  0
             return mapToMessage((Map<?, ?>) object, session);
 125  
         }
 126  0
         else if (object instanceof InputStream)
 127  
         {
 128  0
             return inputStreamToMessage((InputStream) object, session);
 129  
         }
 130  0
         else if (object instanceof List<?>)
 131  
         {
 132  0
             return listToMessage((List<?>) object, session);
 133  
         }
 134  0
         else if (object instanceof byte[])
 135  
         {
 136  0
             return byteArrayToMessage((byte[]) object, session);
 137  
         }
 138  0
         else if (object instanceof Serializable)
 139  
         {
 140  0
             return serializableToMessage((Serializable) object, session);
 141  
         }
 142  0
         else if (object instanceof OutputHandler)
 143  
         {
 144  0
             return outputHandlerToMessage((OutputHandler) object, session);
 145  
         }
 146  
         else
 147  
         {
 148  0
             throw new JMSException(
 149  
                 "Source was not of a supported type. Valid types are Message, String, Map, InputStream, List, byte[], Serializable or OutputHandler, "
 150  
                                 + "but was " + ClassUtils.getShortClassName(object, "<null>"));
 151  
         }
 152  
     }
 153  
 
 154  
     private static Message stringToMessage(String value, Session session) throws JMSException
 155  
     {
 156  0
         return session.createTextMessage(value);
 157  
     }
 158  
 
 159  
     private static Message mapToMessage(Map<?, ?> value, Session session) throws JMSException
 160  
     {
 161  0
         MapMessage mMsg = session.createMapMessage();
 162  
 
 163  0
         for (Iterator<?> i = value.entrySet().iterator(); i.hasNext();)
 164  
         {
 165  0
             Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
 166  0
             mMsg.setObject(entry.getKey().toString(), entry.getValue());
 167  0
         }
 168  
 
 169  0
         return mMsg;
 170  
     }
 171  
 
 172  
     private static Message inputStreamToMessage(InputStream value, Session session) throws JMSException
 173  
     {
 174  0
         StreamMessage streamMessage = session.createStreamMessage();
 175  0
         byte[] buffer = new byte[4096];
 176  
         int len;
 177  
 
 178  
         try
 179  
         {
 180  0
             while ((len = value.read(buffer)) != -1)
 181  
             {
 182  0
                 streamMessage.writeBytes(buffer, 0, len);
 183  
             }
 184  0
         }
 185  0
         catch (IOException e)
 186  
         {
 187  0
             throw new JMSException("Failed to read input stream to create a stream message: " + e);
 188  
         }
 189  
         finally
 190  
         {
 191  0
             IOUtils.closeQuietly(value);
 192  0
         }
 193  
 
 194  0
         return streamMessage;
 195  
     }
 196  
 
 197  
     private static Message listToMessage(List<?> value, Session session)
 198  
         throws JMSException
 199  
     {
 200  0
         StreamMessage sMsg = session.createStreamMessage();
 201  
 
 202  0
         for (Iterator<?> iter = value.iterator(); iter.hasNext();)
 203  
         {
 204  0
             Object o = iter.next();
 205  0
             if (validateStreamMessageType(o))
 206  
             {
 207  0
                 sMsg.writeObject(o);
 208  
             }
 209  
             else
 210  
             {
 211  0
                 throw new MessageFormatException(String.format(
 212  
                     "Invalid type passed to StreamMessage: %s . Allowed types are: "
 213  
                                     + "Boolean, Byte, Short, Character, Integer, Long, Float, Double,"
 214  
                                     + "String and byte[]", ClassUtils.getShortClassName(o, "null")));
 215  
             }
 216  0
         }
 217  0
         return sMsg;
 218  
     }
 219  
 
 220  
     private static Message byteArrayToMessage(byte[] value, Session session) throws JMSException
 221  
     {
 222  0
         BytesMessage bMsg = session.createBytesMessage();
 223  0
         bMsg.writeBytes(value);
 224  
 
 225  0
         return bMsg;
 226  
     }
 227  
 
 228  
     private static Message serializableToMessage(Serializable value, Session session) throws JMSException
 229  
     {
 230  0
         ObjectMessage oMsg = session.createObjectMessage();
 231  0
         oMsg.setObject(value);
 232  
 
 233  0
         return oMsg;
 234  
     }
 235  
 
 236  
     private static Message outputHandlerToMessage(OutputHandler value, Session session) throws JMSException
 237  
     {
 238  0
         ByteArrayOutputStream output = new ByteArrayOutputStream();
 239  
         try
 240  
         {
 241  0
             value.write(null, output);
 242  
         }
 243  0
         catch (IOException e)
 244  
         {
 245  0
             JMSException j = new JMSException("Could not serialize OutputHandler.");
 246  0
             j.initCause(e);
 247  0
             throw j;
 248  0
         }
 249  
 
 250  0
         BytesMessage bMsg = session.createBytesMessage();
 251  0
         bMsg.writeBytes(output.toByteArray());
 252  
 
 253  0
         return bMsg;
 254  
     }
 255  
 
 256  
     public static Object toObject(Message source, String jmsSpec, String encoding) throws JMSException, IOException
 257  
     {
 258  0
         if (source instanceof ObjectMessage)
 259  
         {
 260  0
             return ((ObjectMessage) source).getObject();
 261  
         }
 262  0
         else if (source instanceof MapMessage)
 263  
         {
 264  0
             Map<String, Object> map = new HashMap<String, Object>();
 265  0
             MapMessage m = (MapMessage) source;
 266  
 
 267  0
             for (Enumeration<?> e = m.getMapNames(); e.hasMoreElements();)
 268  
             {
 269  0
                 String name = (String) e.nextElement();
 270  0
                 Object obj = m.getObject(name);
 271  0
                 map.put(name, obj);
 272  0
             }
 273  
 
 274  0
             return map;
 275  
         }
 276  0
         else if (source instanceof TextMessage)
 277  
         {
 278  0
             return ((TextMessage) source).getText();
 279  
         }
 280  0
         else if (source instanceof BytesMessage)
 281  
         {
 282  0
             return toByteArray(source, jmsSpec, encoding);
 283  
         }
 284  0
         else if (source instanceof StreamMessage)
 285  
         {
 286  0
             List<Object> result = new ArrayList<Object>();
 287  
             try
 288  
             {
 289  0
                 StreamMessage sMsg = (StreamMessage) source;
 290  
                 Object obj;
 291  0
                 while ((obj = sMsg.readObject()) != null)
 292  
                 {
 293  0
                     result.add(obj);
 294  
                 }
 295  
             }
 296  0
             catch (MessageEOFException eof)
 297  
             {
 298  
                 // ignored
 299  
             }
 300  0
             catch (Exception e)
 301  
             {
 302  0
                 throw new JMSException("Failed to extract information from JMS Stream Message: " + e);
 303  0
             }
 304  0
             return result;
 305  
         }
 306  
 
 307  
         // what else is there to do?
 308  0
         return source;
 309  
     }
 310  
 
 311  
     /**
 312  
      * @param message the message to receive the bytes from. Note this only works for
 313  
      *                TextMessge, ObjectMessage, StreamMessage and BytesMessage.
 314  
      * @param jmsSpec indicates the JMS API version, either
 315  
      *                {@link JmsConstants#JMS_SPECIFICATION_102B} or
 316  
      *                {@link JmsConstants#JMS_SPECIFICATION_11}. Any other value
 317  
      *                including <code>null</code> is treated as fallback to
 318  
      *                {@link JmsConstants#JMS_SPECIFICATION_102B}.
 319  
      * @return a byte array corresponding with the message payload
 320  
      * @throws JMSException        if the message can't be read or if the message passed is
 321  
      *                             a MapMessage
 322  
      * @throws java.io.IOException if a failure occurs while reading the stream and
 323  
      *                             converting the message data
 324  
      */
 325  
     public static byte[] toByteArray(Message message, String jmsSpec, String encoding) throws JMSException, IOException
 326  
     {
 327  0
         if (message instanceof BytesMessage)
 328  
         {
 329  0
             BytesMessage bMsg = (BytesMessage) message;
 330  0
             bMsg.reset();
 331  
 
 332  0
             if (JmsConstants.JMS_SPECIFICATION_11.equals(jmsSpec))
 333  
             {
 334  0
                 long bmBodyLength = bMsg.getBodyLength();
 335  0
                 if (bmBodyLength > Integer.MAX_VALUE)
 336  
                 {
 337  0
                     throw new JMSException("Size of BytesMessage exceeds Integer.MAX_VALUE; "
 338  
                             + "please consider using JMS StreamMessage instead");
 339  
                 }
 340  
 
 341  0
                 if (bmBodyLength > 0)
 342  
                 {
 343  0
                     byte[] bytes = new byte[(int) bmBodyLength];
 344  0
                     bMsg.readBytes(bytes);
 345  0
                     return bytes;
 346  
                 }
 347  
                 else
 348  
                 {
 349  0
                     return ArrayUtils.EMPTY_BYTE_ARRAY;
 350  
                 }
 351  
             }
 352  
             else
 353  
             {
 354  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
 355  0
                 byte[] buffer = new byte[4096];
 356  
                 int len;
 357  
 
 358  0
                 while ((len = bMsg.readBytes(buffer)) != -1)
 359  
                 {
 360  0
                     baos.write(buffer, 0, len);
 361  
                 }
 362  
 
 363  0
                 if (baos.size() > 0)
 364  
                 {
 365  0
                     return baos.toByteArray();
 366  
                 }
 367  
                 else
 368  
                 {
 369  0
                     return ArrayUtils.EMPTY_BYTE_ARRAY;
 370  
                 }
 371  
             }
 372  
         }
 373  0
         else if (message instanceof StreamMessage)
 374  
         {
 375  0
             StreamMessage sMsg = (StreamMessage) message;
 376  0
             sMsg.reset();
 377  
 
 378  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
 379  0
             byte[] buffer = new byte[4096];
 380  
             int len;
 381  
 
 382  0
             while ((len = sMsg.readBytes(buffer)) != -1)
 383  
             {
 384  0
                 baos.write(buffer, 0, len);
 385  
             }
 386  
 
 387  0
             return baos.toByteArray();
 388  
         }
 389  0
         else if (message instanceof ObjectMessage)
 390  
         {
 391  0
             ObjectMessage oMsg = (ObjectMessage) message;
 392  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 393  0
             ObjectOutputStream os = new ObjectOutputStream(baos);
 394  0
             os.writeObject(oMsg.getObject());
 395  0
             os.flush();
 396  0
             os.close();
 397  0
             return baos.toByteArray();
 398  
         }
 399  0
         else if (message instanceof TextMessage)
 400  
         {
 401  0
             TextMessage tMsg = (TextMessage) message;
 402  0
             String tMsgText = tMsg.getText();
 403  
 
 404  0
             if (null == tMsgText)
 405  
             {
 406  
                 // Avoid creating new instances of byte arrays, even empty ones. The
 407  
                 // load on this part of the code can be high.
 408  0
                 return ArrayUtils.EMPTY_BYTE_ARRAY;
 409  
             }
 410  
             else
 411  
             {
 412  0
                 return tMsgText.getBytes(encoding);
 413  
             }
 414  
         }
 415  
         else
 416  
         {
 417  0
             throw new JMSException("Cannot get bytes from Map Message");
 418  
         }
 419  
     }
 420  
 
 421  
     public static String getNameForDestination(Destination dest) throws JMSException
 422  
     {
 423  0
         if (dest instanceof Queue)
 424  
         {
 425  0
             return ((Queue) dest).getQueueName();
 426  
         }
 427  0
         else if (dest instanceof Topic)
 428  
         {
 429  0
             return ((Topic) dest).getTopicName();
 430  
         }
 431  
         else
 432  
         {
 433  0
             return null;
 434  
         }
 435  
     }
 436  
 
 437  
     public static Message copyJMSProperties(Message from, Message to, JmsConnector connector)
 438  
             throws JMSException
 439  
     {
 440  0
         if (connector.supportsProperty(JmsConstants.JMS_CORRELATION_ID))
 441  
         {
 442  0
             to.setJMSCorrelationID(from.getJMSCorrelationID());
 443  
         }
 444  0
         if (connector.supportsProperty(JmsConstants.JMS_DELIVERY_MODE))
 445  
         {
 446  0
             to.setJMSDeliveryMode(from.getJMSDeliveryMode());
 447  
         }
 448  0
         if (connector.supportsProperty(JmsConstants.JMS_DESTINATION))
 449  
         {
 450  0
             to.setJMSDestination(from.getJMSDestination());
 451  
         }
 452  0
         if (connector.supportsProperty(JmsConstants.JMS_EXPIRATION))
 453  
         {
 454  0
             to.setJMSExpiration(from.getJMSExpiration());
 455  
         }
 456  0
         if (connector.supportsProperty(JmsConstants.JMS_MESSAGE_ID))
 457  
         {
 458  0
             to.setJMSMessageID(from.getJMSMessageID());
 459  
         }
 460  0
         if (connector.supportsProperty(JmsConstants.JMS_PRIORITY))
 461  
         {
 462  0
             to.setJMSPriority(from.getJMSPriority());
 463  
         }
 464  0
         if (connector.supportsProperty(JmsConstants.JMS_REDELIVERED))
 465  
         {
 466  0
             to.setJMSRedelivered(from.getJMSRedelivered());
 467  
         }
 468  0
         if (connector.supportsProperty(JmsConstants.JMS_REPLY_TO))
 469  
         {
 470  0
             to.setJMSReplyTo(from.getJMSReplyTo());
 471  
         }
 472  0
         if (connector.supportsProperty(JmsConstants.JMS_TIMESTAMP))
 473  
         {
 474  0
             to.setJMSTimestamp(from.getJMSTimestamp());
 475  
         }
 476  0
         if (connector.supportsProperty(JmsConstants.JMS_TYPE))
 477  
         {
 478  0
             to.setJMSType(from.getJMSType());
 479  
         }
 480  0
         return to;
 481  
     }
 482  
 
 483  
     /**
 484  
      * {@link StreamMessage#writeObject(Object)} accepts only primitives (and wrappers), String and byte[].
 485  
      * An attempt to write anything else must fail with a MessageFormatException as per
 486  
      * JMS 1.1 spec, Section 7.3 Standard Exceptions, page 89, 1st paragraph.
 487  
      * <p/>
 488  
      * Unfortunately, some JMS vendors are not compliant in this area, enforce here for consistent behavior.
 489  
      *
 490  
      * @param candidate object to validate
 491  
      */
 492  
     protected static boolean validateStreamMessageType(Object candidate)
 493  
     {
 494  0
         if (candidate == null ||
 495  
                 candidate instanceof Boolean ||
 496  
                 candidate instanceof Byte ||
 497  
                 candidate instanceof Short ||
 498  
                 candidate instanceof Character ||
 499  
                 candidate instanceof Integer ||
 500  
                 candidate instanceof Long ||
 501  
                 candidate instanceof Float ||
 502  
                 candidate instanceof Double ||
 503  
                 candidate instanceof String ||
 504  
                 candidate instanceof byte[])
 505  
         {
 506  0
             return true;
 507  
         }
 508  
 
 509  0
         return false;
 510  
     }
 511  
 
 512  
     /**
 513  
      * <code>MapMessage#writeObject(Object)</code> accepts only primitives (and wrappers), String and byte[].
 514  
      * An attempt to write anything else must fail with a MessageFormatException as per
 515  
      * JMS 1.1 spec, Section 7.3 Standard Exceptions, page 89, 1st paragraph.
 516  
      * <p/>
 517  
      * Unfortunately, some JMS vendors are not compliant in this area, enforce here for consistent behavior.
 518  
      * Here we handle non-primitive maps as {@link ObjectMessage} rather than creating a {@link MapMessage}
 519  
      *
 520  
      * @param candidate Map to validate
 521  
      */
 522  
     protected static boolean validateMapMessageType(Map<?, ?> candidate)
 523  
     {
 524  0
         for (Iterator<?> iterator = candidate.values().iterator(); iterator.hasNext();)
 525  
         {
 526  0
             Object o = iterator.next();
 527  0
             if (!validateStreamMessageType(o))
 528  
             {
 529  0
                     return false;
 530  
             }
 531  0
         }
 532  0
         return true;
 533  
     }
 534  
 }