-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlBatchClient.cs
130 lines (110 loc) · 5.88 KB
/
XmlBatchClient.cs
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
using ObixClientLibrary.Framework;
using ObixClientLibrary.Extensions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ObixClientLibrary
{
public class XmlBatchClient
{
/// <summary>
/// Gets a reference to the XML oBIX client used to send and retrieve messages for this
/// batch operation.
/// </summary>
XmlObixClient ObixClient { get; set; }
public XmlBatchClient(XmlObixClient ObixClient)
{
this.ObixClient = ObixClient;
}
public static XmlBatch CreateBatch()
{
XmlBatch batch = new XmlBatch();
return batch;
}
/// <summary>
/// Parses an obix:BatchOut contract referenced by <paramref name="BatchOutContract"/> from an oBIX server into the XML batch referenced by <paramref name="BatchObject"/>.
///
/// Results returned from an oBIX server are placed into the XmlBatchResponse property of each item in the XmlBatch container.
/// </summary>
/// <param name="BatchOutContract">A reference to an XElement containing the XML obix:BatchOut contract</param>
/// <param name="BatchObject">A reference to the XmlBatch object containing the request data to bind to.</param>
/// <returns>kObixClientSuccess if the operation is to succeed without error, another value otherwise.</returns>
protected ObixResult ParseBatchOutContract(ref XElement BatchOutContract, ref XmlBatch BatchObject)
{
if (BatchOutContract == null || BatchObject == null)
{
return ObixClient.ErrorStack.Push(GetType(), ObixResult.kObixClientInputError, "ParseBatchOutContract failed: Provided BatchOutContract or BatchObject is null.");
}
// The document being returned looks like this:
//
// <? xml - stylesheet type = 'text/xsl' href = '/obix/xsl' ?>
// < list of = "obix:BatchOut" xmlns = "http://obix.org/ns/schema/1.0" xsi: schemaLocation = "http://obix.org/ns/schema/1.0 /obix/xsd" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" >
//
// but is still failing? Commenting out next section (lines 52 - 55):
//if (BatchOutContract.ObixIs() != "obix:BatchOut")
//{
// return ObixClient.ErrorStack.Push(GetType(), ObixResult.kObixServerError, "The response provided from the obix:Batch operation was not an obix:BatchOut contract.");
//}
IEnumerable<XElement> batchOutElements = BatchOutContract.Elements();
if (batchOutElements == null)
{
return ObixClient.ErrorStack.Push(GetType(), ObixResult.kObixClientXMLParseError, "batchOutElements is null.");
}
int batchOutCount = batchOutElements.Count();
if (batchOutCount != BatchObject.BatchItemCount)
{
return ObixClient.ErrorStack.Push(GetType(), ObixResult.kObixClientXMLParseError, "The number of response objects in the obix:BatchOut contract does not match " +
"the number of items in the obix:BatchIn contract. Something on the server side is really broken.");
}
for (int i = 0; i < batchOutCount; i++)
{
XElement batchOutNode = batchOutElements.ElementAt(i);
ObixXmlBatchItem batchItem = BatchObject.GetXmlBatchItem(i);
if (batchOutNode == null || batchItem == null)
{
return ObixClient.ErrorStack.Push(GetType(), ObixResult.kObixClientXMLElementNotFoundError, "Could not bind the obix:BatchOut node to the batch item, one of the nodes is null.");
}
batchItem.XmlBatchResponse = batchOutNode;
}
return ObixResult.kObixClientSuccess;
}
/// <summary>
/// Sends an oBIX:Batch to the registered batch endpoint in the XmlObixClient, and waits for
/// the BatchOut response from the server.
///
/// The provided reference to <paramref name="Batch"/> will have each Batch item updated
/// with the response from the obix:Batch operation on the server.
/// </summary>
/// <param name="Batch">The XmlBatch object that contains a list of Batch items.</param>
public ObixResult SubmitBatch(ref XmlBatch Batch)
{
if (Batch == null)
{
return ObixClient.ErrorStack.PushWithObject<XElement>(GetType(), ObixResult.kObixClientInputError);
}
ObixResult<XElement> batchInBuffer = Batch.ToBatchInContract();
if (batchInBuffer.ResultSucceeded == false)
{
return ObixClient.ErrorStack.PushWithObject<XElement>(GetType(), batchInBuffer);
}
//todo: send batch
ObixResult<XElement> batchOutResponse = ObixClient.InvokeUriXml(ObixClient.BatchUri, batchInBuffer.Result);
if (batchInBuffer.ResultSucceeded == false)
{
return ObixClient.ErrorStack.PushWithObject<XElement>(GetType(), batchInBuffer);
}
XElement batchOut = batchOutResponse.Result;
ObixResult batchOutParseResult = ParseBatchOutContract(ref batchOut, ref Batch);
if (batchOutParseResult != ObixResult.kObixClientSuccess)
{
return ObixClient.ErrorStack.PushWithObject<XElement>(GetType(), batchOutParseResult);
}
return ObixResult.kObixClientSuccess;
}
public async Task<ObixResult> SubmitBatchTask(XmlBatch Batch)
{
return await Task.Factory.StartNew<ObixResult>(() => { return SubmitBatch(ref Batch); });
}
}
}