PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/connectors/cs/src/core/util/FreeBusyUrl.cs

http://google-calendar-connectors.googlecode.com/
C# | 129 lines | 65 code | 11 blank | 53 comment | 0 complexity | 4e79ede240e200089a7ce6fe14f77f18 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /* Copyright (c) 2008 Google Inc. All Rights Reserved
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using System.Web;
  19. namespace Google.GCalExchangeSync.Library.Util
  20. {
  21. /// <summary>
  22. /// Tools for Generating Free Busy URLs for Exchange
  23. /// </summary>
  24. public class FreeBusyUrl
  25. {
  26. /// <summary>
  27. /// Generate a URL for the users free busy appointments
  28. /// </summary>
  29. /// <param name="exchangeServerUrl">The Exchange Server to use</param>
  30. /// <param name="adminGroup">The group the user is in</param>
  31. /// <param name="userAlias">Alias for the user</param>
  32. /// <returns>The URL for the users appointments</returns>
  33. public static string GenerateUrl( string exchangeServerUrl, string adminGroup, string userAlias )
  34. {
  35. return string.Format(
  36. "{0}/public/NON_IPM_SUBTREE/SCHEDULE%2B%20FREE%20BUSY/EX:{1}/USER-{2}.EML",
  37. exchangeServerUrl,
  38. ExchangeEncode(adminGroup),
  39. ExchangeEncode(string.Format("/cn=RECIPIENTS/cn={0}", userAlias)));
  40. }
  41. /// <summary>
  42. /// Generate a Free Busy URL from the legacy Exchange distinguished name - legacy
  43. /// exchange names are of the form:
  44. /// /o={ORG}/ou={ORG-UNIT}/cn={CATEGORY}/cn={USERS CN}
  45. /// </summary>
  46. /// <param name="exchangeServerUrl">The exchange server to use</param>
  47. /// <param name="legacyExchangeDN">The legacy exchange distingished name</param>
  48. /// <returns>The URL for the users appointments</returns>
  49. public static string GenerateUrlFromDN( string exchangeServerUrl, string legacyExchangeDN )
  50. {
  51. string adminGroup =
  52. legacyExchangeDN.Substring( 0, legacyExchangeDN.IndexOf( "/cn" ) );
  53. string userAlias =
  54. legacyExchangeDN.Substring( legacyExchangeDN.IndexOf( "/cn" ) );
  55. return string.Format(
  56. "{0}/public/NON_IPM_SUBTREE/SCHEDULE%2B%20FREE%20BUSY/EX:{1}/USER-{2}.EML",
  57. exchangeServerUrl,
  58. ExchangeEncode( adminGroup ),
  59. ExchangeEncode( userAlias ) );
  60. }
  61. /// <summary>
  62. /// Generate a URL for the appointments within an Admin group
  63. /// </summary>
  64. /// <param name="exchangeServerUrl">The exchange server to use</param>
  65. /// <param name="adminGroup">The admin group</param>
  66. /// <returns>The URL to use to get the appointments for an admin group</returns>
  67. public static string GenerateAdminGroupUrl( string exchangeServerUrl, string adminGroup )
  68. {
  69. return string.Format(
  70. "{0}/public/NON_IPM_SUBTREE/SCHEDULE%2B%20FREE%20BUSY/EX:{1}/",
  71. exchangeServerUrl,
  72. ExchangeEncode( adminGroup ) );
  73. }
  74. /// <summary>
  75. /// Generate a URL for the appointments within an Admin group
  76. /// </summary>
  77. /// <param name="exchangeServerUrl">The exchange server to use</param>
  78. /// <param name="legacyExchangeDN">Get the legacy exchange DN for the group</param>
  79. /// <returns>The URL to use to get the appointments for an admin group</returns>
  80. public static string GenerateAdminGroupUrlFromDN(string exchangeServerUrl, string legacyExchangeDN)
  81. {
  82. string adminGroup =
  83. legacyExchangeDN.Substring( 0, legacyExchangeDN.IndexOf( "/cn" ) );
  84. return GenerateAdminGroupUrl( exchangeServerUrl, adminGroup );
  85. }
  86. /// <summary>
  87. /// Generate a URL for the new Free Busy Lookup format which is a replacement
  88. /// for public folders - documented here: http://support.microsoft.com/kb/813268
  89. /// </summary>
  90. /// <param name="exchangeServer">The exchange server to use</param>
  91. /// <param name="users">The set of users to lookup</param>
  92. /// <param name="range">The datetime range to lookup for</param>
  93. /// <param name="interval">The time interval to use</param>
  94. /// <returns>The URL to obtain FB info for the set of users</returns>
  95. public static string GenerateFreeBusyLookupUrl(
  96. string exchangeServer,
  97. ExchangeUserDict users,
  98. DateTimeRange range,
  99. int interval)
  100. {
  101. StringBuilder result = new StringBuilder(256);
  102. result.AppendFormat("{0}/public/?cmd=freebusy&start={1}&end={2}&interval={3}",
  103. exchangeServer,
  104. DateUtil.FormatDateForISO8601(range.Start),
  105. DateUtil.FormatDateForISO8601(range.End),
  106. interval);
  107. foreach (ExchangeUser user in users.Values)
  108. {
  109. result.AppendFormat("&u={0}", user.Email);
  110. }
  111. return result.ToString();
  112. }
  113. private static string ExchangeEncode( string element )
  114. {
  115. return HttpUtility.UrlPathEncode( element.Replace( @"/", "_xF8FF_" ) );
  116. }
  117. }
  118. }