Search in sources :

Example 1 with ParseException

use of javax.mail.internet.ParseException in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method sign.

/**
     * Signs an entity with the provided certificates.
     * @param message The entity that will be signed.
     * @param signingCertificates The certificates used to sign the message.
     * @return A signed entity that consists of a multipart/signed entity containing the original entity and a message signature. 
     */
public SignedEntity sign(MimeEntity entity, Collection<X509Certificate> signingCertificates) {
    if (entity == null) {
        throw new IllegalArgumentException();
    }
    // Serialize message out as ASCII encoded...
    byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity);
    MimeMultipart mm = this.createSignatureEntity(messageBytes, signingCertificates);
    SignedEntity retVal = null;
    try {
        retVal = new SignedEntity(new ContentType(mm.getContentType()), mm);
    } catch (ParseException e) {
        throw new MimeException(MimeError.InvalidHeader, e);
    }
    return retVal;
}
Also used : ContentType(javax.mail.internet.ContentType) MimeMultipart(javax.mail.internet.MimeMultipart) MimeException(org.nhindirect.stagent.mail.MimeException) ParseException(javax.mail.internet.ParseException)

Example 2 with ParseException

use of javax.mail.internet.ParseException in project nhin-d by DirectProject.

the class SignedEntity method createContentType.

/**
     * Creates a MIME content type based on the digest algorithm.
     * @param digestAlgorithm The digest algorithm used to generate the message signature.
     * @return a MIME content type based on the digest algorithm.
     */
static ContentType createContentType(DigestAlgorithm digestAlgorithm) {
    ContentType contentType = null;
    try {
        contentType = new ContentType(SMIMEStandard.MultiPartType_Signed);
        contentType.setParameter(SMIMEStandard.MICAlgorithmKey, SMIMEStandard.toString(digestAlgorithm));
    } catch (ParseException e) {
    }
    return contentType;
}
Also used : ContentType(javax.mail.internet.ContentType) ParseException(javax.mail.internet.ParseException)

Example 3 with ParseException

use of javax.mail.internet.ParseException in project jbossws-cxf by jbossws.

the class ClientHandler method getContentType.

protected ContentType getContentType(SOAPMessageContext msgContext) {
    ContentType contentType = null;
    try {
        @SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_RESPONSE_HEADERS);
        List<String> ctype = (headers == null) ? null : headers.get("Content-Type");
        if (ctype == null) {
            // Cxf stores it in lower case
            ctype = (headers == null) ? null : headers.get("content-type");
        }
        log.info("ctype=" + ctype);
        if (ctype == null) {
            // Native has already processed this header into the message
            SOAPMessage soapMessage = msgContext.getMessage();
            MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();
            String[] ct = mimeHeaders.getHeader("Content-Type");
            log.info("ct=" + ct);
            if (ct != null) {
                contentType = new ContentType(ct[0]);
            }
        } else {
            contentType = new ContentType(ctype.get(0));
        }
    } catch (ParseException e) {
        throw new WebServiceException(e);
    }
    return contentType;
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) ContentType(javax.mail.internet.ContentType) WebServiceException(javax.xml.ws.WebServiceException) List(java.util.List) ParseException(javax.mail.internet.ParseException) Map(java.util.Map) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 4 with ParseException

use of javax.mail.internet.ParseException in project spring-integration-samples by spring-projects.

the class EmailParserUtils method handleMultipart.

/**
 * Parses any {@link Multipart} instances that contain text or Html attachments,
 * {@link InputStream} instances, additional instances of {@link Multipart}
 * or other attached instances of {@link javax.mail.Message}.
 *
 * Will create the respective {@link EmailFragment}s representing those attachments.
 *
 * Instances of {@link javax.mail.Message} are delegated to
 * {@link #handleMessage(File, javax.mail.Message, List)}. Further instances
 * of {@link Multipart} are delegated to
 * {@link #handleMultipart(File, Multipart, javax.mail.Message, List)}.
 *
 * @param directory Must not be null
 * @param multipart Must not be null
 * @param mailMessage Must not be null
 * @param emailFragments Must not be null
 */
