Search in sources :

Example 6 with ContentType

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

the class NotificationMessage method createHeaders.

private static InternetHeaders createHeaders(String to, String from, Notification notification) throws MessagingException {
    InternetHeaders headers = new InternetHeaders();
    if (to != null && !to.isEmpty())
        headers.addHeader(MailStandard.Headers.To, to);
    if (from != null && !from.isEmpty())
        headers.addHeader(MailStandard.Headers.From, from);
    headers.addHeader(MimeStandard.VersionHeader, "1.0");
    // get the boundary
    ContentType type = new ContentType(notification.getAsMultipart().getContentType());
    String boundary = type.getParameter("boundary");
    headers.addHeader(MailStandard.Headers.ContentType, MDNStandard.MediaType.DispositionReport + "; boundary=\"" + boundary + "\"");
    return headers;
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) ContentType(javax.mail.internet.ContentType)

Example 7 with ContentType

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

the class CryptographerTest method testEncryptAndDecryptMimeEntity.

private void testEncryptAndDecryptMimeEntity(EncryptionAlgorithm encAlg, boolean enforceStrongEncryption, boolean expectDecException) throws Exception {
    X509Certificate cert = TestUtils.getExternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    if (encAlg != null)
        cryptographer.setEncryptionAlgorithm(encAlg);
    cryptographer.setStrongEncryptionEnforced(enforceStrongEncryption);
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity encEntity = cryptographer.encrypt(entity, cert);
    assertNotNull(encEntity);
    /*
		 * explicit header checking for compliance with Applicability
		 * Statement v 1.2
		 */
    final ContentType type = new ContentType(encEntity.getContentType());
    assertTrue(type.match(SMIMEStandard.CmsEnvelopeMediaType));
    assertFalse(type.match(SMIMEStandard.CmsEnvelopeMediaTypeAlt));
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    if (expectDecException) {
        boolean exceptionOccured = false;
        try {
            cryptographer.decrypt(encEntity, certex);
        } catch (Exception e) {
            exceptionOccured = true;
        }
        assertTrue(exceptionOccured);
    } else {
        MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
        assertNotNull(decryEntity);
        byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
        byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
        assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
    }
}
Also used : ContentType(javax.mail.internet.ContentType) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) X509Certificate(java.security.cert.X509Certificate) NHINDException(org.nhindirect.stagent.NHINDException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException)

Example 8 with ContentType

use of javax.mail.internet.ContentType in project ats-framework by Axway.

the class MimePackage method getRegularPartCharset.

/**
     * Get the character set of a regular part
     *
     * @param partIndex
     *            the index of the part
     * @return the charset
     * @throws PackageException
     */
@PublicAtsApi
public String getRegularPartCharset(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= regularPartIndices.size()) {
        throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(regularPartIndices.get(partIndex));
        // get the content type header
        ContentType contentType = new ContentType(part.getContentType());
        return contentType.getParameter("charset");
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 9 with ContentType

use of javax.mail.internet.ContentType in project ats-framework by Axway.

the class StringInMimePartRule method performMatch.

@Override
public boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    InputStream actualPartDataStream = null;
    BufferedInputStream bufferedStream = null;
    try {
        List<MimePart> partsToCheck = new ArrayList<MimePart>();
        if (partIndex == PART_MAIN_MESSAGE) {
            partsToCheck = emailMessage.getMimeParts();
        } else {
            partsToCheck.add(emailMessage.getPart(partIndex, isPartAttachment));
        }
        for (MimePart partToCheck : partsToCheck) {
            Object partContent = partToCheck.getContent();
            ContentType partContentType = new ContentType(partToCheck.getContentType());
            //skip if no content
            if (partContent == null) {
                log.debug("MIME part does not have any content");
                continue;
            }
            String partContentAsString;
            if (partContent instanceof String) {
                //directly read the content of the part
                partContentAsString = (String) partContent;
            } else if (partContent instanceof InputStream && partContentType.getBaseType().toLowerCase().startsWith(TEXT_MIME_TYPE_LC)) {
                // to be closed in finally
                actualPartDataStream = (InputStream) partContent;
                //get the charset of the part - default to us-ascii
                String charset = partContentType.getParameter("charset");
                if (charset == null) {
                    charset = "us-ascii";
                }
                //read stream by large chunks to minimize memory fragmentation
                int bufLen = 4096;
                byte[] buffer = new byte[bufLen];
                StringBuffer dataStringBuffer = new StringBuffer();
                bufferedStream = new BufferedInputStream(actualPartDataStream);
                int numBytesRead = bufLen;
                while (numBytesRead == bufLen) {
                    numBytesRead = bufferedStream.read(buffer, 0, bufLen);
                    if (numBytesRead != -1) {
                        dataStringBuffer.append(new String(buffer, 0, numBytesRead, charset));
                    } else {
                        //we've reached end of stream
                        break;
                    }
                }
                partContentAsString = dataStringBuffer.toString();
            } else {
                log.debug("Skipping MIME part as it is binary");
                continue;
            }
            if (isValueRegularExpression) {
                actualResult = Pattern.compile(expectedValue).matcher(partContentAsString).find();
            } else {
                actualResult = partContentAsString.indexOf(expectedValue) >= 0;
            }
            //continue anymore
            if (actualResult) {
                break;
            }
        }
        return actualResult;
    } catch (MessagingException me) {
        throw new RbvException(me);
    } catch (PackageException pe) {
        throw new RbvException(pe);
    } catch (IOException ioe) {
        throw new RbvException(ioe);
    } finally {
        IoUtils.closeStream(actualPartDataStream);
        IoUtils.closeStream(bufferedStream);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) RbvException(com.axway.ats.rbv.model.RbvException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MimePackage(com.axway.ats.action.objects.MimePackage) BufferedInputStream(java.io.BufferedInputStream) MimePart(javax.mail.internet.MimePart) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 10 with ContentType

use of javax.mail.internet.ContentType in project ats-framework by Axway.

the class MimePackage method getRegularPartContentType.

/**
     * Get the content type of a regular part
     *
     * @param partIndex
     *            the index of the regular part
     * @return the content type as string
     * @throws PackageException
     */
@PublicAtsApi
public String getRegularPartContentType(int partIndex) throws PackageException {
    // first check if there is part at this position at all
    if (partIndex >= regularPartIndices.size()) {
        throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'");
    }
    try {
        MimePart part = getPart(regularPartIndices.get(partIndex));
        // get the content type header
        ContentType contentType = new ContentType(part.getContentType());
        return contentType.getBaseType();
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) MimePart(javax.mail.internet.MimePart) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

ContentType (javax.mail.internet.ContentType)40 IOException (java.io.IOException)17 MessagingException (javax.mail.MessagingException)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 ByteString (com.linkedin.data.ByteString)10 MimeMessage (javax.mail.internet.MimeMessage)9 MimeMultipart (javax.mail.internet.MimeMultipart)9 InputStream (java.io.InputStream)8 ParseException (javax.mail.internet.ParseException)8 MimePart (javax.mail.internet.MimePart)6 PackageException (com.axway.ats.action.objects.model.PackageException)5 DataHandler (javax.activation.DataHandler)5 BodyPart (javax.mail.BodyPart)5 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)4 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URI (java.net.URI)3