PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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