Search in sources :

Example 46 with CalendarItem

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

the class ContentServlet method getCommand.

private void getCommand(HttpServletRequest req, HttpServletResponse resp, AuthToken token) throws ServletException, IOException {
    ItemId iid = null;
    try {
        iid = new ItemId(req.getParameter(PARAM_MSGID), (String) null);
    } catch (ServiceException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, L10nUtil.getMessage(MsgKey.errInvalidId, req));
        return;
    }
    String part = req.getParameter(PARAM_PART);
    String fmt = req.getParameter(PARAM_FORMAT);
    String dumpsterParam = req.getParameter(PARAM_DUMPSTER);
    boolean fromDumpster = dumpsterParam != null && !dumpsterParam.equals("0") && !dumpsterParam.equalsIgnoreCase("false");
    try {
        // need to proxy the fetch if the mailbox lives on another server
        if (!iid.isLocal()) {
            // wrong server; proxy to the right one...
            proxyServletRequest(req, resp, iid.getAccountId());
            return;
        }
        String authId = token.getAccountId();
        String accountId = iid.getAccountId() != null ? iid.getAccountId() : authId;
        AccountUtil.addAccountToLogContext(Provisioning.getInstance(), accountId, ZimbraLog.C_NAME, ZimbraLog.C_ID, token);
        if (!accountId.equalsIgnoreCase(authId))
            ZimbraLog.addToContext(ZimbraLog.C_AID, authId);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(accountId);
        if (mbox == null) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, L10nUtil.getMessage(MsgKey.errMailboxNotFound, req));
            return;
        }
        ZimbraLog.addMboxToContext(mbox.getId());
        MailItem item = mbox.getItemById(new OperationContext(token), iid.getId(), MailItem.Type.UNKNOWN, fromDumpster);
        if (item == null) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, L10nUtil.getMessage(MsgKey.errMessageNotFound, req));
            return;
        }
        try {
            if (part == null) {
                // they want the entire message...
                boolean sync = "1".equals(req.getParameter(PARAM_SYNC));
                StringBuffer hdr = new StringBuffer();
                if (sync) {
                    // for sync, return metadata as headers to avoid extra SOAP round-trips
                    resp.addHeader("X-Zimbra-Tags", TagUtil.getTagIdString(item));
                    resp.addHeader("X-Zimbra-Tag-Names", TagUtil.encodeTags(item.getTags()));
                    resp.addHeader("X-Zimbra-Flags", item.getFlagString());
                    resp.addHeader("X-Zimbra-Received", Long.toString(item.getDate()));
                    resp.addHeader("X-Zimbra-Modified", Long.toString(item.getChangeDate()));
                    // also return metadata inline in the message content for now
                    hdr.append("X-Zimbra-Tags: ").append(TagUtil.getTagIdString(item)).append("\n");
                    hdr.append("X-Zimbra-Tag-Names: ").append(TagUtil.encodeTags(item.getTags()));
                    hdr.append("X-Zimbra-Flags: ").append(item.getFlagString()).append("\n");
                    hdr.append("X-Zimbra-Received: ").append(item.getDate()).append("\n");
                    hdr.append("X-Zimbra-Modified: ").append(item.getChangeDate()).append("\n");
                }
                if (item instanceof Message) {
                    Message msg = (Message) item;
                    if (sync) {
                        resp.addHeader("X-Zimbra-Conv", Integer.toString(msg.getConversationId()));
                        hdr.append("X-Zimbra-Conv: ").append(msg.getConversationId()).append("\n");
                        resp.getOutputStream().write(hdr.toString().getBytes());
                    }
                    resp.setContentType(MimeConstants.CT_TEXT_PLAIN);
                    InputStream is = msg.getContentStream();
                    ByteUtil.copy(is, true, resp.getOutputStream(), false);
                } else if (item instanceof CalendarItem) {
                    CalendarItem calItem = (CalendarItem) item;
                    if (sync) {
                        resp.getOutputStream().write(hdr.toString().getBytes());
                    }
                    resp.setContentType(MimeConstants.CT_TEXT_PLAIN);
                    if (iid.hasSubpart()) {
                        int invId = iid.getSubpartId();
                        MimeMessage mm = calItem.getSubpartMessage(invId);
                        if (mm == null) {
                            // Backward compatibility for pre-5.0.16 ZDesktop: Build a MIME message on the fly.
                            Invite[] invs = calItem.getInvites(invId);
                            if (invs != null && invs.length > 0) {
                                Invite invite = invs[0];
                                mm = CalendarMailSender.createCalendarMessage(invite);
                            }
                        }
                        if (mm != null)
                            mm.writeTo(resp.getOutputStream());
                    } else {
                        InputStream is = calItem.getRawMessage();
                        if (is != null)
                            ByteUtil.copy(is, true, resp.getOutputStream(), false);
                    }
                }
                return;
            } else {
                MimePart mp = null;
                if (item instanceof Message) {
                    mp = getMimePart((Message) item, part);
                } else {
                    CalendarItem calItem = (CalendarItem) item;
                    if (iid.hasSubpart()) {
                        MimeMessage mbp = calItem.getSubpartMessage(iid.getSubpartId());
                        if (mbp != null)
                            mp = Mime.getMimePart(mbp, part);
                    } else {
                        mp = getMimePart(calItem, part);
                    }
                }
                if (mp != null) {
                    String contentType = mp.getContentType();
                    if (contentType == null) {
                        contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
                    }
                    if (contentType.toLowerCase().startsWith(MimeConstants.CT_TEXT_HTML) && (FORMAT_DEFANGED_HTML.equals(fmt) || FORMAT_DEFANGED_HTML_NOT_IMAGES.equals(fmt))) {
                        sendbackDefangedHtml(mp, contentType, resp, fmt);
                    } else {
                        if (!isTrue(Provisioning.A_zimbraAttachmentsViewInHtmlOnly, mbox.getAccountId())) {
                            sendbackOriginalDoc(mp, contentType, req, resp);
                        } else {
                            req.setAttribute(ATTR_MIMEPART, mp);
                            req.setAttribute(ATTR_MSGDIGEST, item.getDigest());
                            req.setAttribute(ATTR_CONTENTURL, req.getRequestURL().toString());
                            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(CONVERSION_PATH);
                            dispatcher.forward(req, resp);
                        }
                    }
                    return;
                }
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, L10nUtil.getMessage(MsgKey.errPartNotFound, req));
            }
        } catch (MessagingException e) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    } catch (NoSuchItemException e) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, L10nUtil.getMessage(MsgKey.errNoSuchItem, req));
    } catch (ServiceException e) {
        returnError(resp, e);
    } catch (HttpException e) {
        throw new IOException("Unknown error", e);
    } finally {
        ZimbraLog.clearContext();
    }
