Skip to content

Commit a7eda42

Browse files
committed
Merge branch 'feature-supportrequestwithbody' into release-2.0
2 parents a62ac9e + 11c59a3 commit a7eda42

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

Src/HttpDisassembler/BizTalkComponents.PipelineComponents.HttpDisassembler.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<SpecificVersion>False</SpecificVersion>
6363
<HintPath>..\..\..\..\..\Windows\assembly\GAC_MSIL\Microsoft.BizTalk.Pipeline\3.0.1.0__31bf3856ad364e35\Microsoft.BizTalk.Pipeline.dll</HintPath>
6464
</Reference>
65+
<Reference Include="Microsoft.BizTalk.Streaming, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
66+
<SpecificVersion>False</SpecificVersion>
67+
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Microsoft BizTalk Server 2013\Microsoft.BizTalk.Streaming.dll</HintPath>
68+
</Reference>
6569
<Reference Include="Microsoft.XLANGs.RuntimeTypes, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
6670
<Reference Include="System" />
6771
<Reference Include="System.ComponentModel.DataAnnotations" />

Src/HttpDisassembler/HttpDisassembler.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using BizTalkComponents.Utils;
1212
using BizTalkComponents.Utilities.ComponentInstrumentation;
1313
using System.Runtime.InteropServices;
14+
using Microsoft.BizTalk.Streaming;
15+
1416
namespace BizTalkComponents.PipelineComponents.HttpDisassembler
1517
{
1618

@@ -50,6 +52,11 @@ public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
5052
throw ex;
5153
}
5254

55+
var data = pInMsg.BodyPart.GetOriginalDataStream();
56+
57+
//Determine of the request body has any data. GET request will not have any body.
58+
var hasData = HasData(data);
59+
5360
//Get a reference to the BizTalk schema.
5461
DocumentSpec documentSpec;
5562
try
@@ -89,11 +96,26 @@ public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
8996
ms.Seek(0, SeekOrigin.Begin);
9097

9198
var outMsg = pInMsg;
92-
outMsg.BodyPart.Data = ms;
9399

94-
//Promote message type and SchemaStrongName
95-
outMsg.Context.Promote(new ContextProperty(SystemProperties.MessageType), documentSpec.DocType);
96-
outMsg.Context.Promote(new ContextProperty(SystemProperties.SchemaStrongName), documentSpec.DocSpecStrongName);
100+
//If the request has a body it should be preserved an the query parameters should be written to it's own message part.
101+
if (hasData)
102+
{
103+
outMsg = pInMsg;
104+
outMsg.BodyPart.Data = pInMsg.BodyPart.Data;
105+
outMsg.Context = PipelineUtil.CloneMessageContext(pInMsg.Context);
106+
var factory = pContext.GetMessageFactory();
107+
var queryPart = factory.CreateMessagePart();
108+
queryPart.Data = ms;
109+
110+
outMsg.AddPart("querypart", queryPart, false);
111+
}
112+
else
113+
{
114+
outMsg.BodyPart.Data = ms;
115+
//Promote message type and SchemaStrongName
116+
outMsg.Context.Promote(new ContextProperty(SystemProperties.MessageType), documentSpec.DocType);
117+
outMsg.Context.Promote(new ContextProperty(SystemProperties.SchemaStrongName), documentSpec.DocSpecStrongName);
118+
}
97119

98120
_outputQueue.Enqueue(outMsg);
99121
_instrumentationHelper.TrackComponentSuccess();
@@ -119,5 +141,23 @@ public IBaseMessage GetNext(IPipelineContext pContext)
119141

120142
return null;
121143
}
144+
145+
private bool HasData(Stream data)
146+
{
147+
byte[] buffer= new byte[10];
148+
const int bufferSize = 0x280;
149+
const int thresholdSize = 0x100000;
150+
151+
if (!data.CanSeek || !data.CanRead)
152+
{
153+
data = new ReadOnlySeekableStream(data, new VirtualStream(bufferSize, thresholdSize), bufferSize);
154+
}
155+
156+
int num = data.Read(buffer, 0, buffer.Length);
157+
data.Seek(0, SeekOrigin.Begin);
158+
data.Position = 0;
159+
160+
return num > 0;
161+
}
122162
}
123163
}

Tests/UnitTests/HttpDisassemblerTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,45 @@ public void CreateMessageTest()
3131
var result = pipeline.Execute(message);
3232

3333
Assert.AreEqual(1, result.Count);
34+
Assert.AreEqual(1, result[0].PartCount);
3435

3536
var doc = XDocument.Load(result[0].BodyPart.GetOriginalDataStream());
3637

3738
Assert.AreEqual("value1", doc.Descendants("TestElement1").Single().Value);
3839
Assert.AreEqual("value2", doc.Descendants("TestElement2").Single().Value);
3940
}
4041

42+
[TestMethod]
43+
public void CreateMessageWithBodyTest()
44+
{
45+
var pipeline = PipelineFactory.CreateEmptyReceivePipeline();
46+
47+
pipeline.AddDocSpec(typeof(TestSchema));
48+
49+
var disassembler = new PipelineComponents.HttpDisassembler.HttpDisassembler
50+
{
51+
DocumentSpecName = "BizTalkComponents.HttpDisassembler.Tests.UnitTests.TestSchema"
52+
};
53+
string testMessage = "<testmessage></testmessage>";
54+
var message = MessageHelper.CreateFromString(testMessage);
55+
message.Context.Promote(new ContextProperty("http://BiztalkComponents.PropertySchema#TestProperty1"), "value1");
56+
message.Context.Promote(new ContextProperty("http://BiztalkComponents.PropertySchema#TestProperty2"), "value2");
57+
58+
pipeline.AddComponent(disassembler, PipelineStage.Disassemble);
59+
60+
var result = pipeline.Execute(message);
61+
62+
Assert.AreEqual(1, result.Count);
63+
Assert.AreEqual(2, result[0].PartCount);
64+
65+
var doc = XDocument.Load(result[0].GetPart("querypart").GetOriginalDataStream());
66+
var bodyDoc = XDocument.Load(result[0].BodyPart.GetOriginalDataStream());
67+
68+
Assert.AreEqual("value1", doc.Descendants("TestElement1").Single().Value);
69+
Assert.AreEqual("value2", doc.Descendants("TestElement2").Single().Value);
70+
Assert.AreEqual(testMessage, bodyDoc.ToString());
71+
}
72+
4173
[TestMethod]
4274
[ExpectedException(typeof(COMException))]
4375
public void UnknownDocType()

0 commit comments

Comments
 (0)