Search in sources :

Example 16 with ZComponent

use of com.zimbra.common.calendar.ZCalendar.ZComponent 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 17 with ZComponent

use of com.zimbra.common.calendar.ZCalendar.ZComponent 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 18 with ZComponent

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

the class CalendarMailSender method createForwardedPrivateInviteMessage.

public static MimeMessage createForwardedPrivateInviteMessage(Account account, Locale lc, String method, List<Invite> invites, String origSenderEmail, String forwarderEmail, String[] forwardTo) throws ServiceException {
    if (invites == null || invites.isEmpty())
        return null;
    List<Address> rcpts = new ArrayList<Address>();
    for (String to : forwardTo) {
        try {
            rcpts.add(new JavaMailInternetAddress(to));
        } catch (AddressException e) {
            ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
        }
    }
    if (rcpts.isEmpty())
        return null;
    String subject = L10nUtil.getMessage(MsgKey.calendarSubjectWithheld, lc);
    // Create filtered version of invites.
    List<Invite> filteredInvs = new ArrayList<Invite>();
    for (Invite inv : invites) {
        Invite filtered = inv.newCopy();
        filtered.clearAlarms();
        filtered.clearPrivateInfo();
        filtered.setName(subject);
        // Add ATTENDEE for forwarder.
        List<ZAttendee> atts = inv.getAttendees();
        if (atts != null && forwarderEmail != null) {
            for (ZAttendee att : atts) {
                if (forwarderEmail.equalsIgnoreCase(att.getAddress())) {
                    filtered.addAttendee(att);
                }
            }
        }
        filteredInvs.add(filtered);
    }
    MimeMessage mm = null;
    try {
        mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
        mm.setFrom(new JavaMailInternetAddress(origSenderEmail));
        mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
        // Set special header to indicate the forwarding attendee.
        mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
        mm.setSubject(subject);
        StringWriter writer = new StringWriter();
        try {
            writer.write("BEGIN:VCALENDAR\r\n");
            ZProperty prop;
            prop = new ZProperty(ICalTok.PRODID, ZCalendar.sZimbraProdID);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.VERSION, ZCalendar.sIcalVersion);
            prop.toICalendar(writer);
            prop = new ZProperty(ICalTok.METHOD, method);
            prop.toICalendar(writer);
            // timezones
            Invite firstInv = filteredInvs.get(0);
            TimeZoneMap tzmap = new TimeZoneMap(firstInv.getTimeZoneMap().getLocalTimeZone());
            for (Invite inv : filteredInvs) {
                tzmap.add(inv.getTimeZoneMap());
            }
            for (Iterator<ICalTimeZone> iter = tzmap.tzIterator(); iter.hasNext(); ) {
                ICalTimeZone tz = iter.next();
                tz.newToVTimeZone().toICalendar(writer);
            }
            // VEVENTs/VTODOs
            for (Invite inv : filteredInvs) {
                ZComponent comp = inv.newToVComponent(false, true);
                comp.toICalendar(writer);
            }
            writer.write("END:VCALENDAR\r\n");
        } catch (IOException e) {
            throw ServiceException.FAILURE("Error writing iCalendar", e);
        } finally {
            Closeables.closeQuietly(writer);
        }
        mm.setText(writer.toString());
        ContentType ct = new ContentType(MimeConstants.CT_TEXT_CALENDAR);
        ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
        ct.setParameter("method", method);
        mm.setHeader("Content-Type", ct.toString());
    } catch (MessagingException e) {
        ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
    }
    return mm;
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mime(com.zimbra.cs.mime.Mime) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) StringWriter(java.io.StringWriter) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 19 with ZComponent

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

the class ForwardCalendarItem method setDescProps.

