forked from ks-no/fiks-io-client-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiksIOSubscriber.cs
123 lines (108 loc) · 6.42 KB
/
FiksIOSubscriber.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
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using KS.Fiks.ASiC_E;
using KS.Fiks.IO.Client;
using KS.Fiks.IO.Client.Models;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace ExampleApplication.FiksIO
{
public class FiksIOSubscriber : BackgroundService
{
private readonly IFiksIOClient _fiksIoClient;
private readonly AppSettings _appSettings;
private static readonly ILogger Log = Serilog.Log.ForContext(MethodBase.GetCurrentMethod()?.DeclaringType);
public FiksIOSubscriber(IFiksIOClient fiksIoClient, AppSettings appSettings)
{
_fiksIoClient = fiksIoClient;
_appSettings = appSettings;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Log.Information("FiksIOSubscriber - Application is starting subscribe");
SubscribeToFiksIOClient();
await Task.CompletedTask;
}
private async void OnReceivedMelding(object sender, MottattMeldingArgs mottatt)
{
var receivedMeldingType = mottatt.Melding.MeldingType;
var konto = await _fiksIoClient.GetKonto(mottatt.Melding.AvsenderKontoId);
Log.Information("FiksIOSubscriber - Received a message with messagetype '{MessageType}' with following attributes: " +
"\n\t messageId : {MeldingId}" +
"\n\t klientMeldingId : {KlientMeldingId}" +
"\n\t svarPaMelding : {SvarPaMelding}" +
"\n\t fiksOrgNavn : {FiksOrgNavn}" ,
receivedMeldingType, mottatt.Melding.MeldingId, mottatt.Melding.KlientMeldingId, mottatt.Melding.SvarPaMelding, konto.FiksOrgNavn);
switch (receivedMeldingType)
{
case Program.FiksIOPong:
case Program.FiksArkivPong:
case Program.FiksPlanPong:
case Program.FiksMatrikkelfoeringPong:
Log.Information($"FiksIOSubscriber - Received {receivedMeldingType}. Do nothing with 'pong' message. End of correspondence for now.");
break;
case Program.FiksIOPing:
{
var klientMeldingId = Guid.NewGuid();
var sendtMelding = await mottatt.SvarSender.Svar(Program.FiksIOPong, klientMeldingId);
var payloadTxt = await GetDecryptedPayloadTxt(mottatt);
Log.Information("FiksIOSubscriber - Received {receivedMeldingType} with payload text {payload}. Replied messagetype 'ping' with messagetype 'pong' with messageId : {MeldingId} and klientMeldingId: {KlientMeldingId}", sendtMelding.MeldingType, payloadTxt, sendtMelding.MeldingId, sendtMelding.KlientMeldingId);
break;
}
case Program.FiksArkivPing:
{
var klientMeldingId = Guid.NewGuid();
var sendtMelding = await mottatt.SvarSender.Svar(Program.FiksArkivPong, klientMeldingId);
var payloadTxt = await GetDecryptedPayloadTxt(mottatt);
Log.Information("FiksIOSubscriber - Received {receivedMeldingType} with payload text {payload}. Replied messagetype 'ping' with messagetype 'pong' with messageId : {MeldingId} and klientMeldingId: {KlientMeldingId}", sendtMelding.MeldingType, payloadTxt, sendtMelding.MeldingId, sendtMelding.KlientMeldingId);
break;
}
case Program.FiksPlanPing:
{
var klientMeldingId = Guid.NewGuid();
var sendtMelding = await mottatt.SvarSender.Svar(Program.FiksPlanPong, klientMeldingId);
var payloadTxt = await GetDecryptedPayloadTxt(mottatt);
Log.Information("FiksIOSubscriber - Received {receivedMeldingType} with payload text {payload}. Replied messagetype 'ping' with messagetype 'pong' with messageId : {MeldingId} and klientMeldingId: {KlientMeldingId}", sendtMelding.MeldingType, payloadTxt, sendtMelding.MeldingId, sendtMelding.KlientMeldingId);
break;
}
case Program.FiksMatrikkelfoeringPing:
{
var klientMeldingId = Guid.NewGuid();
var sendtMelding = await mottatt.SvarSender.Svar(Program.FiksMatrikkelfoeringPong, klientMeldingId);
var payloadTxt = await GetDecryptedPayloadTxt(mottatt);
Log.Information("FiksIOSubscriber - Received {receivedMeldingType} with payload text {payload}. Replied messagetype 'ping' with messagetype 'pong' with messageId : {MeldingId} and klientMeldingId: {KlientMeldingId}",sendtMelding.MeldingType, payloadTxt, sendtMelding.MeldingId, sendtMelding.KlientMeldingId);
break;
}
}
mottatt.SvarSender.Ack();
}
private static async Task<string> GetDecryptedPayloadTxt(MottattMeldingArgs mottattMeldingArgs)
{
var payloadTxt = "empty";
IAsicReader asiceReader = new AsiceReader();
using var asiceReadModel = asiceReader.Read(await mottattMeldingArgs.Melding.DecryptedStream);
// Verify asice and read payload
foreach (var asiceVerifyReadEntry in asiceReadModel.Entries)
{
await using (var entryStream = asiceVerifyReadEntry.OpenStream())
{
Log.Information($"GetDecryptedPayloadTxt - {asiceVerifyReadEntry.FileName}");
await using var fileStream = new FileStream($"received-{asiceVerifyReadEntry.FileName}", FileMode.Create, FileAccess.Write);
await entryStream.CopyToAsync(fileStream);
}
payloadTxt = await File.ReadAllTextAsync($"received-{asiceVerifyReadEntry.FileName}");
}
// Only one payload in this example, and only one text
return payloadTxt;
}
private void SubscribeToFiksIOClient()
{
var accountId = _appSettings.FiksIOConfig.FiksIoAccountId;
Log.Information($"FiksIOSubscriber - Starting FiksIOReceiveAndReplySubscriber subscribe on account {accountId}...");
_fiksIoClient.NewSubscription(OnReceivedMelding);
}
}
}