File size: 7,700 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Globalization;
using System.Xml;

namespace Microsoft.WSMan.Management
{
    /// <summary>
    /// Class that queries the server and gets current configurations.
    /// Also provides a generic way to update the configurations.
    /// </summary>
    internal sealed class CurrentConfigurations
    {
        /// <summary>
        /// Prefix used to add NameSpace of root element to namespace manager.
        /// </summary>
        public const string DefaultNameSpacePrefix = "defaultNameSpace";

        /// <summary>
        /// This holds the current configurations XML.
        /// </summary>
        private readonly XmlDocument rootDocument;

        /// <summary>
        /// Holds the reference to the current document element.
        /// </summary>
        private XmlElement documentElement;

        /// <summary>
        /// Holds the Namespace Manager to use for XPATH queries.
        /// </summary>
        private XmlNamespaceManager nameSpaceManger;

        /// <summary>
        /// Session of the WsMan sserver.
        /// </summary>
        private readonly IWSManSession serverSession;

        /// <summary>
        /// Gets the server session associated with the configuration.
        /// </summary>
        public IWSManSession ServerSession
        {
            get { return serverSession; }
        }

        /// <summary>
        /// Gets the current configuration XML.
        /// </summary>
        public XmlDocument RootDocument
        {
            get { return this.rootDocument; }
        }

        /// <summary>
        /// Gets the current configuration on the given server and for given URI.
        /// This issues a GET request to the server.
        /// </summary>
        /// <param name="serverSession">Current server session.</param>
        public CurrentConfigurations(IWSManSession serverSession)
        {
            ArgumentNullException.ThrowIfNull(serverSession);

            this.rootDocument = new XmlDocument();
            this.serverSession = serverSession;
        }

        /// <summary>
        /// Refresh the CurrentConfiguration. This method calls GET operation for the given
        /// URI on the server and update the current configuration. It also initialize some
        /// of required class members.
        /// </summary>
        /// <param name="responseOfGet">Plugin configuration.</param>
        /// <returns>False, if operation failed.</returns>
        public bool RefreshCurrentConfiguration(string responseOfGet)
        {
            ArgumentException.ThrowIfNullOrEmpty(responseOfGet);

            this.rootDocument.LoadXml(responseOfGet);
            this.documentElement = this.rootDocument.DocumentElement;

            this.nameSpaceManger = new XmlNamespaceManager(this.rootDocument.NameTable);
            this.nameSpaceManger.AddNamespace(CurrentConfigurations.DefaultNameSpacePrefix, this.documentElement.NamespaceURI);

            return string.IsNullOrEmpty(this.serverSession.Error);
        }

        /// <summary>
        /// Update the server with updated XML.
        /// Issues a PUT request with the ResourceUri provided.
        /// </summary>
        /// <param name="resourceUri">Resource URI to use.</param>
        /// <returns>False, if operation is not successful.</returns>
        public void PutConfigurationOnServer(string resourceUri)
        {
            ArgumentException.ThrowIfNullOrEmpty(resourceUri);

            this.serverSession.Put(resourceUri, this.rootDocument.InnerXml, 0);
        }

        /// <summary>
        /// This method will remove the configuration from the XML.
        /// Currently the method will only remove the attributes. But it is extensible enough to support
        /// Node removals in future.
        /// </summary>
        /// <param name="pathToNodeFromRoot">Path with namespace to the node from Root element. Must not end with '/'.</param>
        public void RemoveOneConfiguration(string pathToNodeFromRoot)
        {
            ArgumentNullException.ThrowIfNull(pathToNodeFromRoot);

            XmlNode nodeToRemove =
                this.documentElement.SelectSingleNode(
                    pathToNodeFromRoot,
                    this.nameSpaceManger);

            if (nodeToRemove != null)
            {
                if (nodeToRemove is XmlAttribute)
                {
                    RemoveAttribute(nodeToRemove as XmlAttribute);
                }
            }
            else
            {
                throw new ArgumentException("Node is not present in the XML, Please give valid XPath", nameof(pathToNodeFromRoot));
            }
        }

        /// <summary>
        /// Create or Update the value of the configuration on the given Node. Currently this
        /// method is supported for updating attributes, but can be easily updated for nodes.
        /// Caller should call this method to add a new attribute to the Node.
        /// </summary>
        /// <param name="pathToNodeFromRoot">Path with namespace to the node from Root element. Must not end with '/'.</param>
        /// <param name="configurationName">Name of the configuration with name space to update or create.</param>
        /// <param name="configurationValue">Value of the configurations.</param>
        public void UpdateOneConfiguration(string pathToNodeFromRoot, string configurationName, string configurationValue)
        {
            ArgumentNullException.ThrowIfNull(pathToNodeFromRoot);
            ArgumentException.ThrowIfNullOrEmpty(configurationName);
            ArgumentNullException.ThrowIfNull(configurationValue);

            XmlNode nodeToUpdate =
                this.documentElement.SelectSingleNode(
                    pathToNodeFromRoot,
                    this.nameSpaceManger);

            if (nodeToUpdate != null)
            {
                foreach (XmlAttribute attribute in nodeToUpdate.Attributes)
                {
                    if (attribute.Name.Equals(configurationName, StringComparison.OrdinalIgnoreCase))
                    {
                        attribute.Value = configurationValue;
                        return;
                    }
                }

                XmlNode attr = this.rootDocument.CreateNode(XmlNodeType.Attribute, configurationName, string.Empty);
                attr.Value = configurationValue;

                nodeToUpdate.Attributes.SetNamedItem(attr);
            }
        }

        /// <summary>
        /// Gets the value of the configuration on the given Node or attribute.
        /// </summary>
        /// <param name="pathFromRoot">Path with namespace to the node from Root element.</param>
        /// <returns>Value of the Node, or Null if no node present.</returns>
        public string GetOneConfiguration(string pathFromRoot)
        {
            ArgumentNullException.ThrowIfNull(pathFromRoot);

            XmlNode requiredNode =
                this.documentElement.SelectSingleNode(
                    pathFromRoot,
                    this.nameSpaceManger);

            if (requiredNode != null)
            {
                return requiredNode.Value;
            }

            return null;
        }

        /// <summary>
        /// Removes the attribute from OwnerNode.
        /// </summary>
        /// <param name="attributeToRemove">Attribute to Remove.</param>
        private static void RemoveAttribute(XmlAttribute attributeToRemove)
        {
            XmlElement ownerElement = attributeToRemove.OwnerElement;
            ownerElement.RemoveAttribute(attributeToRemove.Name);
        }
    }
}