1 8 9 package org.enhydra.oyster.util; 10 11 import org.enhydra.oyster.exception.ErrorStorage; 12 import org.enhydra.oyster.exception.SMIMEException; 13 import java.net.InetAddress; 14 import java.util.Random; 15 import java.io.File; 16 import javax.mail.internet.MimeMessage; 17 import java.io.FileInputStream; 18 import java.io.ByteArrayOutputStream; 19 import javax.mail.Header; 20 import java.util.Enumeration; 21 import javax.activation.MimetypesFileTypeMap; 22 import java.io.*; 23 import org.bouncycastle.util.encoders.Base64; 24 25 26 30 public class MimeAssist { 31 32 35 private static long sesionIncrement = 0L; 36 37 55 public static byte[] messageConvertor(MimeMessage message0) throws SMIMEException{ 56 String s = new String(); byte[] returnByteArray = null; 58 59 try { 60 MimeMessage message = new MimeMessage(message0); 61 62 Enumeration enum = message0.getAllHeaders(); 63 64 while(enum.hasMoreElements()) { 65 Header temp = (Header)enum.nextElement(); 66 if( !(temp.getName().equalsIgnoreCase("Content-Type") || 67 temp.getName().equalsIgnoreCase("Content-Transfer-Encoding") || 68 temp.getName().equalsIgnoreCase("Content-Disposition") || 69 temp.getName().equalsIgnoreCase("Content-Description")) ) { 70 message.removeHeader(temp.getName()); 71 } 72 } 73 74 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 75 String[] excludeHeaders = {"Message-ID", "Mime-Version"}; 76 message.writeTo(baos, excludeHeaders); 77 s = baos.toString("ISO-8859-1"); 78 returnByteArray = s.getBytes("ISO-8859-1"); 79 } 80 catch (Exception e) { 81 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 82 e, "MessageConvertor"); 83 } 84 return returnByteArray; 85 } 86 87 104 public static String messageStringConvertor(MimeMessage message0) throws SMIMEException{ 105 String s = new String(); try { 107 MimeMessage message = new MimeMessage(message0); 108 109 Enumeration enum = message0.getAllHeaders(); 110 111 while(enum.hasMoreElements()) { 112 Header temp = (Header)enum.nextElement(); 113 if( !(temp.getName().equalsIgnoreCase("Content-Type") || 114 temp.getName().equalsIgnoreCase("Content-Transfer-Encoding") || 115 temp.getName().equalsIgnoreCase("Content-Disposition") || 116 temp.getName().equalsIgnoreCase("Content-Description")) ) { 117 message.removeHeader(temp.getName()); 118 } 119 } 120 121 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 122 String[] excludeHeaders = {"Message-ID", "Mime-Version"}; 123 message.writeTo(baos, excludeHeaders); 124 s = baos.toString(); 125 } 126 catch (Exception e) { 127 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 128 e, "messageStringConvertor"); 129 } 130 return s; 131 } 132 133 145 public static String generateID() throws SMIMEException { 146 147 String contentID = ""; 148 String hostName = ""; 149 try { 150 hostName = InetAddress.getLocalHost().getHostName(); 151 } 152 catch (Exception e) { 153 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 154 e, "generateID"); 155 } 156 157 Random random = new Random(System.currentTimeMillis()); 158 contentID = random.nextLong() + "$" + sesionIncrement + "$" + 159 System.currentTimeMillis() + "@" + hostName ; 160 sesionIncrement++; 161 162 return contentID; 163 } 164 165 166 176 public static MimetypesFileTypeMap getFileTypeMap(String path0) throws SMIMEException { 177 try { 178 return new MimetypesFileTypeMap(new FileInputStream(path0)); 179 } 180 catch (Exception e) { 181 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 182 e, "getFileTypeMap"); 183 } 184 } 185 186 187 196 public static String getMimeTypeFromFileName(File file0, String mimeFile0) throws SMIMEException { 197 198 String contType; 199 try { 200 contType = getFileTypeMap(mimeFile0).getContentType(file0); 201 } 202 catch (Exception e) { 203 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 204 e, "getMimeTypeFromFileName"); 205 } 206 return contType; 207 } 208 209 217 public static String getMimeTypeFromFileName(String file0, String mimeFile0) throws SMIMEException { 218 219 File virtualFile = new File(file0); 220 221 return getMimeTypeFromFileName(virtualFile, mimeFile0); 222 } 223 224 225 238 public static String getStringBASE64WithBreakOn76(byte[] b0) throws SMIMEException { 239 return getStringBASE64WithBreak(b0, 76); 240 } 241 242 243 256 public static byte[] getBASE64WithBreakOn76(byte[] b0) throws SMIMEException { 257 return getBASE64WithBreak(b0, 76); 258 } 259 260 261 274 public static String getStringBASE64WithBreakOn76(String s0) throws SMIMEException { 275 return getStringBASE64WithBreak(s0, 76); 276 } 277 278 279 292 public static byte[] getBASE64WithBreakOn76(String s0) throws SMIMEException { 293 return getBASE64WithBreak(s0, 76); 294 } 295 296 297 313 public static String getStringBASE64WithBreak (byte[] b0, int breakPosition0) throws SMIMEException { 314 if(breakPosition0<1 | breakPosition0>76) 315 throw new SMIMEException("org.enhydra.oyster.util.Base64ForMime", 1032); 316 StringBuffer b = new StringBuffer(65536); 317 try { 318 String a = new String(Base64.encode(b0), "ISO-8859-1"); 319 320 for (int i = 0; breakPosition0*i < a.length(); i++) { 321 try { 322 b.append(a.substring(i * breakPosition0, (i + 1) * breakPosition0)); 323 } catch (IndexOutOfBoundsException e) { 324 b.append(a.substring(i * breakPosition0)); 325 break; 326 } 327 b.append("\r\n"); 328 } 329 } 330 catch(Exception e) { 331 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 332 e, "getStringBASE64WithBreak"); 333 } 334 return b.toString(); 335 } 336 337 353 public static byte[] getBASE64WithBreak (byte[] b0, int breakPosition0) throws SMIMEException { 354 byte[] finalBASE64 = null; 355 356 try { 357 finalBASE64 = getStringBASE64WithBreak(b0, breakPosition0).getBytes("ISO-8859-1"); 358 } 359 catch(Exception e) { 360 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 361 e, "getBASE64WithBreak"); 362 } 363 return finalBASE64; 364 } 365 366 367 383 public static String getStringBASE64WithBreak (String s0, int breakPosition0) throws SMIMEException { 384 byte[] bArray; 385 386 try { 387 bArray = s0.getBytes("ISO-8859-1"); 388 } 389 catch(Exception e) { 390 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 391 e, "getStringBASE64WithBreak"); 392 } 393 return getStringBASE64WithBreak(bArray, breakPosition0); 394 } 395 396 397 413 public static byte[] getBASE64WithBreak (String s0, int breakPosition0) throws SMIMEException { 414 byte[] bArray; 415 416 try { 417 bArray = s0.getBytes("ISO-8859-1"); 418 } 419 catch(Exception e) { 420 throw SMIMEException.getInstance("org.enhydra.oyster.util.MimeAssist", 421 e, "getBASE64WithBreak"); 422 } 423 return getBASE64WithBreak(bArray, breakPosition0); 424 } 425 426 427 } | Popular Tags |