use of org.nhindirect.xd.common.type.FormatCodeEnum in project nhin-d by DirectProject.
the class DefaultMimeXdsTransformer method transform.
/*
* (non-Javadoc)
*
* @see org.nhindirect.transform.MimeXdsTransformer#transform(javax.mail.internet.MimeMessage)
*/
@Override
public ProvideAndRegisterDocumentSetRequestType transform(MimeMessage mimeMessage) throws TransformationException {
ProvideAndRegisterDocumentSetRequestType request;
DirectDocuments documents = new DirectDocuments();
byte[] xdsDocument = null;
String xdsMimeType = null;
FormatCodeEnum xdsFormatCode = null;
DirectDocumentType documentType = null;
try {
Date sentDate = mimeMessage.getSentDate();
String subject = mimeMessage.getSubject();
String from = mimeMessage.getFrom()[0].toString();
Address[] recipients = mimeMessage.getAllRecipients();
// Plain mail (no attachments)
if (MimeType.TEXT_PLAIN.matches(mimeMessage.getContentType())) {
LOGGER.info("Handling plain mail (no attachments) - " + mimeMessage.getContentType());
// Get the document type
documentType = DirectDocumentType.lookup(mimeMessage);
// Get the format code and MIME type
xdsFormatCode = documentType.getFormatCode();
xdsMimeType = documentType.getMimeType().getType();
// Get the contents
xdsDocument = ((String) mimeMessage.getContent()).getBytes();
// Add document to the collection of documents
documents.getDocuments().add(getDocument(sentDate, from, xdsMimeType, xdsFormatCode, xdsDocument, documentType));
documents.setSubmissionSet(getSubmissionSet(subject, sentDate, from, recipients, xdsDocument, documentType));
} else // Multipart/mixed (attachments)
if (MimeType.MULTIPART.matches(mimeMessage.getContentType())) {
LOGGER.info("Handling multipart/mixed - " + mimeMessage.getContentType());
MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
BodyPart xdmBodyPart = null;
for (int i = 0; i < mimeMultipart.getCount(); i++) {
//check for XDM
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
documentType = DirectDocumentType.lookup(bodyPart);
if (DirectDocumentType.XDM.equals(documentType)) {
xdmBodyPart = bodyPart;
}
}
// For each BodyPart
for (int i = 0; i < mimeMultipart.getCount(); i++) {
/*
* Special handling for XDM attachments.
*
* Spec says if XDM package is present, this will be the
* only attachment.
*
* Overwrite all documents with XDM content and then break
*/
if (xdmBodyPart != null) {
XdmPackage xdmPackage = XdmPackage.fromXdmZipDataHandler(xdmBodyPart.getDataHandler());
// Spec says if XDM package is present, this will be the only attachment
// Overwrite all documents with XDM content and then break
System.out.println("XDM FILE FOUND");
documents = xdmPackage.getDocuments();
break;
}
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
// Skip empty BodyParts
if (bodyPart.getSize() <= 0) {
LOGGER.warn("Empty body, skipping");
continue;
}
// Get the document type
documentType = DirectDocumentType.lookup(bodyPart);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("File name: " + bodyPart.getFileName());
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Content type: " + bodyPart.getContentType());
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("DocumentType: " + documentType.toString());
}
// Get the format code and MIME type
xdsFormatCode = documentType.getFormatCode();
xdsMimeType = documentType.getMimeType().getType();
// Best guess for UNKNOWN MIME type
if (DirectDocumentType.UNKNOWN.equals(documentType)) {
xdsMimeType = bodyPart.getContentType();
}
// Get the contents
xdsDocument = read(bodyPart);
// Add the document to the collection of documents
documents.getDocuments().add(getDocument(sentDate, from, xdsMimeType, xdsFormatCode, xdsDocument, documentType));
documents.setSubmissionSet(getSubmissionSet(subject, sentDate, from, recipients, xdsDocument, documentType));
}
} else {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Message content type (" + mimeMessage.getContentType() + ") is not supported, skipping");
}
}
} catch (MessagingException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected MessagingException occured while handling MimeMessage", e);
}
throw new TransformationException("Unable to complete transformation.", e);
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected IOException occured while handling MimeMessage", e);
}
throw new TransformationException("Unable to complete transformation.", e);
} catch (Exception e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected Exception occured while handling MimeMessage", e);
}
throw new TransformationException("Unable to complete transformation", e);
}
try {
request = documents.toProvideAndRegisterDocumentSetRequestType();
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unexpected IOException occured while transforming to ProvideAndRegisterDocumentSetRequestType", e);
}
throw new TransformationException("Unable to complete transformation", e);
}
return request;
}
Aggregations