Search in sources :

Example 16 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class Invite method getDescription.

/**
     * Returns the meeting notes.  Meeting notes is the text/plain part in an
     * invite.  It typically includes CUA-generated meeting summary as well as
     * text entered by the user.
     *
     * @return null if notes is not found
     * @throws ServiceException
     */
public static String getDescription(Part mmInv, String mimeType) throws ServiceException {
    if (mmInv == null)
        return null;
    try {
        // If top-level is text/calendar, parse the iCalendar object and return
        // the DESCRIPTION of the first VEVENT/VTODO encountered.
        String mmCtStr = mmInv.getContentType();
        if (mmCtStr != null) {
            ContentType mmCt = new ContentType(mmCtStr);
            if (mmCt.match(MimeConstants.CT_TEXT_CALENDAR)) {
                boolean wantHtml = MimeConstants.CT_TEXT_HTML.equalsIgnoreCase(mimeType);
                Object mmInvContent = mmInv.getContent();
                InputStream is = null;
                try {
                    String charset = MimeConstants.P_CHARSET_UTF8;
                    if (mmInvContent instanceof InputStream) {
                        charset = mmCt.getParameter(MimeConstants.P_CHARSET);
                        if (charset == null)
                            charset = MimeConstants.P_CHARSET_UTF8;
                        is = (InputStream) mmInvContent;
                    } else if (mmInvContent instanceof String) {
                        String str = (String) mmInvContent;
                        charset = MimeConstants.P_CHARSET_UTF8;
                        is = new ByteArrayInputStream(str.getBytes(charset));
                    }
                    if (is != null) {
                        ZVCalendar iCal = ZCalendarBuilder.build(is, charset);
                        for (Iterator<ZComponent> compIter = iCal.getComponentIterator(); compIter.hasNext(); ) {
                            ZComponent component = compIter.next();
                            ICalTok compTypeTok = component.getTok();
                            if (compTypeTok == ICalTok.VEVENT || compTypeTok == ICalTok.VTODO) {
                                if (!wantHtml)
                                    return component.getPropVal(ICalTok.DESCRIPTION, null);
                                else
                                    return component.getDescriptionHtml();
                            }
                        }
                    }
                } finally {
                    ByteUtil.closeStream(is);
                }
            }
        }
        Object mmInvContent = mmInv.getContent();
        if (!(mmInvContent instanceof MimeMultipart)) {
            if (mmInvContent instanceof InputStream) {
                ByteUtil.closeStream((InputStream) mmInvContent);
            }
            return null;
        }
        MimeMultipart mm = (MimeMultipart) mmInvContent;
        // If top-level is multipart, get description from text/* part.
        int numParts = mm.getCount();
        String charset = null;
        for (int i = 0; i < numParts; i++) {
            BodyPart part = mm.getBodyPart(i);
            String ctStr = part.getContentType();
            try {
                ContentType ct = new ContentType(ctStr);
                if (ct.match(mimeType)) {
                    charset = ct.getParameter(MimeConstants.P_CHARSET);
                    if (charset == null)
                        charset = MimeConstants.P_CHARSET_DEFAULT;
                    byte[] descBytes = ByteUtil.getContent(part.getInputStream(), part.getSize());
                    return new String(descBytes, charset);
                }
                // If part is a multipart, recurse.
                if (ct.getBaseType().matches(MimeConstants.CT_MULTIPART_WILD)) {
                    String str = getDescription(part, mimeType);
                    if (str != null) {
                        return str;
                    }
                }
            } catch (javax.mail.internet.ParseException e) {
                ZimbraLog.calendar.warn("Invalid Content-Type found: \"" + ctStr + "\"; skipping part", e);
            }
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
    }
    return null;
}
Also used : BodyPart(javax.mail.BodyPart) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart)

Example 17 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class RemoteFreeBusyProvider method getResults.

