Ejemplo n.º 1
0
        /// <summary>
        /// Generates a response from a GCalRequest
        /// </summary>
        /// <returns></returns>
        public string GenerateResponse()
        {
            /* Create a string builder to hold the text for the response */
            StringBuilder result = new StringBuilder(4096);

            /* Create an exchange provider */
            ExchangeService gateway = new ExchangeService(
                ConfigCache.ExchangeServerUrl,
                ConfigCache.ExchangeUserLogin,
                ConfigCache.ExchangeUserPassword);

            /* Return the exchangers from the GCal Request that was passed in */
            DateTimeRange    range         = new DateTimeRange(request.UTCStartDate, request.UTCEndDate);
            ExchangeUserDict exchangeUsers = gateway.SearchByEmail(range, request.ExchangeUsers);

            /* Create the header of the request */
            result.AppendFormat("['{0}','{1}',", request.VersionNumber, request.MessageId);

            result.AppendFormat("['_ME_AddData','{0}/{1}','{2}'",
                                DateUtil.FormatDateForGoogle(request.StartDate),
                                DateUtil.FormatDateForGoogle(request.EndDate),
                                DateUtil.FormatDateTimeForGoogle(request.Since));

            /* Flag for inserting commas */
            bool firstUser = true;

            result.Append(",[");

            foreach (ExchangeUser user in exchangeUsers.Values)
            {
                /* Don't add a comma if this is the first user */
                if (!firstUser)
                {
                    result.Append(",");
                }

                /* Add the user's credentials */
                string email = ConfigCache.MapToExternalDomain(user.Email);
                result.AppendFormat("'{0}','{1}','{2}',[",
                                    user.DisplayName,
                                    email,
                                    (int)user.AccessLevel);

                GenerateResponseForTimeBlocks(user,
                                              result);

                result.Append("]");
                firstUser = false;
            }

            result.Append("]");
            result.Append("]");
            result.Append("]");

            log.Info("GCal Free/Busy response successfully generated.");
            log.DebugFormat("Response = {0}", result);

            return(result.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Query Google Calendar for a user and return the event feed
        /// </summary>
        /// <param name="email">Email address of the user to query</param>
        /// <param name="visibility">Feed Visibility (Public/Private) to query for</param>
        /// <param name="projection">Feed projection - type of feed to get</param>
        /// <param name="modifiedSince">Last modified time from last check</param>
        /// <param name="window">DateTime range to query between</param>
        /// <returns>An event feed for the user</returns>
        public EventFeed QueryGCal(
            string email,
            GCalVisibility visibility,
            GCalProjection projection,
            DateTime modifiedSince,
            DateTimeRange window)
        {
            // Perform mapping on the username if necessary
            string user = ConfigCache.MapToExternalDomain(email);

            if (log.IsDebugEnabled)
            {
                log.InfoFormat(
                    "FeedQuery with parameters: {0}, {1}, {2}, {3} [{4}]",
                    user,
                    visibility,
                    projection,
                    modifiedSince,
                    window);
            }

            StringBuilder sb = new StringBuilder(ConfigCache.GCalAddress);

            if (!ConfigCache.GCalAddress.EndsWith("/"))
            {
                sb.Append("/");
            }

            sb.AppendFormat("feeds/{0}/", user);

            switch (visibility)
            {
            case GCalVisibility.Public:
                sb.Append("public/");
                break;

            case GCalVisibility.Private:
            default:
                sb.Append("private/");
                break;
            }

            switch (projection)
            {
            case GCalProjection.Full:
                sb.Append("full");
                break;

            case GCalProjection.FullNoAttendees:
                sb.Append("full-noattendees");
                break;

            case GCalProjection.Composite:
                sb.Append("composite");
                break;

            case GCalProjection.AttendeesOnly:
                sb.Append("attendees-only");
                break;

            case GCalProjection.FreeBusy:
                sb.Append("free-busy");
                break;

            case GCalProjection.Basic:
            default:
                sb.Append("basic");
                break;
            }

            EventQuery query = new EventQuery(sb.ToString());

            if (projection != GCalProjection.FreeBusy)
            {
                query.SingleEvents = true;
            }

            GDataRequestFactory f = (GDataRequestFactory)service.RequestFactory;

            f.UseGZip = ConfigCache.EnableHttpCompression;

            if (window.Start != DateTime.MinValue)
            {
                query.StartTime = window.Start;
            }

            if (window.End != DateTime.MaxValue)
            {
                query.EndTime = window.End;
            }

            query.NumberToRetrieve = int.MaxValue; // Make sure we get everything

            try
            {
                return(QueryGCal(query, user, modifiedSince));
            }
            catch (System.IO.IOException e)
            {
                // Known problem with .NET 2.0 - Sometimes keep-alive connection is
                // closed by a proxy and we need to re-attemp the connection
                //
                // http://code.google.com/p/google-gdata/wiki/KeepAliveAndUnderlyingConnectionIsClosed

                if (e.InnerException.GetType().ToString().Equals("System.Net.Sockets.SocketException"))
                {
                    log.Info(String.Format("Attempt Retry Query after keep-alive termination"));
                    // One shot retry i case the keep-alive was closed
                    return(QueryGCal(query, user, modifiedSince));
                }
                else
                {
                    throw;
                }
            }
        }