public string getConfigXML()
        {
            string retVal = client.Get("config") as string;
              if(retVal == null || retVal == string.Empty )
              {
            string diagnosticsConnection = RoleEnvironment.GetConfigurationSettingValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
            string azureAccount = string.Empty;
            string azureEndpoint = string.Empty;
            string azureSharedKey = string.Empty;
            string defaultEndpointsProtocol = string.Empty;

            foreach (string item in diagnosticsConnection.Split(";".ToCharArray()))
            {
              string[] parsedItem = item.Split("=".ToCharArray());
              switch (parsedItem[0])
              {
            case "AccountKey": azureSharedKey = parsedItem[1];
              for (int i = 2; i < parsedItem.Length; i++)
                azureSharedKey += "=";
              break;
            case "AccountName": azureAccount = parsedItem[1]; break;
            case "DefaultEndpointsProtocol": defaultEndpointsProtocol = parsedItem[1]; break;
            case "Endpoint": azureEndpoint = parsedItem[1]; break;
            default: break;
              }
            }

            if (azureEndpoint == string.Empty)
              azureEndpoint = string.Format("{0}://{1}.blob.core.windows.net/", defaultEndpointsProtocol, azureAccount);

            byte[] xmlFragment = null;
            Finsel.AzureCommands.AzureBlobStorage abs = new Finsel.AzureCommands.AzureBlobStorage(azureAccount, azureEndpoint, azureSharedKey, "SharedKey");
            azureResults ar = new azureResults();
            xmlFragment = abs.GetBlob(configBlobContainer, "configuration.xml", "", ref ar);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            retVal = enc.GetString(xmlFragment);
            client.Store(Enyim.Caching.Memcached.StoreMode.Set, "config", retVal, new TimeSpan(0, 10, 0));
               // HttpRuntime.Cache.Insert("configuration", retVal,null,  System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0));
              }

              return retVal;
        }
 public azureResults SnapshotBlob(string containerName, string blobName)
 {
     azureResults retVal = new azureResults();
       Hashtable headers = new Hashtable();
       retVal = Blobs(cmdType.put, containerName, blobName, headers, "?comp=snapshot");
       if (retVal.StatusCode == HttpStatusCode.Created)
     retVal.Succeeded = true;
       return retVal;
 }
        /// <summary>
        /// Metadata is a quick way to get/set the metadata about a queue
        /// </summary>
        /// <param name="cmd">The type of command you want to execute. GET, PUT and DELETE are supported</param>
        /// <param name="queueName">Name of the queue</param>
        /// <param name="htMetaData">A hashtable containing the Name-Value pairs of MetaData.</param>
        /// <returns>An azureResults showing the results of the request.</returns>
        public azureResults MetaData(cmdType cmd, string queueName, Hashtable htMetaData)
        {
            azureResults retVal = new azureResults();
              Hashtable headers = new Hashtable();
              HttpStatusCode success = HttpStatusCode.NotImplemented;
              try
              {
            StringBuilder sb = new StringBuilder();
            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}", auth.EndPoint, queueName);
            requestUrl += string.Format(CultureInfo.CurrentCulture, "?comp=metadata");
            requestDate = DateTime.UtcNow;

            switch (cmd)
            {
              case cmdType.get:
              case cmdType.head:
            method = cmd.ToString().ToUpper();
            StringBuilder metaDataInformation = new StringBuilder();
            success = HttpStatusCode.OK;
            break;
              case cmdType.put:
            method = "PUT";
            foreach (DictionaryEntry item in htMetaData)
            {
              string metaDataName = item.Key.ToString().ToLower().Replace(" ", "-").Replace("\r", "");
              if (!metaDataName.StartsWith("x-ms-meta-"))
                metaDataName = "x-ms-meta-" + metaDataName;
              try
              {
                if (item.Value.ToString().Trim() != string.Empty)
                  //client.RequestHeaders[metaDataName] = item.Value.ToString();
                  headers.Add(ac.CleanMetaDataNames(metaDataName), item.Value.ToString());
              }
              catch
              {
              }
            }
            success = HttpStatusCode.NoContent;
            break;
              default:
            retVal.StatusCode = HttpStatusCode.NotImplemented;
            break;
            }
            retVal = ad.ProcessRequest(cmd, requestUrl, "", headers);
            retVal.Succeeded = (retVal.StatusCode == success);

              }
              catch (HttpException hex)
              {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
              }
              catch (Exception ex)
              {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
              }
              return retVal;
        }
