Search in sources :

Example 1 with MimeHandlerException

use of com.zimbra.cs.mime.MimeHandlerException in project zm-mailbox by Zimbra.

the class TextHtmlHandler method getContentImpl.

@Override
protected String getContentImpl() throws MimeHandlerException {
    if (content == null) {
        DataSource source = getDataSource();
        if (source != null) {
            InputStream is = null;
            try {
                Reader reader = getReader(is = source.getInputStream(), source.getContentType());
                content = HtmlTextExtractor.extract(reader, MimeHandlerManager.getIndexedTextLimit());
            } catch (Exception e) {
                throw new MimeHandlerException(e);
            } finally {
                ByteUtil.closeStream(is);
            }
        }
    }
    if (content == null) {
        content = "";
    }
    return content;
}
Also used : MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) InputStream(java.io.InputStream) Reader(java.io.Reader) MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Example 2 with MimeHandlerException

use of com.zimbra.cs.mime.MimeHandlerException in project zm-mailbox by Zimbra.

the class TextPlainHandler method getContentImpl.

@Override
protected String getContentImpl() throws MimeHandlerException {
    if (content == null) {
        DataSource source = getDataSource();
        if (source != null) {
            String ctype = source.getContentType();
            InputStream is = null;
            try {
                Reader reader = Mime.getTextReader(is = source.getInputStream(), ctype, getDefaultCharset());
                content = ByteUtil.getContent(reader, MimeHandlerManager.getIndexedTextLimit(), false);
            } catch (IOException e) {
                throw new MimeHandlerException(e);
            } finally {
                ByteUtil.closeStream(is);
            }
        }
    }
    if (content == null) {
        content = "";
    }
    return content;
}
Also used : MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Example 3 with MimeHandlerException

use of com.zimbra.cs.mime.MimeHandlerException in project zm-mailbox by Zimbra.

the class MessageRFC822Handler method getContentImpl.

/**
 * Returns the subject of the attached message.
 */
@Override
protected String getContentImpl() throws MimeHandlerException {
    DataSource ds = getDataSource();
    if (ds == null) {
        return null;
    }
    InputStream is = null;
    String content = null;
    try {
        is = ds.getInputStream();
        if (is == null) {
            return null;
        }
        InternetHeaders headers = new InternetHeaders(is);
        String[] subject = headers.getHeader("Subject");
        if (subject == null || subject.length == 0 || subject[0] == null) {
            return null;
        }
        int maxLength = MimeHandlerManager.getIndexedTextLimit();
        if (subject[0].length() > maxLength) {
            content = subject[0].substring(0, maxLength);
        } else {
            content = subject[0];
        }
    } catch (Exception e) {
        throw new MimeHandlerException(e);
    } finally {
        ByteUtil.closeStream(is);
    }
    return content;
}
Also used : MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) InternetHeaders(javax.mail.internet.InternetHeaders) InputStream(java.io.InputStream) MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) DataSource(javax.activation.DataSource)

Example 4 with MimeHandlerException

use of com.zimbra.cs.mime.MimeHandlerException in project zm-mailbox by Zimbra.

the class TextCalendarHandler method analyze.

private void analyze(boolean needCal) throws MimeHandlerException {
    if (mContent != null)
        return;
    DataSource source = getDataSource();
    if (source == null) {
        mContent = "";
        return;
    }
    InputStream is = null;
    try {
        is = source.getInputStream();
        String charset = MimeConstants.P_CHARSET_UTF8;
        String ctStr = source.getContentType();
        if (ctStr != null) {
            String cs = Mime.getCharset(ctStr);
            if (cs != null)
                charset = cs;
        }
        if (needCal) {
            miCalendar = ZCalendarBuilder.build(is, charset);
            StringBuilder buf = new StringBuilder(1024);
            int maxLength = MimeHandlerManager.getIndexedTextLimit();
            for (Iterator<ZComponent> compIter = miCalendar.getComponentIterator(); compIter.hasNext() && buf.length() < maxLength; ) {
                ZComponent comp = compIter.next();
                for (Iterator<ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext(); ) {
                    ZProperty prop = propIter.next();
                    if (sIndexedProps.contains(prop.getName())) {
                        String value = prop.getValue();
                        if (value != null && value.length() > 0) {
                            if (buf.length() > 0)
                                buf.append(' ');
                            buf.append(value);
                        }
                    }
                }
            }
            mContent = buf.toString();
        } else {
            IcsParseHandler handler = new IcsParseHandler();
            ZCalendarBuilder.parse(is, charset, handler);
            mContent = handler.getContent();
        }
    } catch (Exception e) {
        mContent = "";
        ZimbraLog.index.warn("error reading text/calendar mime part", e);
        throw new MimeHandlerException(e);
    } finally {
        ByteUtil.closeStream(is);
    }
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) InputStream(java.io.InputStream) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ParserException(net.fortuna.ical4j.data.ParserException) MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) DataSource(javax.activation.DataSource)

Aggregations

MimeHandlerException (com.zimbra.cs.mime.MimeHandlerException)4 InputStream (java.io.InputStream)4 DataSource (javax.activation.DataSource)4 IOException (java.io.IOException)2 Reader (java.io.Reader)2 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)1 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)1 InternetHeaders (javax.mail.internet.InternetHeaders)1 ParserException (net.fortuna.ical4j.data.ParserException)1