Search in sources :

Example 1 with FriendlyCalendaringDescription

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

the class ScheduleOutbox method handleEventRequest.

private void handleEventRequest(DavContext ctxt, ZCalendar.ZVCalendar cal, ZComponent req, DelegationInfo delegationInfo, String rcpt, Element resp) throws ServiceException, DavException {
    if (!DavResource.isSchedulingEnabled()) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No scheduling for the user");
        return;
    }
    ArrayList<Address> recipients = new java.util.ArrayList<Address>();
    InternetAddress from, sender, to;
    Account target = null;
    try {
        sender = new JavaMailInternetAddress(delegationInfo.getOriginatorEmail());
        Provisioning prov = Provisioning.getInstance();
        if (ctxt.getActingAsDelegateFor() != null) {
            target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
        }
        if (target != null) {
            from = AccountUtil.getFriendlyEmailAddress(target);
        } else {
            if (delegationInfo.getOwnerEmail() != null) {
                from = new JavaMailInternetAddress(delegationInfo.getOwnerEmail());
            } else {
                target = getMailbox(ctxt).getAccount();
                if (AccountUtil.addressMatchesAccount(target, delegationInfo.getOriginatorEmail())) {
                    // Make sure we don't use two different aliases for From and Sender.
                    // This is a concern with Apple iCal, which picks a random alias as originator.
                    from = sender;
                } else {
                    from = AccountUtil.getFriendlyEmailAddress(target);
                }
            }
        }
        if (sender.getAddress() != null && sender.getAddress().equalsIgnoreCase(from.getAddress())) {
            sender = null;
        }
        to = new JavaMailInternetAddress(CalDavUtils.stripMailto(rcpt));
        recipients.add(to);
    } catch (AddressException e) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.7;" + rcpt);
        return;
    }
    String status = req.getPropVal(ICalTok.STATUS, "");
    String method = cal.getPropVal(ICalTok.METHOD, "REQUEST");
    String subject = "";
    if (method.equals("REQUEST")) {
        ZProperty organizerProp = req.getProperty(ICalTok.ORGANIZER);
        if (organizerProp != null) {
            String organizerStr = this.getAddressFromPrincipalURL(new ZOrganizer(organizerProp).getAddress());
            if (!AccountUtil.addressMatchesAccount(getMailbox(ctxt).getAccount(), organizerStr)) {
                ZimbraLog.dav.debug("scheduling appointment on behalf of %s", organizerStr);
            }
        }
    } else if (method.equals("REPLY")) {
        ZProperty attendeeProp = req.getProperty(ICalTok.ATTENDEE);
        if (attendeeProp == null)
            throw new DavException("missing property ATTENDEE", HttpServletResponse.SC_BAD_REQUEST);
        ZAttendee attendee = new ZAttendee(attendeeProp);
        String partStat = attendee.getPartStat();
        if (partStat.equals(IcalXmlStrMap.PARTSTAT_ACCEPTED)) {
            subject = "Accept: ";
        } else if (partStat.equals(IcalXmlStrMap.PARTSTAT_TENTATIVE)) {
            subject = "Tentative: ";
        } else if (partStat.equals(IcalXmlStrMap.PARTSTAT_DECLINED)) {
            subject = "Decline: ";
        }
    }
    if (status.equals("CANCELLED"))
        subject = "Cancelled: ";
    subject += req.getPropVal(ICalTok.SUMMARY, "");
    String uid = req.getPropVal(ICalTok.UID, null);
    if (uid == null) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.1;UID");
        return;
    }
    try {
        List<Invite> components = Invite.createFromCalendar(ctxt.getAuthAccount(), null, cal, false);
        FriendlyCalendaringDescription friendlyDesc = new FriendlyCalendaringDescription(components, ctxt.getAuthAccount());
        String desc = friendlyDesc.getAsPlainText();
        String descHtml = req.getDescriptionHtml();
        if ((descHtml == null) || (descHtml.length() == 0))
            descHtml = friendlyDesc.getAsHtml();
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
        MimeMessage mm = CalendarMailSender.createCalendarMessage(target, from, sender, recipients, subject, desc, descHtml, uid, cal);
        mbox.getMailSender().setSendPartial(true).sendMimeMessage(ctxt.getOperationContext(), mbox, true, mm, null, null, null, null, false);
    } catch (ServiceException e) {
        resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
        resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.1");
        return;
    }
    resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
    resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
}
Also used : Account(com.zimbra.cs.account.Account) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) DavException(com.zimbra.cs.dav.DavException) ArrayList(java.util.ArrayList) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) Provisioning(com.zimbra.cs.account.Provisioning) FriendlyCalendaringDescription(com.zimbra.cs.mailbox.calendar.FriendlyCalendaringDescription) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ZAttendee(com.zimbra.cs.mailbox.calendar.ZAttendee) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Aggregations

ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)1 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)1 ServiceException (com.zimbra.common.service.ServiceException)1 Account (com.zimbra.cs.account.Account)1 Provisioning (com.zimbra.cs.account.Provisioning)1 DavException (com.zimbra.cs.dav.DavException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 FriendlyCalendaringDescription (com.zimbra.cs.mailbox.calendar.FriendlyCalendaringDescription)1 Invite (com.zimbra.cs.mailbox.calendar.Invite)1 ZAttendee (com.zimbra.cs.mailbox.calendar.ZAttendee)1 ZOrganizer (com.zimbra.cs.mailbox.calendar.ZOrganizer)1 ArrayList (java.util.ArrayList)1 Address (javax.mail.Address)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1