Exemple #4
0
        void ProcessContainer(AzureBlobStorage abs, System.Collections.Hashtable metadata, string DirectoryPath, string ContainerName)
        {
            string baseDirectory = DirectoryPath + (DirectoryPath.EndsWith(@"\") ? "" : @"\") + ContainerName;
               if (!Directory.Exists(baseDirectory))
            Directory.CreateDirectory(baseDirectory);
               if(! baseDirectory.EndsWith(@"\"))
            baseDirectory = baseDirectory + @"\";

               azureResults arList = abs.GetBlobList(ContainerName, "");
               XmlDocument xdoc = new XmlDocument();
               xdoc.LoadXml(arList.Body);
               XmlNode xContainerInfo = xdoc.SelectSingleNode("//EnumerationResults [1]");
               string containerStart = xContainerInfo.Attributes["ContainerName"].Value;
               XmlNodeList xnl = xdoc.SelectNodes("//Url");

               foreach (XmlNode blobNode in xnl)
               {
            // get the relative path of the blob
            string baseName = blobNode.InnerText.Replace(baseDirectory, "");
            // get directory
            string relativeDirectory = baseName.Replace(containerStart,"");
            if(relativeDirectory.Contains("/"))
             relativeDirectory = relativeDirectory.Replace("/", @"\");
            // Get filename
            string fileName = blobNode.InnerText.Substring(blobNode.InnerText.LastIndexOf("/") + 1);
            relativeDirectory = relativeDirectory.Replace(fileName, "");
            fileName = baseDirectory + relativeDirectory + fileName;
            if (!Directory.Exists(baseDirectory + relativeDirectory))
             Directory.CreateDirectory(baseDirectory + relativeDirectory);
            azureResults arBlob = new azureResults();
            byte[] fileBytes = abs.GetBlob(ContainerName, blobNode.InnerText.Replace(containerStart,"") , "", ref arBlob);
            if (fileBytes == null)
             Console.WriteLine("Skipped {0}: 0 bytes", fileName);
            else
             File.WriteAllBytes(fileName, fileBytes);
            Console.WriteLine("Retrieved {0}", fileName);
            if (deleteAfterRetrieval)
            {
              abs.DeleteBlob(ContainerName, blobNode.InnerText.Replace(containerStart, ""));
            }
            // save

               }
        }
Exemple #5
0
        private void txtProcessDemo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

               StringBuilder sbResultsForStorage = new StringBuilder();
               AzureQueueStorage aqs = new AzureQueueStorage(txtAccount.Text, string.Format("http://{0}.queue.core.windows.net", txtAccount.Text), txtSharedKey.Text, "SharedKey");
               azureResults ar = aqs.Messages(cmdType.get, bulkTag, "", "visibilitytimeout=7200", "");
               string MessageID = "";
               string PopReceipt = "";
               string Message = "";
               if (ar.Body != null)
               {
            System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
            if (ar.Succeeded)
            {
             xdoc.LoadXml(ar.Body);
             System.Xml.XmlNodeList nodes = xdoc.SelectNodes("//QueueMessage");
             StringBuilder sbMultipart = new StringBuilder();
             if (nodes.Count == 0)
              txtMessage.Text = "No message to process";
             else
              foreach (System.Xml.XmlNode node in nodes)
              {
               MessageID = node.SelectSingleNode("MessageId").InnerText;
               PopReceipt = node.SelectSingleNode("PopReceipt").InnerText;
               Message = node.SelectSingleNode("MessageText").InnerText;

               System.Xml.XmlDocument msgDoc = new XmlDocument();
               msgDoc.LoadXml(Message);
               string newAccount = msgDoc.SelectSingleNode("//account[1]").InnerXml;
               string newKey = msgDoc.SelectSingleNode("//key[1]").InnerXml;
               string newSource = msgDoc.SelectSingleNode("//source[1]").InnerXml;
               string updateID = msgDoc.SelectSingleNode("//updateID[1]").InnerXml;
               string newTable = msgDoc.SelectSingleNode("//table[1]").InnerXml;
               AzureTableStorage ats = new AzureTableStorage(txtAccount.Text, "", txtSharedKey.Text, "SharedKey");
               AzureTableStorage ats1 = new AzureTableStorage(txtAccount.Text, "", txtSharedKey.Text, "SharedKey");

               azureHelper ah = new azureHelper(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey");

               string mrgMessage = string.Format(queueUpdateCreationEntity, updateID, 0, 0);
               ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", mrgMessage, "");
               ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", string.Format("<m:properties>\n\t<d:PartitionKey>{0}</d:PartitionKey>\n\t<d:RowKey>000000000000000</d:RowKey><d:StartedProcessing>{1}</d:StartedProcessing></m:properties>", updateID, DateTime.UtcNow.ToLongTimeString()), "");

               AzureBlobStorage abs = new AzureBlobStorage(newAccount, string.Format("http://{0}.blob.core.windows.net", newAccount), newKey, "SharedKey");

               AzureTableStorage atsNew = new AzureTableStorage(newAccount, string.Format("http://{0}.blob.core.windows.net", newAccount), newKey, "SharedKey");
               ar = atsNew.Tables(cmdType.post, newTable);
               if (ar.Succeeded || ar.StatusCode == System.Net.HttpStatusCode.Conflict)
               {
            ar = new azureResults();
            string newContainer = newSource.Replace(string.Format("http://{0}.blob.core.windows.net/", newAccount), "");
            newContainer = newContainer.Substring(0, newContainer.IndexOf("/"));
            string newBlob = newSource.Replace(string.Format("http://{0}.blob.core.windows.net/", newAccount), "").Replace(newContainer, "");
            byte[] blob = abs.GetBlob(newContainer, newBlob, "", ref ar);
            string x = new System.Text.UTF8Encoding().GetString(blob);
            x = x.Substring(x.IndexOf("<"));
            msgDoc.LoadXml(x);

            int errorCt = 0;
            int processedCt = 0;
            //Instantiate an XmlNamespaceManager object.
            System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xdoc.NameTable);

            //Add the namespaces used in books.xml to the XmlNamespaceManager.
            xmlnsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
            xmlnsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            XmlNodeList pnodes = msgDoc.SelectNodes("//m:properties", xmlnsManager);
            int iCounter = 101;
            int iResponse = 1;
            foreach (XmlNode pnode in pnodes)
            {
             if (iCounter > 100)
             {
              if (sbMultipart.Length > 0)
              {
               sbMultipart.Append("</entry>");
               ProcessMultiPartForStatus(ats.auth, ah.entityGroupTransaction(cmdType.post, newTable, sbMultipart.ToString()), bulkTag, updateID, iResponse.ToString("D15"), ref processedCt, ref errorCt, "201 Created");
               ar = ats1.Entities(cmdType.post, bulkTag, updateID, iResponse.ToString("D15"), sbResultsForStorage.ToString(), "");
               mrgMessage = string.Format(queueUpdateCreationEntity, updateID, processedCt, errorCt);
               ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", mrgMessage, "", "*");
               iResponse++;
              }
              sbMultipart = new StringBuilder();
              sbMultipart.AppendFormat(@"<?xml version=""1.0"" encoding=""utf-8"" ?><entry xml:base=""http://finseldemos.table.core.windows.net/"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom"">");
              iCounter = 0;
             }
             sbMultipart.Append(pnode.OuterXml);
             iCounter++;

            }
            sbMultipart.Append("</entry>");
            ProcessMultiPartForStatus(ats.auth, ah.entityGroupTransaction(cmdType.post, newTable, sbMultipart.ToString()), bulkTag, updateID, iResponse.ToString("D15"), ref processedCt, ref errorCt, "201 Created");
            mrgMessage = string.Format(queueUpdateCreationEntity, updateID, processedCt, errorCt).Replace("Processing", "Completed");
            ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", mrgMessage, "", "*");

               }
               else
               {
            mrgMessage = string.Format(queueUpdateCreationEntity, updateID, 0, 0).Replace("Processing", "Failed to create table!");
            ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", mrgMessage, "");
               }
               aqs.Messages(cmdType.delete, bulkTag, "", string.Format("popreceipt={0}", PopReceipt), MessageID);
               ats.Entities(cmdType.merge, bulkTag, updateID, "000000000000000", string.Format("<m:properties>\n\t<d:PartitionKey>{0}</d:PartitionKey>\n\t<d:RowKey>000000000000000</d:RowKey><d:CompletedProcessing>{1}</d:CompletedProcessing></m:properties>", updateID, DateTime.UtcNow.ToLongTimeString()), "");
              }

            }
            else txtMessage.Text = "No message to process";
            ProcessResults(ar);
            this.Cursor = Cursors.Default;
               }
        }
Exemple #6
0
 private void ProcessGeneric(cmdType cmd)
 {
     this.Cursor = Cursors.WaitCursor;
        azureResults ar = new azureResults();
        try
        {
     azureDirect  ad= new azureDirect(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey");
     Hashtable ht = new Hashtable();
     string[] AllData = txtGenericHeaders.Text.Split("\n".ToCharArray());
     foreach (string metadataDetail in AllData)
     {
      string[] detail = metadataDetail.Split(":".ToCharArray());
      string key = "";
      string value = "";
      if (detail[0] != string.Empty)
      {
       key = detail[0];
       if (metadataDetail.Contains(":"))
        value = detail[1];
       ht.Add(key, value.Replace("\r",""));
      }
     }
     ar= ad.ProcessRequest(cmd, txtURI.Text, new System.Text.ASCIIEncoding().GetBytes(txtGenericBody.Text), ht);
     ProcessResults(ar);
        }
        catch (Exception ex)
        {
     //Literal1.Text = string.Format("<textarea id=\"txtResponse\" name=\"S1\">{0}</textarea>", ex.ToString()); //.Replace("<", "&lt").Replace(">", "&gt;");
     lblError.Text = ex.ToString();
     lblStatus.Text = "Error:";
     lblCalledURL.Text = "";
        }
        this.Cursor = Cursors.Default;
 }
Exemple #7
0
 private void btnVerifyBlob_Click(object sender, EventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
     System.Collections.Hashtable ht = new Hashtable();
     ht.Add("If-Modified", txtETag.Text);
     AzureBlobStorage abs = new AzureBlobStorage(txtAccount.Text, string.Format("http://{0}.queue.core.windows.net", txtAccount.Text), txtSharedKey.Text, "SharedKey");
     //azureResults ar =abs.CheckBlobCache(cbBlobContainers.Text, cbBlobs.Text, txtETag.Text);
     azureResults ar = new azureResults();
     abs.GetBlob(cbBlobContainers.Text, cbBlobs.Text, "", ref ar, txtETag.Text);
     ProcessResults(ar);
     this.Cursor = Cursors.Default;
 }
Exemple #8
0
 private void btnGetBlob_Click(object sender, EventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
        AzureBlobStorage abs = new AzureBlobStorage(txtAccount.Text, string.Format("http://{0}.blob.core.windows.net", txtAccount.Text), txtSharedKey.Text, "SharedKey");
        azureResults ar = new azureResults();
        byte[] blob = abs.GetBlob(cbBlobContainers.Text, cbBlobs.Text, "", ref ar);
        if (blob != null)
        {
     string fileName = txtBlobLocation.Text;
     if (File.Exists(fileName))
      fileName = string.Format("{0}{1}", fileName, Guid.NewGuid().ToString());
      if(fileName != string.Empty)
       File.WriteAllBytes(fileName, blob);
        }
        ProcessResults(ar);
        this.Cursor = Cursors.Default;
 }
        public void ProcessRequest(HttpContext context)
        {
            string accountName = string.Empty;
              string sharedKey = string.Empty;
              string blobTypeOfCall = string.Empty;

              string containerName = string.Empty;
              string blobName = string.Empty;
              string leaseAction = string.Empty;
              string leaseID = string.Empty;
              string eTag = string.Empty;
              string mimicDirectories = string.Empty;
              string includeSnapshots = string.Empty;
              string includeUncommitted = string.Empty;

              if (context.Request.Params.AllKeys.Contains("accountname"))
            accountName = context.Request.Params["accountname"].ToString();
              if (context.Request.Params.AllKeys.Contains("sharedkey"))
            sharedKey = context.Request.Params["sharedkey"].ToString().Replace(" ", "+");
              if (context.Request.Params.AllKeys.Contains("blobtypeofcall"))
            blobTypeOfCall = context.Request.Params["blobtypeofcall"].ToString();
              if (context.Request.Params.AllKeys.Contains("containername"))
            containerName = context.Request.Params["containername"].ToString();
              if (context.Request.Params.AllKeys.Contains("blobname"))
            blobName = context.Request.Params["blobname"].ToString();
              if (context.Request.Params.AllKeys.Contains("leaseaction"))
            leaseAction = context.Request.Params["leaseaction"].ToString().ToLower();
              if (context.Request.Params.AllKeys.Contains("leaseid"))
            leaseID = context.Request.Params["leaseid"].ToString();
              if (context.Request.Params.AllKeys.Contains("etag"))
            eTag = context.Request.Params["etag"].ToString();
              if (context.Request.Params.AllKeys.Contains("mimicdirectories"))
            mimicDirectories = "1";
              if (context.Request.Params.AllKeys.Contains("includesnapshots"))
            includeSnapshots = "1";
              if (context.Request.Params.AllKeys.Contains("includeuncommitted"))
            includeUncommitted = "1";

              string retVal = string.Empty;
              string retValType = string.Empty;
              retValType = "text/xml";

              Hashtable ht = new Hashtable();

              foreach (string key in context.Request.Params.AllKeys)
              {
            if (key.StartsWith("x-ms-meta-"))
              if (ht.ContainsKey(key))
            ht[key] = string.Format("{0},{1}", ht[key].ToString(), context.Request.Params[key].ToString());
              else
            ht.Add(key, context.Request.Params[key].ToString());
              }

              azureResults ar = new azureResults();
              Finsel.AzureCommands.AzureBlobStorage abs = new Finsel.AzureCommands.AzureBlobStorage(accountName,
            string.Format("http://{0}.blob.core.windows.net", accountName), sharedKey,
            "SharedKey");

              switch (blobTypeOfCall.ToLower())
              {
            case "addblobmetadata":
              ar = abs.MetaData(cmdType.put, containerName, blobName, ht);
              retVal = processAzureResults(ar);
              break;
            case "addcontainermetadata":
              ar = abs.MetaData(cmdType.put, containerName, "", ht);
              retVal = processAzureResults(ar);
              break;
            case "copyblob":
            //ar=abs.CopyBlob(
            case "createcontainer":
              ht.Add("x-ms-meta-CreatedBy", "Finsel.AzureCommands");
              ar = abs.Containers(cmdType.put, containerName, ht);
              retVal = processAzureResults(ar);
              break;
            case "deleteblob":
              ar = abs.DeleteBlob(containerName, blobName);
              retVal = processAzureResults(ar);
              break;
            case "deletecontainer":
              ar = abs.Containers(cmdType.delete, containerName, new Hashtable());
              retVal = processAzureResults(ar);
              break;
            case "deleteblobmetadata":
              ar = abs.MetaData(cmdType.put, containerName, blobName, null);
              retVal = processAzureResults(ar);
              break;
            case "deletecontainermetadata":
              ar = abs.MetaData(cmdType.put, containerName, "", null);
              retVal = processAzureResults(ar);
              break;
            case "displayblobmetadata":
              ar = abs.MetaData(cmdType.get, containerName, blobName, null);
              retVal = processAzureResults(ar);
              break;
            case "displaycontainermetadata":
              ar = abs.MetaData(cmdType.get, containerName, "", null);
              retVal = processAzureResults(ar);
              break;
            case "getblob":
              break;
            case "getbloblist":
              string parameterList = string.Empty;
              if (mimicDirectories == "1")
            if (blobName == string.Empty)
              parameterList = "delimiter=/";
            else
              parameterList = string.Format("prefix={0}&delimiter={1}", (blobName == string.Empty ? "/" : blobName), "/");
              if (includeSnapshots == "1")
              {
            if (parameterList != string.Empty)
              parameterList = string.Format("{0}&include=snapshots", parameterList);
            else parameterList = "include=snapshots";
              }
              if (includeUncommitted == "1")
              {
            if (parameterList != string.Empty)
              parameterList = string.Format("{0}&include=uncommittedblobs", parameterList);
            else parameterList = "include=uncommittedblobs";
              }
              ar = abs.GetBlobList(containerName, parameterList);
              retVal = processAzureResults(ar);
              break;
            case "getcontainerlist":
              ar = abs.GetContainerList(containerName);
              retVal = processAzureResults(ar);
              break;
            case "leaseblob":
              AzureBlobStorage.BlobLease bl;
              switch (leaseAction)
              {
            case "acquirelease": bl = AzureBlobStorage.BlobLease.acquireLease; break;
            case "breaklease": bl = AzureBlobStorage.BlobLease.breakLease; break;
            case "releaselease": bl = AzureBlobStorage.BlobLease.releaseLease; break;
            case "renewlease": bl = AzureBlobStorage.BlobLease.renewLease; break;
            default: bl = AzureBlobStorage.BlobLease.acquireLease; break;
              }

              ar = abs.LeaseBlob(containerName, blobName, bl, leaseID);
              retVal = processAzureResults(ar);
              break;
            case "makecontainerprivate":
              ar = abs.SetContainerAccess(containerName, false);
              retVal = processAzureResults(ar);
              break;
            case "makecontainerpublic":
              ar = abs.SetContainerAccess(containerName, true);
              retVal = processAzureResults(ar);
              break;

            case "snapshotblob":
              ar = abs.SnapshotBlob(containerName, blobName);
              retVal = processAzureResults(ar);
              break;
            case "verifyblob":
              ar = abs.CheckBlobCache(containerName, blobName, eTag);
              retVal = processAzureResults(ar);
              break;
            default:
              retVal = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
            <html xmlns='http://www.w3.org/1999/xhtml' >
            <head>
            <title>Azure Architect Azure Blob Storage Handler Form</title>
            </head>
            <body>
            <form action='absHandler.ashx' method='post'>
            <table border='1'>
            <tr>
            <td>Account</td><td><input name='accountname' maxlength='100' /></td>
            </tr><tr>
            <td>Shared Key</td><td><input name='sharedkey' maxlength='100' /></td>
            </tr><tr>
            <td>Container Name</td><td><input name='containername' maxlength='100' /></td>
            </tr><tr>
            <td>Blob Name</td><td><input name='blobname' maxlength='1240' /></td>
            </tr><tr>
            <td>Type of call</td><td>
            <select name='blobtypeofcall'>
            <option>AddContainerMetadata</option>
            <option value='CreateContainer'>CreateContainer</option>
            <option value='DeleteBlob'>DeleteBlob</option>
            <option value='DeleteContainer'>DeleteContainer</option>
            <option value='DeleteContainerMetadata'>DeleteContainerMetadata</option>
            <option value='DisplayContainerMetadata'>DisplayContainerMetadata</option>
            <option value='GetBlob'>GetBlob</option>
            <option value='GetBlobList'>GetBlobList</option>
            <option value='GetContainerList'>GetContainerList</option>
            <option value='MakeContainerPrivate'>MakeContainerPrivate</option>
            <option value='MakeContainerPublic'>MakeContainerPublic</option></select>
            </td>
            </tr><tr>
            <td colspan='2'><input type='submit' /></td>
            </tr>
            </table>
            </form>
            </body>
            </html>";
              retValType = "text/html";
              //          retVal = @"<?xml version='1.0' encoding='utf-8' ?>
              //<ValidFormat>
              //  <!-- Post format -->
              //  <AccountName />
              //  <SharedKey />
              //  <ContainerName />
              //  <Blobname />
              //  <BlobTypeOfCall>
              //    <AddContainerMetadata />
              //    <CreateContainer />
              //    <DeleteBlob />
              //    <DeleteContainer />
              //    <DeleteContainerMetadata />
              //    <DisplayContainerMetadata />
              //    <GetBlob />
              //    <GetBlobList />
              //    <GetContainerList />
              //    <MakeContainerPrivate />
              //    <MakeContainerPublic />
              //  </BlobTypeOfCall>
              //</ValidFormat>";
              break;
              }

              context.Response.ContentType = retValType;
              context.Response.Write(retVal);
        }
 private void click_Queues(object sender, RoutedEventArgs e)
 {
     if (e.Source.ToString().StartsWith("System.Windows.Controls.Button"))
     {
         this.Cursor = Cursors.Wait;
         AzureQueueStorage aqs = new AzureQueueStorage(azAccount, string.Format("http://{0}.queue.core.windows.net", azAccount), azSharedKey, "SharedKey");
         azureResults ar = new azureResults();
         Hashtable ht = new Hashtable();
         if (e.Source == btnAddQueueMetaData)
         {
             string[] AllData = txtQMetaData.Text.Split("\n".ToCharArray());
             foreach (string metadataDetail in AllData)
             {
                 string[] detail = metadataDetail.Split(":".ToCharArray());
                 string key = "";
                 string value = "";
                 if (detail[0] != string.Empty)
                 {
                     key = detail[0];
                     if (metadataDetail.Contains(":"))
                         value = detail[1];
                     ht.Add(key, value);
                 }
             }
             ar = aqs.MetaData(cmdType.put, cbQueues.Text, ht);
         }
         else if (e.Source == btnClearQueue)
         {
             ar = aqs.Messages(cmdType.delete, cbQueues.Text, "", "", "");
         }
         else if (e.Source == btnCreateMessage)
         {
             ar = aqs.Messages(cmdType.post, cbQueues.Text, txtMessage.Text, "", "");
         }
         else if (e.Source == btnCreateQueue)
         {
             ht.Add("createdBy", "Finsel.AzureCommands");
             ar = aqs.Queues(cmdType.put, cbQueues.Text, "", ht);
         }
         else if (e.Source == btnDeleteMessage)
         {
             string parameters = string.Empty;
             if (txtPopReceipt.Text != string.Empty)
                 parameters = string.Format("popreceipt={0}", txtPopReceipt.Text);
             ar = aqs.Messages(cmdType.delete, cbQueues.Text, "", parameters, txtMessageID.Text);
         }
         else if (e.Source == btnDeleteQueue)
         {
             ar = aqs.Queues(cmdType.delete, cbQueues.Text, "", new Hashtable());
         }
         else if (e.Source == btnDeleteQueueMetaData)
         {
             ar = aqs.MetaData(cmdType.put, cbQueues.Text, new Hashtable());
         }
         else if (e.Source == btnDisplayQueueMetadata)
         {
             ar = aqs.MetaData(cmdType.get, cbQueues.Text, new Hashtable());
         }
         else if (e.Source == btnGetMessage)
         {
             ar = aqs.Messages(cmdType.get, cbQueues.Text, "", txtQParameters.Text, "");
             txtMessageID.Text = "";
             txtPopReceipt.Text = "";
             txtMessage.Text = "";
             if (ar.Body != null)
             {
                 System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
                 xdoc.LoadXml(ar.Body);
                 System.Xml.XmlNodeList nodes = xdoc.SelectNodes("//QueueMessage");
                 if (nodes.Count == 0)
                     txtMessage.Text = "No message to process";
                 else
                     foreach (System.Xml.XmlNode node in nodes)
                     {
                         txtMessageID.Text = node.SelectSingleNode("MessageId").InnerText;
                         if (node.SelectNodes("//PopReceipt").Count > 0)
                             txtPopReceipt.Text = node.SelectSingleNode("PopReceipt").InnerText;
                         if (node.SelectNodes("//MessageText").Count > 0)
                             txtMessage.Text = node.SelectSingleNode("MessageText").InnerText;
                     }
             }
             else txtMessage.Text = "No message to process";
         }
         else if (e.Source == btnGetQueues)
         {
             cbQueues.Items.Clear();
             cbQueues.Text = string.Empty;
             ar = aqs.GetQueueList("");
             if (ar.Succeeded)
             {
                 XmlDocument xdoc = new XmlDocument();
                 xdoc.LoadXml(ar.Body);
                 XmlNodeList nodes = xdoc.SelectNodes("//Queue");
                 foreach (XmlNode node in nodes)
                 {
                     cbQueues.Items.Add(node.SelectSingleNode("Name").InnerText);
                 }
                 if (cbQueues.Items.Count > 0)
                     cbQueues.SelectedIndex = 0;
             }
         }
         ProcessResults(ar);
         this.Cursor = Cursors.Arrow;
     }
 }
        private void click_Tables(object sender, RoutedEventArgs e)
        {
            AzureTableStorage ats = new AzureTableStorage(azAccount, azEndPoint, azSharedKey, "SharedKey");
            azureResults ar = new azureResults();
            if (e.Source.ToString().StartsWith("System.Windows.Controls.Button"))
            {
                this.Cursor = Cursors.Wait;
                if (e.Source == btnGet)
                    Process(cmdType.get);
                else if (e.Source == btnPut)
                    Process(cmdType.put);
                else if (e.Source == btnPost)
                    Process(cmdType.post);
                else if (e.Source == btnDelete)
                    Process(cmdType.delete);
                else if (e.Source == btnMerge)
                    Process(cmdType.merge);
                else if (e.Source == btnQuery)
                {
                    string tableName = cbTables.Text;
                    string filter = txtDocumentData.Text;
                    string parameters = txtTParameters.Text;
                    if (filter.StartsWith("?"))
                        filter = filter.Substring(1);
                    if (!filter.StartsWith("$filter="))
                        filter = string.Format("$filter={0}", filter);
                    if (parameters != string.Empty)
                    {
                        if (parameters.StartsWith("?"))
                            parameters = parameters.Substring(1);
                        if (!parameters.StartsWith("&"))
                            parameters = string.Format("&{0}", parameters);
                        filter = string.Format("{0}{1}", filter, parameters);
                    }

                     ar = ats.Entities(cmdType.get, tableName, "", "", "", filter);
                    ProcessResults(ar);
                }
                else if (e.Source == btnGetTables)
                {
                    cbTables.Items.Clear();
                    cbTables.Text = "";
                    ar = ats.GetTableList();
                    if (ar.Succeeded)
                    {
                        XmlDocument xdoc = new XmlDocument();
                        xdoc.LoadXml(ar.Body);
                        //Instantiate an XmlNamespaceManager object.
                        System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xdoc.NameTable);

                        //Add the namespaces used in books.xml to the XmlNamespaceManager.
                        xmlnsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                        xmlnsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
                        XmlNodeList nodes = xdoc.SelectNodes("//d:TableName", xmlnsManager);

                        foreach (XmlNode node in nodes)
                        {
                            cbTables.Items.Add(node.InnerText);
                        }
                    }
                    ProcessResults(ar);
                }
                this.Cursor = Cursors.Arrow;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            Int64 expirationSeconds = 1;
              HttpCacheability hc = HttpCacheability.NoCache;

              try
              {
            string retVal = string.Empty;
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(new Utility().getConfigXML());
            XmlNode xNode = xdoc.SelectSingleNode("//blobData[@name='default']");

            string azureAccount = xNode.SelectSingleNode("Setting[@name='account']").Attributes["value"].Value;
            string azureEndpoint = xNode.SelectSingleNode("Setting[@name='endpoint']").Attributes["value"].Value;
            string azureSharedKey = xNode.SelectSingleNode("Setting[@name='accountSharedKey']").Attributes["value"].Value;
            string blobStorage = azureEndpoint;

            xNode = xdoc.SelectSingleNode(string.Format("//fragmentData/Setting[@name='luceneFragments']"));
            string fragmentLocation = xNode.Attributes["value"].Value ;

            string queryName = string.Empty;

            SetCompression(context);

            try
            {
              AzureBlobStorage abs = new AzureBlobStorage(azureAccount, blobStorage, azureSharedKey, "SharedKey");
              azureResults ar = new azureResults();
              // Get the page name and replace the .q extension with .xml
              queryName = context.Request.Path;
              queryName = queryName.Substring(queryName.LastIndexOf("/") + 1);
              queryName = queryName.Substring(0, queryName.LastIndexOf(".")) + ".xml";
              byte[] xmlFragment = abs.GetBlob(fragmentLocation, queryName, "", ref ar, "");
              if (!ar.Succeeded)
              {
            context.Response.StatusCode = (int)ar.StatusCode;
              }
              else
              {
            xdoc = new XmlDocument();
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            xdoc.LoadXml(enc.GetString(xmlFragment));
            /*
             * http://azure-architect.com/portals/16/MOOPData.xsd

             */
            string azureContainerName = string.Empty;
            azureContainerName = xdoc.SelectSingleNode("//MOOPData/luceneSearch/azureContainer[1]").InnerText;
            Microsoft.WindowsAzure.StorageCredentialsAccountAndKey scaak = new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(azureAccount, azureSharedKey);
            Microsoft.WindowsAzure.CloudStorageAccount csa = new Microsoft.WindowsAzure.CloudStorageAccount(scaak, false);
            AzureDirectory azDir = new AzureDirectory(csa, azureContainerName, new RAMDirectory());

            //            IndexSearcher searcher = new IndexSearcher("azure-lucene-search");
            try
            {
              var q = new BooleanQuery();
              XmlNode xn = xdoc.SelectSingleNode("//luceneSearch[1]");
              XmlNodeList xnl = xn.SelectNodes("//query");
              Query[] qArray = new Query[xnl.Count];
              bool hasReplaceableSearchValues = false;
              bool hasReplacedAtLeastOneValue = false;
              bool requirementsMet = true;
              for (int i = 0; i < xnl.Count; i++)
              {
                XmlNode node = xnl[i];
                string term = string.Empty;
                term = node.Attributes["term"].Value;
                string termValue = node.Attributes["termValue"].Value;
                string variableValue = node.Attributes["variableValue"].Value;
                string parmValue = string.Empty;
                string requiredField = node.Attributes["required"].Value;
                if (variableValue == "true") // See if there is a replacement attempt
                {
                  if (requiredField == "true" && !context.Request.Params.AllKeys.Contains(termValue))
                    requirementsMet = false;
                  hasReplaceableSearchValues = true; // Set the flag to say we are attempting to replace the value
                  if (context.Request.Params.AllKeys.Contains(termValue)) // The parameter exists
                  {
                    hasReplacedAtLeastOneValue = true;
                    parmValue = context.Request.Params[termValue].Replace("+", " ").Replace("%20", " ");
                  }
                }
                if (node.Attributes["useAnalyzer"].Value == "true") // Should we use the analyzer
                {

                  if (variableValue == "true" && context.Request.Params.AllKeys.Contains(termValue)) // Did we actually have a replaceable value or a static value
                    qArray[i] = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, term, new SnowballAnalyzer("English")).Parse(parmValue);
                   if (variableValue != "true")
                    qArray[i] = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, term, new SnowballAnalyzer("English")).Parse(termValue);
                }
                else // Using a Boolean
                {
                  if (variableValue == "true" && context.Request.Params.AllKeys.Contains(termValue)) // Did we actually have a replaceable value or a static value
                    qArray[i] = new TermQuery(new Term(term, parmValue));
                  if (variableValue != "true")
                    qArray[i] = new TermQuery(new Term(term, termValue));
                }
                if (qArray[i] != null)
                {
                  switch (node.Attributes["booleanClauseOccurs"].Value.ToLower())
                  {
                    case "must": q.Add(new BooleanClause(qArray[i], BooleanClause.Occur.MUST)); break;
                    case "must_not": q.Add(new BooleanClause(qArray[i], BooleanClause.Occur.MUST_NOT)); break;
                    case "should": q.Add(new BooleanClause(qArray[i], BooleanClause.Occur.SHOULD)); break;
                    default: q.Add(new BooleanClause(qArray[i], BooleanClause.Occur.MUST)); break;
                  }
                }
              }
              IndexSearcher searcher2 = new IndexSearcher(azDir, true);
              TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);
              // If we have either no replaceable values or have replaceable values and at least one was provided
              if (!hasReplaceableSearchValues || (hasReplaceableSearchValues && hasReplacedAtLeastOneValue))
                if (requirementsMet)
                  searcher2.Search(q, collector);
              int indexID = 1;
              ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
              StringBuilder xmlOutput = new StringBuilder();
              xmlOutput.AppendFormat("<?xml version=\"1.0\"?><root>");
              for (int i = 0; i < hits.Length; ++i)
              {
                xmlOutput.AppendFormat("<hit>");
                int docId = hits[i].Doc;
                Document d = searcher2.Doc(docId);
                xmlOutput.AppendFormat("<score>{0}</score><docID>{1}</docID>", hits[i].Score, indexID ++);
                foreach (Field f in d.GetFields())
                {
                  if (f.StringValue() == null)
                    xmlOutput.AppendFormat("<{0} />", f.Name());
                  else
                    xmlOutput.AppendFormat("<{0}>{1}</{0}>", f.Name(), System.Security.SecurityElement.Escape(f.StringValue()));
                }
                xmlOutput.AppendFormat("</hit>");
              }
              xmlOutput.AppendFormat("</root>");
              retVal = xmlOutput.ToString();

            }
            catch (Exception ex)
            {
              retVal = "<root />";
            }
            MessageFormatOptions defaultOptions = new MessageFormatOptions();
            string preferredContentType = string.Empty;
            if (context.Request.HttpMethod == "GET" || context.Request.HttpMethod == "DELETE")
            {
                preferredContentType = (context.Request.AcceptTypes ?? new string[0])
                          .Select(m => m.Split(';')[0])
                          .FirstOrDefault(m => defaultOptions.MimeInputTypes.ContainsKey(m))
                          ?? defaultOptions.DefaultContentType;
                if (preferredContentType.Trim() == string.Empty)
                    preferredContentType = context.Request.Headers["Content-Type"];
            }
            else preferredContentType = context.Request.Headers["Content-Type"];
            if (preferredContentType == "application/json")
            {
                context.Response.ContentType = preferredContentType;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retVal);
                retVal = JsonConvert.SerializeXmlNode(doc);
            }
            else
            {
                string luceneTransform = string.Empty;
                luceneTransform = xdoc.SelectSingleNode("/MOOPData/luceneSearch/transform").InnerText;
                if (luceneTransform != string.Empty && luceneTransform != null)
                {
                    xmlFragment = abs.GetBlob(fragmentLocation, luceneTransform, "", ref ar, "");
                    if (ar.Succeeded)
                    {
                       XsltUtil xslu = new XsltUtil();
                        retVal = XsltUtil.TransformXml(retVal, System.Text.ASCIIEncoding.ASCII.GetString(xmlFragment));
                        string transformContentType = "text/html";
                        try { transformContentType = xdoc.SelectSingleNode("/MOOPData/luceneSearch/transform").Attributes["contentType"].Value; }
                        catch { }
                        context.Response.ContentType = transformContentType;
                    }
                }

                else
                {
                    context.Response.ContentType = "text/xml";
                }
            }
              }
            }
            catch (Exception ex)
            {
              FrameworkUtility u = new FrameworkUtility(new Utility().ResolveDataConnection("sqlAzureConnection"));
              FrameworkUtility.ParsedURI pUri = new FrameworkUtility.ParsedURI(context.Request.RawUrl);
              u.LogData(pUri.domainName, pUri.folderName, pUri.pageName, context.Request.HttpMethod, context.Request.UserHostAddress, "", "QueryError", ex.ToString(),
            u.nvc2XML(context.Request.Headers));
              //retVal = string.Format("<!-- {0} -->", ex.ToString());
              context.Response.StatusDescription = "An error occured but it was logged for later review";
              context.Response.StatusCode = 400;
            }
            finally { if (retVal == string.Empty) retVal = "<root />"; }

            context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(expirationSeconds));
            context.Response.Cache.SetCacheability(hc);
            context.Response.Write(retVal);
              }
              catch (Exception ex)
              {
            context.Response.StatusCode = 404;
            context.Response.Close();
              }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["files"]))
              {
            string eTag = string.Empty;
            string cacheKeyETag = string.Format("etag:{0}{1}{2}", context.Request.Url.DnsSafeHost, context.Request.QueryString["files"], context.Request.QueryString["m"]);
            string retVal = string.Empty;
            if (context.Request.Headers["If-None-Match"] != string.Empty && context.Request.Headers["If-None-Match"] != null)
            {
              retVal = client.Get(cacheKeyETag) as string;
            }
            if (retVal == string.Empty || retVal == null)
            {
              FrameworkUtility aau = new FrameworkUtility();
              string[] relativeFiles = context.Request.QueryString["files"].Split(',');
              string[] absoluteFiles = new string[relativeFiles.Length];
              bool minifyData = (context.Request.QueryString["m"] != null);

              // Azure Blob Set up
              //string retVal = string.Empty;
              XmlDocument xdoc = new XmlDocument();
              xdoc.LoadXml(new Utility().getConfigXML());
              string hostName = context.Request.Headers["Host"];
              if (hostName.Contains(":"))
            hostName = hostName.Substring(0, hostName.IndexOf(":"));
              XmlNode xNode = xdoc.SelectSingleNode(string.Format("//blobData[@name='{0}']", hostName));
              if (xNode == null)
            xNode = xdoc.SelectSingleNode(string.Format("//blobData[@name='{0}']", hostName.Substring(hostName.IndexOf(".") + 1)));

              if (xNode == null)
            xNode = xdoc.SelectSingleNode("//blobData[@name='default']");
              string mimeType = string.Empty;
              string azureAccount = xNode.SelectSingleNode("Setting[@name='account']").Attributes["value"].Value;
              string azureEndpoint = xNode.SelectSingleNode("Setting[@name='endpoint']").Attributes["value"].Value;
              string azureSharedKey = xNode.SelectSingleNode("Setting[@name='accountSharedKey']").Attributes["value"].Value;
              string azureRootContainer = string.Empty;
              try { azureRootContainer = xNode.SelectSingleNode("Setting[@name='rootContainer']").Attributes["value"].Value; }
              catch { azureRootContainer = "$root"; }
              string blobStorage = azureEndpoint;
              AzureBlobStorage abs = new AzureBlobStorage(azureAccount, blobStorage, azureSharedKey, "SharedKey");
              azureResults ar = new azureResults();
              context.Response.Headers.Add("P3P", "CP=HONK");
              SetCompression(context);
              StringBuilder sbReturnData = new StringBuilder();
              for (int i = 0; i < relativeFiles.Length; i++)
              {
            string cPath = relativeFiles[i];
            if (mimeType == string.Empty)
            {
              mimeType = GetMimeType(cPath.Substring(cPath.LastIndexOf('.'))); //ar.Headers["Content-Type"].ToString();
            }
            {
              try
              {
                if (!cPath.Contains(".") && !cPath.EndsWith("/"))
                  cPath += "/";
                string containerName = string.Empty;
                string blobName = string.Empty;
                string[] aPath = cPath.Split("/".ToCharArray());
                if (aPath.Count() > 1)
                  containerName = aPath[1];
                if (!cPath.Substring(1).Contains("/"))
                {
                  containerName = azureRootContainer;
                  blobName = cPath.Substring(1).TrimStart();
                }
                else
                {
                  if (cPath.Length > containerName.Length + 2)
                    blobName = cPath.Substring(containerName.Length + 2).TrimStart();
                }
                if (blobName != string.Empty)
                {
                  ar = new azureResults();
                  byte[] xmlFragment = abs.GetBlob(containerName, blobName, "", ref ar, eTag);
                  if (ar.StatusCode == System.Net.HttpStatusCode.NotModified)
                  {
                  }
                  else if (ar.StatusCode != System.Net.HttpStatusCode.OK)
                  {
                    Console.WriteLine("HUH?");
                  }
                  else
                  {
                    context.Response.AppendToLog(string.Format("trying to send {0} {1}", containerName, blobName));
                    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                    eTag = string.Format("{0}{1}", eTag, ar.Headers["ETag"].ToString());
                    if (minifyData)
                    {
                      if (mimeType == "text/css")
                        sbReturnData.Append(new CssCompressor().Compress(enc.GetString(xmlFragment)));
                      else if (cPath.Substring(cPath.LastIndexOf('.')).ToLower() == ".js")
                        sbReturnData.Append(new JavaScriptCompressor().Compress(enc.GetString(xmlFragment)));
                      else sbReturnData.Append(enc.GetString(xmlFragment));
                    }
                    else
                      sbReturnData.Append(enc.GetString(xmlFragment));
                  }
                }

              }
              catch (Exception ex)
              {
                //context.Response.Status =
                context.Response.StatusCode = 404;
                context.Response.Close();
              }
            }
              }
              retVal = sbReturnData.ToString();
              client.Store(Enyim.Caching.Memcached.StoreMode.Set, cacheKeyETag, retVal, new TimeSpan(0, 10, 0));
            }

              context.Response.Write(retVal );

            context.Response.ContentType = context.Request.QueryString["contenttype"];
            //context.Response.AddFileDependencies(files);
            //context.Response.Cache.VaryByParams["stylesheets"] = true;
            //context.Response.Cache.SetETagFromFileDependencies();
            //context.Response.Cache.SetLastModifiedFromFileDependencies();
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Headers.Add("ETag", cacheKeyETag);
            context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            //SetHeaders(context, absoluteFiles);
              }
        }
        private azureResults putPageBlob(Int64 ContentLength, string ContentType, byte[] Content, string containerName, string blobName, Hashtable htMetaData)
        {
            azureResults retVal = new azureResults();
            try
            {
            // first we call put blob to initialize the blob
            Int64 boundaryContentLength = 0;
            StringBuilder sb = new StringBuilder();
            string sendBody = string.Empty;
            string rtnBody = string.Empty;
            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/{2}", auth.EndPoint, containerName, blobName);
            requestDate = DateTime.UtcNow;
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                req.Method = "PUT";
                req.ContentType = ContentType;
                req.ContentLength = 0;
                if (htMetaData.Count > 0)
                    foreach (DictionaryEntry item in htMetaData)
                    {
                        string metaDataName = item.Key.ToString().ToLower().Replace(" ", "").Replace("\r", "");
                        if (!metaDataName.StartsWith("x-ms-meta-"))
                            metaDataName = "x-ms-meta-" + metaDataName;
                        try
                        {
                            if (item.Value.ToString().Trim() != string.Empty)
                                req.Headers[metaDataName] = item.Value.ToString();
                        }
                        catch (Exception ex) { }
                    }
                req.Headers["x-ms-blob-type"] = "PageBlob";
                req.Headers["x-ms-blob-sequence-number"] = "0";

                if (ContentLength % 512 == 0)
                    boundaryContentLength = ContentLength;
                else
                    boundaryContentLength = ((ContentLength / 512) * 512) + 512;
                req.Headers["x-ms-blob-content-length"] = boundaryContentLength.ToString();
                req.Headers["x-ms-date"] = string.Format(CultureInfo.CurrentCulture, "{0:R}", requestDate);
                req.Headers["x-ms-version"] = "2012-02-12";
                retVal.CanonicalResource = ac.CanonicalizeUrl2(requestUrl);
                authHeader = ac.CreateSharedKeyAuth(req.Method, retVal.CanonicalResource, contentMD5, requestDate, req, auth);
                req.Headers["authorization"] = authHeader;

                Stream requestStream = null;// req.GetRequestStream();

                //requestStream.Write(new byte[0], 0, 0);
                //requestStream.Flush();
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                response.Close();

                retVal.Url = requestUrl;
                retVal.Body = "";
                retVal.StatusCode = response.StatusCode;
                retVal.Headers = ac.Headers2Hash(response.Headers);
                retVal.Succeeded = (retVal.StatusCode == HttpStatusCode.Created);
                if (retVal.Succeeded) // we have an initialized blob, now we need to post it
                {
                    Int64 blockSize = 65536;
                    Int64 currentBlock = 0;
                    // now we need to load the blob

                    while ((currentBlock * blockSize) < boundaryContentLength)
                    {

                        sendBody = string.Empty;
                        rtnBody = string.Empty;
                        requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/{2}?comp=page", auth.EndPoint, containerName, blobName);
                        req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                        req.Method = "PUT";
                        req.ContentType = ContentType;
                        Int64 currentBlockSize = (boundaryContentLength - (currentBlock * blockSize) > blockSize ? blockSize : boundaryContentLength - (currentBlock * blockSize));
                        Int64 lowByte = (currentBlock) * blockSize;
                        Int64 highByte = (currentBlock) * blockSize + currentBlockSize - 1;
                        req.Headers["x-ms-range"] = string.Format("bytes={0}-{1}", lowByte, highByte);
                        req.ContentLength = currentBlockSize;
                        req.Headers["x-ms-date"] = string.Format(CultureInfo.CurrentCulture, "{0:R}", requestDate);
                        req.Headers["x-ms-version"] = "2012-02-12";
                        req.Headers["x-ms-blob-type"] = "PageBlob";
                        req.Headers["x-ms-page-write"] = "update";
                        retVal.CanonicalResource = ac.CanonicalizeUrl2(requestUrl);
                        authHeader = ac.CreateSharedKeyAuth(req.Method, retVal.CanonicalResource, contentMD5, requestDate, req, auth);
                        req.Headers["authorization"] = authHeader;

                        requestStream = req.GetRequestStream();
                        byte[] contentBlock = new byte[blockSize];
                        if(currentBlockSize < blockSize)
                            Buffer.BlockCopy(Content, (int)lowByte, contentBlock, 0, (int)(ContentLength - (currentBlock * blockSize)) );
                        else
                            Buffer.BlockCopy(Content, (int)lowByte , contentBlock, 0,(int)currentBlockSize);

                        requestStream.Write(contentBlock, 0, (int)currentBlockSize);
                        requestStream.Flush();
                        response = (HttpWebResponse)req.GetResponse();
                        response.Close();

                        retVal.Url = requestUrl;
                        retVal.Body = "";
                        retVal.StatusCode = response.StatusCode;
                        retVal.Headers = ac.Headers2Hash(response.Headers);
                        retVal.Succeeded = (retVal.StatusCode == HttpStatusCode.Created);
                        currentBlock++;
                    }
                }
            }
            }
            catch (HttpException hex)
            {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
            }
            catch (Exception ex)
            {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
            }
            finally
            {

            }
            return retVal;
        }
        private azureResults putBlockBlob(Int64 ContentLength, string ContentType, byte[] Content, string containerName, string blobName, Hashtable htMetaData)
        {
            azureResults retVal = new azureResults();
            try
            {
            StringBuilder sb = new StringBuilder();
            string sendBody = string.Empty;
            string rtnBody = string.Empty;
            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/{2}", auth.EndPoint, containerName, blobName);
            requestDate = DateTime.UtcNow;
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                req.Method = "PUT";
                req.ContentType = ContentType;
                req.ContentLength = ContentLength;
                if (htMetaData.Count > 0)
                    foreach (DictionaryEntry item in htMetaData)
                    {
                        string metaDataName = item.Key.ToString().ToLower().Replace(" ", "").Replace("\r", "");
                        if (!metaDataName.StartsWith("x-ms-meta-"))
                            metaDataName = "x-ms-meta-" + metaDataName;
                        try
                        {
                            if (item.Value.ToString().Trim() != string.Empty)
                                req.Headers[metaDataName] = item.Value.ToString();
                        }
                        catch (Exception ex) { }
                    }
                req.Headers["x-ms-date"] = string.Format(CultureInfo.CurrentCulture, "{0:R}", requestDate);
                req.Headers["x-ms-version"] = "2012-02-12";
                req.Headers["x-ms-blob-type"] = "BlockBlob";
                retVal.CanonicalResource = ac.CanonicalizeUrl2(requestUrl);
                authHeader = ac.CreateSharedKeyAuth(req.Method, retVal.CanonicalResource, contentMD5, requestDate, req, auth);
                req.Headers["authorization"] = authHeader;

                Stream requestStream = req.GetRequestStream();
                requestStream.Write(Content, 0, (int)ContentLength);
                requestStream.Flush();
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                response.Close();

                retVal.Url = requestUrl;
                retVal.Body = "";
                retVal.StatusCode = response.StatusCode;
                retVal.Headers = ac.Headers2Hash(response.Headers);
                retVal.Succeeded = (retVal.StatusCode == HttpStatusCode.Created);
            }
            }
            catch (HttpException hex)
            {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
            }
            catch (Exception ex)
            {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
            }
            finally
            {

            }
            return retVal;
        }
        /// <summary>
        /// Entities returns information about entities
        /// </summary>
        /// <param name="cmd">The type of command you want to execute</param>
        /// <param name="tableName">Table name</param>
        /// <param name="PartitionKey">Partition Key</param>
        /// <param name="rowKey">Row Key</param>
        /// <param name="docData">Additional Data for Updates, Adds, Merges</param>
        /// <param name="IfMatch">The entity tag or * to execute an update/merge/delete regardless of the entity tag</param>
        /// <param name="parameters">Any Uri parameters to be passed in. comp=list can be passed in to get a list.</param>
        /// <returns></returns>
        public azureResults Entities(cmdType cmd, string tableName, string PartitionKey, string rowKey, string docData, string parameters, string IfMatch)
        {
            Hashtable headers = new Hashtable();
               azureResults retVal = new azureResults();
               HttpStatusCode success = HttpStatusCode.NotImplemented;
            try
            {
             string sendBody = string.Empty;
             string readBody = string.Empty;
             string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}", auth.EndPoint, tableName);
             requestDate = DateTime.UtcNow;
             switch (cmd)
             {
              case cmdType.get:
               method = "GET";
               IfMatch = string.Empty;
               sendBody = string.Empty;
               if (PartitionKey != string.Empty && rowKey != string.Empty)
               {
            requestUrl += string.Format(CultureInfo.CurrentCulture, "(PartitionKey='{0}',RowKey='{1}')", PartitionKey, rowKey);
               }
               else if (PartitionKey != string.Empty)
               {
            requestUrl += string.Format(CultureInfo.CurrentCulture, "(PartitionKey='{0}')", PartitionKey);
               }
               else
               {
            requestUrl += string.Format(CultureInfo.CurrentCulture, "()");
               }
               if (parameters != string.Empty)
               {
            if (!parameters.StartsWith("?"))
             parameters = "?" + parameters;
            requestUrl += string.Format(CultureInfo.CurrentCulture, parameters);
               }
               // do GET
               success = HttpStatusCode.OK;
               break;

              case cmdType.post:
               method = "POST";

               // accept input doc and parse into valid Atom for Azure Tables
               readBody = docData;
               IfMatch = "";
               sendBody = string.Format(createEntityXml, requestDate, readBody);
               sendBody = string.Format(sendBody, PartitionKey, rowKey);
               success = HttpStatusCode.Created;
               break;

              case cmdType.put:
               method = "PUT";
               requestUrl += string.Format(CultureInfo.CurrentCulture, "(PartitionKey='{0}',RowKey='{1}')", PartitionKey, rowKey);
               // accept input doc and parse into valid Atom for Azure Tables
               sendBody = docData;
               sendBody = string.Format(updateEntityXml, tableName, PartitionKey, rowKey, requestDate, sendBody, this.ETag.Replace(@"""", "&quot;"), auth.Account);

               success = HttpStatusCode.NoContent;
               break;

              case cmdType.merge:
               method = "MERGE";
               requestUrl += string.Format(CultureInfo.CurrentCulture, "(PartitionKey='{0}',RowKey='{1}')", PartitionKey, rowKey);
               // accept input doc and parse into valid Atom for Azure Tables
               sendBody = docData;
               sendBody = string.Format(updateEntityXml, tableName, PartitionKey, rowKey, requestDate, sendBody, this.ETag.Replace(@"""", "&quot;"), auth.Account);
               success = HttpStatusCode.NoContent;
               break;

              case cmdType.delete:
               method = "DELETE";
               sendBody = string.Empty;

               requestUrl += string.Format(CultureInfo.CurrentCulture, "(PartitionKey='{0}',RowKey='{1}')", PartitionKey, rowKey);

               success = HttpStatusCode.NoContent;
               break;

              default:
               retVal.Succeeded = false;
               retVal.StatusCode = HttpStatusCode.NotImplemented;
               break;
             }
             headers.Add("Content-Type", contentType);
             if (IfMatch != string.Empty)
               headers.Add("If-Match", IfMatch);
             retVal = ad.ProcessRequest(cmd, requestUrl, sendBody, headers);
             if (success == HttpStatusCode.NotImplemented)
             {
               retVal.Succeeded = false;
               retVal.StatusCode = success;
             }
             else
               retVal.Succeeded = (retVal.StatusCode == success);

            }
            catch (HttpException hex)
            {
             retVal.Succeeded = false;
             retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
             retVal.Body = hex.GetHtmlErrorMessage();
            }
            catch (Exception ex)
            {
             retVal.StatusCode = HttpStatusCode.SeeOther;
             retVal.Body = ex.ToString();
             retVal.Succeeded = false;
            }

               // Note: Body may contain data elements with XML... will need to use
               // Uri.UnescapeDataString() to format correctly
               return retVal;
        }
        /// <summary>
        /// Tables handles table level operations against Azure Table Storage
        /// </summary>
        /// <param name="cmd">The type of operation you want to commit: Delete, Post, Put, Get </param>
        /// <param name="tableName">Name of the table to perform the command agains</param>
        /// <param name="parameters">Uri Parameters to include</param>
        /// <returns>An azureResults showing the results of the request.</returns>
        public azureResults Tables(cmdType cmd, string tableName, string parameters)
        {
            azureResults retVal = new azureResults();
               HttpStatusCode success = HttpStatusCode.NotImplemented;
               Hashtable headers = new Hashtable();
               try
               {
            StringBuilder sb = new StringBuilder();
            string sendBody = string.Empty;
            string rtnBody = string.Empty;
            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}", auth.EndPoint, "Tables");
            requestDate = DateTime.UtcNow;
            switch (cmd)
            {
             case cmdType.get:
              method = "GET";
              string allData = tableName;
              if (tableName != string.Empty)
              {
               if (parameters == string.Empty)
               {
            requestUrl = requestUrl.Replace("Tables", string.Format(CultureInfo.CurrentCulture, "{0}()", allData));
               }
               else
               {
            requestUrl += string.Format(CultureInfo.CurrentCulture, "('{0}')", allData);
            if (!parameters.StartsWith("?"))
             parameters = "?" + parameters;
            requestUrl += string.Format(CultureInfo.CurrentCulture, "{0}", parameters);
               }
              }
              success = HttpStatusCode.OK;
              break;
             case cmdType.post:
              method = "POST";
              // build valid Atom document
              sendBody = string.Format(createTableXml, requestDate, tableName);
              success=  HttpStatusCode.Created;
              break;
             case cmdType.delete:
              method = "DELETE";
              requestUrl += string.Format(CultureInfo.CurrentCulture, "('{0}')", tableName);
              success = HttpStatusCode.NoContent;
              break;
             default:
              break;
            }

            headers.Add("Content-Type", contentType);
            retVal = ad.ProcessRequest(cmd, requestUrl, sendBody, headers);
            if (success == HttpStatusCode.NotImplemented)
            {
              retVal.Succeeded = false;
              retVal.StatusCode = success;
            }
            else
              retVal.Succeeded = (retVal.StatusCode == success);

               }
               catch (HttpException hex)
               {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
               }
               catch (Exception ex)
               {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
               }

               return retVal;
        }
        /// <summary>
        /// Send a request to Azure, adding only the authetication
        /// </summary>
        /// <param name="cmd">Method to execute</param>
        /// <param name="requestUrl">URL to execute against</param>
        /// <param name="body">Body to send</param>
        /// <param name="headers">Headers to send</param>
        /// <returns>An Azure Result object</returns>
        public azureResults ProcessRequest(cmdType cmd, string requestUrl, byte[] body, Hashtable headers)
        {
            Hashing h = new Hashing();
               HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
               req.Method = cmd.ToString().ToUpper();
               azureResults retVal = new azureResults();

               try
               {
            StringBuilder sb = new StringBuilder();
            string contentMD5 = string.Empty ;

            DateTime requestDate = DateTime.UtcNow;
            retVal.CanonicalResource = ac.CanonicalizeUrl2(requestUrl);
            string contentType = string.Empty;
            if (cmd == cmdType.head)
            req.ContentLength = 0;
            else
            req.ContentLength = body.Length;
            if (headers.ContainsKey("Content-Type"))
            {
             req.ContentType = headers["Content-Type"].ToString().Replace("\r", "");
             headers.Remove("Content-Type");
            }

            foreach (DictionaryEntry de in headers)
            {
             req.Headers[de.Key.ToString()] = de.Value.ToString();
            }

            //req.Headers["x-ms-version"] = "2009-07-17";
            //req.Headers["x-ms-version"] = "2009-09-19";
            req.Headers["x-ms-version"] = "2012-02-12";
            req.Headers["x-ms-date"] = string.Format(CultureInfo.CurrentCulture, "{0:R}", requestDate);
            string authHeader = string.Empty;
            if (requestUrl.Contains(".table."))
            {
              // Required with the 2009-09-19 authentiation
              req.Headers["DataServiceVersion"] = "1.0;NetFx";
              req.Headers["MaxDataServiceVersion"] = "1.0;NetFx";
              authHeader = ac.CreateSharedKeyAuthLite(req.Method, retVal.CanonicalResource, contentMD5, requestDate, req.ContentType, auth);
            }
            else
              authHeader = ac.CreateSharedKeyAuth(req.Method, retVal.CanonicalResource, contentMD5, requestDate, req, auth);
            req.Headers["authorization"] = authHeader;
            retVal.Url = requestUrl;
            if (body.Length > 0 && cmd != cmdType.head)
            {
            System.IO.Stream requestStream = req.GetRequestStream();
            requestStream.Write(body, 0, body.Length);
            requestStream.Flush();
            }

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
               // response = (HttpWebResponse)req.GetResponse();

            System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
            retVal.Body = sr.ReadToEnd();
            response.Close();

            retVal.StatusCode = response.StatusCode;
            retVal.Headers = ac.Headers2Hash(response.Headers);
              }
               catch (WebException wex)
               {
             retVal.StatusCode = ((System.Net.HttpWebResponse)(wex.Response)).StatusCode;
            //retVal.StatusCode = wex.  (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = wex.Message;
               /* for (int i=0;i< wex.Response.Headers.Count;i++)
              retVal.Headers.Add(wex.Response.Headers.Keys[i].ToString(), wex.Response.Headers[i].ToString());
             */
               }
               catch (Exception ex)
               {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
               }

               return retVal;
        }
