PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.ACS/WindowsPhoneCloud.Web/Handlers/StorageProxyHandler.vb

#
Visual Basic | 151 lines | 104 code | 32 blank | 15 comment | 0 complexity | f0a4e9e2091a83a180a13c0d37b68b6b 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.Samples.WindowsPhoneCloud.Web.Infrastructure
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Serializers
  18. Imports System.Net
  19. Imports Microsoft.IdentityModel.Claims
  20. Imports Microsoft.WindowsAzure
  21. Imports System.Xml
  22. Imports System.Globalization
  23. Imports System.IO
  24. Namespace Handlers
  25. Public MustInherit Class StorageProxyHandler
  26. Implements IHttpHandler
  27. Private ReadOnly _formatSerializerFactory As IFormatSerializerFactory
  28. Private _webRequestCreator As IWebRequestCreate
  29. Protected Sub New(ByVal cloudStorageAccount As CloudStorageAccount, ByVal formatSerializerFactory As IFormatSerializerFactory)
  30. Me._formatSerializerFactory = formatSerializerFactory
  31. Me.CloudStorageAccount = cloudStorageAccount
  32. End Sub
  33. Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
  34. Get
  35. Return True
  36. End Get
  37. End Property
  38. Friend Property WebRequestCreator() As IWebRequestCreate
  39. Get
  40. If Me._webRequestCreator Is Nothing Then
  41. Me._webRequestCreator = New WebRequestCreator()
  42. End If
  43. Return Me._webRequestCreator
  44. End Get
  45. Set(ByVal value As IWebRequestCreate)
  46. Me._webRequestCreator = value
  47. End Set
  48. End Property
  49. Protected ReadOnly Property UserId() As String
  50. Get
  51. Dim identity = TryCast(HttpContext.Current.User.Identity, IClaimsIdentity)
  52. Return identity.Claims.Single(Function(c) c.ClaimType = ClaimTypes.NameIdentifier).Value
  53. End Get
  54. End Property
  55. Private privateCloudStorageAccount As CloudStorageAccount
  56. Protected Property CloudStorageAccount() As CloudStorageAccount
  57. Get
  58. Return privateCloudStorageAccount
  59. End Get
  60. Private Set(ByVal value As CloudStorageAccount)
  61. privateCloudStorageAccount = value
  62. End Set
  63. End Property
  64. Protected MustOverride ReadOnly Property AzureStorageUrl() As String
  65. Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  66. Dim azureResponse = Me.GetAzureStorageResponse(Me.GetAzureStorageRequestUri(context.Request), context.Request)
  67. Dim serializer = Me._formatSerializerFactory.GetSerializer(context.Request.Headers, context.Request.QueryString)
  68. Dim azureStorageResponseBody = azureResponse.ExtractBodyString()
  69. Dim serializedContentType = String.Empty
  70. If (Not String.IsNullOrWhiteSpace(azureStorageResponseBody)) Then
  71. Dim xmlDoc = New XmlDocument()
  72. xmlDoc.LoadXml(azureStorageResponseBody)
  73. azureStorageResponseBody = serializer.SerializeReply(xmlDoc, serializedContentType)
  74. End If
  75. azureResponse.CopyResponseHeadersTo(context.Response, serializedContentType)
  76. context.Response.Write(Me.GetProxyResponseBody(azureStorageResponseBody, context.Request))
  77. Me.PostProcessProxyRequest(context)
  78. context.Response.End()
  79. End Sub
  80. Protected Overridable Function GetAzureStorageRequestBody(ByVal proxyRequestBody As String, ByVal proxyRequest As HttpRequest) As String
  81. Dim oldValue = String.Format(CultureInfo.InvariantCulture, "{0}{1}", proxyRequest.Url.GetComponents(UriComponents.SchemeAndServer And (Not UriComponents.Port), UriFormat.SafeUnescaped), proxyRequest.FilePath)
  82. Dim newValue = Me.AzureStorageUrl
  83. Return proxyRequestBody.Replace(oldValue, newValue)
  84. End Function
  85. Protected Overridable Function GetProxyResponseBody(ByVal azureStorageResponseBody As String, ByVal proxyRequest As HttpRequest) As String
  86. Dim oldValue = Me.AzureStorageUrl
  87. Dim newValue = String.Format(CultureInfo.InvariantCulture, "{0}{1}", proxyRequest.Url.GetComponents(UriComponents.SchemeAndServer And (Not UriComponents.Port), UriFormat.SafeUnescaped), proxyRequest.FilePath)
  88. Return azureStorageResponseBody.Replace(oldValue, newValue)
  89. End Function
  90. Protected MustOverride Sub SignRequest(ByVal webRequest As WebRequest)
  91. Protected MustOverride Sub PostProcessProxyRequest(ByVal context As HttpContext)
  92. Private Function GetAzureStorageRequestUri(ByVal request As HttpRequest) As Uri
  93. Dim relativeUrl = If((request.QueryString.Count > 0), String.Format(CultureInfo.InvariantCulture, "{0}?{1}", request.PathInfo, request.QueryString.ToString()), request.PathInfo)
  94. Return New Uri(String.Format(CultureInfo.InvariantCulture, "{0}{1}", Me.AzureStorageUrl, relativeUrl), UriKind.Absolute)
  95. End Function
  96. Private Function GetAzureStorageResponse(ByVal azureStorageRequestUri As Uri, ByVal proxyRequest As HttpRequest) As WebResponse
  97. Dim azureStorageRequest = Me.WebRequestCreator.Create(azureStorageRequestUri)
  98. Dim azureStorageRequestBody = Me.GetAzureStorageRequestBody(New StreamReader(proxyRequest.InputStream).ReadToEnd(), proxyRequest)
  99. azureStorageRequest.Method = proxyRequest.HttpMethod
  100. If azureStorageRequestBody.Length > 0 OrElse ((Not proxyRequest.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase)) AndAlso (Not proxyRequest.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase))) Then
  101. azureStorageRequest.ContentLength = azureStorageRequestBody.Length
  102. End If
  103. proxyRequest.CopyRequestHeadersTo(azureStorageRequest)
  104. Me.SignRequest(azureStorageRequest)
  105. If (Not proxyRequest.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase)) AndAlso (Not proxyRequest.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase)) AndAlso proxyRequest.ContentLength > 0 Then
  106. Dim azureStorageRequestStream = azureStorageRequest.GetRequestStream()
  107. Using writer = New StreamWriter(azureStorageRequestStream)
  108. azureStorageRequestStream = Nothing
  109. writer.Write(azureStorageRequestBody)
  110. End Using
  111. End If
  112. Try
  113. Return azureStorageRequest.GetResponse()
  114. Catch webException As WebException
  115. Return webException.Response
  116. End Try
  117. End Function
  118. End Class
  119. End Namespace