private static void setDescProps(ZVCalendar cal, String descPlain, String descHtml) {
    for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
        ZComponent comp = compIter.next();
        ICalTok compName = ICalTok.lookup(comp.getName());
        if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
            // Remove existing DESCRIPTION and X-ALT-DESC properties.
            for (Iterator<ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext(); ) {
                ZProperty prop = propIter.next();
                ICalTok tok = prop.getToken();
                if (ICalTok.DESCRIPTION.equals(tok) || ICalTok.X_ALT_DESC.equals(tok))
                    propIter.remove();
            }
            if (descPlain != null && descPlain.length() > 0) {
                comp.addProperty(new ZProperty(ICalTok.DESCRIPTION, descPlain));
            }
            if (descHtml != null && descHtml.length() > 0) {
                ZProperty prop = new ZProperty(ICalTok.X_ALT_DESC, descHtml);
                prop.addParameter(new ZParameter(ICalTok.FMTTYPE, MimeConstants.CT_TEXT_HTML));
                comp.addProperty(prop);
            }
            // only update the first component (comps are ordered correctly)
            break;
        }
    }
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ZParameter(com.zimbra.common.calendar.ZCalendar.ZParameter) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok)

Example 20 with ZComponent

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

the class ForwardCalendarItem method setSentByAndAttendees.

private static void setSentByAndAttendees(ZVCalendar cal, String sentBy, Address[] rcpts) throws ServiceException {
    // Set SENT-BY to sender's email address in ORGANIZER property of all VEVENT/VTODO components.
    // Required by Outlook.
    // Also, remove existing ATTENDEEs and add ATTENDEE lines for forwardees.
    String sentByAddr = "mailto:" + sentBy;
    for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
        ZComponent comp = compIter.next();
        ICalTok compName = ICalTok.lookup(comp.getName());
        if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
            // Remove existing ATTENDEEs and X-MS-OLK-SENDER.
            for (Iterator<ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext(); ) {
                ZProperty prop = propIter.next();
                if (ICalTok.ATTENDEE.equals(prop.getToken()) || "X-MS-OLK-SENDER".equalsIgnoreCase(prop.getName()))
                    propIter.remove();
            }
            // SENT-BY
            ZProperty orgProp = comp.getProperty(ICalTok.ORGANIZER);
            if (orgProp != null) {
                ZParameter sentByParam = orgProp.getParameter(ICalTok.SENT_BY);
                if (sentByParam != null) {
                    sentByParam.setValue(sentByAddr);
                } else {
                    sentByParam = new ZParameter(ICalTok.SENT_BY, sentByAddr);
                    orgProp.addParameter(sentByParam);
                }
                // Set X-MS-OLK-SENDER, another Outlook special.
                ZProperty xMsOlkSender = new ZProperty("X-MS-OLK-SENDER");
                xMsOlkSender.setValue(sentByAddr);
                comp.addProperty(xMsOlkSender);
            }
            // ATTENDEEs
            if (rcpts == null)
                throw ServiceException.INVALID_REQUEST("Missing forwardees", null);
            for (Address r : rcpts) {
                InternetAddress rcpt = (InternetAddress) r;
                String email = "mailto:" + rcpt.getAddress();
                ZProperty att = new ZProperty(ICalTok.ATTENDEE, email);
                String name = rcpt.getPersonal();
                if (name != null && name.length() > 0)
                    att.addParameter(new ZParameter(ICalTok.CN, name));
                att.addParameter(new ZParameter(ICalTok.PARTSTAT, ICalTok.NEEDS_ACTION.toString()));
                att.addParameter(new ZParameter(ICalTok.RSVP, "TRUE"));
                comp.addProperty(att);
            }
        }
    }
}
Also used : ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) 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) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ZParameter(com.zimbra.common.calendar.ZCalendar.ZParameter) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok)

Aggregations

ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)35 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)18 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)16 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)14 ArrayList (java.util.ArrayList)10 ICalTok (com.zimbra.common.calendar.ZCalendar.ICalTok)9 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)7 ZParameter (com.zimbra.common.calendar.ZCalendar.ZParameter)7 Account (com.zimbra.cs.account.Account)7 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 IOException (java.io.IOException)7 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.Test)5 ServiceException (com.zimbra.common.service.ServiceException)4 InputStream (java.io.InputStream)4 MessagingException (javax.mail.MessagingException)4 MimeMessage (javax.mail.internet.MimeMessage)4 StringWriter (java.io.StringWriter)3 Address (javax.mail.Address)3