Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrations support for results and task cancellation #109

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ protected async Task<IActionResult> ExecuteCommandAsync<TAggregateRoot>(IApplica
return new OkResult();
}

private void LogCurrentScope<TAggregateRoot>(IApplicationCommand<TAggregateRoot> command) where TAggregateRoot : AggregateRoot
protected async Task<IActionResult> ExecuteIntegrationCommandAsync(IIntegrationCommand command, IIntegrationContract handlerIntegrationService, CancellationToken cancellationToken = default)
{
LogCurrentScope(command);

var result = await handlerIntegrationService.HandleCommandAsync(command, cancellationToken);

if (result.IsFailure) return GetFailureResponse(result.Error);

return new OkResult();
}

private void LogCurrentScope(ICommand command)
{
string currentPath = Request.GetEncodedPathAndQuery() ?? string.Empty;
string currentHost = Request.GetIpAddressString() ?? string.Empty;
Expand All @@ -57,22 +68,5 @@ private static IActionResult GetFailureResponse(Error error)
// TODO: Pass error message
return new BadRequestResult();
}

[Obsolete("Use the overload with the CancellationToken")]
protected async Task<IActionResult> ExecuteService<TRequest>(TRequest request, Func<TRequest, Task> serviceHandler) where TRequest : class
{
if (request is IIntegrationCommand command)
{
_logger.LogCommandController(command, Request.GetIpAddressString());
}
else
{
_logger.LogCommandController(request, Request.GetIpAddressString());
}

await serviceHandler(request);

return new OkResult();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Claims;
Expand Down Expand Up @@ -87,7 +86,7 @@ protected async Task<IActionResult> HandleQueryResult<T>(Task<Result<T>> queryRe
return new OkObjectResult(mappedResultValue);
}

protected async Task<IActionResult> HandleFileResult(byte[] fileBuffer, bool useCaching, string mimeType, CancellationToken cancellationToken = default)
protected async Task<IActionResult> HandleFileResult(ExportFile file, bool useCaching, string mimeType, CancellationToken cancellationToken = default)
{
LogCurrentScope();

Expand All @@ -103,10 +102,10 @@ protected async Task<IActionResult> HandleFileResult(byte[] fileBuffer, bool use
return await Task.FromResult(File((byte[])cachedFileBuffer, mimeType));
}

StoreToMemoryCache(fileBuffer, cancellationToken);
StoreToMemoryCache(file.FileBuffer, cancellationToken);
}

return await Task.FromResult(File(fileBuffer, mimeType));
return await Task.FromResult(File(file.FileBuffer, mimeType));
}

protected int NormalizePaginationLimit(int? limit)
Expand Down Expand Up @@ -152,20 +151,5 @@ private void LogCurrentScope()

_logger.LogQueryController(currentPath, currentIdentityId, currentHost);
}

[Obsolete("Use the overload with the CancellationToken")]
protected FileStreamResult MapToFile(Stream fileStream, string mimeType)
{
if (fileStream is null || string.IsNullOrEmpty(mimeType)) return null;

Response.ContentType = new MediaTypeHeaderValue(mimeType).ToString();
return File(fileStream, mimeType);
}

