Search in sources :

Example 1 with TransformationException

use of org.nhindirect.xd.transform.exception.TransformationException in project nhin-d by DirectProject.

the class DefaultXdmXdsTransformer method transform.

/*
     * (non-Javadoc)
     * 
     * @see org.nhindirect.transform.XdmXdsTransformer#transform(java.io.File)
     */
@Override
public ProvideAndRegisterDocumentSetRequestType transform(File file) throws TransformationException {
    LOGGER.trace("Begin transformation of XDM to XDS (file)");
    String docId = null;
    ZipFile zipFile = null;
    String docName = getDocName(file);
    if (docName != null) {
        XDM_FILENAME_DATA = docName;
    }
    ProvideAndRegisterDocumentSetRequestType prsr = new ProvideAndRegisterDocumentSetRequestType();
    try {
        zipFile = new ZipFile(file, ZipFile.OPEN_READ);
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        ZipEntry zipEntry = null;
        // load the ZIP archive into memory
        while (zipEntries.hasMoreElements()) {
            zipEntry = zipEntries.nextElement();
            String zname = zipEntry.getName();
            LOGGER.trace("Processing a ZipEntry " + zname);
            if (!zipEntry.isDirectory()) {
                String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
                // Read metadata
                if (matchName(zname, subsetDirspec, XDM_FILENAME_METADATA)) {
                    ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
                    SubmitObjectsRequest submitObjectRequest = (SubmitObjectsRequest) XmlUtils.unmarshal(byteArrayOutputStream.toString(), oasis.names.tc.ebxml_regrep.xsd.lcm._3.ObjectFactory.class);
                    prsr.setSubmitObjectsRequest(submitObjectRequest);
                    docId = getDocId(submitObjectRequest);
                } else // Read data
                if (matchName(zname, subsetDirspec, XDM_FILENAME_DATA)) {
                    ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
                    DataSource source = new ByteArrayDataSource(byteArrayOutputStream.toByteArray(), MimeType.APPLICATION_XML + "; charset=UTF-8");
                    DataHandler dhnew = new DataHandler(source);
                    Document pdoc = new Document();
                    pdoc.setValue(dhnew);
                    pdoc.setId(docId);
                    List<Document> docs = prsr.getDocument();
                    docs.add(pdoc);
                }
            }
            if (!prsr.getDocument().isEmpty()) {
                ((Document) prsr.getDocument().get(0)).setId(zname);
            }
        }
        zipFile.close();
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Unable to complete transformation.", e);
        }
        throw new TransformationException("Unable to complete transformation.", e);
    }
    return prsr;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataHandler(javax.activation.DataHandler) Document(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document) SubmitObjectsRequest(oasis.names.tc.ebxml_regrep.xsd.lcm._3.SubmitObjectsRequest) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) ZipFile(java.util.zip.ZipFile) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 2 with TransformationException

use of org.nhindirect.xd.transform.exception.TransformationException in project nhin-d by DirectProject.

the class DefaultXdsDirectDocumentsTransformer method transform.

@Override
public DirectDocuments transform(ProvideAndRegisterDocumentSetRequestType provideAndRegisterDocumentSetRequestType) throws TransformationException {
    DirectDocuments documents = new DirectDocuments();
    try {
        documents.setValues(provideAndRegisterDocumentSetRequestType.getSubmitObjectsRequest());
    } catch (MetadataException e) {
        throw new TransformationException("Unable to complete transformation due to metadata error", e);
    }
    for (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document document : provideAndRegisterDocumentSetRequestType.getDocument()) {
        byte[] data = null;
        try {
            DataHandler dataHandler = document.getValue();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            dataHandler.writeTo(outputStream);
            data = outputStream.toByteArray();
        } catch (IOException e) {
            throw new TransformationException("Unable to complete transformation due to document IO error", e);
        }
        DirectDocument2 doc = documents.getDocumentByUniqueId(document.getId());
        if (doc != null) {
            doc.setData(data);
        } else {
            documents.getDocumentById(document.getId()).setData(data);
        }
    }
    return documents;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) DirectDocument2(org.nhindirect.xd.common.DirectDocument2) DirectDocuments(org.nhindirect.xd.common.DirectDocuments) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MetadataException(org.nhindirect.xd.common.exception.MetadataException) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 3 with TransformationException