public static void handleMultipart(File directory, Multipart multipart, javax.mail.Message mailMessage, List<EmailFragment> emailFragments) {
    Assert.notNull(directory, "The directory must not be null.");
    Assert.notNull(multipart, "The multipart object to be parsed must not be null.");
    Assert.notNull(mailMessage, "The mail message to be parsed must not be null.");
    Assert.notNull(emailFragments, "The collection of emailfragments must not be null.");
    final int count;
    try {
        count = multipart.getCount();
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Number of enclosed BodyPart objects: %s.", count));
        }
    } catch (MessagingException e) {
        throw new IllegalStateException("Error while retrieving the number of enclosed BodyPart objects.", e);
    }
    for (int i = 0; i < count; i++) {
        final BodyPart bp;
        try {
            bp = multipart.getBodyPart(i);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving body part.", e);
        }
        final String contentType;
        String filename;
        final String disposition;
        final String subject;
        try {
            contentType = bp.getContentType();
            filename = bp.getFileName();
            disposition = bp.getDisposition();
            subject = mailMessage.getSubject();
            if (filename == null && bp instanceof MimeBodyPart) {
                filename = ((MimeBodyPart) bp).getContentID();
            }
        } catch (MessagingException e) {
            throw new IllegalStateException("Unable to retrieve body part meta data.", e);
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("BodyPart - Content Type: '%s', filename: '%s', disposition: '%s', subject: '%s'", new Object[] { contentType, filename, disposition, subject }));
        }
        if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
            LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
        }
        final Object content;
        try {
            content = bp.getContent();
        } catch (IOException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        }
        if (content instanceof String) {
            if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                emailFragments.add(new EmailFragment(directory, i + "-" + filename, content));
                LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
            } else {
                final String textFilename;
                final ContentType ct;
                try {
                    ct = new ContentType(contentType);
                } catch (ParseException e) {
                    throw new IllegalStateException("Error while parsing content type '" + contentType + "'.", e);
                }
                if ("text/plain".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.txt";
                } else if ("text/html".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.html";
                } else {
                    textFilename = "message.other";
                }
                emailFragments.add(new EmailFragment(directory, textFilename, content));
            }
        } else if (content instanceof InputStream) {
            final InputStream inputStream = (InputStream) content;
            final ByteArrayOutputStream bis = new ByteArrayOutputStream();
            try {
                IOUtils.copy(inputStream, bis);
            } catch (IOException e) {
                throw new IllegalStateException("Error while copying input stream to the ByteArrayOutputStream.", e);
            }
            emailFragments.add(new EmailFragment(directory, filename, bis.toByteArray()));
        } else if (content instanceof javax.mail.Message) {
            handleMessage(directory, (javax.mail.Message) content, emailFragments);
        } else if (content instanceof Multipart) {
            final Multipart mp2 = (Multipart) content;
            handleMultipart(directory, mp2, mailMessage, emailFragments);
        } else {
            throw new IllegalStateException("Content type not handled: " + content.getClass().getSimpleName());
        }
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(javax.mail.internet.ParseException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with ParseException

use of javax.mail.internet.ParseException in project service-proxy by membrane.

the class XMLContentFilter method removeMatchingElements.

/**
 * Removes parts of an XML document based on an XPath expression.
 *
 * If the message is not valid XML, it is left unchanged.
 */
public void removeMatchingElements(Message message) {
    try {
        Message xop = null;
        try {
            xop = xopReconstitutor.getReconstitutedMessage(message);
        } catch (ParseException e) {
        } catch (EndOfStreamException e) {
        } catch (FactoryConfigurationError e) {
        }
        if (elementFinder != null && !elementFinder.matches(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream())) {
            return;
        }
        DocumentBuilder db = createDocumentBuilder();
        Document d;
        try {
            d = db.parse(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream());
        } finally {
            db.reset();
        }
        removeElementsIfNecessary(message, xop, d);
    } catch (SAXException e) {
        return;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (XMLStreamException e) {
        return;
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) Message(com.predic8.membrane.core.http.Message) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParseException(javax.mail.internet.ParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) TransformerException(javax.xml.transform.TransformerException)

Aggregations

ParseException (javax.mail.internet.ParseException)29 ContentType (javax.mail.internet.ContentType)25 ByteString (com.linkedin.data.ByteString)6 Map (java.util.Map)6 IOException (java.io.IOException)5 List (java.util.List)5 MimeHeaders (javax.xml.soap.MimeHeaders)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 InputStream (java.io.InputStream)4 WebServiceException (javax.xml.ws.WebServiceException)4 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)4 Builder (org.apache.axis2.builder.Builder)4 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)4 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)3 BodyPart (javax.mail.BodyPart)3 MessagingException (javax.mail.MessagingException)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 Callback (com.linkedin.common.callback.Callback)2 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)2