//****************************************************************************************************** // RestWebServiceMetadataProvider.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/11/2009 - Pinal C. Patel // Generated original version of source code. // 08/18/2009 - Pinal C. Patel // Added cleanup code for response stream of the REST web service. // 08/21/2009 - Pinal C. Patel // Moved RestDataFormat to Services namespace as SerializationFormat. // 09/15/2009 - Stephen C. Wills // Added new header and license agreement. // 10/11/2010 - Mihir Brahmbhatt // Updated header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.IO; using System.Net; using GSF.Configuration; namespace GSF.Historian.MetadataProviders { /// /// Represents a provider of data to a from a REST (Representational State Transfer) web service. /// /// public class RestWebServiceMetadataProvider : MetadataProviderBase { #region [ Members ] // Fields private string m_serviceUri; private SerializationFormat m_serviceDataFormat; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public RestWebServiceMetadataProvider() { m_serviceUri = string.Empty; m_serviceDataFormat = SerializationFormat.Xml; } #endregion #region [ Properties ] /// /// Gets or sets the URI where the REST web service is hosted. /// public string ServiceUri { get { return m_serviceUri; } set { m_serviceUri = value; } } /// /// Gets or sets the in which the REST web service exposes the data. /// public SerializationFormat ServiceDataFormat { get { return m_serviceDataFormat; } set { m_serviceDataFormat = value; } } #endregion #region [ Methods ] /// /// Saves settings to the config file if the property is set to true. /// public override void SaveSettings() { base.SaveSettings(); if (PersistSettings) { // Save settings under the specified category. ConfigurationFile config = ConfigurationFile.Current; CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; settings["ServiceUri", true].Update(m_serviceUri); settings["ServiceDataFormat", true].Update(m_serviceDataFormat); config.Save(); } } /// /// Loads saved settings from the config file if the property is set to true. /// public override void LoadSettings() { base.LoadSettings(); if (PersistSettings) { // Load settings from the specified category. ConfigurationFile config = ConfigurationFile.Current; CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; settings.Add("ServiceUri", m_serviceUri, "URI where the REST web service is hosted."); settings.Add("ServiceDataFormat", m_serviceDataFormat, "Format (Json; PoxAsmx; PoxRest) in which the REST web service exposes the data."); ServiceUri = settings["ServiceUri"].ValueAs(m_serviceUri); ServiceDataFormat = settings["ServiceDataFormat"].ValueAs(m_serviceDataFormat); } } /// /// Refreshes the from a REST web service. /// /// is set to a null or empty string. protected override void RefreshMetadata() { if (string.IsNullOrEmpty(m_serviceUri)) throw new ArgumentNullException("ServiceUri"); WebResponse response = null; Stream responseStream = null; try { // Retrieve new metadata. response = WebRequest.Create(m_serviceUri).GetResponse(); responseStream = response.GetResponseStream(); // Update existing metadata. MetadataUpdater metadataUpdater = new MetadataUpdater(Metadata); metadataUpdater.UpdateMetadata(responseStream, m_serviceDataFormat); } finally { if (response != null) response.Close(); if (responseStream != null) responseStream.Dispose(); } } #endregion } }