use of javax.mail.internet.InternetHeaders in project camel by apache.
the class MimeMultipartDataFormat method unmarshal.
@Override
public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, MessagingException {
MimeBodyPart mimeMessage;
String contentType;
Message camelMessage;
Object content = null;
if (headersInline) {
mimeMessage = new MimeBodyPart(stream);
camelMessage = exchange.getOut();
MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
contentType = mimeMessage.getHeader(CONTENT_TYPE, null);
// write the MIME headers not generated by javamail as Camel headers
Enumeration<?> headersEnum = mimeMessage.getNonMatchingHeaders(STANDARD_HEADERS);
while (headersEnum.hasMoreElements()) {
Object ho = headersEnum.nextElement();
if (ho instanceof Header) {
Header header = (Header) ho;
camelMessage.setHeader(header.getName(), header.getValue());
}
}
} else {
// check if this a multipart at all. Otherwise do nothing
contentType = exchange.getIn().getHeader(CONTENT_TYPE, String.class);
if (contentType == null) {
return stream;
}
try {
ContentType ct = new ContentType(contentType);
if (!ct.match("multipart/*")) {
return stream;
}
} catch (ParseException e) {
LOG.warn("Invalid Content-Type " + contentType + " ignored");
return stream;
}
camelMessage = exchange.getOut();
MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(stream, bos);
InternetHeaders headers = new InternetHeaders();
extractHeader(CONTENT_TYPE, camelMessage, headers);
extractHeader(MIME_VERSION, camelMessage, headers);
mimeMessage = new MimeBodyPart(headers, bos.toByteArray());
bos.close();
}
DataHandler dh;
try {
dh = mimeMessage.getDataHandler();
if (dh != null) {
content = dh.getContent();
contentType = dh.getContentType();
}
} catch (MessagingException e) {
LOG.warn("cannot parse message, no unmarshalling done");
}
if (content instanceof MimeMultipart) {
MimeMultipart mp = (MimeMultipart) content;
content = mp.getBodyPart(0);
for (int i = 1; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
DefaultAttachment camelAttachment = new DefaultAttachment(bp.getDataHandler());
@SuppressWarnings("unchecked") Enumeration<Header> headers = bp.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = headers.nextElement();
camelAttachment.addHeader(header.getName(), header.getValue());
}
camelMessage.addAttachmentObject(getAttachmentKey(bp), camelAttachment);
}
}
if (content instanceof BodyPart) {
BodyPart bp = (BodyPart) content;
camelMessage.setBody(bp.getInputStream());
contentType = bp.getContentType();
if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) {
camelMessage.setHeader(CONTENT_TYPE, contentType);
ContentType ct = new ContentType(contentType);
String charset = ct.getParameter("charset");
if (charset != null) {
camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset));
}
}
} else {
// If we find no body part, try to leave the message alone
LOG.info("no MIME part found");
}
return camelMessage;
}
use of javax.mail.internet.InternetHeaders in project nexus-repository-r by sonatype-nexus-community.
the class RHostedFacetImpl method getPackages.
@Override
@TransactionalTouchMetadata
public Content getPackages(final String packagesPath) {
checkNotNull(packagesPath);
try {
// TODO: Do NOT do this on each request as there is at least some overhead, and memory usage is proportional to
// the number of packages contained in a particular path. We should be able to generate this when something has
// changed or via a scheduled task using an approach similar to the Yum implementation rather than this method.
StorageTx tx = UnitOfWork.currentTx();
RPackagesBuilder packagesBuilder = new RPackagesBuilder(packagesPath);
for (Asset asset : tx.browseAssets(tx.findBucket(getRepository()))) {
packagesBuilder.append(asset);
}
CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
for (Entry<String, Map<String, String>> eachPackage : packagesBuilder.getPackageInformation().entrySet()) {
Map<String, String> packageInfo = eachPackage.getValue();
InternetHeaders headers = new InternetHeaders();
headers.addHeader(P_PACKAGE, packageInfo.get(P_PACKAGE));
headers.addHeader(P_VERSION, packageInfo.get(P_VERSION));
headers.addHeader(P_DEPENDS, packageInfo.get(P_DEPENDS));
headers.addHeader(P_IMPORTS, packageInfo.get(P_IMPORTS));
headers.addHeader(P_SUGGESTS, packageInfo.get(P_SUGGESTS));
headers.addHeader(P_LICENSE, packageInfo.get(P_LICENSE));
headers.addHeader(P_NEEDS_COMPILATION, packageInfo.get(P_NEEDS_COMPILATION));
Enumeration<String> headerLines = headers.getAllHeaderLines();
while (headerLines.hasMoreElements()) {
String line = headerLines.nextElement();
writer.write(line, 0, line.length());
writer.write('\n');
}
writer.write('\n');
}
}
}
return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
} catch (CompressorException | IOException e) {
throw new RException(packagesPath, e);
}
}
use of javax.mail.internet.InternetHeaders in project nexus-repository-r by sonatype-nexus-community.
the class RPackagesUtils method buildPackages.
static Content buildPackages(final Collection<Map<String, String>> entries) throws IOException {
CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (CompressorOutputStream cos = compressorStreamFactory.createCompressorOutputStream(GZIP, os)) {
try (OutputStreamWriter writer = new OutputStreamWriter(cos, UTF_8)) {
for (Map<String, String> entry : entries) {
InternetHeaders headers = new InternetHeaders();
headers.addHeader(P_PACKAGE, entry.get(P_PACKAGE));
headers.addHeader(P_VERSION, entry.get(P_VERSION));
headers.addHeader(P_DEPENDS, entry.get(P_DEPENDS));
headers.addHeader(P_IMPORTS, entry.get(P_IMPORTS));
headers.addHeader(P_SUGGESTS, entry.get(P_SUGGESTS));
headers.addHeader(P_LICENSE, entry.get(P_LICENSE));
headers.addHeader(P_NEEDS_COMPILATION, entry.get(P_NEEDS_COMPILATION));
Enumeration<String> headerLines = headers.getAllHeaderLines();
while (headerLines.hasMoreElements()) {
String line = headerLines.nextElement();
writer.write(line, 0, line.length());
writer.write('\n');
}
writer.write('\n');
}
}
} catch (CompressorException e) {
throw new RException(null, e);
}
return new Content(new BytesPayload(os.toByteArray(), "application/x-gzip"));
}
use of javax.mail.internet.InternetHeaders in project zm-mailbox by Zimbra.
the class MessageRFC822Handler method getContentImpl.
/**
* Returns the subject of the attached message.
*/
@Override
protected String getContentImpl() throws MimeHandlerException {
DataSource ds = getDataSource();
if (ds == null) {
return null;
}
InputStream is = null;
String content = null;
try {
is = ds.getInputStream();
if (is == null) {
return null;
}
InternetHeaders headers = new InternetHeaders(is);
String[] subject = headers.getHeader("Subject");
if (subject == null || subject.length == 0 || subject[0] == null) {
return null;
}
int maxLength = MimeHandlerManager.getIndexedTextLimit();
if (subject[0].length() > maxLength) {
content = subject[0].substring(0, maxLength);
} else {
content = subject[0];
}
} catch (Exception e) {
throw new MimeHandlerException(e);
} finally {
ByteUtil.closeStream(is);
}
return content;
}
use of javax.mail.internet.InternetHeaders 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);
}
}
Aggregations