package org.jboss.axis.attachments;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Part;
import org.jboss.axis.transport.http.HTTPConstants;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import javax.activation.DataHandler;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
public class MultiPartRelatedInputStream extends MultiPartInputStream
{
private static Logger log = Logger.getLogger(MultiPartRelatedInputStream.class.getName());
public static final String MIME_MULTIPART_RELATED = "multipart/related";
protected HashMap parts = new HashMap();
protected LinkedList orderedParts = new LinkedList();
protected int rootPartLength = 0;
protected boolean closed = false;
protected boolean eos =
false;
protected BoundaryDelimitedStream boundaryDelimitedStream =
null;
protected InputStream soapStream =
null;
protected InputStream soapStreamBDS =
null;
protected byte[] boundary = null;
protected ByteArrayInputStream cachedSOAPEnvelope =
null;
protected String contentLocation = null;
protected String contentId = null;
public MultiPartRelatedInputStream(String contentType, InputStream stream)
throws AxisFault
{
super(null);
try
{
ContentType ct =
new ContentType(contentType);
String rootPartContentId =
ct.getParameter("start");
if (rootPartContentId != null)
{
rootPartContentId = rootPartContentId.trim();
if (rootPartContentId.startsWith("<"))
{
rootPartContentId = rootPartContentId.substring(1);
}
if (rootPartContentId.endsWith(">"))
{
rootPartContentId = rootPartContentId.substring(0,
rootPartContentId.length() - 1);
}
}
if (ct.getParameter("boundary") != null)
{
String boundaryStr =
"--"
+ ct.getParameter("boundary");
byte[][] boundaryMarker = new byte[2][boundaryStr.length() + 2];
stream.read(boundaryMarker[0]);
boundary = (boundaryStr + "\r\n").getBytes("US-ASCII");
int current = 0;
for (boolean found = false; !found; ++current)
{
if (!(found =
Arrays.equals(boundaryMarker[current & 0x1],
boundary)))
{
System.arraycopy(boundaryMarker[current & 0x1], 1,
boundaryMarker[(current + 1) & 0x1], 0,
boundaryMarker[0].length - 1);
if (stream.read(boundaryMarker[(current + 1) & 0x1],
boundaryMarker[0].length - 1, 1) < 1)
{
throw new AxisFault(Messages.getMessage("mimeErrorNoBoundary", new String(boundary)));
}
}
}
boundaryStr = "\r\n" + boundaryStr;
boundary = boundaryStr.getBytes("US-ASCII");
}
else
{
for (boolean found = false; !found;)
{
boundary = readLine(stream);
if (boundary == null)
throw new AxisFault(Messages.getMessage("mimeErrorNoBoundary", "--"));
found = boundary.length > 4 && boundary[2] == '-' && boundary[3] == '-';
}
}
boundaryDelimitedStream =
new BoundaryDelimitedStream(stream,
boundary, 1024);
String contentTransferEncoding = null;
do
{
contentId = null;
contentLocation = null;
contentTransferEncoding = null;
InternetHeaders headers =
new InternetHeaders(boundaryDelimitedStream);
contentId = headers.getHeader(HTTPConstants.HEADER_CONTENT_ID,
null);
if (contentId != null)
{
contentId = contentId.trim();
if (contentId.startsWith("<"))
{
contentId = contentId.substring(1);
}
if (contentId.endsWith(">"))
{
contentId = contentId.substring(0, contentId.length()
- 1);
}
contentId = contentId.trim();
}
contentLocation =
headers.getHeader(HTTPConstants.HEADER_CONTENT_LOCATION,
null);
if (contentLocation != null)
{
contentLocation = contentLocation.trim();
if (contentLocation.startsWith("<"))
{
contentLocation = contentLocation.substring(1);
}
if (contentLocation.endsWith(">"))
{
contentLocation = contentLocation.substring(0, contentLocation.length() - 1);
}
contentLocation = contentLocation.trim();
}
contentType =
headers.getHeader(HTTPConstants.HEADER_CONTENT_TYPE, null);
if (contentType != null)
{
contentType = contentType.trim();
}
contentTransferEncoding = headers.getHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, null);
if (contentTransferEncoding != null)
{
contentTransferEncoding = contentTransferEncoding.trim();
}
InputStream decodedStream = boundaryDelimitedStream;
if ((contentTransferEncoding != null)
&& (0 != contentTransferEncoding.length()))
{
decodedStream = MimeUtility.decode(decodedStream,
contentTransferEncoding);
}
if ((rootPartContentId != null) && !rootPartContentId.equals(contentId))
{ DataHandler dh = new DataHandler(new ManagedMemoryDataSource(decodedStream, contentType));
AttachmentPartImpl ap = new AttachmentPartImpl(dh);
if (contentId != null)
{
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID,
contentId);
}
if (contentLocation != null)
{
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION,
contentLocation);
}
for (Enumeration en =
headers.getNonMatchingHeaders(new String[]{
HTTPConstants.HEADER_CONTENT_ID,
HTTPConstants.HEADER_CONTENT_LOCATION,
HTTPConstants.HEADER_CONTENT_TYPE}); en.hasMoreElements();)
{
Header header =
(Header)en.nextElement();
String name = header.getName();
String value = header.getValue();
if ((name != null) && (value != null))
{
name = name.trim();
if (name.length() != 0)
{
ap.addMimeHeader(name, value);
}
}
}
addPart(contentId, contentLocation, ap);
boundaryDelimitedStream =
boundaryDelimitedStream.getNextStream(); }
}
while ((null != boundaryDelimitedStream)
&& (rootPartContentId != null)
&& !rootPartContentId.equals(contentId));
if (boundaryDelimitedStream == null)
{
throw new AxisFault(Messages.getMessage("noRoot", rootPartContentId));
}
soapStreamBDS = boundaryDelimitedStream;
if ((contentTransferEncoding != null)
&& (0 != contentTransferEncoding.length()))
{
soapStream = MimeUtility.decode(boundaryDelimitedStream,
contentTransferEncoding);
}
else
{
soapStream =
boundaryDelimitedStream; }
}
catch (ParseException e)
{
throw new AxisFault(Messages.getMessage("mimeErrorParsing", e.getMessage()));
}
catch (IOException e)
{
throw new AxisFault(Messages.getMessage("readError", e.getMessage()));
}
catch (MessagingException e)
{
throw new AxisFault(Messages.getMessage("readError", e.getMessage()));
}
}
private final byte[] readLine(InputStream is) throws IOException
{
ByteArrayOutputStream input = new ByteArrayOutputStream(1024);
int c = 0;
input.write('\r');
input.write('\n');
int next = -1;
for (; c != -1;)
{
c = -1 != next ? next : is.read();
next = -1;
switch (c)
{
case -1:
break;
case '\r':
next = is.read();
if (next == '\n') return input.toByteArray();
if (next == -1) return null;
default:
input.write((byte)c);
break;
}
}
return null;
}
public Part getAttachmentByReference(final String[] id)
throws AxisFault
{
Part ret = null;
for (int i = id.length - 1; (ret == null) && (i > -1); --i)
{
ret = (AttachmentPartImpl)parts.get(id[i]);
}
if (null == ret)
{
ret = readTillFound(id);
}
log.debug(Messages.getMessage("return02",
"getAttachmentByReference(\"" + id
+ "\"", ((ret == null)
? "null"
: ret.toString())));
return ret;
}
protected void addPart(String contentId, String locationId,
AttachmentPartImpl ap)
{
if ((contentId != null) && (contentId.trim().length() != 0))
{
parts.put(contentId, ap);
}
if ((locationId != null) && (locationId.trim().length() != 0))
{
parts.put(locationId, ap);
}
orderedParts.add(ap);
}
protected static final String[] READ_ALL = {
" * \0 ".intern()};
protected void readAll() throws AxisFault
{
readTillFound(READ_ALL);
}
public Collection getAttachments()
throws AxisFault
{
readAll();
return orderedParts;
}
protected Part readTillFound(final String[] id)
throws AxisFault
{
if (boundaryDelimitedStream == null)
{
return null; }
Part ret = null;
try
{
if (soapStreamBDS
== boundaryDelimitedStream)
{ if (!eos)
{ ByteArrayOutputStream soapdata =
new ByteArrayOutputStream(1024 * 8);
byte[] buf =
new byte[1024 * 16];
int byteread = 0;
do
{
byteread = soapStream.read(buf);
if (byteread > 0)
{
soapdata.write(buf, 0, byteread);
}
}
while (byteread > -1);
soapdata.close();
soapStream = new ByteArrayInputStream(soapdata.toByteArray());
}
boundaryDelimitedStream =
boundaryDelimitedStream.getNextStream();
}
if (null != boundaryDelimitedStream)
{
do
{
String contentType = null;
String contentId = null;
String contentTransferEncoding = null;
String contentLocation = null;
InternetHeaders headers =
new InternetHeaders(boundaryDelimitedStream);
contentId = headers.getHeader("Content-Id", null);
if (contentId != null)
{
contentId = contentId.trim();
if (contentId.startsWith("<"))
{
contentId = contentId.substring(1);
}
if (contentId.endsWith(">"))
{
contentId =
contentId.substring(0, contentId.length() - 1);
}
contentId = contentId.trim();
}
contentType =
headers.getHeader(HTTPConstants.HEADER_CONTENT_TYPE,
null);
if (contentType != null)
{
contentType = contentType.trim();
}
contentLocation =
headers.getHeader(HTTPConstants.HEADER_CONTENT_LOCATION,
null);
if (contentLocation != null)
{
contentLocation = contentLocation.trim();
}
contentTransferEncoding = headers.getHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, null);
if (contentTransferEncoding != null)
{
contentTransferEncoding =
contentTransferEncoding.trim();
}
InputStream decodedStream = boundaryDelimitedStream;
if ((contentTransferEncoding != null)
&& (0 != contentTransferEncoding.length()))
{
decodedStream =
MimeUtility.decode(decodedStream,
contentTransferEncoding);
}
ManagedMemoryDataSource source = new ManagedMemoryDataSource(decodedStream, contentType);
DataHandler dh = new DataHandler(source);
AttachmentPartImpl ap = new AttachmentPartImpl(dh);
if (contentId != null)
{
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID,
contentId);
}
if (contentLocation != null)
{
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION,
contentLocation);
}
for (Enumeration en =
headers.getNonMatchingHeaders(new String[]{
HTTPConstants.HEADER_CONTENT_ID,
HTTPConstants.HEADER_CONTENT_LOCATION,
HTTPConstants.HEADER_CONTENT_TYPE}); en.hasMoreElements();)
{
Header header =
(Header)en.nextElement();
String name = header.getName();
String value = header.getValue();
if ((name != null) && (value != null))
{
name = name.trim();
if (name.length() != 0)
{
ap.addMimeHeader(name, value);
}
}
}
addPart(contentId, contentLocation, ap);
for (int i = id.length - 1; (ret == null) && (i > -1);
--i)
{
if ((contentId != null) && id[i].equals(contentId))
{ ret = ap;
}
else if ((contentLocation != null)
&& id[i].equals(contentLocation))
{
ret = ap;
}
}
boundaryDelimitedStream =
boundaryDelimitedStream.getNextStream();
}
while ((null == ret) && (null != boundaryDelimitedStream));
}
}
catch (Exception e)
{
throw AxisFault.makeFault(e);
}
return ret;
}
public String getContentLocation()
{
return contentLocation;
}
public String getContentId()
{
return contentId;
}
public int read(byte[] b, int off, int len) throws IOException
{
if (closed)
{
throw new IOException(Messages.getMessage("streamClosed"));
}
if (eos)
{
return -1;
}
int read = soapStream.read(b, off, len);
if (read < 0)
{
eos = true;
}
return read;
}
public int read(byte[] b) throws IOException
{
return read(b, 0, b.length);
}
public int read() throws IOException
{
if (closed)
{
throw new IOException(Messages.getMessage("streamClosed"));
}
if (eos)
{
return -1;
}
int ret = soapStream.read();
if (ret < 0)
{
eos = true;
}
return ret;
}
public void close() throws IOException
{
closed = true;
soapStream.close();
}
}