Search in sources :

Example 16 with ContentType

use of javax.mail.internet.ContentType in project zm-mailbox by Zimbra.

the class ForwardAppointmentInvite method getMessagePair.

public static Pair<MimeMessage, MimeMessage> getMessagePair(Mailbox mbox, Account senderAcct, Message msg, MimeMessage mmFwdWrapper) throws ServiceException {
    Pair<MimeMessage, MimeMessage> msgPair;
    mbox.lock.lock();
    try {
        MimeMessage mmInv = msg.getMimeMessage();
        List<Invite> invs = new ArrayList<Invite>();
        for (Iterator<CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext(); ) {
            CalendarItemInfo cii = iter.next();
            Invite inv = cii.getInvite();
            if (inv != null) {
                invs.add(inv);
            }
        }
        ZVCalendar cal = null;
        Invite firstInv = null;
        if (!invs.isEmpty()) {
            // Recreate the VCALENDAR from Invites.
            boolean first = true;
            for (Invite inv : invs) {
                if (first) {
                    first = false;
                    firstInv = inv;
                    cal = inv.newToICalendar(true);
                } else {
                    ZComponent comp = inv.newToVComponent(true, true);
                    cal.addComponent(comp);
                }
            }
        } else {
            // If no invites found in metadata, parse from text/calendar MIME part.
            try {
                CalPartDetectVisitor visitor = new CalPartDetectVisitor();
                visitor.accept(mmInv);
                MimeBodyPart calPart = visitor.getCalendarPart();
                if (calPart != null) {
                    String ctHdr = calPart.getContentType();
                    ContentType ct = new ContentType(ctHdr);
                    String charset = ct.getParameter(MimeConstants.P_CHARSET);
                    if (charset == null || charset.length() == 0)
                        charset = MimeConstants.P_CHARSET_UTF8;
                    InputStream is = calPart.getInputStream();
                    try {
                        cal = ZCalendarBuilder.build(is, charset);
                    } finally {
                        ByteUtil.closeStream(is);
                    }
                    List<Invite> invList = Invite.createFromCalendar(senderAcct, msg.getFragment(), cal, false);
                    if (invList != null && !invList.isEmpty())
                        firstInv = invList.get(0);
                    if (firstInv == null)
                        throw ServiceException.FAILURE("Error building Invite for calendar part in message " + msg.getId(), null);
                }
            } catch (MessagingException e) {
                throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
            } catch (IOException e) {
                throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
            }
        }
        msgPair = getInstanceFwdMsg(senderAcct, firstInv, cal, mmInv, mmFwdWrapper);
    } finally {
        mbox.lock.release();
    }
    return msgPair;
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CalendarItemInfo(com.zimbra.cs.mailbox.Message.CalendarItemInfo) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) MimeMessage(javax.mail.internet.MimeMessage) MimeBodyPart(javax.mail.internet.MimeBodyPart) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 17 with ContentType

use of javax.mail.internet.ContentType in project stanbol by apache.

the class SimpleMailExtractor method processContent.

// the recursive part
protected void processContent(Object content, StringBuilder buffer, RDFContainer rdf) throws MessagingException, IOException, ExtractorException {
    if (content instanceof String) {
        buffer.append(content);
        buffer.append(' ');
    } else if (content instanceof BodyPart) {
        BodyPart bodyPart = (BodyPart) content;
        DataHandler handler = bodyPart.getDataHandler();
        String encoding = null;
        if (handler != null) {
            encoding = MimeUtility.getEncoding(handler);
        }
        String fileName = bodyPart.getFileName();
        String contentType = bodyPart.getContentType();
        if (fileName != null) {
            try {
                fileName = MimeUtility.decodeWord(fileName);
            } catch (MessagingException e) {
            // happens on unencoded file names! so just ignore it and leave the file name as it is
            }
            URI attachURI = URIGenerator.createNewRandomUniqueURI();
            rdf.add(NMO.hasAttachment, attachURI);
            Model m = rdf.getModel();
            m.addStatement(attachURI, RDF.type, NFO.Attachment);
            m.addStatement(attachURI, NFO.fileName, fileName);
            if (handler != null) {
                if (encoding != null) {
                    m.addStatement(attachURI, NFO.encoding, encoding);
                }
            }
            if (contentType != null) {
                contentType = (new ContentType(contentType)).getBaseType();
                m.addStatement(attachURI, NIE.mimeType, contentType.trim());
            }
        // TODO: encoding?
        }
        // append the content, if any
        content = bodyPart.getContent();
        // remove any html markup if necessary
        if (contentType != null && content instanceof String) {
            contentType = contentType.toLowerCase();
            if (contentType.indexOf("text/html") >= 0) {
                if (encoding != null) {
                    encoding = MimeUtility.javaCharset(encoding);
                }
                content = extractTextFromHtml((String) content, encoding, rdf);
            }
        }
        processContent(content, buffer, rdf);
    } else if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        String subType = null;
        String contentType = multipart.getContentType();
        if (contentType != null) {
            ContentType ct = new ContentType(contentType);
            subType = ct.getSubType();
            if (subType != null) {
                subType = subType.trim().toLowerCase();
            }
        }
        if ("alternative".equals(subType)) {
            handleAlternativePart(multipart, buffer, rdf);
        } else if ("signed".equals(subType)) {
            handleProtectedPart(multipart, 0, buffer, rdf);
        } else if ("encrypted".equals(subType)) {
            handleProtectedPart(multipart, 1, buffer, rdf);
        } else {
            // handles multipart/mixed, /digest, /related, /parallel, /report and unknown subtypes
            handleMixedPart(multipart, buffer, rdf);
        }
    }
}
Also used : BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) Model(org.ontoware.rdf2go.model.Model) DataHandler(javax.activation.DataHandler) URI(org.ontoware.rdf2go.model.node.URI)

