}
return parseMultipartRelatedMessage(buffer, httpHeaders, encoding);
}
public static MimeParts parseMultipartRelatedMessage(byte[] message, String httpHeaders, String encoding)
{
MimeParts mimeParts = new MimePartsImpl();
String boundary = Utils.getHttpHeaderSubAttribute(httpHeaders, HTTPConstants.HEADER_CONTENT_TYPE, "boundary");
ArrayList parts = new ArrayList();
if (boundary == null)
{
// assume it is a simple SOAP message
return null;
}
else
{
String start = Utils.getHttpHeaderSubAttribute(httpHeaders, HTTPConstants.HEADER_CONTENT_TYPE, "start");
int[] indices = Utils.getBoundaryIndices(message, boundary);
boolean rootNotFound = true;
for (int i= indices.length - 2; i>=0; i--)
{
try
{
MimePart part = new MimePartImpl();
int index = Utils.getFirstCRLFCRLF(message, indices[i]);
if ((index > indices[i]) && (index < indices[i+1]))
{
// the boundary string & mime headers (include the trailing CRLF CRLF)
String str = new String(message, indices[i], (index - indices[i]), "US-ASCII");
String delimiter = str.substring(0, str.indexOf("\r\n", 2) + 2);
if (i == indices.length -2)
{
String endDelimiter = new String(message, indices[i + 1], message.length - indices[i + 1], "US-ASCII");
int j = str.indexOf("\r\n", 2);
if (j != -1)
endDelimiter = str.substring(0, str.indexOf("\r\n", 2) + 2);
part.setBoundaryStrings(new String[]{delimiter, endDelimiter});
}
else
part.setBoundaryStrings(new String[]{delimiter});
// the headers
String headers = str.substring(delimiter.length());
if (headers.startsWith("\r\n"))
{
// no headers present
part.setHeaders("");
}
else
{
part.setHeaders(headers);
}
// the content
String contentId = Utils.getMimeHeaderAttribute(headers, MIMEConstants.HEADER_CONTENT_ID);
int size = indices[i+1] - (index);
byte[] content = new byte[size];
System.arraycopy(message, index, content, 0, size);
if ((rootNotFound && (i == 0)) ||
((start != null) && (!start.equals("")) && (start.equals(contentId))))
{
// root part -- do not encode
part.setContent(new String(content, encoding));
mimeParts.setRootPart(part);
}
else
{
String transferEncoding = Utils.getMimeHeaderAttribute(headers, MIMEConstants.HEADER_CONTENT_TRANSFER_ENCODING);
if ((transferEncoding != null) && transferEncoding.equalsIgnoreCase("base64"))
part.setContent(new String(content, encoding));
else
part.setContent(Utils.encodeBase64(content));
}
parts.add(part);
}
}
catch (Exception e)
{
return null;
}
}
int size = parts.size();
for (int i = size-1; i>=0; i--)
mimeParts.addPart((MimePart)parts.get(i));
}
return mimeParts;
}