/// <summary> /// Copies the resource data to the specified resource. Both resources are assumed to originate from the same given connection /// </summary> /// <remarks> /// Avoid using this method if you are copying a IFeatureSource with MG_USER_CREDENTIALS resource data, as MapGuide will automatically return /// the decrypted username for MG_USER_CREDENTIALS, rendering the resource data invalid for the target resource. Instead use the /// <see cref="M:OSGeo.MapGuide.MaestroAPI.Services.IResourceService.CopyResource"/> method, which will copy the resource and its resource /// data and keep any MG_USER_CREDENTIALS items intact /// </remarks> /// <param name="source">The source.</param> /// <param name="conn">The server connection</param> /// <param name="targetID">The target ID.</param> public static void CopyResourceDataTo(this IResource source, IServerConnection conn, string targetID) { Check.ArgumentNotNull(source, nameof(source)); Check.ArgumentNotEmpty(targetID, nameof(targetID)); var resData = conn.ResourceService.EnumerateResourceData(source.ResourceID); foreach (var res in resData.ResourceData) { var data = conn.ResourceService.GetResourceData(source.ResourceID, res.Name); bool bDispose = false; if (!data.CanSeek) { var ms = MemoryStreamPool.GetStream(); Utility.CopyStream(data, ms); data = ms; bDispose = true; } conn.ResourceService.SetResourceData(targetID, res.Name, res.Type, data); if (bDispose) { data.Dispose(); } } }
/// <summary> /// Deserializes the specified stream for the specified resource type. /// </summary> /// <param name="resourceType">Type of the resource.</param> /// <param name="stream">The stream.</param> /// <returns>The deserialized resource</returns> public static IResource Deserialize(string resourceType, Stream stream) { //UGLY: We have to peek inside the stream to determine the version number //House the stream inside a rewindable memory stream using (var ms = MemoryStreamPool.GetStream("ResourceTypeRegistry.Deserialize")) { Utils.CopyStream(stream, ms); ms.Position = 0L; //Rewind var rd = ResourceContentVersionChecker.GetVersionFromXmlStream(ms); Debug.Assert(rd.ResourceType.Equals(resourceType.ToString())); ms.Position = 0L; //Rewind using (var reader = new StreamReader(ms)) { var xml = reader.ReadToEnd(); if (_serializers.ContainsKey(rd)) { return(_serializers[rd].Deserialize(xml)); } else { return(new UntypedResource(xml, resourceType, rd.Version)); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="stream">The resource content stream. Inspection is done on a copy of this stream</param> public ResourceContentVersionChecker(Stream stream) { var ms = MemoryStreamPool.GetStream("ResourceContentVersionChecker.ctor"); Utils.CopyStream(stream, ms); ms.Position = 0L; //Rewind _stream = ms; }
/// <summary> /// Serializes the given object as a UTF-8 encoded XML string. Any BOM is stripped from the XML string /// </summary> /// <param name="serializer"></param> /// <param name="o"></param> /// <returns></returns> internal static string NormalizedSerialize(XmlSerializer serializer, object o) { using (var ms = MemoryStreamPool.GetStream()) { using (var xw = new Utf8XmlWriter(ms)) { serializer.Serialize(xw, o); using (var ms2 = RemoveUTF8BOM(ms)) { using (var sr = new StreamReader(ms2)) { return(sr.ReadToEnd()); } } } } }
/// <summary> /// Alternate constructor /// </summary> /// <param name="xmlContent"></param> public ResourceContentVersionChecker(string xmlContent) { var ms = MemoryStreamPool.GetStream("ResourceContentVersionChecker.ctor", Encoding.UTF8.GetBytes(xmlContent)); _stream = ms; }
/// <summary> /// Serializes to stream. /// </summary> /// <param name="res">The resource</param> /// <returns>The serialized stream</returns> public static Stream SerializeToStream(this IResource res) { string str = res.Serialize(); return(MemoryStreamPool.GetStream("ResourceExtensions.SerializeToStream", Encoding.UTF8.GetBytes(str))); }