Example 18 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class RestResponseDecoder method decodeResponse.

public void decodeResponse(final StreamResponse streamResponse, final Callback<Response<T>> responseCallback) throws RestLiDecodingException {
    //Determine content type and take appropriate action.
    //If 'multipart/related', then use MultiPartMIMEReader to read first part (which can be json or pson).
    final String contentTypeString = streamResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE);
    if (contentTypeString != null) {
        ContentType contentType = null;
        try {
            contentType = new ContentType(contentTypeString);
        } catch (ParseException parseException) {
            responseCallback.onError(new RestLiDecodingException("Could not decode Content-Type header in response", parseException));
            return;
        }
        if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
            final MultiPartMIMEReader multiPartMIMEReader = MultiPartMIMEReader.createAndAcquireStream(streamResponse);
            final TopLevelReaderCallback topLevelReaderCallback = new TopLevelReaderCallback(responseCallback, streamResponse, multiPartMIMEReader);
            multiPartMIMEReader.registerReaderCallback(topLevelReaderCallback);
            return;
        }
    }
    //Otherwise if the whole body is json/pson then read everything in.
    //This will not have an extra copy due to assembly since FullEntityReader uses a compound ByteString.
    final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            responseCallback.onError(e);
        }

        @Override
        public void onSuccess(ByteString result) {
            try {
                responseCallback.onSuccess(createResponse(streamResponse.getHeaders(), streamResponse.getStatus(), result, streamResponse.getCookies()));
            } catch (Exception exception) {
                onError(exception);
            }
        }
    });
    streamResponse.getEntityStream().setReader(fullEntityReader);
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) ByteString(com.linkedin.data.ByteString) ParseException(javax.mail.internet.ParseException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(javax.mail.internet.ParseException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MimeTypeParseException(javax.activation.MimeTypeParseException)

Example 19 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createSmallDataSource.

private static final MimeBodyPart createSmallDataSource() {
    try {
        //Small body.
        final String body = "A small body";
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ContentType contentType = new ContentType(TEXT_PLAIN_CONTENT_TYPE);
        dataPart.setContent(body, contentType.getBaseType());
        dataPart.setHeader(HEADER_CONTENT_TYPE, contentType.toString());
        dataPart.setHeader("SomeCustomHeader", "SomeCustomValue");
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

Example 20 with ContentType

use of javax.mail.internet.ContentType in project rest.li by linkedin.

the class MIMETestUtils method createBodyLessBody.

private static final MimeBodyPart createBodyLessBody() {
    try {
        //Body-less body. This has no body but does have headers, some of which are folded.
        final MimeBodyPart dataPart = new MimeBodyPart();
        final ParameterList parameterList = new ParameterList();
        parameterList.set("AVeryVeryVeryVeryLongHeader", "AVeryVeryVeryVeryLongValue");
        parameterList.set("AVeryVeryVeryVeryLongHeader2", "AVeryVeryVeryVeryLongValue2");
        parameterList.set("AVeryVeryVeryVeryLongHeader3", "AVeryVeryVeryVeryLongValue3");
        parameterList.set("AVeryVeryVeryVeryLongHeader4", "AVeryVeryVeryVeryLongValue4");
        final ContentType contentType = new ContentType("text", "plain", parameterList);
        dataPart.setContent("", contentType.getBaseType());
        dataPart.setHeader(HEADER_CONTENT_TYPE, contentType.toString());
        dataPart.setHeader("YetAnotherCustomHeader", "YetAnotherCustomValue");
        return dataPart;
    } catch (Exception exception) {
        Assert.fail();
    }
    return null;
}
Also used : ContentType(javax.mail.internet.ContentType) ParameterList(javax.mail.internet.ParameterList) MimeBodyPart(javax.mail.internet.MimeBodyPart) IOException(java.io.IOException)

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