Microsoft.Protocols.TestSuites.MS_WOPI.WOPIResponseHelper.GetBytesStringValue C# (CSharp) Method

GetBytesStringValue() public static method

A method used to get byte string values from the response. 50 bytes per line.
public static GetBytesStringValue ( byte rawData ) : string
rawData byte A parameter represents a WOPI response instance which contains the response body.
return string
        public static string GetBytesStringValue(byte[] rawData)
        {
            if (null == rawData || 0 == rawData.Length)
            {
                throw new ArgumentNullException("rawData");
            }

            string bitsString = BitConverter.ToString(rawData);
            char[] splitSymbol = new char[] { '-' };
            string[] bitStringValue = bitsString.Split(splitSymbol, StringSplitOptions.RemoveEmptyEntries);

            // Output 50 bits string value per line.
            int wrapNumberOfBitString = 50;
            if (bitStringValue.Length <= wrapNumberOfBitString)
            {
                return bitsString;
            }
            else
            {
                StringBuilder strBuilder = new StringBuilder();
                int beginIndexOfOneLine = 0;
                bool stopSplitLine = false;
                int oneLineLength = wrapNumberOfBitString * 3;

                // Wrap the bits string value.
                do
                {
                    // If it meets the last line, get the actual bits value.
                    if (beginIndexOfOneLine + oneLineLength > bitsString.Length)
                    {
                        oneLineLength = bitsString.Length - beginIndexOfOneLine;
                        stopSplitLine = true;
                    }

                    string oneLineContent = bitsString.Substring(beginIndexOfOneLine, oneLineLength);
                    beginIndexOfOneLine = beginIndexOfOneLine + oneLineLength;
                    strBuilder.AppendLine(oneLineContent);
                }
                while (!stopSplitLine);

                return strBuilder.ToString();
            }
        }

Usage Example

        /// <summary>
        /// A method used to log the HTTP transport information.
        /// </summary>
        /// <param name="wopiRequest">A parameter represents the request instance of a WOPI operation. It must not be null.</param>
        /// <param name="requestBody">A parameter represents the request body of a WOPI operation.</param>
        /// <param name="wopiResponse">A parameter represents the response of a WOPI operation. It must not be null if the "isResponse" parameter is true.</param>
        /// <param name="operationName">A parameter represents the WOPI operation name.</param>
        /// <param name="isResponse">A parameter represents the HTTP transport information recorded by this method whether belong to the response of a WOPI operation.</param>
        private void LogHttpTransportInfo(HttpWebRequest wopiRequest, byte[] requestBody, WOPIHttpResponse wopiResponse, WOPIOperationName operationName, bool isResponse = true)
        {
            #region validate parameters
            if (isResponse)
            {
                if (null == wopiResponse)
                {
                    throw new ArgumentNullException("wopiResponse");
                }

                // For log response, requires the request URL.
                if (null == wopiRequest)
                {
                    throw new ArgumentNullException("wopiRequest");
                }
            }
            else
            {
                if (null == wopiRequest)
                {
                    throw new ArgumentNullException("wopiRequest");
                }
            }
            #endregion

            #region headers

            // Build headers information
            StringBuilder       headerInfoBuilder = new StringBuilder();
            WebHeaderCollection headers           = isResponse ? wopiResponse.Headers : wopiRequest.Headers;
            if (null != headers && 0 != headers.Count)
            {
                foreach (string oheaderNameItem in headers.AllKeys)
                {
                    string headerValueString = string.Format(
                        "[{0}]:({1})",
                        oheaderNameItem,
                        headers[oheaderNameItem]);

                    headerInfoBuilder.AppendLine(headerValueString);
                }
            }

            #endregion

            #region body information

            // Build body information
            byte[] httpBodyOfResponse = null;
            if (isResponse && 0 != wopiResponse.ContentLength)
            {
                httpBodyOfResponse = WOPIResponseHelper.GetContentFromResponse(wopiResponse);
            }

            byte[]        httpBody        = isResponse ? httpBodyOfResponse : requestBody;
            StringBuilder bodyInfoBuilder = new StringBuilder();
            if (null != httpBody && 0 != httpBody.Length)
            {
                switch (operationName)
                {
                case WOPIOperationName.CheckFileInfo:
                case WOPIOperationName.ReadSecureStore:
                case WOPIOperationName.CheckFolderInfo:
                case WOPIOperationName.EnumerateChildren:
                {
                    if (isResponse)
                    {
                        // log response body by JSON format
                        bodyInfoBuilder.AppendLine(Encoding.UTF8.GetString(httpBody));
                    }

                    break;
                }

                case WOPIOperationName.PutRelativeFile:
                {
                    if (isResponse)
                    {
                        // log the body by JSON format
                        bodyInfoBuilder.AppendLine(Encoding.UTF8.GetString(httpBody));
                    }
                    else
                    {
                        // log the body as bytes string value
                        bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
                    }

                    break;
                }

                case WOPIOperationName.GetFile:
                {
                    if (isResponse)
                    {
                        // log the body as bytes string value
                        bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
                    }

                    break;
                }

                case WOPIOperationName.PutFile:
                {
                    if (!isResponse)
                    {
                        // log the body as bytes string value
                        bodyInfoBuilder.AppendLine(WOPIResponseHelper.GetBytesStringValue(httpBody));
                    }

                    break;
                }
                }
            }

            #endregion

            string credentialInfo = string.Format(
                "User:[{0}] Domain:[{1}]",
                this.defaultUserName,
                this.defaultDomain);

            string logTitle = string.Format(
                "{0} HTTP {1}{2} for [{3}] operation:",
                isResponse ? "Receive" : "Sending",
                isResponse ? "Response" : "Request",
                isResponse ? string.Empty : " with " + credentialInfo,
                operationName);

            StringBuilder logBuilder = new StringBuilder();
            logBuilder.AppendLine(logTitle);

            string urlInfor = string.Format("Request URL:[{0}]", wopiRequest.RequestUri.AbsoluteUri);
            logBuilder.AppendLine(urlInfor);

            string httpMethodValue = string.Format("HTTP method:[{0}]", this.GetHttpMethodForWOPIOperation(operationName));
            logBuilder.AppendLine(httpMethodValue);

            if (isResponse)
            {
                string httpStatusCodeValue = string.Format("HTTP status code:[{0}]", wopiResponse.StatusCode);
                logBuilder.AppendLine(httpStatusCodeValue);
            }

            string headerInfo = string.Format("Headers:\r\n{0}", 0 == headerInfoBuilder.Length ? "None" : headerInfoBuilder.ToString());
            logBuilder.AppendLine(headerInfo);

            string bodyInfo = string.Format("Body:\r\n{0}", 0 == bodyInfoBuilder.Length ? "None" : bodyInfoBuilder.ToString());
            logBuilder.AppendLine(bodyInfo);

            this.Site.Log.Add(LogEntryKind.Debug, logBuilder.ToString());
        }