use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method parseContent.
private void parseContent(MimePart part, boolean doNotParseBrokenParts) throws PackageException {
if (exceedsMaxNestedLevel()) {
if (log.isInfoEnabled() && !skippedParsingMsgIsAlreadyLogged) {
log.info("Skipping parsing of nested message parts from current MimePackage because max nested level is reached." + " Current max nesting level is " + ActionLibraryConfigurator.getInstance().getMimePackageMaxNestedLevel());
skippedParsingMsgIsAlreadyLogged = true;
}
return;
}
try {
Object content = part.getContent();
if (content instanceof Multipart) {
// if multipart recurse through all child parts
MimeMultipart mimeMultipart = (MimeMultipart) content;
int partCount = mimeMultipart.getCount();
for (int i = 0; i < partCount; i++) {
try {
parseContent((MimeBodyPart) mimeMultipart.getBodyPart(i));
} catch (PackageException pe) {
if (doNotParseBrokenParts) {
log.warn("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
} else {
log.error("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
throw new PackageException(pe);
}
}
}
} else if (content instanceof MimeMessage) {
MimeMessage mimeMessage = (MimeMessage) content;
nestedMimePackages.add(new MimePackage(this.nestedPath, nestedMimePackages.size(), mimeMessage, partOfImapFolder));
// to treat it as such - it will not be decomposed
if (isPartAttachment(part)) {
parts.add(part);
attachmentPartIndices.add(parts.size() - 1);
} else {
try {
parseContent(mimeMessage);
} catch (PackageException pe) {
if (doNotParseBrokenParts) {
log.warn("Could not parse part: " + mimeMessage.getContentID());
} else {
log.error("Could not parse part: " + mimeMessage.getContentID());
throw pe;
}
}
}
} else {
InternetHeaders internetHeaders = null;
if (part.getContentType().toLowerCase().startsWith(CONTENT_TYPE_RFC822_HEADERS)) {
try {
// check for "text/rfc822-headers"
internetHeaders = getInternetHeaders(content);
} catch (PackageException e) {
throw new PackageException("Content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but headers are not parsed successfully.", e);
}
if (internetHeaders == null) {
// javax.mail implementation is not very strict
log.error("Mail part with content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but could not be parsed. Contents: " + content);
}
}
if (internetHeaders != null) {
// the "Content-Type" is "text/rfc822-headers" and javamail returns it as InternetHeaders.
// this is a message with headers only, we will keep it as a
// nested MimePackage
MimePackage nestedMimePackage = new MimePackage();
nestedMimePackage.setNestedPath(this.nestedPath, nestedMimePackages.size());
Enumeration<?> enumerator = internetHeaders.getAllHeaders();
while (enumerator.hasMoreElements()) {
Header inetHeader = (Header) enumerator.nextElement();
nestedMimePackage.addHeader(inetHeader.getName(), inetHeader.getValue());
}
nestedMimePackages.add(nestedMimePackage);
} else {
// add the body
parts.add(part);
if (isPartAttachment(part)) {
attachmentPartIndices.add(parts.size() - 1);
} else {
regularPartIndices.add(parts.size() - 1);
}
}
}
} catch (MessagingException me) {
throw new PackageException("Could not parse MIME part", me);
} catch (IOException ioe) {
throw new PackageException("Could not parse MIME message", ioe);
}
}
use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method addPartFromFile.
/**
* adds a given body part of a specified type from a specified file to the
* specified position all line endings are set to "\r\n"
*
* @param fileName
* name of the file to get the data from
* @param contentType
* the content type of the part
* @throws PackageException
* on error
*/
@PublicAtsApi
public void addPartFromFile(String fileName, String contentType) throws PackageException {
// the normalized buffer - Windows like line ending
StringBuffer normalizedBuff = new StringBuffer();
try (BufferedReader buffReader = new BufferedReader(new java.io.FileReader(fileName))) {
String currLine;
do {
currLine = buffReader.readLine();
if (currLine != null) {
normalizedBuff.append(currLine);
normalizedBuff.append("\r\n");
}
} while (currLine != null);
// add the new body part
addPart(normalizedBuff.toString(), contentType);
} catch (IOException ioe) {
throw new PackageException(ioe);
}
}
Aggregations