Search in sources :

Example 1 with FileBufferedWriter

use of com.zimbra.common.util.FileBufferedWriter in project zm-mailbox by Zimbra.

the class IcsFormatter method formatCallback.

@Override
public void formatCallback(UserServletContext context) throws IOException, ServiceException {
    Iterator<? extends MailItem> iterator = null;
    List<CalendarItem> calItems = new ArrayList<CalendarItem>();
    // ZimbraLog.mailbox.info("end = "+new Date(context.getEndTime()));
    try {
        long start = context.getStartTime();
        long end = context.getEndTime();
        boolean hasTimeRange = start != TIME_UNSPECIFIED && end != TIME_UNSPECIFIED;
        iterator = getMailItems(context, start, end, Integer.MAX_VALUE);
        // this is lame
        while (iterator.hasNext()) {
            MailItem item = iterator.next();
            if (item instanceof CalendarItem) {
                CalendarItem calItem = (CalendarItem) item;
                if (hasTimeRange) {
                    Collection<Instance> instances = calItem.expandInstances(start, end, false);
                    if (!instances.isEmpty())
                        calItems.add(calItem);
                } else {
                    calItems.add(calItem);
                }
            }
        }
    } finally {
        if (iterator instanceof QueryResultIterator)
            ((QueryResultIterator) iterator).finished();
    }
    // todo: get from folder name
    String filename = context.itemPath;
    if (mayAttach(context)) {
        if (filename == null || filename.length() == 0)
            filename = "contacts";
        // Let the client specify the filename to save as
        String requestFilename = context.req.getParameter("filename");
        if (requestFilename != null)
            filename = requestFilename;
        else
            // Trim off leading non-word characters (e.g. forward slash)
            filename = filename.replaceAll("^\\W", "");
        if (filename.toLowerCase().endsWith(".ics") == false)
            filename = filename + ".ics";
        String cd = HttpUtil.createContentDisposition(context.req, Part.ATTACHMENT, filename);
        context.resp.addHeader("Content-Disposition", cd);
    }
    Browser browser = HttpUtil.guessBrowser(context.req);
    boolean useOutlookCompatMode = Browser.IE.equals(browser);
    // bug 15549
    boolean needAppleICalHacks = Browser.APPLE_ICAL.equals(browser);
    // Use only htmlFormat when the file isn't supposed to be downloaded (ie. it's supposed to be shown in the browser). Mangles the code so it can be displayed correctly, especially by IE
    boolean htmlFormat = !mayAttach(context) && Browser.IE.equals(browser);
    context.resp.setCharacterEncoding(MimeConstants.P_CHARSET_UTF8);
    String contentType;
    if (htmlFormat) {
        contentType = MimeConstants.CT_TEXT_HTML;
    } else if (mayAttach(context)) {
        contentType = MimeConstants.CT_TEXT_CALENDAR;
    } else {
        contentType = MimeConstants.CT_TEXT_PLAIN;
    }
    context.resp.setContentType(contentType);
    OperationContext octxt = new OperationContext(context.getAuthAccount(), context.isUsingAdminPrivileges());
    FileBufferedWriter fileBufferedWriter = new FileBufferedWriter(context.resp.getWriter(), LC.calendar_ics_export_buffer_size.intValueWithinRange(0, FileBufferedWriter.MAX_BUFFER_SIZE));
    try {
        if (htmlFormat)
            fileBufferedWriter.write("<html><body><pre>");
        context.targetMailbox.writeICalendarForCalendarItems(fileBufferedWriter, octxt, calItems, (context.target != null && context.target instanceof Folder) ? (Folder) context.target : null, useOutlookCompatMode, true, needAppleICalHacks, true, htmlFormat, includeInlineAttaches(context));
        if (htmlFormat)
            fileBufferedWriter.write("</pre></body></html>");
    } finally {
        fileBufferedWriter.finish();
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) ArrayList(java.util.ArrayList) FileBufferedWriter(com.zimbra.common.util.FileBufferedWriter) Folder(com.zimbra.cs.mailbox.Folder) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) Browser(com.zimbra.common.util.HttpUtil.Browser)

Aggregations

FileBufferedWriter (com.zimbra.common.util.FileBufferedWriter)1 Browser (com.zimbra.common.util.HttpUtil.Browser)1 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)1 Instance (com.zimbra.cs.mailbox.CalendarItem.Instance)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ArrayList (java.util.ArrayList)1