in axis-rt-core/src/main/java/org/apache/axis/attachments/MultiPartRelatedInputStream.java [100:345]
public MultiPartRelatedInputStream(
String contentType, java.io.InputStream stream)
throws org.apache.axis.AxisFault {
super(null); // don't cache this stream.
if(!(stream instanceof BufferedInputStream)) {
stream = new BufferedInputStream(stream);
}
try {
// First find the start and boundary parameters. There are real weird rules regard what
// can be in real headers what needs to be escaped etc let mail parse it.
javax.mail.internet.ContentType ct =
new javax.mail.internet.ContentType(contentType);
String rootPartContentId =
ct.getParameter("start"); // Get the root part content.
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"); // The boundary with -- add as always the case.
// if start is null then the first attachment is the rootpart
// First read the start boundary -- this is done with brute force since the servlet may swallow the crlf between headers.
// after this we use the more efficient boundarydelimeted stream. There should never be any data here anyway.
byte[][] boundaryMarker = new byte[2][boundaryStr.length() + 2];
IOUtils.readFully(stream, boundaryMarker[0]);
boundary = (boundaryStr + "\r\n").getBytes("US-ASCII");
int current = 0;
// This just goes brute force one byte at a time to find the first boundary.
// in most cases this just a crlf.
for (boolean found = false; !found; ++current) {
if (!(found =
java.util.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 org.apache.axis.AxisFault(
Messages.getMessage(
"mimeErrorNoBoundary", new String(boundary)));
}
}
}
// after the first boundary each boundary will have a cr lf at the beginning since after the data in any part there
// is a cr lf added to put the boundary at the begining of a line.
boundaryStr = "\r\n" + boundaryStr;
boundary = boundaryStr.getBytes("US-ASCII");
} else {
// Since boundary is not specified, we try to find one.
for (boolean found = false; !found;) {
boundary= readLine(stream);
if( boundary == null)
throw new org.apache.axis.AxisFault(
Messages.getMessage(
"mimeErrorNoBoundary", "--"));
found = boundary.length >4 && boundary[2] == '-' && boundary[3]== '-';
}
}
// create the boundary delmited stream.
boundaryDelimitedStream =
new org.apache.axis.attachments.BoundaryDelimitedStream(stream,
boundary, 1024);
// Now read through all potential streams until we have found the root part.
String contentTransferEncoding = null;
do {
contentId = null;
contentLocation = null;
contentTransferEncoding = null;
// Read this attachments headers from the stream.
javax.mail.internet.InternetHeaders headers =
new javax.mail.internet.InternetHeaders(
boundaryDelimitedStream);
// Use java mail utility to read through the headers.
contentId = headers.getHeader(HTTPConstants.HEADER_CONTENT_ID,
null);
// Clean up the headers and remove any < >
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();
// if (!contentId.startsWith("cid:")) {
// contentId =
// "cid:"
// + contentId; // make sure its identified as cid
// }
}
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();
}
java.io.InputStream decodedStream = boundaryDelimitedStream;
if ((contentTransferEncoding != null)
&& (0 != contentTransferEncoding.length())) {
decodedStream = MimeUtility.decode(decodedStream,
contentTransferEncoding);
}
if ((rootPartContentId != null) && !rootPartContentId.equals(
contentId)) { // This is a part that has come in prior to the root part. Need to buffer it up.
javax.activation.DataHandler dh =
new javax.activation.DataHandler(
new org.apache.axis.attachments.ManagedMemoryDataSource(
decodedStream, MAX_CACHED, contentType, true));
AttachmentPart ap = new AttachmentPart(dh);
if (contentId != null) {
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID,
contentId);
}
if (contentLocation != null) {
ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION,
contentLocation);
}
for (java.util.Enumeration en =
headers.getNonMatchingHeaders(new String[]{
HTTPConstants.HEADER_CONTENT_ID,
HTTPConstants.HEADER_CONTENT_LOCATION,
HTTPConstants.HEADER_CONTENT_TYPE}); en.hasMoreElements();) {
javax.mail.Header header =
(javax.mail.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(); // Gets the next stream.
}
} while ((null != boundaryDelimitedStream)
&& (rootPartContentId != null)
&& !rootPartContentId.equals(contentId));
if (boundaryDelimitedStream == null) {
throw new org.apache.axis.AxisFault(
Messages.getMessage("noRoot", rootPartContentId));
}
soapStreamBDS = boundaryDelimitedStream;
if ((contentTransferEncoding != null)
&& (0 != contentTransferEncoding.length())) {
soapStream = MimeUtility.decode(boundaryDelimitedStream,
contentTransferEncoding);
} else {
soapStream =
boundaryDelimitedStream; // This should be the SOAP part
}
// Read from the input stream all attachments prior to the root part.
} catch (javax.mail.internet.ParseException e) {
throw new org.apache.axis.AxisFault(
Messages.getMessage("mimeErrorParsing", e.getMessage()));
} catch (java.io.IOException e) {
throw new org.apache.axis.AxisFault(
Messages.getMessage("readError", e.getMessage()));
} catch (javax.mail.MessagingException e) {
throw new org.apache.axis.AxisFault(
Messages.getMessage("readError", e.getMessage()));
}
}