use of org.nhindirect.xd.transform.exception.TransformationException 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;
}
Also used : BodyPart(javax.mail.BodyPart) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Date(java.util.Date) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) DirectDocumentType(org.nhindirect.xd.common.type.DirectDocumentType) MimeMultipart(javax.mail.internet.MimeMultipart) DirectDocuments(org.nhindirect.xd.common.DirectDocuments) FormatCodeEnum(org.nhindirect.xd.common.type.FormatCodeEnum) XdmPackage(org.nhindirect.xd.common.XdmPackage) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 4 with TransformationException

use of org.nhindirect.xd.transform.exception.TransformationException in project nhin-d by DirectProject.

the class DefaultXdmXdsTransformer method getDocName.

public String getDocName(File file) throws TransformationException {
    LOGGER.trace("Begin transformation of XDM to XDS (file)");
    ZipFile zipFile = null;
    String objectId = null;
    ProvideAndRegisterDocumentSetRequestType prsr = new ProvideAndRegisterDocumentSetRequestType();
    try {
        zipFile = new ZipFile(file, ZipFile.OPEN_READ);
        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        ZipEntry zipEntry = null;
        // load the ZIP archive into memory
        while (zipEntries.hasMoreElements()) {
            zipEntry = zipEntries.nextElement();
            String zname = zipEntry.getName();
            LOGGER.trace("Processing a ZipEntry " + zname);
            if (!zipEntry.isDirectory()) {
                String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
                // Read metadata
                if (matchName(zname, subsetDirspec, XDM_FILENAME_METADATA)) {
                    ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
                    SubmitObjectsRequest submitObjectRequest = (SubmitObjectsRequest) XmlUtils.unmarshal(byteArrayOutputStream.toString(), oasis.names.tc.ebxml_regrep.xsd.lcm._3.ObjectFactory.class);
                    prsr.setSubmitObjectsRequest(submitObjectRequest);
                    objectId = getDocName(submitObjectRequest);
                }
            // Read data
            }
        }
        zipFile.close();
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Unable to complete getObjectId.", e);
        }
        throw new TransformationException("Unable to complete getObjectId.", e);
    }
    return objectId;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SubmitObjectsRequest(oasis.names.tc.ebxml_regrep.xsd.lcm._3.SubmitObjectsRequest) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ZipFile(java.util.zip.ZipFile) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Example 5 with TransformationException

use of org.nhindirect.xd.transform.exception.TransformationException in project nhin-d by DirectProject.

the class DefaultXdmXdsTransformer method transform.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.nhindirect.transform.XdmXdsTransformer#transform(javax.activation
     * .DataHandler)
     */
@Override
public ProvideAndRegisterDocumentSetRequestType transform(DataHandler dataHandler) throws TransformationException {
    LOGGER.trace("Begin transformation of XDM to XDS (datahandler)");
    File file = null;
    try {
        // Create a temporary work file
        file = fileFromDataHandler(dataHandler);
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Error creating temporary work file, unable to complete transformation.", e);
        }
        throw new TransformationException("Error creating temporary work file, unable to complete transformation.", e);
    }
    ProvideAndRegisterDocumentSetRequestType request = transform(file);
    boolean delete = file.delete();
    if (delete) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Deleted temporary work file " + file.getAbsolutePath());
        }
    } else {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Unable to delete temporary work file " + file.getAbsolutePath());
        }
    }
    return request;
}
Also used : TransformationException(org.nhindirect.xd.transform.exception.TransformationException) ZipFile(java.util.zip.ZipFile) File(java.io.File) TransformationException(org.nhindirect.xd.transform.exception.TransformationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Aggregations

ProvideAndRegisterDocumentSetRequestType (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)5 IOException (java.io.IOException)5 TransformationException (org.nhindirect.xd.transform.exception.TransformationException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 ZipFile (java.util.zip.ZipFile)3 ZipEntry (java.util.zip.ZipEntry)2 DataHandler (javax.activation.DataHandler)2 SubmitObjectsRequest (oasis.names.tc.ebxml_regrep.xsd.lcm._3.SubmitObjectsRequest)2 DirectDocuments (org.nhindirect.xd.common.DirectDocuments)2 Document (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document)1 File (java.io.File)1 Date (java.util.Date)1 List (java.util.List)1 DataSource (javax.activation.DataSource)1 Address (javax.mail.Address)1 BodyPart (javax.mail.BodyPart)1 MessagingException (javax.mail.MessagingException)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1