@Override
public List<FreeBusy> getResults() {
    ArrayList<FreeBusy> fbList = new ArrayList<FreeBusy>();
    for (Request req : mRequestList) {
        HttpMethod method = null;
        Account acct = (Account) req.data;
        try {
            StringBuilder targetUrl = new StringBuilder();
            targetUrl.append(UserServlet.getRestUrl(acct));
            targetUrl.append("/Calendar?fmt=ifb");
            targetUrl.append("&start=").append(mStart);
            targetUrl.append("&end=").append(mEnd);
            if (req.folder != FreeBusyQuery.CALENDAR_FOLDER_ALL)
                targetUrl.append("&").append(UserServlet.QP_FREEBUSY_CALENDAR).append("=").append(req.folder);
            try {
                if (mExApptUid != null)
                    targetUrl.append("&").append(UserServlet.QP_EXUID).append("=").append(URLEncoder.encode(mExApptUid, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
            }
            String authToken = null;
            try {
                if (mSoapCtxt != null)
                    authToken = mSoapCtxt.getAuthToken().getEncoded();
            } catch (AuthTokenException e) {
            }
            if (authToken != null) {
                targetUrl.append("&").append(ZimbraServlet.QP_ZAUTHTOKEN).append("=");
                try {
                    targetUrl.append(URLEncoder.encode(authToken, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                }
            }
            HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
            HttpProxyUtil.configureProxy(client);
            method = new GetMethod(targetUrl.toString());
            String fbMsg;
            try {
                HttpClientUtil.executeMethod(client, method);
                byte[] buf = ByteUtil.getContent(method.getResponseBodyAsStream(), 0);
                fbMsg = new String(buf, "UTF-8");
            } catch (IOException ex) {
                // ignore this recipient and go on
                fbMsg = null;
            }
            if (fbMsg != null) {
                ZVCalendar cal = ZCalendarBuilder.build(fbMsg);
                for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
                    ZComponent comp = compIter.next();
                    if (ICalTok.VFREEBUSY.equals(comp.getTok())) {
                        FreeBusy fb = FreeBusy.parse(comp);
                        fbList.add(fb);
                    }
                }
            }
        } catch (ServiceException e) {
            ZimbraLog.fb.warn("can't get free/busy information for " + req.email, e);
        } finally {
            if (method != null)
                method.releaseConnection();
        }
    }
    return fbList;
}
Also used : Account(com.zimbra.cs.account.Account) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ServiceException(com.zimbra.common.service.ServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 18 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class CalendarMailSender method createResourceAutoReply.

public static MimeMessage createResourceAutoReply(OperationContext octxt, String fromIdentityId, String authIdentityId, Mailbox mbox, Verb verb, boolean partialAccept, String additionalMsgBody, CalendarItem calItem, Invite inv, Invite[] replies, MimeMessage mmInv, boolean addSignature) throws ServiceException {
    boolean onBehalfOf = false;
    Account acct = mbox.getAccount();
    Account authAcct = acct;
    if (octxt != null) {
        Account authuser = octxt.getAuthenticatedUser();
        if (authuser != null) {
            onBehalfOf = !acct.getId().equalsIgnoreCase(authuser.getId());
            if (onBehalfOf)
                authAcct = authuser;
        }
    }
    Locale lc;
    Account organizer = inv.getOrganizerAccount();
    if (organizer != null)
        lc = organizer.getLocale();
    else
        lc = authAcct.getLocale();
    boolean asAdmin = octxt != null ? octxt.isUsingAdminPrivileges() : false;
    boolean allowPrivateAccess = calItem.allowPrivateAccess(authAcct, asAdmin);
    boolean hidePrivate = !inv.isPublic() && !allowPrivateAccess;
    String subject;
    if (hidePrivate)
        subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, lc);
    else
        subject = inv.getName();
    String replySubject = getReplySubject(verb, subject, lc);
    // Put all REPLY VEVENTs into a single VCALENDAR object.
    ZVCalendar iCal = null;
    for (Invite replyInv : replies) {
        if (iCal == null) {
            iCal = replyInv.newToICalendar(!hidePrivate);
        } else {
            ZComponent cancelComp = replyInv.newToVComponent(true, !hidePrivate);
            iCal.addComponent(cancelComp);
        }
    }
    return createDefaultReply(acct, fromIdentityId, authAcct, authIdentityId, asAdmin, onBehalfOf, calItem, inv, mmInv, replySubject, verb, partialAccept, additionalMsgBody, iCal, addSignature);
}
Also used : Locale(java.util.Locale) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) Account(com.zimbra.cs.account.Account) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar)

Example 19 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class CalendarMailSender method createOrganizerChangeMessage.

public static MimeMessage createOrganizerChangeMessage(Account fromAccount, Account authAccount, boolean asAdmin, CalendarItem calItem, Invite inv, List<Address> rcpts) throws ServiceException {
    ZOrganizer organizer = inv.getOrganizer();
    assert (organizer != null);
    boolean onBehalfOf = organizer.hasSentBy();
    String senderAddr = onBehalfOf ? organizer.getSentBy() : organizer.getAddress();
    Locale locale = fromAccount.getLocale();
    boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAccount, asAdmin);
    String subject;
    if (hidePrivate) {
        subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, locale);
    } else {
        subject = inv.getName();
    }
    StringBuilder sb = new StringBuilder("Organizer has been changed to " + fromAccount.getName());
    sb.append("\r\n\r\n");
    if (!hidePrivate) {
        MimeMessage mmInv = inv.getMimeMessage();
        if (mmInv != null) {
            attachInviteSummary(sb, inv, mmInv, locale);
        }
    }
    ZVCalendar iCal = inv.newToICalendar(true);
    Address from = AccountUtil.getFriendlyEmailAddress(fromAccount);
    Address sender = null;
    if (onBehalfOf) {
        try {
            sender = new JavaMailInternetAddress(senderAddr);
        } catch (AddressException e) {
            throw MailServiceException.ADDRESS_PARSE_ERROR(e);
        }
    }
    return createCalendarMessage(authAccount, from, sender, rcpts, subject, sb.toString(), null, inv.getUid(), iCal);
}
Also used : Locale(java.util.Locale) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 20 with ZVCalendar

