private static string FormatString(string content)
        {
            try
            {
                if (CloudException.IsJson(content))
                {
                    return(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content), Formatting.Indented));
                }
                if (CloudException.IsXml(content))
                {
                    return(XDocument.Parse(content).ToString());
                }
            }
            catch
            {
                return(content);
            }

            if (content.Length > Authentication.Constants.MaxContentLength)
            {
                return(content.Substring(0, Authentication.Constants.MaxContentLength) + "\r\nDATA TRUNCATED DUE TO SIZE\r\n");
            }

            return(content);
        }
        /// <summary>
        /// Initializes a new instance of AzureSMProfile and loads its content from specified path.
        /// Any errors generated in the process are stored in ProfileLoadErrors collection.
        /// </summary>
        /// <param name="path">Location of profile file on disk.</param>
        public AzureSMProfile(string path) : this()
        {
            ProfilePath       = path;
            ProfileLoadErrors = new List <string>();

            if (!AzureSession.DataStore.DirectoryExists(AzureSession.ProfileDirectory))
            {
                AzureSession.DataStore.CreateDirectory(AzureSession.ProfileDirectory);
            }

            if (AzureSession.DataStore.FileExists(ProfilePath))
            {
                string contents = AzureSession.DataStore.ReadFileAsText(ProfilePath);

                IProfileSerializer serializer;

                if (CloudException.IsXml(contents))
                {
                    serializer = new XmlProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
                else if (CloudException.IsJson(contents))
                {
                    serializer = new JsonProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
            }
        }
 public static string FormatString(string content)
 {
     if (CloudException.IsXml(content))
     {
         return(TryFormatXml(content));
     }
     else if (CloudException.IsJson(content))
     {
         return(TryFormatJson(content));
     }
     else
     {
         return(content);
     }
 }
        /// <summary>
        /// Initializes a new instance of AzureSMProfile and loads its content from specified path.
        /// Any errors generated in the process are stored in ProfileLoadErrors collection.
        /// </summary>
        /// <param name="path">Location of profile file on disk.</param>
        public AzureSMProfile(string path) : this()
        {
            ProfilePath       = path;
            ProfileLoadErrors = new List <string>();

            if (!AzureSession.Instance.DataStore.DirectoryExists(AzureSession.Instance.ProfileDirectory))
            {
                AzureSession.Instance.DataStore.CreateDirectory(AzureSession.Instance.ProfileDirectory);
            }

            if (AzureSession.Instance.DataStore.FileExists(ProfilePath))
            {
                string contents = AzureSession.Instance.DataStore.ReadFileAsText(ProfilePath);

                IProfileSerializer serializer;

                if (CloudException.IsXml(contents))
                {
                    serializer = new XmlProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
                else if (CloudException.IsJson(contents))
                {
                    bool converted = false;
                    // first try legacy conversion
                    try
                    {
                        var legacyProfile = JsonConvert.DeserializeObject <LegacyAzureSMProfile>(contents);
                        converted = legacyProfile.TryConvert(this);
                    }
                    catch
                    {
                    }

                    if (!converted)
                    {
                        serializer = new JsonProfileSerializer();
                        if (!serializer.Deserialize(contents, this))
                        {
                            ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                        }
                    }
                }
            }
        }
 public static string FormatString(string content)
 {
     if (CloudException.IsXml(content))
     {
         return(TryFormatXml(content));
     }
     else if (CloudException.IsJson(content))
     {
         return(TryFormatJson(content));
     }
     else
     {
         return(content.Length <= GeneralUtilities.StreamCutOffSize ?
                content :
                content.Substring(0, StreamCutOffSize) + "\r\nDATA TRUNCATED DUE TO SIZE\r\n");
     }
 }
        private void Load()
        {
            Environments      = new Dictionary <string, AzureEnvironment>(StringComparer.InvariantCultureIgnoreCase);
            Subscriptions     = new Dictionary <Guid, AzureSubscription>();
            Accounts          = new Dictionary <string, AzureAccount>(StringComparer.InvariantCultureIgnoreCase);
            ProfileLoadErrors = new List <string>();

            if (!store.DirectoryExists(AzureSession.ProfileDirectory))
            {
                store.CreateDirectory(AzureSession.ProfileDirectory);
            }

            if (store.FileExists(profilePath))
            {
                string contents = store.ReadFileAsText(profilePath);

                IProfileSerializer serializer;

                if (CloudException.IsXml(contents))
                {
                    serializer = new XmlProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
                else if (CloudException.IsJson(contents))
                {
                    serializer = new JsonProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
            }

            // Adding predefined environments
            foreach (AzureEnvironment env in AzureEnvironment.PublicEnvironments.Values)
            {
                Environments[env.Name] = env;
            }
        }
        /// <summary>
        /// Initializes a new instance of AzureProfile and loads its content from specified path.
        /// Any errors generated in the process are stored in ProfileLoadErrors collection.
        /// </summary>
        /// <param name="path">Location of profile file on disk.</param>
        public AzureProfile(string path)
        {
            ProfilePath       = path;
            Environments      = new Dictionary <string, AzureEnvironment>(StringComparer.InvariantCultureIgnoreCase);
            Subscriptions     = new Dictionary <Guid, AzureSubscription>();
            Accounts          = new Dictionary <string, AzureAccount>(StringComparer.InvariantCultureIgnoreCase);
            ProfileLoadErrors = new List <string>();

            if (!AzureSession.DataStore.DirectoryExists(AzureSession.ProfileDirectory))
            {
                AzureSession.DataStore.CreateDirectory(AzureSession.ProfileDirectory);
            }

            if (AzureSession.DataStore.FileExists(ProfilePath))
            {
                string contents = AzureSession.DataStore.ReadFileAsText(ProfilePath);

                IProfileSerializer serializer;

                if (CloudException.IsXml(contents))
                {
                    serializer = new XmlProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
                else if (CloudException.IsJson(contents))
                {
                    serializer = new JsonProfileSerializer();
                    if (!serializer.Deserialize(contents, this))
                    {
                        ProfileLoadErrors.AddRange(serializer.DeserializeErrors);
                    }
                }
            }

            LoadDefaultEnvironments();
        }
Example #8
0
        /// <summary>
        /// For now, the 2nd message KVP inside "details" contains useful info about the failure. Eventually, a code KVP
        /// will be added such that we can search on that directly.
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        internal static string FindDetailedMessage(string content)
        {
            // TODO: Revise after Task 2362107 is completed on the server side
            string message = null;

            if (CloudException.IsJson(content))
            {
                var response = JObject.Parse(content);

                // check that we have a details section
                var detailsToken = response["details"];

                if (detailsToken != null)
                {
                    var details = detailsToken as JArray;
                    if (details != null && details.Count > 1)
                    {
                        // for now, 2nd entry in array is the one we're interested in. Need a better way of identifying the
                        // detailed error message
                        var dObj = detailsToken[1] as JObject;
                        var code = dObj.GetValue("code", StringComparison.CurrentCultureIgnoreCase);
                        if (code != null)
                        {
                            message = code.ToString() + ": ";
                        }

                        var detailedMsg = dObj.GetValue("message", StringComparison.CurrentCultureIgnoreCase);
                        if (detailedMsg != null)
                        {
                            message += detailedMsg.ToString();
                        }
                    }
                }
            }

            return(message);
        }
Example #9
0
 public void IsJsonWorksWithValidation(string body, bool expectedResult)
 {
     Assert.Equal(expectedResult, CloudException.IsJson(body, true));
 }