Example #1
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;
               }
        }
Example #2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            Int64 iCounter = 0;

               string regexCSVSplit = string.Empty;
               regexCSVSplit = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
               regexCSVSplit = @"([^\\x22\\]*(:?:\\.[^\\x22\\]*)*)\\x22,?|([^,]+),?|,";
               System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regexCSVSplit);
               string tableName = "MaxMindData";
               string cityPartition = "Cities";
               string blockPartition = "IPBlocks";
               this.Cursor = Cursors.WaitCursor;
               System.IO.StreamReader file =   new System.IO.StreamReader(txtCityFile.Text);
               string inputString = string.Empty;
               AzureTableStorage ats = new AzureTableStorage(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey");
               StringBuilder sb = new StringBuilder();
               sb.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"">");
               int entityCount = 0;
               ats.Tables(cmdType.post, tableName);
               inputString = file.ReadLine(); // read copyright
               inputString = file.ReadLine(); // read column headers
               azureHelper ah = new azureHelper(ats.auth);
               while ((inputString = file.ReadLine()) != null)
               {
            iCounter++;

            string[] dataSet = r.Split(inputString);
            for (int i = 0; i < dataSet.GetUpperBound(0); i++)
             dataSet[i] = dataSet[i].ToString().Replace("\"", "");
            if ((iCounter % 100) == 0)
            {
             Notify(string.Format("Processing location {0}: {1}", iCounter, DateTime.Now.ToString("O")));
            }
            //locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
               // string ds = string.Format(locationTemplate, tableName, cityPartition, dataSet[0], inputString);
            sb.AppendFormat(locationTemplate, dataSet);
            entityCount++;
            if (entityCount >= 99)
            {
             sb.Append("</entry>");
            // Notify(string.Format("Transmitting Group of transactions, processed through {0}",iCounter));
             ah.entityGroupTransaction(cmdType.post , tableName, sb.ToString());
             sb = new StringBuilder();
             sb.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"">");
             entityCount = 0;
            }
            if ((iCounter % 100)==0)
             Application.DoEvents();
               }
               if (entityCount > 0)
               {
            Notify("Transmitting Final Group of transactions");
            sb.Append("</entry>");
            ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString());
            sb = new StringBuilder();
            entityCount = 0;
               }
               file.Close();

               sb = new StringBuilder();
               iCounter = 0;
               sb.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"">");
               entityCount = 0;
               file = new System.IO.StreamReader(txtBlocksFile.Text);
               inputString = string.Empty;
               inputString = file.ReadLine(); // read copyright
               inputString = file.ReadLine(); // read column headers
               Int64 ipLoadRecord = 0;
               while ((inputString = file.ReadLine()) != null)
               {
            iCounter++;

            string[] dataSet = r.Split(inputString);
            for (int i = 0; i < dataSet.GetUpperBound(0); i++)
             dataSet[i] = dataSet[i].ToString().Replace("\"", "");
            if ((iCounter % 100) == 0)
            {
             Notify(string.Format("Processing location {0}: {1}", iCounter, DateTime.Now.ToString("O")));
            }
            sb.AppendFormat(ipTemplate, dataSet);
            entityCount++;
            if (entityCount >= 500)
            {
             sb.Append("</entry>");
              Notify(string.Format("Transmitting Group of transactions, processed through {0}",iCounter));
             ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString());
             sb = new StringBuilder();
             sb.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"">");
             entityCount = 0;
            }
            if ((iCounter % 100) == 0)
            {
             Notify(string.Format("Processed through {0}", iCounter));
             Application.DoEvents();
            }
               }
               if (entityCount > 0)
               {
            Notify("Transmitting Final Group of transactions");
            sb.Append("</entry>");
            ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString());
            sb = new StringBuilder();
            entityCount = 0;
               }
               file.Close();
               this.Cursor = Cursors.Default;
        }
Example #3
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 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);
        }