Skip to content

Commit

Permalink
XRoadRequest fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
janno-p committed Mar 4, 2016
1 parent 417a0aa commit 3a39311
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 1.0.0-beta007 - 04.03.2016
* Fix XRoadRequest helper class.
* Add helper method to generate X-Road request ID-s.

#### 1.0.0-beta006 - 04.03.2016
* Fix XRoadRequest class to make POST requests instead of GET.
* Refactor X-Road meta services to handle them separately.
Expand Down
23 changes: 23 additions & 0 deletions src/XRoadLib/XRoadHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Security.Cryptography;

namespace XRoadLib
{
public static class XRoadHelper
{
public static string GenerateRequestID()
{
const int randomLength = 32;
const int nonceLength = (int)(4.0d / 3.0d * randomLength);

var random = new byte[randomLength];
var rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(random);

var nch = new char[nonceLength + 2];
Convert.ToBase64CharArray(random, 0, randomLength, nch, 0);

return new string(nch);
}
}
}
1 change: 1 addition & 0 deletions src/XRoadLib/XRoadLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<Compile Include="Soap\FaultCode.cs" />
<Compile Include="Soap\ServerFaultCode.cs" />
<Compile Include="XRoadException.cs" />
<Compile Include="XRoadHelper.cs" />
<Compile Include="XRoadRequest.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
32 changes: 17 additions & 15 deletions src/XRoadLib/XRoadRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -13,36 +12,39 @@

namespace XRoadLib
{
public class XRoadRequest
public interface IXRoadRequest
{
private readonly XRoadProtocol protocol;
private readonly IXRoadHeader header;
private readonly XName operationName;
TResult Execute<TResult>(object arg, IXRoadHeader xRoadHeader);
}

public IDictionary<string, object> Parameters { get; } = new Dictionary<string, object>();
public class XRoadRequest : IXRoadRequest
{
private readonly XRoadProtocol protocol;
private readonly Uri uri;

public XRoadRequest(XRoadProtocol protocol, IXRoadHeader header, XName operationName)
public XRoadRequest(Uri uri, XRoadProtocol protocol)
{
this.header = header;
this.operationName = operationName;
this.protocol = protocol;
this.uri = uri;
}

public T Execute<T>(Uri uri)
public T Execute<T>(object arg, IXRoadHeader xRoadHeader)
{
using (var requestMessage = new XRoadMessage(protocol, header))
using (var requestMessage = new XRoadMessage(protocol, xRoadHeader))
{
var writer = XmlWriter.Create(requestMessage.ContentStream);

writer.WriteStartDocument();

protocol.WriteSoapEnvelope(writer);
protocol.WriteSoapHeader(writer, header);
protocol.WriteSoapHeader(writer, xRoadHeader);

writer.WriteStartElement("Body", NamespaceConstants.SOAP_ENV);

var serviceMap = requestMessage.GetSerializerCache().GetServiceMap(operationName);
serviceMap.SerializeRequest(writer, Parameters, requestMessage);
var serviceMap = requestMessage.GetSerializerCache()
.GetServiceMap(XName.Get(xRoadHeader.Service.ServiceCode, protocol.ProducerNamespace));

serviceMap.SerializeRequest(writer, arg, requestMessage);

writer.WriteEndElement();
writer.WriteEndElement();
Expand All @@ -64,7 +66,7 @@ public T Execute<T>(Uri uri)
{
responseStream?.CopyTo(seekableStream);
responseMessage.LoadResponse(seekableStream, response.Headers, Encoding.UTF8, Path.GetTempPath(), Enumerable.Repeat(protocol, 1));
return (T)responseMessage.DeserializeMessageContent(operationName.LocalName);
return (T)responseMessage.DeserializeMessageContent(xRoadHeader.Service.ServiceCode);
}
}
}
Expand Down

0 comments on commit 3a39311

Please sign in to comment.