Coverage Report - org.mule.util.StringMessageUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringMessageUtils
0%
0/98
0%
0/62
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.util;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.MuleRuntimeException;
 11  
 import org.mule.api.transport.PropertyScope;
 12  
 import org.mule.config.i18n.CoreMessages;
 13  
 
 14  
 import java.io.UnsupportedEncodingException;
 15  
 import java.text.MessageFormat;
 16  
 import java.util.ArrayList;
 17  
 import java.util.Arrays;
 18  
 import java.util.Collection;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 import java.util.TreeSet;
 23  
 
 24  
 /**
 25  
  * Useful methods for formatting message strings for logging or exceptions.
 26  
  */
 27  
 // @ThreadSafe
 28  
 public final class StringMessageUtils
 29  
 {
 30  
     // The maximum number of Collection and Array elements used for messages
 31  
     public static final int MAX_ELEMENTS = 50;
 32  
     public static final int DEFAULT_MESSAGE_WIDTH = 80;
 33  
 
 34  
     /** Do not instanciate. */
 35  
     private StringMessageUtils ()
 36  0
     {
 37  
         // no-op
 38  0
     }
 39  
 
 40  
     public static String getFormattedMessage(String msg, Object[] arguments)
 41  
     {
 42  0
         if (arguments != null)
 43  
         {
 44  0
             for (int i = 0; i < arguments.length; i++)
 45  
             {
 46  0
                 arguments[i] = toString(arguments[i]);
 47  
             }
 48  
         }
 49  0
         return MessageFormat.format(msg, arguments);
 50  
     }
 51  
 
 52  
     public static String getBoilerPlate(String message)
 53  
     {
 54  0
         return getBoilerPlate(message, '*', DEFAULT_MESSAGE_WIDTH);
 55  
     }
 56  
 
 57  
     public static String getBoilerPlate(String message, char c, int maxlength)
 58  
     {
 59  0
         return getBoilerPlate(new ArrayList(Arrays.asList(new String[]{message})), c, maxlength);
 60  
     }
 61  
 
 62  
     public static String getBoilerPlate(List messages, char c, int maxlength)
 63  
     {
 64  
         int size;
 65  0
         StringBuffer buf = new StringBuffer(messages.size() * maxlength);
 66  0
         int trimLength = maxlength - (c == ' ' ? 2 : 4);
 67  
 
 68  0
         for (int i = 0; i < messages.size(); i++)
 69  
         {
 70  0
             size = messages.get(i).toString().length();
 71  0
             if (size > trimLength)
 72  
             {
 73  0
                 String temp = messages.get(i).toString();
 74  0
                 int k = i;
 75  
                 int x;
 76  
                 int len;
 77  0
                 messages.remove(i);
 78  0
                 while (temp.length() > 0)
 79  
                 {
 80  0
                     len = (trimLength <= temp.length() ? trimLength : temp.length());
 81  0
                     String msg = temp.substring(0, len);
 82  0
                     x = msg.indexOf(SystemUtils.LINE_SEPARATOR);
 83  
 
 84  0
                     if (x > -1)
 85  
                     {
 86  0
                         msg = msg.substring(0, x);
 87  0
                         len = x + 1;
 88  
                     }
 89  
                     else
 90  
                     {
 91  0
                         x = msg.lastIndexOf(' ');
 92  0
                         if (x > -1 && len == trimLength)
 93  
                         {
 94  0
                             msg = msg.substring(0, x);
 95  0
                             len = x + 1;
 96  
                         }
 97  
                     }
 98  0
                     if (msg.startsWith(" "))
 99  
                     {
 100  0
                         msg = msg.substring(1);
 101  
                     }
 102  
 
 103  0
                     temp = temp.substring(len);
 104  0
                     messages.add(k, msg);
 105  0
                     k++;
 106  0
                 }
 107  
             }
 108  
         }
 109  
 
 110  0
         buf.append(SystemUtils.LINE_SEPARATOR);
 111  0
         if (c != ' ')
 112  
         {
 113  0
             buf.append(StringUtils.repeat(c, maxlength));
 114  
         }
 115  
 
 116  0
         for (int i = 0; i < messages.size(); i++)
 117  
         {
 118  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 119  0
             if (c != ' ')
 120  
             {
 121  0
                 buf.append(c);
 122  
             }
 123  0
             buf.append(" ");
 124  0
             buf.append(messages.get(i));
 125  
 
 126  0
             String osEncoding = CharSetUtils.defaultCharsetName();
 127  
             int padding;
 128  
             try
 129  
             {
 130  0
                 padding = trimLength - messages.get(i).toString().getBytes(osEncoding).length;
 131  
             }
 132  0
             catch (UnsupportedEncodingException ueex)
 133  
             {
 134  0
                 throw new MuleRuntimeException(CoreMessages.failedToConvertStringUsingEncoding(osEncoding), ueex);
 135  0
             }
 136  0
             if (padding > 0)
 137  
             {
 138  0
                 buf.append(StringUtils.repeat(' ', padding));
 139  
             }
 140  0
             buf.append(' ');
 141  0
             if (c != ' ')
 142  
             {
 143  0
                 buf.append(c);
 144  
             }
 145  
         }
 146  0
         buf.append(SystemUtils.LINE_SEPARATOR);
 147  0
         if (c != ' ')
 148  
         {
 149  0
             buf.append(StringUtils.repeat(c, maxlength));
 150  
         }
 151  0
         return buf.toString();
 152  
     }
 153  
 
 154  
     public static String truncate(String message, int length, boolean includeCount)
 155  
     {
 156  0
         if (message == null)
 157  
         {
 158  0
             return null;
 159  
         }
 160  0
         if (message.length() <= length)
 161  
         {
 162  0
             return message;
 163  
         }
 164  0
         String result = message.substring(0, length) + "...";
 165  0
         if (includeCount)
 166  
         {
 167  0
             result += "[" + length + " of " + message.length() + "]";
 168  
         }
 169  0
         return result;
 170  
     }
 171  
 
 172  
     public static byte[] getBytes(String string)
 173  
     {
 174  
         try
 175  
         {
 176  0
             return string.getBytes(FileUtils.DEFAULT_ENCODING);
 177  
         }
 178  0
         catch (UnsupportedEncodingException e)
 179  
         {
 180  
             // We can ignore this as the encoding is validated on start up
 181  0
             return null;
 182  
         }
 183  
     }
 184  
 
 185  
     public static String getString(byte[] bytes, String encoding)
 186  
     {
 187  
         try
 188  
         {
 189  0
             return new String(bytes, encoding);
 190  
         }
 191  0
         catch (UnsupportedEncodingException e)
 192  
         {
 193  
             // We can ignore this as the encoding is validated on start up
 194  0
             return null;
 195  
         }
 196  
     }
 197  
 
 198  
     /**
 199  
      * @see ArrayUtils#toString(Object, int)
 200  
      * @see CollectionUtils#toString(Collection, int)
 201  
      * @see MapUtils#toString(Map, boolean)
 202  
      */
 203  
     public static String toString(Object o)
 204  
     {
 205  0
         if (o == null)
 206  
         {
 207  0
             return "null";
 208  
         }
 209  0
         else if (o instanceof Class)
 210  
         {
 211  0
             return ((Class) o).getName();
 212  
         }
 213  0
         else if (o instanceof Map)
 214  
         {
 215  0
             return MapUtils.toString((Map) o, false);
 216  
         }
 217  0
         else if (o.getClass().isArray())
 218  
         {
 219  0
             return ArrayUtils.toString(o, MAX_ELEMENTS);
 220  
         }
 221  0
         else if (o instanceof Collection)
 222  
         {
 223  0
             return CollectionUtils.toString((Collection) o, MAX_ELEMENTS);
 224  
         }
 225  
         else
 226  
         {
 227  0
             return o.toString();
 228  
         }
 229  
     }
 230  
 
 231  
     public static String headersToString(MuleMessage m)
 232  
     {
 233  0
         if (m == null)
 234  
         {
 235  0
             return null;
 236  
         }
 237  0
         StringBuilder buf = new StringBuilder();
 238  0
         buf.append(SystemUtils.LINE_SEPARATOR).append("Message properties:").append(SystemUtils.LINE_SEPARATOR);
 239  
 
 240  0
         for (int i = 0; i < PropertyScope.ALL_SCOPES.length; i++)
 241  
         {
 242  0
             PropertyScope scope = PropertyScope.ALL_SCOPES[i];
 243  
             try
 244  
             {
 245  0
                 Set names = new TreeSet(m.getPropertyNames(scope));
 246  0
                 buf.append("  ").append(scope.getScopeName().toUpperCase()).append(" scoped properties:").append(SystemUtils.LINE_SEPARATOR);
 247  
 
 248  0
                 for (Object name : names)
 249  
                 {
 250  0
                     Object value = m.getProperty(name.toString(), scope);
 251  0
                     if (name.equals("password") || name.toString().contains("secret") || name.equals("pass"))
 252  
                     {
 253  0
                         value = "****";
 254  
                     }
 255  0
                     buf.append("    ").append(name).append("=").append(value).append(SystemUtils.LINE_SEPARATOR);
 256  0
                 }
 257  
             }
 258  0
             catch (IllegalArgumentException e)
 259  
             {
 260  
                 // ignored
 261  0
             }
 262  
         }
 263  0
         return buf.toString();
 264  
     }
 265  
 }