[Obsolete("Use the overload with the CancellationToken")]
protected FileStreamResult MapToFile(byte[] fileBuffer, string mimeType)
{
return MapToFile(new MemoryStream(fileBuffer), mimeType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using AccessPointMap.API.Controllers.Base;
using Aircrackng = AccessPointMap.Application.Integration.Aircrackng;
using Wigle = AccessPointMap.Application.Integration.Wigle;
using Wireshark = AccessPointMap.Application.Integration.Wireshark;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

using Aircrackng = AccessPointMap.Application.Integration.Aircrackng;
using Wigle = AccessPointMap.Application.Integration.Wigle;
using Wireshark = AccessPointMap.Application.Integration.Wireshark;

namespace AccessPointMap.API.Controllers
{
Expand Down Expand Up @@ -34,23 +36,33 @@ public IntegrationCommandController(
}

[HttpPost("wigle/csv")]
public async Task<IActionResult> CreateFromWigle([FromForm] Wigle.Commands.CreateAccessPointsFromCsvFile command) =>
await ExecuteService(command, _wigleIntegrationService.Handle);
public async Task<IActionResult> CreateFromWigle(
[FromForm] Wigle.Commands.CreateAccessPointsFromCsvFile command,
CancellationToken cancellationToken) =>
await ExecuteIntegrationCommandAsync(command, _wigleIntegrationService, cancellationToken);

[HttpPost("wigle/csvgz")]
public async Task<IActionResult> CreateFromWigle([FromForm] Wigle.Commands.CreateAccessPointsFromCsvGzFile command) =>
await ExecuteService(command, _wigleIntegrationService.Handle);
public async Task<IActionResult> CreateFromWigle(
[FromForm] Wigle.Commands.CreateAccessPointsFromCsvGzFile command,
CancellationToken cancellationToken) =>
await ExecuteIntegrationCommandAsync(command, _wigleIntegrationService, cancellationToken);

[HttpPost("aircrackng/csv")]
public async Task<IActionResult> CreateAccessPointsFromCsvFile([FromForm] Aircrackng.Commands.CreateAccessPointsFromCsvFile command) =>
await ExecuteService(command, _aircrackngIntegrationService.Handle);
public async Task<IActionResult> CreateAccessPointsFromCsvFile(
[FromForm] Aircrackng.Commands.CreateAccessPointsFromCsvFile command,
CancellationToken cancellationToken = default) =>
await ExecuteIntegrationCommandAsync(command, _aircrackngIntegrationService, cancellationToken);

[HttpPost("aircrackng/cap")]
public async Task<IActionResult> CreatePacketsFromPcapFile([FromForm] Aircrackng.Commands.CreatePacketsFromPcapFile command) =>
await ExecuteService(command, _aircrackngIntegrationService.Handle);
public async Task<IActionResult> CreatePacketsFromPcapFile(
[FromForm] Aircrackng.Commands.CreatePacketsFromPcapFile command,
CancellationToken cancellationToken) =>
await ExecuteIntegrationCommandAsync(command, _aircrackngIntegrationService, cancellationToken);

[HttpPost("wireshark/pcap")]
public async Task<IActionResult> CreatePacketsFromPcapFile([FromForm] Wireshark.Commands.CreatePacketsFromPcapFile command) =>
await ExecuteService(command, _wiresharkIntegrationService.Handle);
public async Task<IActionResult> CreatePacketsFromPcapFile(
[FromForm] Wireshark.Commands.CreatePacketsFromPcapFile command,
CancellationToken cancellationToken) =>
await ExecuteIntegrationCommandAsync(command, _wiresharkIntegrationService, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using AccessPointMap.API.Controllers.Base;
using AccessPointMap.Application.Abstraction;
using AccessPointMap.Infrastructure.Core.Abstraction;
using Wigle = AccessPointMap.Application.Integration.Wigle;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

using Wigle = AccessPointMap.Application.Integration.Wigle;

namespace AccessPointMap.API.Controllers
{
Expand All @@ -16,7 +20,7 @@ public class IntegrationQueryController : QueryController
{
private readonly Wigle.IWigleIntegrationService _wigleIntegrationService;

private const string _csvContentType = "text/csv";
private const string CsvContentMimeType = "text/csv";

public IntegrationQueryController(IUnitOfWork unitOfWork, IMapper mapper, IMemoryCache memoryCache, ILogger<IntegrationQueryController> logger, Wigle.IWigleIntegrationService wigleIntegrationService) : base(unitOfWork, mapper, memoryCache, logger)
{
Expand All @@ -25,12 +29,16 @@ public IntegrationQueryController(IUnitOfWork unitOfWork, IMapper mapper, IMemor
}

[HttpGet("wigle/csv")]
[Produces(_csvContentType)]
public async Task<IActionResult> DownloadWigleCsv()
[Produces(CsvContentMimeType)]
public async Task<IActionResult> DownloadWigleCsv(
CancellationToken cancellationToken)
{
var response = await _wigleIntegrationService.Query(new Wigle.Queries.ExportAccessPointsToCsv());
var result = await _wigleIntegrationService.HandleQueryAsync(new Wigle.Queries.ExportAccessPointsToCsv(), cancellationToken);

// TODO: Error message
if (result.IsFailure) return StatusCode((int)HttpStatusCode.InternalServerError);

return MapToFile((byte[])response, _csvContentType);
return await HandleFileResult(result.Value as ExportFile, true, CsvContentMimeType, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using AccessPointMap.Application.Integration.Core;

namespace AccessPointMap.Application.Integration.Aircrackng
{
internal class AircrackngIntegrationError : IntegrationError
{
protected AircrackngIntegrationError(string message) : base(message) { }

public static AircrackngIntegrationError UploadedCsvFileIsNull => new("The provided CSV file can not be accessed.");
public static AircrackngIntegrationError UploadedPcapFileIsNull => new("The provided CAP/PCAP file can not be accessed.");
public static AircrackngIntegrationError UploadedFileHasInvalidFormat => new("The provided file format is not matching the requirements.");
}
}
Loading