package org.jboss.axis.attachments;
import org.jboss.axis.AxisFault;
import org.jboss.axis.AxisProperties;
import org.jboss.axis.Part;
import org.jboss.axis.transport.http.HTTPConstants;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.SessionUtils;
import org.jboss.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
public class MimeUtils
{
private static Logger log = Logger.getLogger(MimeUtils.class.getName());
public static long getContentLength(Multipart mp)
throws MessagingException
{
int totalParts = mp.getCount();
long totalContentLength = 0;
for (int i = 0; i < totalParts; ++i)
{
MimeBodyPart bp = (MimeBodyPart)mp.getBodyPart(i);
totalContentLength += getContentLength(bp);
}
String ctype = mp.getContentType();
ContentType ct = new ContentType(ctype);
String boundaryStr = ct.getParameter("boundary");
int boundaryStrLen = boundaryStr.length() + 4;
return totalContentLength + boundaryStrLen * (totalParts + 1) + 2 * totalParts + +4;
}
protected static long getContentLength(MimeBodyPart bp)
{
long headerLength = -1L;
long dataSize = -1L;
try
{
headerLength = getHeaderLength(bp);
DataHandler dh = bp.getDataHandler();
DataSource ds = dh.getDataSource();
if (ds instanceof FileDataSource)
{
FileDataSource fdh = (FileDataSource)ds;
File df = fdh.getFile();
if (!df.exists())
{
throw new RuntimeException(Messages.getMessage("noFile", df.getAbsolutePath()));
}
dataSize = df.length();
}
else
{
dataSize = bp.getSize();
if (-1 == dataSize)
{ dataSize = 0;
InputStream in = ds.getInputStream();
byte[] readbuf = new byte[64 * 1024];
int bytesread;
do
{
bytesread = in.read(readbuf);
if (bytesread > 0)
{
dataSize += bytesread;
}
}
while (bytesread > -1);
in.close();
}
}
}
catch (Exception e)
{
log.error(Messages.getMessage("exception00"), e);
}
return dataSize + headerLength;
}
private static long getHeaderLength(MimeBodyPart bp)
throws MessagingException, IOException
{
MimeBodyPart headersOnly = new MimeBodyPart(new InternetHeaders(), new byte[0]);
for (Enumeration en = bp.getAllHeaders(); en.hasMoreElements();)
{
Header header = (Header)en.nextElement();
headersOnly.addHeader(header.getName(), header.getValue());
}
ByteArrayOutputStream bas = new ByteArrayOutputStream(1024 * 16);
headersOnly.writeTo(bas);
bas.close();
return (long)bas.size(); }
public static String[] filter = new String[]{"Message-ID", "Mime-Version",
"Content-Type"};
public static void writeToMultiPartStream(OutputStream os, MimeMultipart mp)
{
try
{
Properties props = AxisProperties.getProperties();
props.setProperty("mail.smtp.host", "localhost");
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setContent(mp);
message.saveChanges();
message.writeTo(os, filter);
}
catch (MessagingException e)
{
log.error(Messages.getMessage("javaxMailMessagingException00"), e);
}
catch (IOException e)
{
log.error(Messages.getMessage("javaIOException00"), e);
}
}
public static String getContentType(MimeMultipart mp)
{
StringBuffer contentType = new StringBuffer(mp.getContentType());
for (int i = 0; i < contentType.length();)
{
char ch = contentType.charAt(i);
if (ch == '\r' || ch == '\n')
contentType.deleteCharAt(i);
else
i++;
}
return contentType.toString();
}
public static MimeMultipart createMP(String env, Collection parts)
throws AxisFault
{
MimeMultipart multipart = null;
try
{
String rootCID = SessionUtils.generateSessionId();
multipart = new MimeMultipart("related; type=\"text/xml\"; start=\"<" + rootCID + ">\"");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(env, "UTF-8");
messageBodyPart.setHeader("Content-Type", "text/xml; charset=UTF-8");
messageBodyPart.setHeader("Content-Id", "<" + rootCID + ">");
messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
multipart.addBodyPart(messageBodyPart);
for (Iterator it = parts.iterator(); it.hasNext();)
{
Part part = (Part)it.next();
DataHandler dh = AttachmentUtils.getActivationDataHandler(part);
String contentID = part.getContentId();
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(dh);
String contentType = part.getContentType();
if (contentType == null)
contentType = dh.getContentType();
if (contentType == null || contentType.trim().length() == 0)
contentType = "application/octet-stream";
messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType);
messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_ID, "<" + contentID + ">");
messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
for (Iterator i = part.getNonMatchingMimeHeaders(new String[]{
HTTPConstants.HEADER_CONTENT_TYPE,
HTTPConstants.HEADER_CONTENT_ID,
HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING}); i.hasNext();)
{
javax.xml.soap.MimeHeader header = (javax.xml.soap.MimeHeader)i.next();
messageBodyPart.setHeader(header.getName(), header.getValue());
}
multipart.addBodyPart(messageBodyPart);
}
}
catch (MessagingException e)
{
log.error(Messages.getMessage("javaxMailMessagingException00"), e);
}
return multipart;
}
}