1 16 17 21 package org.apache.axis.attachments; 22 23 import org.apache.axis.AxisProperties; 24 import org.apache.axis.components.logger.LogFactory; 25 import org.apache.axis.transport.http.HTTPConstants; 26 import org.apache.axis.utils.Messages; 27 import org.apache.axis.utils.SessionUtils; 28 import org.apache.commons.logging.Log; 29 30 import java.util.Properties ; 31 32 33 36 public class MimeUtils { 37 38 39 protected static Log log = 40 LogFactory.getLog(MimeUtils.class.getName()); 41 42 50 public static long getContentLength(javax.mail.Multipart mp) 51 throws javax.mail.MessagingException , java.io.IOException { 52 53 int totalParts = mp.getCount(); 54 long totalContentLength = 0; 55 56 for (int i = 0; i < totalParts; ++i) { 57 javax.mail.internet.MimeBodyPart bp = 58 (javax.mail.internet.MimeBodyPart ) mp.getBodyPart(i); 59 60 totalContentLength += getContentLength(bp); 61 } 62 63 String ctype = mp.getContentType(); 64 javax.mail.internet.ContentType ct = 65 new javax.mail.internet.ContentType (ctype); 66 String boundaryStr = 67 ct.getParameter("boundary"); 68 int boundaryStrLen = 69 boundaryStr.length() 70 + 4; 72 return totalContentLength + boundaryStrLen * (totalParts + 1) 76 + 2 * totalParts + +4; 77 } 78 79 84 protected static long getContentLength( 85 javax.mail.internet.MimeBodyPart bp) { 86 87 long headerLength = -1L; 88 long dataSize = -1L; 89 90 try { 91 headerLength = getHeaderLength(bp); 92 93 javax.activation.DataHandler dh = bp.getDataHandler(); 94 javax.activation.DataSource ds = dh.getDataSource(); 95 96 if (ds instanceof javax.activation.FileDataSource ) { 99 javax.activation.FileDataSource fdh = 100 (javax.activation.FileDataSource ) ds; 101 java.io.File df = fdh.getFile(); 102 103 if (!df.exists()) { 104 throw new RuntimeException (Messages.getMessage("noFile", 105 df.getAbsolutePath())); 106 } 107 108 dataSize = df.length(); 109 } else { 110 dataSize = bp.getSize(); 111 112 if (-1 == dataSize) { dataSize = 0; 114 115 java.io.InputStream in = ds.getInputStream(); 116 byte[] readbuf = new byte[64 * 1024]; 117 int bytesread; 118 119 do { 120 bytesread = in.read(readbuf); 121 122 if (bytesread > 0) { 123 dataSize += bytesread; 124 } 125 } while (bytesread > -1); 126 127 in.close(); 128 } 129 } 130 } catch (Exception e) { 131 log.error(Messages.getMessage("exception00"), e); 132 } 133 134 return dataSize + headerLength; 135 } 136 137 145 private static long getHeaderLength(javax.mail.internet.MimeBodyPart bp) 146 throws javax.mail.MessagingException , java.io.IOException { 147 148 javax.mail.internet.MimeBodyPart headersOnly = 149 new javax.mail.internet.MimeBodyPart ( 150 new javax.mail.internet.InternetHeaders (), new byte[0]); 151 152 for (java.util.Enumeration en = bp.getAllHeaders(); 153 en.hasMoreElements();) { 154 javax.mail.Header header = (javax.mail.Header ) en.nextElement(); 155 156 headersOnly.addHeader(header.getName(), header.getValue()); 157 } 158 159 java.io.ByteArrayOutputStream bas = 160 new java.io.ByteArrayOutputStream (1024 * 16); 161 162 headersOnly.writeTo(bas); 163 bas.close(); 164 165 return (long) bas.size(); } 167 168 172 public static String [] filter = new String []{"Message-ID", "Mime-Version", 173 "Content-Type"}; 174 175 185 public static void writeToMultiPartStream( 186 java.io.OutputStream os, javax.mail.internet.MimeMultipart mp) { 187 188 try { 189 Properties props = AxisProperties.getProperties(); 190 191 props.setProperty( 192 "mail.smtp.host", 193 "localhost"); 195 javax.mail.Session session = 196 javax.mail.Session.getInstance(props, null); 197 javax.mail.internet.MimeMessage message = 198 new javax.mail.internet.MimeMessage (session); 199 200 message.setContent(mp); 201 message.saveChanges(); 202 message.writeTo(os, filter); 203 } catch (javax.mail.MessagingException e) { 204 log.error(Messages.getMessage("javaxMailMessagingException00"), e); 205 } catch (java.io.IOException e) { 206 log.error(Messages.getMessage("javaIOException00"), e); 207 } 208 } 209 210 216 public static String getContentType(javax.mail.internet.MimeMultipart mp) { 217 StringBuffer contentType = new StringBuffer (mp.getContentType()); 218 for(int i=0;i<contentType.length();){ 221 char ch = contentType.charAt(i); 222 if(ch=='\r'||ch=='\n') 223 contentType.deleteCharAt(i); 224 else 225 i++; 226 } 227 return contentType.toString(); 228 } 229 230 239 public static javax.mail.internet.MimeMultipart createMP( 240 String env, java.util.Collection parts) 241 throws org.apache.axis.AxisFault { 242 243 javax.mail.internet.MimeMultipart multipart = null; 244 245 try { 246 String rootCID = SessionUtils.generateSessionId(); 247 248 multipart = new javax.mail.internet.MimeMultipart ( 249 "related; type=\"text/xml\"; start=\"<" + rootCID + ">\""); 250 251 javax.mail.internet.MimeBodyPart messageBodyPart = 252 new javax.mail.internet.MimeBodyPart (); 253 254 messageBodyPart.setText(env, "UTF-8"); 255 messageBodyPart.setHeader("Content-Type", 256 "text/xml; charset=UTF-8"); 257 messageBodyPart.setHeader("Content-Id", "<" + rootCID + ">"); 258 messageBodyPart.setHeader( 259 HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary"); 260 multipart.addBodyPart(messageBodyPart); 261 262 for (java.util.Iterator it = parts.iterator(); it.hasNext();) { 263 org.apache.axis.Part part = 264 (org.apache.axis.Part) it.next(); 265 javax.activation.DataHandler dh = 266 org.apache.axis.attachments.AttachmentUtils.getActivationDataHandler( 267 part); 268 String contentID = part.getContentId(); 269 270 messageBodyPart = new javax.mail.internet.MimeBodyPart (); 271 272 messageBodyPart.setDataHandler(dh); 273 274 String contentType = part.getContentType(); 275 if ((contentType == null) 276 || (contentType.trim().length() == 0)) { 277 contentType = dh.getContentType(); 278 } 279 if ((contentType == null) 280 || (contentType.trim().length() == 0)) { 281 contentType = "application/octet-stream"; 282 } 283 284 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, 285 contentType); 286 messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_ID, 287 "<" + contentID + ">"); 288 messageBodyPart.setHeader( 289 HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, 290 "binary"); 292 for (java.util.Iterator i = 293 part.getNonMatchingMimeHeaders(new String []{ 294 HTTPConstants.HEADER_CONTENT_TYPE, 295 HTTPConstants.HEADER_CONTENT_ID, 296 HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING}); i.hasNext();) { 297 javax.xml.soap.MimeHeader header = (javax.xml.soap.MimeHeader ) i.next(); 298 299 messageBodyPart.setHeader(header.getName(), header.getValue()); 300 } 301 302 multipart.addBodyPart(messageBodyPart); 303 } 304 } catch (javax.mail.MessagingException e) { 305 log.error(Messages.getMessage("javaxMailMessagingException00"), e); 306 } 307 308 return multipart; 309 } 310 } 311 | Popular Tags |