public HttpStatusCode InsertTableEntity(string tableName, TableEntity pEntity, ContType pContentType = ContType.applicationIatomIxml, AcceptType pAcceptType = AcceptType.applicationIjson, ResponseType pResponseType = ResponseType.returnContent, bool useSharedKeyLite = false)
        {
            OperationResultsClear();;
            string timestamp = GetDateHeader();

            //RoSchmi: for tests
            //timestamp = "Wed, 21 Oct 2020 08:53:19 GMT";



            string content = string.Empty;

            string contentType = getContentTypeString(pContentType);
            string acceptType  = getAcceptTypeString(pAcceptType);

            //long totalMemory = GC.GetTotalMemory(true);

            switch (contentType)
            {
            case "application/json":
            {
                content = pEntity.ReadJson();
            }
            break;

            case "application/atom+xml":
            {
                content =
                    String.Format("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><entry 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\">" +
                                  "<id>http://{0}.table.core.windows.net/{5}(PartitionKey='{2}',RowKey='{3}')</id>" +
                                  "<title/><updated>{1}</updated>" +
                                  "<author><name /></author>" +
                                  "<content type=\"application/atom+xml\"><m:properties><d:PartitionKey>{2}</d:PartitionKey><d:RowKey>{3}</d:RowKey>" +
                                  "{4}" +
                                  "</m:properties>" +
                                  "</content>" +
                                  "</entry>", _account.AccountName, timestamp, pEntity.PartitionKey, pEntity.RowKey, GetTableXml(pEntity.Properties), tableName);
            }
            break;

            default:
            {
                throw new NotSupportedException("ContentType must be 'application/json' or 'application/atom+xml'");
            }
            }
            string HttpVerb      = "POST";
            int    contentLength = 0;

            byte[] payload = GetBodyBytesAndLength(content, out contentLength);
            content = null;     // free memory
            string ContentMD5 = string.Empty;

            byte[] hashContentMD5 = null;

            var authorizationHeader = CreateTableAuthorizationHeader(payload, String.Format("/{0}/{1}", _account.AccountName, tableName + "()"), timestamp, HttpVerb, pContentType, out ContentMD5, out hashContentMD5, useSharedKeyLite);

            string urlPath = String.Format("{0}", tableName);

            string canonicalizedResource = String.Format("/{0}/{1}", _account.AccountName, urlPath);

            string canonicalizedHeaders = String.Format("Date:{0}\nx-ms-date:{1}\nx-ms-version:{2}", timestamp, timestamp, VersionHeader);

            string TableEndPoint = _account.UriEndpoints["Table"].ToString();

            Uri uri = new Uri(TableEndPoint + "/" + tableName + "()");

            var tableTypeHeaders = new Hashtable
            {
                { "Accept-Charset", "UTF-8" },
                { "MaxDataServiceVersion", "3.0;NetFx" },
                { "Content-Type", contentType },
                { "DataServiceVersion", "3.0" },
                { "Prefer", getResponseTypeString(pResponseType) },
                { "Content-MD5", ContentMD5 }
            };


            if (_fiddlerIsAttached)
            {
                AzureStorageHelper.AttachFiddler(_fiddlerIsAttached, _fiddlerIP, _fiddlerPort);
            }

            BasicHttpResponse response = new BasicHttpResponse();

            try
            {
                AzureStorageHelper.SetDebugMode(_debug);
                AzureStorageHelper.SetDebugLevel(_debug_level);

                response = AzureStorageHelper.SendWebRequest(caCerts, uri, authorizationHeader, timestamp, VersionHeader, payload, contentLength, HttpVerb, false, acceptType, tableTypeHeaders);
                _OperationResponseETag = response.ETag;
                _OperationResponseMD5  = response.Content_MD5;
                return(response.StatusCode);
            }
            catch (OutOfMemoryException ex1)
            {
                throw new OutOfMemoryException("Exc at 01: " + ex1.Message);
            }
            catch (Exception ex)
            {
                _Print_Debug("Exception was cought: " + ex.Message);
                //Debug.WriteLine("Exception was cought: " + ex.Message);
                response.StatusCode = HttpStatusCode.Forbidden;
                return(response.StatusCode);
            }
        }
