/// <summary> /// Initializes a new instance of the <see cref="RestWebServiceMetadataProvider"/> class. /// </summary> public RestWebServiceMetadataProvider() : base() { m_serviceUri = string.Empty; m_serviceDataFormat = RestDataFormat.RestXml; }
/// <summary> /// Serializes <see cref="Metadata"/> to <see cref="SerializableMetadata"/> in the specified <paramref name="dataFormat"/>. /// </summary> /// <param name="outputStream"><see cref="Stream"/> where serialized <see cref="Metadata"/> is to be outputted.</param> /// <param name="dataFormat"><see cref="RestDataFormat"/> in which the <see cref="Metadata"/> is to be serialized.</param> /// <exception cref="ArgumentNullException"><paramref name="outputStream"/> is null.</exception> /// <exception cref="NotSupportedException">Specified <paramref name="dataFormat"/> is not supported.</exception> public void ExtractMetadata(ref Stream outputStream, RestDataFormat dataFormat) { if (outputStream == null) throw new ArgumentNullException("outputStream"); // Serialize data to the provided stream. if (dataFormat == RestDataFormat.AsmxXml) { // Serialize to ASMX XML data format. XmlSerializer serializer = new XmlSerializer(typeof(SerializableMetadata)); serializer.Serialize(outputStream, new SerializableMetadata(m_metadata)); } else if (dataFormat == RestDataFormat.RestXml) { // Serialize to REST XML data format. DataContractSerializer serializer = new DataContractSerializer(typeof(SerializableMetadata)); serializer.WriteObject(outputStream, new SerializableMetadata(m_metadata)); } else if (dataFormat == RestDataFormat.RestJson) { // Serialize to REST JSON data format. DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SerializableMetadata)); serializer.WriteObject(outputStream, new SerializableMetadata(m_metadata)); } else { // Data format is not supported. throw new NotSupportedException(string.Format("{0} data format is not supported.", dataFormat)); } }
/// <summary> /// Loads saved <see cref="RestWebServiceMetadataProvider"/> settings from the config file if the <see cref="MetadataProviderBase.PersistSettings"/> property is set to true. /// </summary> public override void LoadSettings() { base.LoadSettings(); if (PersistSettings) { // Ensure that settings category is specified. if (string.IsNullOrEmpty(SettingsCategory)) throw new InvalidOperationException("SettingsCategory property has not been set."); // 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 (AsmxXml; RestXml; RestJson) in which the REST web service exposes the data."); ServiceUri = settings["ServiceUri"].ValueAs(m_serviceUri); ServiceDataFormat = settings["ServiceDataFormat"].ValueAs(m_serviceDataFormat); } }
/// <summary> /// Updates the <see cref="Metadata"/> from <paramref name="streamData"/> /// </summary> /// <param name="streamData"><see cref="Stream"/> containing serialized <see cref="SerializableMetadata"/>.</param> /// <param name="dataFormat"><see cref="RestDataFormat"/> in which the <see cref="SerializableMetadata"/> is serialized.</param> /// <exception cref="ArgumentNullException"><paramref name="streamData"/> is null.</exception> /// <exception cref="NotSupportedException">Specified <paramref name="dataFormat"/> is not supported.</exception> public void UpdateMetadata(Stream streamData, RestDataFormat dataFormat) { if (streamData == null) throw new ArgumentNullException("streamData"); // Deserialize serialized metadata. SerializableMetadata deserializedMetadata = null; if (dataFormat == RestDataFormat.AsmxXml) { // Data is in ASMX XML format. XmlSerializer serializer = new XmlSerializer(typeof(SerializableMetadata)); deserializedMetadata = (SerializableMetadata)serializer.Deserialize(streamData); } else if (dataFormat == RestDataFormat.RestXml) { // Data is in REST XML format. DataContractSerializer serializer = new DataContractSerializer(typeof(SerializableMetadata)); deserializedMetadata = (SerializableMetadata)serializer.ReadObject(streamData); } else if (dataFormat == RestDataFormat.RestJson) { // Data is in REST JSON format. DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SerializableMetadata)); deserializedMetadata = (SerializableMetadata)serializer.ReadObject(streamData); } else { // Data format is not supported. throw new NotSupportedException(string.Format("{0} data format is not supported.", dataFormat)); } // Update metadata from the deserialized metadata. foreach (SerializableMetadataRecord deserializedMetadataRecord in deserializedMetadata.MetadataRecords) { m_metadata.Write(deserializedMetadataRecord.HistorianID, deserializedMetadataRecord.Deflate()); } }