Exemple #19
0
 private void btnHeadBlob_Click(object sender, EventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
       AzureBlobStorage abs = new AzureBlobStorage(txtAccount.Text, string.Format("http://{0}.blob.core.windows.net", txtAccount.Text), txtSharedKey.Text, "SharedKey");
       azureResults ar = new azureResults();
       ar= abs.Blobs(cmdType.head,cbBlobContainers.Text, cbBlobs.Text, new Hashtable(), "");
       ProcessResults(ar);
       this.Cursor = Cursors.Default;
 }
        /// <summary>
        /// Process Entity Group Transactions against a table by taking an XML block of Entity Data and turning it into Multipart and executing it.
        /// </summary>
        /// <param name="cmd">Method to execute</param>
        /// <param name="tableName">Table to execute the data against</param>
        /// <param name="entityData">Entity Data</param>
        /// <returns>string with results of the group transaction</returns>
        public string entityGroupTransaction(cmdType cmd,string tableName, string entityData)
        {
            int entityCounter = 1;
               StringBuilder sbResults = new StringBuilder();
               StringBuilder sbMultiPart = new StringBuilder();
               string singlePart = string.Empty;
               string PartitionKey = string.Empty;
               string oldPartitionKey = string.Empty;
               string boundaryIdentifier = Guid.NewGuid().ToString();
               string batchIdentifier = Guid.NewGuid().ToString();
               string contentType = string.Format("multipart/mixed; boundary=batch_{0}", boundaryIdentifier);
               azureResults ar = new azureResults();
            DateTime requestDate = DateTime.UtcNow.AddHours(1);

               Hashtable headers = new Hashtable();
               headers.Add("x-ms-version", "2009-04-14");
               headers.Add("Content-Type", contentType);
               azureDirect ad = new azureDirect(auth.Account, "", auth.SharedKey, auth.KeyType);
               XmlDocument msgDoc = new XmlDocument();

               //Instantiate an XmlNamespaceManager object.
               System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(msgDoc.NameTable);
               //Add the namespaces used in books.xml to the XmlNamespaceManager.
               xmlnsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
               xmlnsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
               msgDoc.LoadXml(entityData);
               XmlNodeList pnodes = msgDoc.SelectNodes("//m:properties", xmlnsManager);
               foreach (XmlNode pnode in pnodes)
               {
            PartitionKey = pnode.SelectSingleNode("d:PartitionKey", xmlnsManager).InnerText;
            string RowKey = pnode.SelectSingleNode("d:RowKey", xmlnsManager).InnerText;
            if (PartitionKey != oldPartitionKey || entityCounter > 98)
            {
             if (sbMultiPart.Length > 0)
             {
              sbMultiPart.AppendFormat("--changeset_{0}--\n--batch_{1}--", batchIdentifier, boundaryIdentifier);
              ar = ad.ProcessRequest(cmdType.post, string.Format("http://{0}.table.core.windows.net/$batch", auth.Account), new System.Text.ASCIIEncoding().GetBytes(sbMultiPart.ToString()), headers);
              sbResults.AppendLine(ar.Body);
             }
             sbMultiPart = new StringBuilder();

             boundaryIdentifier = Guid.NewGuid().ToString();
             batchIdentifier = Guid.NewGuid().ToString();
             contentType = string.Format("multipart/mixed; boundary=batch_{0}", boundaryIdentifier);
             headers["Content-Type"] = contentType;
             sbMultiPart.AppendFormat(batchTemplate, boundaryIdentifier,batchIdentifier );
             sbMultiPart.AppendLine();
             sbMultiPart.AppendLine();
             entityCounter = 1;
             oldPartitionKey = PartitionKey;
            }
            string postUrl =string.Format("http://{0}.table.core.windows.net/{1}", auth.Account,tableName);
            if (cmd != cmdType.post)
             postUrl = string.Format("{0}(PartitionKey='{1}',RowKey='{2}')", postUrl, PartitionKey, RowKey);

            string atomData = string.Format(createEntityXML, requestDate, pnode.OuterXml);

            singlePart = string.Format(entityTemplate, batchIdentifier, cmd.ToString().ToUpper(), postUrl , entityCounter,atomData.Length, atomData );
            sbMultiPart.AppendLine(singlePart);
            entityCounter++;
               }
               sbMultiPart.AppendFormat("--changeset_{0}--\n--batch_{1}--", batchIdentifier, boundaryIdentifier);
               ar = ad.ProcessRequest(cmdType.post, string.Format("http://{0}.table.core.windows.net/$batch", auth.Account), new System.Text.ASCIIEncoding().GetBytes(sbMultiPart.ToString()), headers);
               sbResults.AppendLine(ar.Body);
               return sbResults.ToString();
        }
