1 29 30 package echo2example.email.faux; 31 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.util.ArrayList ; 37 import java.util.Calendar ; 38 import java.util.Date ; 39 import java.util.GregorianCalendar ; 40 import java.util.List ; 41 import java.util.StringTokenizer ; 42 43 import javax.mail.Address ; 44 import javax.mail.MessagingException ; 45 import javax.mail.internet.AddressException ; 46 import javax.mail.internet.InternetAddress ; 47 48 52 public class MessageGenerator { 53 54 private static final String DOMAIN = "nextapp.com"; 55 private static final Address YOUR_ADDRESS; 56 static { 57 try { 58 YOUR_ADDRESS = new InternetAddress ("Joe Smith <joe.smith@nextapp.com>"); 59 } catch (AddressException ex) { 60 throw new RuntimeException (ex); 61 } 62 } 63 64 private static final int MINIMUM_DATE_DELTA = -365; 65 private static final int MAXIMUM_DATE_DELTA = 0; 66 67 82 private static String generateText(String [] sentenceArray, int minimumSentences, int maximumSentences, 83 int minimumParagraphs, int maximumParagraphs) { 84 85 int paragraphCount = randomInteger(minimumParagraphs, maximumParagraphs); 86 StringBuffer text = new StringBuffer (); 87 for (int paragraphIndex = 0; paragraphIndex < paragraphCount; ++paragraphIndex) { 88 int sentenceCount = randomInteger(minimumSentences, maximumSentences); 89 for (int sentenceIndex = 0; sentenceIndex < sentenceCount; ++sentenceIndex) { 90 text.append(sentenceArray[randomInteger(sentenceArray.length)]); 91 text.append("."); 92 if (sentenceIndex < sentenceCount - 1) { 93 text.append(" "); 94 } 95 } 96 if (paragraphIndex < paragraphCount - 1) { 97 text.append("\n\n"); 98 } 99 } 100 return text.toString(); 101 } 102 103 110 private static String [] loadStrings(String resourceName) { 111 InputStream in = null; 112 List strings = new ArrayList (); 113 try { 114 in = MessageGenerator.class.getResourceAsStream(resourceName); 115 if (in == null) { 116 throw new IllegalArgumentException ("Resource does not exist: \"" + resourceName + "\""); 117 } 118 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 119 String line; 120 while ((line = reader.readLine()) != null) { 121 line = line.trim(); 122 if (line.length() > 0) { 123 strings.add(Character.toUpperCase(line.charAt(0)) + line.substring(1).toLowerCase()); 124 } 125 } 126 return (String []) strings.toArray(new String [strings.size()]); 127 } catch (IOException ex) { 128 throw new IllegalArgumentException ("Cannot get resource: \"" + resourceName + "\": " + ex); 129 } finally { 130 if (in != null) { try { in.close(); } catch (IOException ex) { } } 131 } 132 } 133 134 140 private static boolean randomBoolean(int truePercent) { 141 int number = (int) (Math.random() * 100); 142 return number < truePercent; 143 } 144 145 152 private static int randomInteger(int minimum, int maximum) { 153 return minimum + (int) (Math.random() * (maximum - minimum + 1)); 154 } 155 156 162 private static int randomInteger(int size) { 163 return (int) (Math.random() * size); 164 } 165 166 172 private static String titleCapitalize(String title) { 173 StringTokenizer st = new StringTokenizer (title); 174 StringBuffer sb = new StringBuffer (); 175 while (st.hasMoreTokens()) { 176 String token = st.nextToken(); 177 sb.append(Character.toUpperCase(token.charAt(0))); 178 sb.append(token.substring(1)); 179 if (st.hasMoreTokens()) { 180 sb.append(" "); 181 } 182 } 183 return sb.toString(); 184 } 185 186 private String [] lastNames = loadStrings("resource/LastNames.txt"); 187 private String [] maleFirstNames = loadStrings("resource/MaleFirstNames.txt"); 188 private String [] femaleFirstNames = loadStrings("resource/FemaleFirstNames.txt"); 189 private String [] latinSentences = loadStrings("resource/LatinSentences.txt"); 190 191 private Address createAddress() 192 throws MessagingException { 193 String firstName; 195 196 boolean male = randomBoolean(50); 197 if (male) { 198 int firstNameIndex = randomInteger(maleFirstNames.length); 199 firstName = maleFirstNames[firstNameIndex]; 200 } else { 201 int firstNameIndex = randomInteger(femaleFirstNames.length); 202 firstName = femaleFirstNames[firstNameIndex]; 203 } 204 String lastName = lastNames[randomInteger(lastNames.length)]; 205 String email = firstName + "." + lastName + "@" + DOMAIN; 206 InternetAddress address = new InternetAddress (firstName + " " + lastName + " <" + email + ">"); 207 return address; 208 } 209 210 public Date randomDate() { 211 Calendar cal = new GregorianCalendar (); 212 cal.add(Calendar.DAY_OF_MONTH, randomInteger(MINIMUM_DATE_DELTA, MAXIMUM_DATE_DELTA)); 213 cal.add(Calendar.SECOND, -randomInteger(86400)); 214 return cal.getTime(); 215 } 216 217 public FauxMessage generateMessage() 218 throws MessagingException { 219 220 Date receivedDate = randomDate(); 221 Address from = createAddress(); 222 223 Address [] to = null; 224 Address [] cc = null; 225 Address [] bcc = null; 226 switch (randomInteger(0, 3)) { 227 case 0: 228 to = new Address []{YOUR_ADDRESS}; 229 break; 230 case 1: 231 to = new Address []{createAddress(), createAddress()}; 232 cc = new Address []{createAddress(), createAddress(), createAddress()}; 233 bcc = new Address []{YOUR_ADDRESS}; 234 break; 235 case 2: 236 to = new Address []{YOUR_ADDRESS}; 237 cc = new Address []{createAddress(), createAddress()}; 238 break; 239 case 3: 240 to = new Address []{createAddress()}; 241 cc = new Address []{createAddress(), YOUR_ADDRESS, createAddress()}; 242 break; 243 } 244 String subject = latinSentences[randomInteger(latinSentences.length)]; 246 if (subject.length() > 40) { 247 subject = subject.substring(0, subject.lastIndexOf(" ", 40)); 248 } 249 if (subject.endsWith(",")) { 250 subject = subject.substring(0, subject.length() - 1); 251 } 252 subject = titleCapitalize(subject); 253 254 String content = generateText(latinSentences, 2, 8, 6, 26); 255 256 return new FauxMessage(from, receivedDate, to, cc, bcc, subject, content); 257 258 } 259 } 260 | Popular Tags |