/*
         out.println("hello world "+req.getParameter("id"));
         out.println("path info: "+req.getPathInfo());
         out.println("pathtrans: "+req.getPathTranslated());
         */
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) RequestDispatcher(javax.servlet.RequestDispatcher) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) HttpException(org.apache.http.HttpException) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 47 with CalendarItem

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

the class AtomFormatter method addCalendarItem.

private void addCalendarItem(CalendarItem calItem, Element feed, UserServletContext context) throws ServiceException {
    Collection<Instance> instances = calItem.expandInstances(context.getStartTime(), context.getEndTime(), false);
    for (Iterator<Instance> instIt = instances.iterator(); instIt.hasNext(); ) {
        CalendarItem.Instance inst = instIt.next();
        InviteInfo invId = inst.getInviteInfo();
        Invite inv = calItem.getInvite(invId.getMsgId(), invId.getComponentId());
        Element entry = feed.addElement("entry");
        entry.addElement("title").setText(inv.getName());
        entry.addElement("updated").setText(DateUtil.toISO8601(new Date(inst.getStart())));
        entry.addElement("summary").setText(inv.getFragment());
        // TODO: only personal part in name
        if (inv.hasOrganizer()) {
            Element author = entry.addElement("author");
            author.addElement("name").setText(inv.getOrganizer().getCn());
            author.addElement("email").setText(inv.getOrganizer().getAddress());
        }
    }
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) InviteInfo(com.zimbra.cs.mailbox.calendar.InviteInfo) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) Element(com.zimbra.common.soap.Element) Invite(com.zimbra.cs.mailbox.calendar.Invite) Date(java.util.Date)