Пример #2
0
        public HttpStatusCode UpdateTableEntity(string tableName, string partitionKey, string rowKey, TableEntity pEntity, ContType pContentType = ContType.applicationIatomIxml, AcceptType pAcceptType = AcceptType.applicationIjson, ResponseType pResponseType = ResponseType.returnContent, string ETag = "", bool useSharedKeyLite = false)
        {
            OperationResultsClear();;
            string timestamp = GetDateHeader();
            string content   = string.Empty;

            string contentType = getContentTypeString(pContentType);
            string acceptType  = getAcceptTypeString(pAcceptType);

            switch (contentType)
            {
            case "application/json":
            {
                content = pEntity.ReadJson();
            }
            break;

            case "application/atom+xml":
            {
                content = StringUtilities.Format("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><entry 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\">" +
                                                 "<id>http://{0}.table.core.windows.net/{5}(PartitionKey='{2}',RowKey='{3}')</id>" +
                                                 "<title/><updated>{1}</updated>" +
                                                 "<author><name /></author>" +
                                                 "<content type=\"application/atom+xml\"><m:properties><d:PartitionKey>{2}</d:PartitionKey><d:RowKey>{3}</d:RowKey>" +
                                                 "{4}" +
                                                 "</m:properties>" +
                                                 "</content>" +
                                                 "</entry>", _account.AccountName, timestamp, pEntity.PartitionKey, pEntity.RowKey, GetTableXml(pEntity.Properties), tableName);
            }
            break;

            default:
            {
                throw new NotSupportedException("ContentType must be 'application/json' or 'application/atom+xml'");
            }
            }


            string HttpVerb      = "PUT";
            var    contentLength = 0;
            var    payload       = GetBodyBytesAndLength(content, out contentLength);
            string ContentMD5    = string.Empty;

            string matchString = StringUtilities.Format("{1}(PartitionKey='{2}',RowKey='{3}')", _account.AccountName, tableName, partitionKey, rowKey);

            var authorizationHeader = CreateTableAuthorizationHeader(payload, StringUtilities.Format("/{0}/{1}", _account.AccountName, matchString), timestamp, HttpVerb, ContType.applicationIatomIxml, out ContentMD5, useSharedKeyLite);

            string TableEndPoint = _account.UriEndpoints["Table"].ToString();

            Uri uri = new Uri(TableEndPoint + "/" + matchString);

            var tableTypeHeaders = new Hashtable();

            if (ETag == "")
            {
                tableTypeHeaders.Add("If-Match", "*");
            }
            else
            {
                tableTypeHeaders.Add("If-Match", ETag);
            }

            tableTypeHeaders.Add("Accept-Charset", "UTF-8");
            tableTypeHeaders.Add("MaxDataServiceVersion", "3.0;NetFx");
            tableTypeHeaders.Add("Content-Type", contentType);
            tableTypeHeaders.Add("DataServiceVersion", "3.0");
            tableTypeHeaders.Add("Prefer", getResponseTypeString(pResponseType));
            tableTypeHeaders.Add("Content-MD5", ContentMD5);

            if (_fiddlerIsAttached)
            {
                AzureStorageHelper.AttachFiddler(_fiddlerIsAttached, _fiddlerIP, _fiddlerPort);
            }

            BasicHttpResponse response = new BasicHttpResponse();

            try
            {
                AzureStorageHelper.SetDebugMode(_debug);
                AzureStorageHelper.SetDebugLevel(_debug_level);
                response = AzureStorageHelper.SendWebRequest(uri, authorizationHeader, timestamp, VersionHeader, payload, contentLength, HttpVerb, false, acceptType, tableTypeHeaders);
                _OperationResponseETag = response.ETag;
                _OperationResponseMD5  = response.Content_MD5;
                return(response.StatusCode);
            }
            catch (Exception ex)
            {
                _Print_Debug("Exception was cought: " + ex.Message);
                response.StatusCode = HttpStatusCode.Forbidden;
                return(response.StatusCode);
            }
        }