1 8 package org.jboss.axis.attachments; 9 10 import org.jboss.axis.AxisFault; 11 import org.jboss.axis.AxisProperties; 12 import org.jboss.axis.Part; 13 import org.jboss.axis.transport.http.HTTPConstants; 14 import org.jboss.axis.utils.Messages; 15 import org.jboss.axis.utils.SessionUtils; 16 import org.jboss.logging.Logger; 17 18 import javax.activation.DataHandler ; 19 import javax.activation.DataSource ; 20 import javax.activation.FileDataSource ; 21 import javax.mail.Header ; 22 import javax.mail.MessagingException ; 23 import javax.mail.Multipart ; 24 import javax.mail.Session ; 25 import javax.mail.internet.ContentType ; 26 import javax.mail.internet.InternetHeaders ; 27 import javax.mail.internet.MimeBodyPart ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.internet.MimeMultipart ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.util.Collection ; 36 import java.util.Enumeration ; 37 import java.util.Iterator ; 38 import java.util.Properties ; 39 40 41 44 public class MimeUtils 45 { 46 47 50 private static Logger log = Logger.getLogger(MimeUtils.class.getName()); 51 52 59 public static long getContentLength(Multipart mp) 60 throws MessagingException 61 { 62 63 int totalParts = mp.getCount(); 64 long totalContentLength = 0; 65 66 for (int i = 0; i < totalParts; ++i) 67 { 68 MimeBodyPart bp = (MimeBodyPart )mp.getBodyPart(i); 69 totalContentLength += getContentLength(bp); 70 } 71 72 String ctype = mp.getContentType(); 73 ContentType ct = new ContentType (ctype); 74 String boundaryStr = ct.getParameter("boundary"); 75 int boundaryStrLen = boundaryStr.length() + 4; 77 78 return totalContentLength + boundaryStrLen * (totalParts + 1) + 2 * totalParts + +4; 82 } 83 84 90 protected static long getContentLength(MimeBodyPart bp) 91 { 92 93 long headerLength = -1L; 94 long dataSize = -1L; 95 96 try 97 { 98 headerLength = getHeaderLength(bp); 99 100 DataHandler dh = bp.getDataHandler(); 101 DataSource ds = dh.getDataSource(); 102 103 if (ds instanceof FileDataSource ) 106 { 107 FileDataSource fdh = (FileDataSource )ds; 108 File df = fdh.getFile(); 109 110 if (!df.exists()) 111 { 112 throw new RuntimeException (Messages.getMessage("noFile", df.getAbsolutePath())); 113 } 114 115 dataSize = df.length(); 116 } 117 else 118 { 119 dataSize = bp.getSize(); 120 121 if (-1 == dataSize) 122 { dataSize = 0; 124 125 InputStream in = ds.getInputStream(); 126 byte[] readbuf = new byte[64 * 1024]; 127 int bytesread; 128 129 do 130 { 131 bytesread = in.read(readbuf); 132 133 if (bytesread > 0) 134 { 135 dataSize += bytesread; 136 } 137 } 138 while (bytesread > -1); 139 140 in.close(); 141 } 142 } 143 } 144 catch (Exception e) 145 { 146 log.error(Messages.getMessage("exception00"), e); 147 } 148 149 return dataSize + headerLength; 150 } 151 152 160 private static long getHeaderLength(MimeBodyPart bp) 161 throws MessagingException , IOException 162 { 163 164 MimeBodyPart headersOnly = new MimeBodyPart (new InternetHeaders (), new byte[0]); 165 166 for (Enumeration en = bp.getAllHeaders(); en.hasMoreElements();) 167 { 168 Header header = (Header )en.nextElement(); 169 headersOnly.addHeader(header.getName(), header.getValue()); 170 } 171 172 ByteArrayOutputStream bas = new ByteArrayOutputStream (1024 * 16); 173 174 headersOnly.writeTo(bas); 175 bas.close(); 176 177 return (long)bas.size(); } 179 180 183 public static String [] filter = new String []{"Message-ID", "Mime-Version", 184 "Content-Type"}; 185 186 197 public static void writeToMultiPartStream(OutputStream os, MimeMultipart mp) 198 { 199 200 try 201 { 202 Properties props = AxisProperties.getProperties(); 203 204 props.setProperty("mail.smtp.host", "localhost"); 206 207 Session session = Session.getInstance(props, null); 208 MimeMessage message = new MimeMessage (session); 209 210 message.setContent(mp); 211 message.saveChanges(); 212 message.writeTo(os, filter); 213 } 214 catch (MessagingException e) 215 { 216 log.error(Messages.getMessage("javaxMailMessagingException00"), e); 217 } 218 catch (IOException e) 219 { 220 log.error(Messages.getMessage("javaIOException00"), e); 221 } 222 } 223 224 230 public static String getContentType(MimeMultipart mp) 231 { 232 StringBuffer contentType = new StringBuffer (mp.getContentType()); 233 for (int i = 0; i < contentType.length();) 236 { 237 char ch = contentType.charAt(i); 238 if (ch == '\r' || ch == '\n') 239 contentType.deleteCharAt(i); 240 else 241 i++; 242 } 243 return contentType.toString(); 244 } 245 246 254 public static MimeMultipart createMP(String env, Collection parts) 255 throws AxisFault 256 { 257 258 MimeMultipart multipart = null; 259 260 try 261 { 262 String rootCID = SessionUtils.generateSessionId(); 263 264 multipart = new MimeMultipart ("related; type=\"text/xml\"; start=\"<" + rootCID + ">\""); 265 266 MimeBodyPart messageBodyPart = new MimeBodyPart (); 267 268 messageBodyPart.setText(env, "UTF-8"); 269 messageBodyPart.setHeader("Content-Type", "text/xml; charset=UTF-8"); 270 messageBodyPart.setHeader("Content-Id", "<" + rootCID + ">"); 271 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary"); 272 multipart.addBodyPart(messageBodyPart); 273 274 for (Iterator it = parts.iterator(); it.hasNext();) 275 { 276 Part part = (Part)it.next(); 277 DataHandler dh = AttachmentUtils.getActivationDataHandler(part); 278 String contentID = part.getContentId(); 279 280 messageBodyPart = new MimeBodyPart (); 281 messageBodyPart.setDataHandler(dh); 282 283 String contentType = part.getContentType(); 284 if (contentType == null) 285 contentType = dh.getContentType(); 286 287 if (contentType == null || contentType.trim().length() == 0) 288 contentType = "application/octet-stream"; 289 290 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType); 291 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_ID, "<" + contentID + ">"); 292 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary"); 294 295 for (Iterator i = part.getNonMatchingMimeHeaders(new String []{ 296 HTTPConstants.HEADER_CONTENT_TYPE, 297 HTTPConstants.HEADER_CONTENT_ID, 298 HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING}); i.hasNext();) 299 { 300 javax.xml.soap.MimeHeader header = (javax.xml.soap.MimeHeader )i.next(); 301 messageBodyPart.setHeader(header.getName(), header.getValue()); 302 } 303 304 multipart.addBodyPart(messageBodyPart); 305 } 306 } 307 catch (MessagingException e) 308 { 309 log.error(Messages.getMessage("javaxMailMessagingException00"), e); 310 } 311 312 return multipart; 313 } 314 } 315 | Popular Tags |