Example 48 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem 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)

Example 49 with CalendarItem

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

the class RssFormatter method addCalendarItem.

private void addCalendarItem(CalendarItem calItem, Element channel, UserServletContext context) throws ServiceException {
    Collection<Instance> instances = calItem.expandInstances(context.getStartTime(), context.getEndTime(), false);
    for (Iterator<Instance> instIt = instances.iterator(); instIt.hasNext(); ) {
        CalendarItem.Instance inst = instIt.next();
        InviteInfo invId = inst.getInviteInfo();
        Invite inv = calItem.getInvite(invId.getMsgId(), invId.getComponentId());
        Element rssItem = channel.addElement("item");
        rssItem.addElement("title").setText(inv.getName());
        rssItem.addElement("pubDate").setText(mDateFormat.format(new Date(inst.getStart())));
        /*
            StringBuffer desc = new StringBuffer();
            sb.append("Start: ").append(sdf.format(new Date(inst.getStart()))).append("\n");
            sb.append("End: ").append(sdf.format(new Date(inst.getEnd()))).append("\n");
            sb.append("Location: ").append(inv.getLocation()).append("\n");
            sb.append("Notes: ").append(inv.getFragment()).append("\n");
            item.addElement("description").setText(sb.toString());
            */
        rssItem.addElement("description").setText(inv.getFragment());
        if (inv.hasOrganizer())
            rssItem.addElement("author").setText(inv.getOrganizer().getAddress());
    }
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) InviteInfo(com.zimbra.cs.mailbox.calendar.InviteInfo) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) Element(com.zimbra.common.soap.Element) Invite(com.zimbra.cs.mailbox.calendar.Invite) Date(java.util.Date)

Example 50 with CalendarItem

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

the class CalDavDataImport method createTargetUrl.

private String createTargetUrl(MailItem mitem) throws ServiceException {
    DataSourceItem folder = DbDataSource.getMapping(getDataSource(), mitem.getFolderId());
    String url = folder.remoteId;
    switch(mitem.getType()) {
        case APPOINTMENT:
            url += ((CalendarItem) mitem).getUid() + ".ics";
            break;
        default:
            String name = mitem.getName();
            if (name != null)
                url += name;
            else
                url += mitem.getSubject();
            break;
    }
    return url;
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem)

Aggregations

CalendarItem (com.zimbra.cs.mailbox.CalendarItem)56 Mailbox (com.zimbra.cs.mailbox.Mailbox)36 Element (com.zimbra.common.soap.Element)27 OperationContext (com.zimbra.cs.mailbox.OperationContext)24 Invite (com.zimbra.cs.mailbox.calendar.Invite)23 ItemId (com.zimbra.cs.service.util.ItemId)22 Account (com.zimbra.cs.account.Account)19 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)17 ServiceException (com.zimbra.common.service.ServiceException)15 MailItem (com.zimbra.cs.mailbox.MailItem)14 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)13 Message (com.zimbra.cs.mailbox.Message)12 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)11 Folder (com.zimbra.cs.mailbox.Folder)9 RecurId (com.zimbra.cs.mailbox.calendar.RecurId)9 Appointment (com.zimbra.cs.mailbox.Appointment)8 ArrayList (java.util.ArrayList)8 MimeMessage (javax.mail.internet.MimeMessage)8 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)7 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)5