use of com.zimbra.common.calendar.ZCalendar.ZVCalendar in project zm-mailbox by Zimbra.

the class ForwardCalendarItem method forwardCalItem.

public static Pair<List<MimeMessage>, List<MimeMessage>> forwardCalItem(Mailbox mbox, OperationContext octxt, CalendarItem calItem, RecurId rid, MimeMessage mm, Account senderAcct) throws ServiceException {
    List<MimeMessage> fwdMsgs = new ArrayList<MimeMessage>();
    List<MimeMessage> notifyMsgs = new ArrayList<MimeMessage>();
    Pair<List<MimeMessage>, List<MimeMessage>> pair = new Pair<List<MimeMessage>, List<MimeMessage>>(fwdMsgs, notifyMsgs);
    mbox.lock.lock();
    try {
        if (rid == null) {
            // Forwarding entire appointment
            pair = getSeriesFwdMsgs(octxt, senderAcct, calItem, mm);
        } else {
            // Forwarding an instance
            Invite inv = calItem.getInvite(rid);
            MimeMessage mmInv = null;
            if (inv != null) {
                mmInv = calItem.getSubpartMessage(inv.getMailItemId());
            } else {
                assert (rid != null);
                // No invite found matching the RECURRENCE-ID.  It must be a non-exception instance.
                // Create an invite based on the series invite.
                Invite seriesInv = calItem.getDefaultInviteOrNull();
                if (seriesInv == null)
                    throw ServiceException.INVALID_REQUEST("Instance specified but no recurrence series found", null);
                Invite exceptInv = seriesInv.newCopy();
                exceptInv.clearAlarms();
                exceptInv.setRecurrence(null);
                exceptInv.setRecurId(rid);
                long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
                exceptInv.setDtStamp(now);
                ParsedDateTime dtStart = rid.getDt();
                ParsedDateTime dtEnd = dtStart.add(exceptInv.getEffectiveDuration());
                exceptInv.setDtStart(dtStart);
                exceptInv.setDtEnd(dtEnd);
                inv = exceptInv;
                // Carry over the MimeMessage/ParsedMessage to preserve any attachments.
                mmInv = calItem.getSubpartMessage(seriesInv.getMailItemId());
            }
            ZVCalendar cal = inv.newToICalendar(true);
            Pair<MimeMessage, MimeMessage> instancePair = getInstanceFwdMsg(senderAcct, inv, cal, mmInv, mm);
            if (instancePair.getFirst() != null) {
                fwdMsgs.add(instancePair.getFirst());
            }
            if (instancePair.getSecond() != null) {
                notifyMsgs.add(instancePair.getSecond());
            }
        }
    } finally {
        mbox.lock.release();
    }
    return pair;
}
Also used : ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) Invite(com.zimbra.cs.mailbox.calendar.Invite) Pair(com.zimbra.common.util.Pair)

Aggregations

ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)47 Invite (com.zimbra.cs.mailbox.calendar.Invite)21 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)16 Account (com.zimbra.cs.account.Account)12 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)10 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)10 IOException (java.io.IOException)10 MimeMessage (javax.mail.internet.MimeMessage)10 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)9 ItemId (com.zimbra.cs.service.util.ItemId)9 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)8 ServiceException (com.zimbra.common.service.ServiceException)7 Mailbox (com.zimbra.cs.mailbox.Mailbox)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Locale (java.util.Locale)7 Test (org.junit.Test)7 Element (com.zimbra.common.soap.Element)6 Date (java.util.Date)6