Exemple #21
0
 private void Process(cmdType cmd)
 {
     this.Cursor = Cursors.WaitCursor;
        azureResults ar = new azureResults();
        try
        {
     AzureTableStorage ats = new AzureTableStorage(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey");
     string tableName = cbTables.Text;
     if (!cbBulkProcess.Checked)
     {
      if (txtRowKey.Text == string.Empty && txtPartitionKey.Text == string.Empty && txtDocumentData.Text == string.Empty )
       ar = ats.Tables(cmd, tableName);
      else
       ar = ats.Entities(cmd, tableName, txtPartitionKey.Text, txtRowKey.Text, txtDocumentData.Text, txtTParameters.Text ,txtIfMatch.Text );
     }
     else
     {
      azureHelper ah = new azureHelper(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey");
      string results = ah.entityGroupTransaction(cmd, cbTables.Text, txtDocumentData.Text);
      ar.Body = results;
      ar.Succeeded = true;
     }
     ProcessResults(ar);
        }
        catch (Exception ex)
        {
     //Literal1.Text = string.Format("<textarea id=\"txtResponse\" name=\"S1\">{0}</textarea>", ex.ToString()); //.Replace("<", "&lt").Replace(">", "&gt;");
     lblError.Text = ex.ToString();
     lblStatus.Text = "Error:";
     lblCalledURL.Text = "";
        }
        this.Cursor = Cursors.Default;
 }
        public void ProcessRequest(HttpContext context)
        {
            string accountName = string.Empty;
              string sharedKey = string.Empty;
              string queueTypeOfCall = string.Empty;
              string message = string.Empty;

              string queueName = string.Empty;
              string messageID = string.Empty;
              string popReceipt = string.Empty;
              string qParameters = string.Empty;

              if (context.Request.Params.AllKeys.Contains("accountname"))
              {
            accountName = context.Request.Params["accountname"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("sharedkey"))
              {
            sharedKey = context.Request.Params["sharedkey"].ToString().Replace(" ", "+");
              }
              if (context.Request.Params.AllKeys.Contains("queuetypeofcall"))
              {
            queueTypeOfCall = context.Request.Params["queuetypeofcall"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("queuename"))
              {
            queueName = context.Request.Params["queuename"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("messageid"))
              {
            messageID = context.Request.Params["messageid"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("popreceipt"))
              {
            popReceipt = context.Request.Params["popreceipt"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("message"))
              {
            message = context.Request.Params["message"].ToString();
              }
              string retVal = string.Empty;
              string retValType = string.Empty;
              retValType = "text/xml";

              Hashtable ht = new Hashtable();

              foreach (string key in context.Request.Params.AllKeys)
            {
              if (key.StartsWith("x-ms-meta-"))
            if (ht.ContainsKey(key))
              ht[key] = string.Format("{0},{1}", ht[key].ToString(), context.Request.Params[key].ToString());
            else
              ht.Add(key, context.Request.Params[key].ToString());
            }

              azureResults ar = new azureResults();
              Finsel.AzureCommands.AzureQueueStorage aqs = new Finsel.AzureCommands.AzureQueueStorage(accountName,
            string.Format("http://{0}.blob.core.windows.net", accountName), sharedKey,
            "SharedKey");

              switch (queueTypeOfCall.ToLower())
              {
            case "addqueuemetadata":
              ar = aqs.MetaData(cmdType.put, queueName, ht);
              retVal = processAzureResults(ar);
              break;
            case "clearqueue":
               ar = aqs.Messages(cmdType.delete, queueName, "", "", "");
               retVal = processAzureResults(ar);
               break;
            case "createmessage":
               ar = aqs.Messages(cmdType.post, queueName, message, "", "");
               retVal = processAzureResults(ar);
               break;
            case "createqueue":
              ht.Add("x-ms-meta-CreatedBy", "Finsel.AzureCommands");
              ar = aqs.Queues(cmdType.put, queueName,qParameters , ht);
              retVal = processAzureResults(ar);
              break;
            case "deletemessage":
              qParameters = string.Format("popreceipt={0}", popReceipt);
              ar = aqs.Messages(cmdType.delete, queueName,"",qParameters , messageID);
              retVal = processAzureResults(ar);
              break;
            case "deletequeue":
              ar = aqs.Queues(cmdType.delete, queueName,qParameters, new Hashtable());
              retVal = processAzureResults(ar);
              break;
            case "deletequeuemetadata":
              ar = aqs.MetaData(cmdType.get, queueName, null);
              retVal = processAzureResults(ar);
              break;
            case "displayqueuemetadata":
              ar = aqs.MetaData(cmdType.get, queueName, null);
              retVal = processAzureResults(ar);
              break;
            case "getmessage":
              ar = aqs.Messages(cmdType.get, queueName, "", qParameters, messageID);
              retVal = processAzureResults(ar);
              break;
            case "getqueuelist":
              ar = aqs.GetQueueList(qParameters);
              retVal = processAzureResults(ar);
              break;
            case "peekmessage":
              ar = aqs.Messages(cmdType.get, queueName, "", "peekonly=true", messageID);
              retVal = processAzureResults(ar);
              break;

            default:
              retVal = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
            <html xmlns='http://www.w3.org/1999/xhtml' >
            <head>
            <title>Finsel Azure Queue Handler Form</title>
            </head>
            <body>
            <form action='aqsHandler.ashx' method='post'>
            <table border='1'>
            <tr>
            <td>Account</td><td><input name='accountname' maxlength='100' /></td>
            </tr><tr>
            <td>Shared Key</td><td><input name='sharedkey' maxlength='100' /></td>
            </tr><tr>
            <td>Container Name</td><td><input name='queuename' maxlength='100' /></td>
            </tr><tr>
            <td>Blob Name</td><td><input name='messageid' maxlength='1240' /></td>
            </tr><tr>
            <td>Parameters</td><td><input name='parameters' maxlength='1240' /></td>
            </tr><tr>
            <td>Type of call</td><td>
            <select name='queuetypeofcall'>
            <option>AddQueueMetadata</option>
            <option value='CreateQueue'>CreateContainer</option>
            <option value='DeleteMessage'>DeleteBlob</option>
            <option value='DeleteQueue'>DeleteContainer</option>
            <option value='DeleteQueueMetadata'>DeleteContainerMetadata</option>
            <option value='DisplayQueueMetadata'>DisplayContainerMetadata</option>
            <option value='GetBlob'>GetMessage</option>
            <option value='GetQueueList'>GetContainerList</option>
            </td>
            </tr><tr>
            <td colspan='2'><input type='submit' /></td>
            </tr>
            </table>
            </form>
            </body>
            </html>";
              retValType = "text/html";

              break;
              }

              context.Response.ContentType = retValType;
              context.Response.Write(retVal);
        }
Exemple #23
0
 private void ProcessResults(azureResults ar)
 {
     txtMetaData.Text = "";
        txtQMetaData.Text = "";
        txtResults.Text = FormatXml(ar.Body);
        lblCalledURL.Text = ar.Url;
        txtURI.Text = ar.Url;
        lblCanonicalUrl.Text = ar.CanonicalResource;
        lblCanonicalUrl.Text = lblCanonicalUrl.Text.Replace("\n","\\n");
        if (Convert.ToInt16(ar.StatusCode) == 0)
        {
     lblError.Text = "";
     lblStatus.Text = "";
        }
        else
        {
     lblError.Text = ar.StatusCode.ToString();
     lblStatus.Text = (ar.Succeeded ? "Success" : "Error");
        }
        StringBuilder msg = new StringBuilder();
        if (ar.Headers != null)
        {
     if (ar.Headers.Count > 0)
     {
      foreach (DictionaryEntry item in ar.Headers)
      {
       msg.AppendFormat("{0}: {1}\r\n", item.Key.ToString(), item.Value.ToString());
       if (item.Key.ToString().StartsWith("x-ms-meta")) // need to populate metadata
       {
        if (ar.Url.Contains(".blob.")) // we have blob metadata
     txtMetaData.Text += string.Format("{0}: {1}\r\n", item.Key.ToString(), item.Value.ToString());
        if (ar.Url.Contains(".queue.")) // we have queue metadata
     txtQMetaData.Text += string.Format("{0}: {1}\r\n", item.Key.ToString(), item.Value.ToString());
       }
      }
     }
        }
        txtHeaders.Text = msg.ToString();
        if (!ar.Succeeded && Convert.ToInt16(ar.StatusCode) != 0)
     MessageBox.Show(ar.StatusCode.ToString(), "Error with request", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
        public void processRequest(string queryName, NameValueCollection htKeys)
        {
            string retVal = string.Empty;
              XmlDocument xdoc = new XmlDocument();
              xdoc.LoadXml(new Utility().getConfigXML());
              XmlNode xNode = xdoc.SelectSingleNode(string.Format("//blobdata[@name='default']"));

              string azureAccount = xNode.Attributes["account"].Value;
              string azureEndpoint = xNode.Attributes["endpoint"].Value;
              string azureSharedKey = xNode.Attributes["accountSharedKey"].Value;
              string blobStorage = xNode.Attributes["endpoint"].Value;

              xNode = xdoc.SelectSingleNode(string.Format("//fragmentData/Setting[@name='HandlerFragments']"));
              string fragmentLocation = xNode.Attributes["value"].Value;
              try
              {
            AzureBlobStorage abs = new AzureBlobStorage(azureAccount, blobStorage, azureSharedKey, "SharedKey");
            azureResults ar = new azureResults();
            // Get the page name and replace the .q extension with .xml
            if (!queryName.ToLower().EndsWith(".xml"))
              queryName += ".xml";
            byte[] xmlFragment = abs.GetBlob(fragmentLocation, queryName, "", ref ar, "");
            if (!ar.Succeeded)
            {
              NotifyError(new Exception(ar.StatusCode.ToString()));
            }
            else
            {

              xdoc = new XmlDocument();
              System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
              xdoc.LoadXml(enc.GetString(xmlFragment));
              /*
               * http://azure-architect.com/portals/16/MOOPData.xsd

               */
              XmlNode xn = xdoc.SelectSingleNode("//storedProcedure[1]");
              string storedProcedureName = xn.Attributes["procedureName"].Value;
              string connectionStringName = xn.Attributes["connectionName"].Value;

              SqlCommand cmd = new SqlCommand(storedProcedureName, new SqlConnection(new Utility().ResolveDataConnection(connectionStringName)));
              cmd.CommandType = CommandType.StoredProcedure;
              XmlNodeList xnl = xdoc.SelectNodes("/MOOPData/luceneData/field");
              Field.Store[] fieldStore = new Field.Store[xnl.Count];
              Field.Index[] indexType = new Field.Index[xnl.Count];
              string[] luceneName = new string[xnl.Count];
              string[] dataName = new string[xnl.Count];
              bool[] isIncludedInOlioSearchFlag = new bool[xnl.Count];
              bool[] isKeyFieldFlag = new bool[xnl.Count];
              string olioSearchFieldName = string.Empty;
              string azureContainerName = string.Empty;
              olioSearchFieldName = xdoc.SelectSingleNode("//MOOPData/luceneData/olioSearchFieldName[1]").InnerText;
              azureContainerName = xdoc.SelectSingleNode("//MOOPData/luceneData/azureContainer[1]").InnerText;
              for (int i = 0; i < xnl.Count; i++)
              {
            XmlNode node = xnl[i];
            switch (node.Attributes["store"].Value.ToLower())
            {
              case "compress": fieldStore[i] = Field.Store.COMPRESS; break;
              case "no": fieldStore[i] = Field.Store.NO; break;
              case "yes": fieldStore[i] = Field.Store.YES; break;
              default: fieldStore[i] = Field.Store.NO; break;
            }

            switch (node.Attributes["index"].Value.ToLower())
            {
              case "analyzed": indexType[i] = Field.Index.ANALYZED; break;
              case "analyzed_no_norms": indexType[i] = Field.Index.ANALYZED_NO_NORMS; break;
              case "no": indexType[i] = Field.Index.NO; break;
              case "no_norms": indexType[i] = Field.Index.NOT_ANALYZED_NO_NORMS; break;
              case "not_analyzed": indexType[i] = Field.Index.NOT_ANALYZED; break;
              case "not_analyzed_no_norms": indexType[i] = Field.Index.NOT_ANALYZED_NO_NORMS; break;
              case "tokenized": indexType[i] = Field.Index.ANALYZED; break;
              case "un_tokenized": indexType[i] = Field.Index.NOT_ANALYZED; break;
              default: indexType[i] = Field.Index.NO; break;
            }
            dataName[i] = node.Attributes["dataName"].Value;
            luceneName[i] = node.Attributes["luceneName"].Value;
            isKeyFieldFlag[i] = node.Attributes["isKeyField"].Value == "true";
            isKeyFieldFlag[i] = node.Attributes["isKeyField"].Value == "true";
            isIncludedInOlioSearchFlag[i] = node.Attributes["isIncludedInOlioSearch"].Value == "true";
              }

              xnl = xdoc.SelectNodes("//parameter");
              foreach (XmlNode node in xnl)
              {
            string parameterName = node.Attributes["parameterName"].Value;
            string urlParameterName = node.Attributes["urlParameterName"].Value;
            string dataType = node.Attributes["dataType"].Value;
            string dataLength = node.Attributes["dataLength"].Value;
            string defaultValue = node.Attributes["defaultValue"].Value;
            if (!parameterName.StartsWith("@"))
              parameterName = "@" + parameterName;
            SqlParameter sp = new SqlParameter();
            sp.ParameterName = parameterName;
            switch (dataType)
            {
              case "bigint": sp.SqlDbType = SqlDbType.BigInt; break;
              case "binary": sp.SqlDbType = SqlDbType.Binary; break;
              case "bit": sp.SqlDbType = SqlDbType.Bit; break;
              case "char": sp.SqlDbType = SqlDbType.Char; break;
              case "date": sp.SqlDbType = SqlDbType.Date; break;
              case "datetime": sp.SqlDbType = SqlDbType.DateTime; break;
              case "datetime2": sp.SqlDbType = SqlDbType.DateTime2; break;
              case "datetimeoffset": sp.SqlDbType = SqlDbType.DateTimeOffset; break;
              case "decimal": sp.SqlDbType = SqlDbType.Decimal; break;
              case "float": sp.SqlDbType = SqlDbType.Float; break;
              case "geography": sp.SqlDbType = SqlDbType.Structured; break;
              case "geometry": sp.SqlDbType = SqlDbType.Structured; break;
              case "hierarchyid": sp.SqlDbType = SqlDbType.Structured; break;
              case "image": sp.SqlDbType = SqlDbType.Image; break;
              case "int": sp.SqlDbType = SqlDbType.Int; break;
              case "money": sp.SqlDbType = SqlDbType.Money; break;
              case "nchar": sp.SqlDbType = SqlDbType.NChar; break;
              case "ntext": sp.SqlDbType = SqlDbType.NText; break;
              case "nvarchar": sp.SqlDbType = SqlDbType.NVarChar; break;
              case "real": sp.SqlDbType = SqlDbType.Real; break;
              case "smalldatetime": sp.SqlDbType = SqlDbType.SmallDateTime; break;
              case "smallint": sp.SqlDbType = SqlDbType.SmallInt; break;
              case "smallmoney": sp.SqlDbType = SqlDbType.SmallMoney; break;
              case "sql_variant": sp.SqlDbType = SqlDbType.Variant; break;
              case "text": sp.SqlDbType = SqlDbType.Text; break;
              case "time": sp.SqlDbType = SqlDbType.Time; break;
              case "timestamp": sp.SqlDbType = SqlDbType.Timestamp; break;
              case "tinyint": sp.SqlDbType = SqlDbType.TinyInt; break;
              case "uniqueidentifier": sp.SqlDbType = SqlDbType.UniqueIdentifier; break;
              case "varbinary": sp.SqlDbType = SqlDbType.VarBinary; break;
              case "varchar": sp.SqlDbType = SqlDbType.VarChar; break;
              case "xml": sp.SqlDbType = SqlDbType.Xml; break;
              default: sp.SqlDbType = SqlDbType.Variant; break;
            }
            switch (urlParameterName.ToLower())
            {
              case "ipaddress": sp.Value = "127.0.0.1"; break;
              case "domainname": sp.Value = ""; break;
              default: if (htKeys[urlParameterName] != null)
                  sp.Value = htKeys[urlParameterName];
                else
                  sp.Value = (defaultValue.ToLower() == "dbnull" ? DBNull.Value
                  : (object)defaultValue);
                break;
            }
            cmd.Parameters.Add(sp);
              }

              cmd.Connection.Open();
              SqlDataReader dr = cmd.ExecuteReader();
              Microsoft.WindowsAzure.StorageCredentialsAccountAndKey scaak = new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(azureAccount, azureSharedKey);
              Microsoft.WindowsAzure.CloudStorageAccount csa = new Microsoft.WindowsAzure.CloudStorageAccount(scaak, false);
              AzureDirectory azureDirectory = new AzureDirectory(csa, azureContainerName, new RAMDirectory());
              bool findexExists = false;
              try
              {
            findexExists = IndexReader.IndexExists(azureDirectory);
            if ((findexExists) && IndexWriter.IsLocked(azureDirectory))
              azureDirectory.ClearLock("write.lock");
              }
              catch (Exception e)
              {
            Trace.WriteLine(e.ToString());
            return;
              }

              IndexWriter idxW = new IndexWriter(azureDirectory, new SnowballAnalyzer("English"), !findexExists, new IndexWriter.MaxFieldLength(1024));
              idxW.SetRAMBufferSizeMB(10.0);
              idxW.SetUseCompoundFile(false);
              idxW.SetMaxMergeDocs(10000);
              idxW.SetMergeFactor(100);
              while (dr.Read())
              {
            StringBuilder olioSearch = new StringBuilder();
            Document doc = new Document();
            for (int i = 0; i <= dataName.GetUpperBound(0); i++)
            {

              if (isKeyFieldFlag[i])
              {

                NotifyCaller(string.Format("Processing {0}", dr[dataName[i]].ToString().ToLower()));
                idxW.DeleteDocuments(new Term(luceneName[i], dr[dataName[i]].ToString().ToLower()));
                doc.Add(new Field(luceneName[i], dr[dataName[i]].ToString().ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
              }
              else
                try
                {
                  doc.Add(new Field(luceneName[i], dr[dataName[i]].ToString(), fieldStore[i], indexType[i]));

                  if (isIncludedInOlioSearchFlag[i])
                    olioSearch.AppendFormat("\r\n{0}", dr[dataName[i]].ToString());
                }
                catch (Exception ex)
                {
                  NotifyError(ex);
                }
            }
            if (olioSearch.ToString() != string.Empty && olioSearchFieldName != string.Empty)
              doc.Add(new Field(olioSearchFieldName, olioSearch.ToString(), Field.Store.NO, Field.Index.ANALYZED));
            idxW.AddDocument(doc);
              }
              idxW.Commit();
              idxW.Close();

            }
              }

              catch (Exception ex)
              {
            MOOPFramework.FrameworkUtility u = new MOOPFramework.FrameworkUtility(new Utility().ResolveDataConnection("sqlAzureConnection"));
            u.LogData("localhost", "quoteSearchLoader", "testing", string.Empty, string.Empty, "", "QueryError", ex.ToString(),
              u.nvc2XML(htKeys));
            //retVal = string.Format("<!-- {0} -->", ex.ToString());
            NotifyError(new Exception("An error occured but it was logged for later review"));
              }
              finally { if (retVal == string.Empty) retVal = "<root />"; }
        }
 private string processAzureResults(azureResults ar)
 {
     StringBuilder retVal = new StringBuilder();
       retVal.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
       retVal.AppendFormat("<azureResults Succeeded=\"{0}\" StatusCode=\"{1}\" >", ar.Succeeded, ar.StatusCode);
       retVal.AppendFormat("<Url>{0}</Url>", System.Web.HttpUtility.HtmlEncode(ar.Url));
       retVal.AppendFormat("<CanonicalResource>{0}</CanonicalResource>", System.Web.HttpUtility.HtmlEncode(ar.CanonicalResource));
       retVal.AppendFormat("<Headers>");
       if (ar.Headers != null)
     if (ar.Headers.Keys.Count > 0)
     {
       foreach (string key in ar.Headers.Keys)
       {
     retVal.AppendFormat("<Header><Key>{0}</Key><Value>{1}</Value></Header>", key, System.Web.HttpUtility.HtmlEncode(ar.Headers[key]));
       }
     }
       retVal.AppendFormat("</Headers>");
       retVal.AppendFormat("<body>{0}</body>", System.Web.HttpUtility.HtmlEncode(ar.Body));
       retVal.Append("</azureResults>");
       return retVal.ToString();
 }
        public void ProcessRequest(HttpContext context)
        {
            string accountName = string.Empty;
              string sharedKey = string.Empty;
              string tableTypeOfCall = string.Empty;

              string tableName = string.Empty;
              string partitionKey = string.Empty;
              string rowKey = string.Empty;
              string docData = string.Empty;
              string tParameters = string.Empty;
              string ifMatch = string.Empty;

              if (context.Request.Params.AllKeys.Contains("accountname"))
              {
            accountName = context.Request.Params["accountname"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("sharedkey"))
              {
            sharedKey = context.Request.Params["sharedkey"].ToString().Replace(" ", "+");
              }
              if (context.Request.Params.AllKeys.Contains("tabletypeofcall"))
              {
            tableTypeOfCall = context.Request.Params["tabletypeofcall"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("tablename"))
              {
            tableName = context.Request.Params["tablename"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("partitionkey"))
              {
            partitionKey = context.Request.Params["partitionkey"].ToString();
              }
              if (context.Request.Params.AllKeys.Contains("rowkey"))
              {
            rowKey = context.Request.Params["rowkey"].ToString();
              }

              // This will be an XML Only request so we need to parse it to get what we need
              if (context.Request.ContentType=="text/xml")// .Params.AllKeys.Contains("docdata"))
              {
            StreamReader reader = new StreamReader( context.Request.InputStream);
            Regex regex = new Regex(@">\s*<");
            string cleanXml = regex.Replace(reader.ReadToEnd(), "><");

              XmlDocument xdoc = new XmlDocument();
            xdoc.PreserveWhitespace = false;
            xdoc.LoadXml(cleanXml);
            //Instantiate an XmlNamespaceManager object.

            //Add the namespaces used in books.xml to the XmlNamespaceManager.
            accountName = xdoc.SelectSingleNode("/root/AccountName").InnerText;
            sharedKey = xdoc.SelectSingleNode("/root/SharedKey").InnerText; ;
            tableTypeOfCall = xdoc.SelectSingleNode("/root/TypeOfCall").InnerText;
            tableName = xdoc.SelectSingleNode("/root/TableName").InnerText;
            try { partitionKey = xdoc.SelectSingleNode("/root/PartitionKey").InnerText; }
            catch { }
            try { rowKey = xdoc.SelectSingleNode("/root/RowKey").InnerText; }
            catch { }
            try { tParameters = xdoc.SelectSingleNode("/root/Parameters").InnerText; }
            catch { }
            try { ifMatch = xdoc.SelectSingleNode("/root/IfMatch").InnerText; }
            catch { }
            docData = xdoc.SelectSingleNode("/root/docData").InnerXml.Replace("<![CDATA[", "").Replace("]]>", "");

              }
              rowKey = rowKey.Replace(" ", "").Replace("\n", "").Replace("\r", "").Replace("\t", "");
              partitionKey = partitionKey.Replace(" ", "").Replace("\n", "").Replace("\r", "").Replace("\t", "");

              string retVal = string.Empty;
              string retValType = string.Empty;
              retValType = "text/xml";

              Hashtable ht = new Hashtable();

              foreach (string key in context.Request.Params.AllKeys)
              {
              if (key != null)
              {
              if (key.StartsWith("x-ms-meta-"))
                  if (ht.ContainsKey(key))
                      ht[key] = string.Format("{0},{1}", ht[key].ToString(), context.Request.Params[key].ToString());
                  else
                      ht.Add(key, context.Request.Params[key].ToString());
              }
              }

              azureResults ar = new azureResults();
              azureHelper ah = new azureHelper(accountName, string.Format("http://{0}.blob.core.windows.net", accountName), sharedKey,
            "SharedKey");
              Finsel.AzureCommands.AzureTableStorage ats = new Finsel.AzureCommands.AzureTableStorage(accountName,
            string.Format("http://{0}.blob.core.windows.net", accountName), sharedKey,
            "SharedKey");

              switch (tableTypeOfCall.ToLower())
              {
              case "bulkinsert":
              ar.Body = ah.entityGroupTransaction(cmdType.post, tableName, docData);
              ar.Succeeded = true;
              break;
              case "bulkupdate":
              ar.Body =  ah.entityGroupTransaction(cmdType.put, tableName, docData);;
              ar.Succeeded = true;
              break;
            case "createtable": ar = ats.Tables(cmdType.post, tableName); retVal = processAzureResults(ar); break;
            case "deleteentity": ar = ats.Entities(cmdType.delete, tableName, partitionKey, rowKey, "", ""); retVal = processAzureResults(ar); break;
            case "deletetable": ar = ats.Tables(cmdType.delete, tableName); retVal = processAzureResults(ar); break;
            case "getentity": ar = ats.Entities(cmdType.get, tableName, partitionKey, rowKey, "", tParameters, ifMatch); retVal = processAzureResults(ar); break;
            case "insertentity": ar = ats.Entities(cmdType.post, tableName, partitionKey, rowKey, docData, ""); retVal = processAzureResults(ar); break;
            case "mergeentity": ar = ats.Entities(cmdType.merge, tableName, partitionKey, rowKey, docData,tParameters, ifMatch ); retVal = processAzureResults(ar); break;
            case "queryentities": ar = ats.Entities(cmdType.get, tableName, partitionKey, rowKey, "", ""); retVal = processAzureResults(ar); break;
            case "querytables": ar = ats.Tables(cmdType.get, ""); retVal = processAzureResults(ar); break;
            case "updateentity": ar = ats.Entities(cmdType.put, tableName, partitionKey, rowKey, docData,tParameters, ifMatch); retVal = processAzureResults(ar); break;
            default:
              retVal = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
            <html xmlns='http://www.w3.org/1999/xhtml' >
            <head>
            <title>Finsel Azure Tables Handler Form</title>
            </head>
            <body>
            <form action='atsHandler.ashx' method='post'>
            <table border='1'>
            <tr>
            <td>Account</td><td><input name='accountname' maxlength='100' /></td>
            </tr><tr>
            <td>Shared Key</td><td><input name='sharedkey' maxlength='100' /></td>
            </tr><tr>
            <td>Table Name</td><td><input name='tablename' maxlength='100' /></td>
            </tr><tr>
            <td>Partition Key</td><td><input name='partitionkey' maxlength='100' /></td>
            </tr><tr>
            <td>Row key</td><td><input name='rowkey' maxlength='100' /></td>
            </tr><tr>
            <td>Document Data</td><td><input name='docdata' Width='398px' Height='92px' TextMode='MultiLine' /></td>
            </tr><tr>
            <td>Parameters</td><td><input name='parameters' maxlength='1240' /></td>
            </tr><tr>
            <td>Type of call</td><td>
            <select name='tabletypeofcall'>
            <option>CreateTable</option>
            <option>DeleteEntity</option>
            <option>DeleteTable</option>
            <option>InsertEntity]</option>
            <option>MergeEntity</option>
            <option>QueryEntities</option>
            <option>QueryTables</option>
            <option>UpdateEntity</option>
            </td>
            </tr><tr>
            <td colspan='2'><input type='submit' /></td>
            </tr>
            </table>
            </form>
            </body>
            </html>";
              retValType = "text/html";

              break;
              }

              context.Response.ContentType = retValType;
              context.Response.Write(retVal);
        }
        /// <summary>
        /// Execute requests on Azure Queue Messages
        /// </summary>
        /// <param name="cmd">Type of command: Get, Delete, Post are supported</param>
        /// <param name="queueName">Name of the queue</param>
        /// <param name="messageBody">Body of the message. Max is 8KB</param>
        /// <param name="parameters">Uri Parameters to be included</param>
        /// <param name="messageID">MessageID</param>
        /// <returns>An azureResults object with the results of the request.</returns>
        public azureResults Messages(cmdType cmd, string queueName, string messageBody, string parameters, string messageID)
        {
            azureResults retVal = new azureResults();
              HttpStatusCode success = HttpStatusCode.NotImplemented;
              Hashtable headers = new Hashtable();
              try
              {
            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/messages", auth.EndPoint, queueName);
            if (messageID != string.Empty)
            {
              requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}/{1}", requestUrl, messageID);
            }
            if (parameters != string.Empty && parameters != null)
            {
              if (!parameters.StartsWith("?"))
            parameters = string.Format("?{0}", parameters);
              requestUrl += string.Format(CultureInfo.CurrentCulture, "{0}", parameters);
            }
            requestDate = DateTime.UtcNow;
            switch (cmd)
            {
              case cmdType.get:
            method = "GET";
            // do GET
            success = HttpStatusCode.OK;
            break;
              case cmdType.delete:
            method = "DELETE";
            // do DELETE
            success = HttpStatusCode.NoContent;

            break;
              case cmdType.post:
            method = "POST";
            messageBody = string.Format("<QueueMessage><MessageText><![CDATA[{0}]]></MessageText></QueueMessage>", messageBody);
            success = HttpStatusCode.Created;
            break;
              default:
            retVal.StatusCode = HttpStatusCode.NotImplemented;
            break;
            }
            headers.Add("Content-Type", contentType);
            retVal = ad.ProcessRequest(cmd, requestUrl, messageBody, headers);
            retVal.Succeeded = (retVal.StatusCode == success);

              }
              catch (HttpException hex)
              {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
              }
              catch (Exception ex)
              {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
              }

              return retVal;
        }
Exemple #28
0
 public void LoadBlobs()
 {
     if (FileFilter == string.Empty || FileFilter == null)
       FileFilter = "*";
        System.Collections.Hashtable metadata = new System.Collections.Hashtable();
        metadata.Add("loadingSource", "finsel.AzureBlobLoader");
        metadata.Add("dateLoaded",DateTime.Now.ToShortDateString());
        azureResults ar = new azureResults();
        AzureBlobStorage abs = new AzureBlobStorage(Account, EndPoint, SharedKey, "SharedKey");
        if (Container == "*")
        {
     foreach (string newContainer in Directory.GetDirectories(StartingDirectory))
     {
      Container = newContainer.Replace(StartingDirectory,"");
      if (Container.StartsWith(@"\")) Container = Container.Substring(1);
      ar = abs.Containers(cmdType.get, Container, new System.Collections.Hashtable());
      if (!ar.Succeeded)
       ar = abs.Containers(cmdType.put, Container, metadata);
      if(ar.Succeeded)
       ProcessDirectory(abs, metadata, newContainer, newContainer );
     }
     // Process $root
     ar = abs.Containers(cmdType.get, "$root%", new System.Collections.Hashtable());
     if (ar.Succeeded)
     ProcessDirectory(abs, metadata, StartingDirectory, "$root");
        }
        else
        {
     ar = abs.Containers(cmdType.get, Container, new System.Collections.Hashtable());
     if (!ar.Succeeded)
      ar = abs.Containers(cmdType.put, Container, metadata);
     ProcessDirectory(abs, metadata, StartingDirectory, StartingDirectory);
        }
 }
        /// <summary>
        /// Queues handles queue level operations against Azure Queue Storage
        /// </summary>
        /// <param name="cmd">The type of operation you want to commit: Delete, Post, Put, Get </param>
        /// <param name="queueName">Name of the table to perform the command agains</param>
        /// <param name="parameters">Uri Parameters to be included</param>
        /// <param name="htMetaData">Hashtable of Name-Value pairs of metadata for the queue</param>
        /// <returns>An azureResults showing the results of the request.</returns>
        public azureResults Queues(cmdType cmd, string queueName, string parameters, Hashtable htMetaData)
        {
            azureResults retVal = new azureResults();
              Hashtable headers = new Hashtable();

              try
              {
            if (parameters == string.Empty && !queueName.EndsWith("/") && queueName != string.Empty)
              queueName = queueName + "/";
            StringBuilder sb = new StringBuilder();
            string sendBody = string.Empty;
            string rtnBody = string.Empty;
            string requestUrl = string.Format(CultureInfo.CurrentCulture, auth.EndPoint);
            requestDate = DateTime.UtcNow;
            HttpStatusCode success = HttpStatusCode.NotImplemented;
            switch (cmd)
            {
              case cmdType.get:
            method = "GET";
            if (queueName == string.Empty)
            {
              requestUrl += string.Format(CultureInfo.CurrentCulture, "?comp=list");
            }
            if (parameters != string.Empty)
            {
              if (!parameters.StartsWith("?"))
                parameters = "?" + parameters;
              requestUrl += string.Format(CultureInfo.CurrentCulture, "{0}", parameters);
            }
            success = HttpStatusCode.OK;

            break;
              case cmdType.delete:
            method = "DELETE";
            requestUrl += string.Format(CultureInfo.CurrentCulture, string.Format("{0}", queueName));
            success = HttpStatusCode.NoContent;

            break;
              case cmdType.put:
            method = "PUT";
            requestUrl += string.Format(CultureInfo.CurrentCulture, string.Format("{0}", queueName));
            // do PUT
            if (htMetaData.Count > 0)
              foreach (DictionaryEntry item in htMetaData)
              {
                string metaDataName = item.Key.ToString().ToLower().Replace(" ", "-").Replace("\r", "");

                if (!metaDataName.StartsWith("x-ms-meta-"))
                  metaDataName = "x-ms-meta-" + metaDataName;

                try
                {
                  if (item.Value.ToString().Trim() != string.Empty)
                    headers.Add(ac.CleanMetaDataNames(metaDataName), item.Value.ToString());
                }
                catch
                {

                }
              }
            success = HttpStatusCode.Created;
            break;
              default:
            break;
            }
            retVal = ad.ProcessRequest(cmd, requestUrl, "", headers);
            retVal.Succeeded = (retVal.StatusCode == success);
              }
              catch (HttpException hex)
              {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
              }
              catch (Exception ex)
              {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
              }
              return retVal;
        }
        /// <summary>
        /// PutBlob creates/updates a blob within Microsoft's Azure Blob Storage
        /// </summary>
        /// <param name="ContentLength">How many bytes are in the Content</param>
        /// <param name="ContentType">Content type of the blob. application/bin is used by default if nothing else is passed in</param>
        /// <param name="Content">A byte array representing the blob being stored.</param>
        /// <param name="containerName">The name of the container to store the blob in.</param>
        /// <param name="blobName">The name of the blob. Can inlcude paths (/path/blob.txt, for instance)</param>
        /// <param name="htMetaData">A hashtable containing the Name-Value pairs of MetaData.</param>
        /// <returns></returns>
        public azureResults PutBlobWithBlocks(string ContentType, byte[] Content, string containerName, string blobName, Hashtable htMetaData, int BlockLength, int StartBlock)
        {
            azureResults retVal = new azureResults();
              try
              {
            StringBuilder sb = new StringBuilder();
            string sendBody = string.Empty;
            string rtnBody = string.Empty;

            string requestUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/{2}", auth.EndPoint, containerName, blobName);
            requestDate = DateTime.UtcNow;

            string blockURI = string.Empty;
            Hashtable htHeaders = htMetaData;
            htHeaders.Add("Content-Type", ContentType);
            azureDirect ad = new azureDirect(auth.Account, auth.EndPoint, auth.SharedKey, auth.KeyType);
            int blocksCount = (int)Math.Ceiling((double)Content.Length / BlockLength);
            string[] blockIds = new string[blocksCount];
            int startPosition = StartBlock * BlockLength;
            int blockIdPosition = 0;
            for (int i = StartBlock; i < blocksCount; i++)
            {
              blockIds[blockIdPosition] = Convert.ToBase64String(BitConverter.GetBytes(i));
              blockURI = string.Format("{0}?comp=block&blockid={1}", requestUrl, blockIds[blockIdPosition]);
              blockIdPosition++;
              byte[] blockContent = new byte[BlockLength];
              Array.Copy(Content, startPosition, blockContent, 0, (startPosition + BlockLength > Content.Length ? Content.Length - startPosition : BlockLength));
              retVal = ad.ProcessRequest(cmdType.put, blockURI, blockContent, htHeaders);
              while (retVal.StatusCode != HttpStatusCode.Created)
            retVal = ad.ProcessRequest(cmdType.put, blockURI, blockContent, htHeaders);
              startPosition += BlockLength;
              Console.WriteLine(i);
            }
            blockURI = string.Format("{0}?comp=blocklist", requestUrl);
            StringBuilder sbBlockList = new StringBuilder();
            sbBlockList.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<BlockList>");
            foreach (string id in blockIds)
            {
              sbBlockList.AppendFormat("<Block>{0}</Block>\n", id);
            }
            sbBlockList.Append("</BlockList>");
            retVal = ad.ProcessRequest(cmdType.put, blockURI, new System.Text.ASCIIEncoding().GetBytes(sbBlockList.ToString()), htHeaders);

              }
              catch (HttpException hex)
              {
            retVal.StatusCode = (HttpStatusCode)hex.GetHttpCode();
            retVal.Succeeded = false;
            retVal.Body = hex.GetHtmlErrorMessage();
              }
              catch (Exception ex)
              {
            retVal.StatusCode = HttpStatusCode.SeeOther;
            retVal.Body = ex.ToString();
            retVal.Succeeded = false;
              }
              return retVal;
        }