PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.Mem/WindowsPhoneCloud.Web/Handlers/AzureTablesProxyHandler.vb

#
Visual Basic | 106 lines | 74 code | 13 blank | 19 comment | 0 complexity | 6b03dfdc3060ab8e1fcc5d836ba507b6 MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports Microsoft.WindowsAzure
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Serializers
  18. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  19. Imports System.Net
  20. Imports System.Globalization
  21. Namespace Handlers
  22. Public Class AzureTablesProxyHandler
  23. Inherits StorageProxyHandler
  24. Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
  25. #If ACS Then
  26. Public Sub New()
  27. Me.New(New UserTablesServiceContext(), CloudStorageAccount.FromConfigurationSetting("DataConnectionString"), New FormatSerializerFactory())
  28. End Sub
  29. <CLSCompliant(False)>
  30. Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal cloudStorageAccount As CloudStorageAccount, ByVal formatSerializerFactory As IFormatSerializerFactory)
  31. MyBase.New(cloudStorageAccount, formatSerializerFactory)
  32. Me.userPrivilegesRepository = userPrivilegesRepository
  33. End Sub
  34. #Else
  35. Public Sub New()
  36. Me.New(New UserTablesServiceContext(), CloudStorageAccount.FromConfigurationSetting("DataConnectionString"), New TableRequestValidator(), New FormatSerializerFactory())
  37. End Sub
  38. <CLSCompliant(False)>
  39. Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal cloudStorageAccount As CloudStorageAccount, ByVal requestValidator As IStorageRequestValidator, ByVal formatSerializerFactory As IFormatSerializerFactory)
  40. MyBase.New(cloudStorageAccount, requestValidator, formatSerializerFactory)
  41. Me.userPrivilegesRepository = userPrivilegesRepository
  42. End Sub
  43. #End If
  44. Protected Overrides ReadOnly Property AzureStorageUrl() As String
  45. Get
  46. Return Me.CloudStorageAccount.TableEndpoint.ToString().TrimEnd("/"c)
  47. End Get
  48. End Property
  49. Protected Overrides Sub SignRequest(ByVal webRequest As WebRequest)
  50. Dim httpWebRequest = TryCast(webRequest, HttpWebRequest)
  51. If httpWebRequest IsNot Nothing Then
  52. Me.CloudStorageAccount.Credentials.SignRequestLite(httpWebRequest)
  53. End If
  54. End Sub
  55. #If ACS Then
  56. Protected Overrides Sub PostProcessProxyRequest(ByVal context As HttpContext)
  57. Dim tableName = StorageRequestAnalyzer.GetRequestedTable(context.Request)
  58. If (context.Response.StatusCode = CInt(HttpStatusCode.Created)) AndAlso StorageRequestAnalyzer.IsCreatingTable(context.Request, tableName) Then
  59. ' A new table was created -> add permissions to the current user.
  60. Me.AddTablePermissions(StorageRequestAnalyzer.GetTableToCreate(context.Request), Me.UserId)
  61. ElseIf (context.Response.StatusCode = CInt(HttpStatusCode.NoContent)) AndAlso StorageRequestAnalyzer.IsDeletingTable(context.Request, tableName) Then
  62. ' A table was deleted -> remove all permissions to that table.
  63. Me.RemoveAllTablePermissions(tableName)
  64. End If
  65. End Sub
  66. #Else
  67. Protected Overrides Sub PostProcessProxyRequest(ByVal context As HttpContext)
  68. Dim tableName = StorageRequestAnalyzer.GetRequestedTable(context.Request)
  69. If (context.Response.StatusCode = CInt(HttpStatusCode.Created)) AndAlso StorageRequestAnalyzer.IsCreatingTable(context.Request, tableName) Then
  70. ' A new table was created -> add permissions to the current user.
  71. Me.AddTablePermissions(StorageRequestAnalyzer.GetTableToCreate(context.Request), Me.RequestValidator.GetUserId(context))
  72. ElseIf (context.Response.StatusCode = CInt(HttpStatusCode.NoContent)) AndAlso StorageRequestAnalyzer.IsDeletingTable(context.Request, tableName) Then
  73. ' A table was deleted -> remove all permissions to that table.
  74. Me.RemoveAllTablePermissions(tableName)
  75. End If
  76. End Sub
  77. #End If
  78. Private Sub AddTablePermissions(ByVal tableName As String, ByVal userName As String)
  79. If Not String.IsNullOrEmpty(tableName) Then
  80. Dim accessTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.TablePrivilegeSuffix)
  81. Me.userPrivilegesRepository.AddPrivilegeToUser(userName, accessTablePrivilege)
  82. End If
  83. End Sub
  84. Private Sub RemoveAllTablePermissions(ByVal tableName As String)
  85. If Not String.IsNullOrEmpty(tableName) Then
  86. Dim publicTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.PublicTablePrivilegeSuffix)
  87. Me.userPrivilegesRepository.DeletePublicPrivilege(publicTablePrivilege)
  88. Dim accessTablePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", tableName, PrivilegeConstants.TablePrivilegeSuffix)
  89. Me.userPrivilegesRepository.DeletePrivilege(accessTablePrivilege)
  90. End If
  91. End Sub
  92. End Class
  93. End Namespace