public async Task <IActionResult> GetNew(string dataType, int count = 1) { // Validate IRdDataStorage rdDataStorage; try { rdDataStorage = await dataRouter.GetSourceSystemAsync(dataType); } catch (KeyNotFoundException e) { return(BadRequest(e.Message)); } // Authorize var loggedInUsername = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name); var resourceDescription = new SubmitDataResourceDescription(dataType); var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername); if (!authorizationResult.IsAuthorized) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized")); } // Provide var idReservationResult = await rdDataStorage.GetIdsAsync(dataType, loggedInUsername, count); if (idReservationResult.Any(reservationResult => !reservationResult.IsReserved)) { return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to reserve ID")); } if (idReservationResult.Any(x => x.IsNewCollection)) { newCollectionTasks.PerformTasks(dataType, authorizationResult.User); } return(new ContentResult { ContentType = "text/plain", Content = string.Join("\n", idReservationResult.Select(x => x.Id)), StatusCode = (int)HttpStatusCode.OK }); }
public async Task <IActionResult> TransferSubmissionData([FromQuery] string dataType, [FromQuery] string submissionId) { if (submissionId == null) { return(BadRequest("Invalid submissionId")); } // Authorize var loggedInUsername = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name); var resourceDescription = new SubmitDataResourceDescription(dataType); var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername); if (!authorizationResult.IsAuthorized) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized")); } IRdDataStorage rdDataStorage; try { rdDataStorage = await dataRouter.GetSourceSystemAsync(dataType); } catch (KeyNotFoundException) { return(BadRequest($"No data storage backend for data type '{dataType}'")); } if (!(rdDataStorage is IBinaryRdDataStorage binaryRdDataStorage)) { return(BadRequest($"Transfer of data for data type '{dataType}' is not supported")); } if (!await binaryRdDataStorage.ExistsAsync(dataType, submissionId)) { return(NotFound()); } apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' has injected binary payload into '{dataType}' with ID '{submissionId}'"); await binaryRdDataStorage.InjectDataAsync(dataType, submissionId, Request.Body); return(Ok()); }
public static async Task <IActionResult> PerformSearch(IDataRouter dataRouter, string query, ResultFormat resultFormat) { try { var parsedQuery = DataApiSqlQueryParser.Parse(query); var collection = parsedQuery.FromArguments.Trim(); var rdDataStorage = await dataRouter.GetSourceSystemAsync(collection); var result = rdDataStorage.SearchAsync(parsedQuery); switch (resultFormat) { case ResultFormat.Json: return(BuildJsonResultAsync(result)); case ResultFormat.Csv: return(BuildCsvResultAsync(result)); default: throw new ArgumentOutOfRangeException(nameof(resultFormat), resultFormat, null); } } catch (FormatException formatException) { return(new ContentResult { ContentType = "text/plain", Content = formatException.Message, StatusCode = (int)HttpStatusCode.BadRequest }); } catch (AggregateException aggregateException) { var innermostException = aggregateException.InnermostException(); if (innermostException is FormatException) { return(new ContentResult { ContentType = "text/plain", Content = aggregateException.Message, StatusCode = (int)HttpStatusCode.BadRequest }); } return(new ContentResult { Content = aggregateException.InnerException?.Message ?? aggregateException.Message, ContentType = "text/plain", StatusCode = (int)HttpStatusCode.InternalServerError }); } }
private async Task <bool> MatchesFilterAsync(string dataType, string dataObjectId, string filter) { if (string.IsNullOrWhiteSpace(dataType)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(dataType)); } if (string.IsNullOrEmpty(filter)) { return(true); } var query = $"SELECT * FROM {dataType} WHERE _id = '{dataObjectId}' AND ({filter})"; var parsedQuery = DataApiSqlQueryParser.Parse(query); var collection = await dataRouter.GetSourceSystemAsync(dataType); return(await collection.SearchAsync(parsedQuery).AnyAsync()); }
public async Task <bool> CheckValidReferenceAsync(JToken jToken, Match regexMatch) { if (jToken == null) { return(false); } var dataType = regexMatch.Groups[1].Value; var id = jToken.Value <string>(); IRdDataStorage dataStorage; try { dataStorage = await dataRouter.GetSourceSystemAsync(dataType); } catch (KeyNotFoundException) { return(false); } return(await dataStorage.ExistsAsync(dataType, id)); }
public async Task <IActionResult> GetFile([FromQuery] string dataType, [FromQuery] string id, [FromQuery] string shortId) { if (!string.IsNullOrEmpty(shortId)) { var shortIdDataStorage = await dataRouter.GetSourceSystemAsync(nameof(ShortId)); var shortIdContainer = await shortIdDataStorage.GetFromIdAsync(nameof(ShortId), shortId); if (shortIdContainer == null) { return(NotFound()); } var shortIdJson = DataEncoder.DecodeToJson(shortIdContainer.Data); var shortIdObject = JsonConvert.DeserializeObject <ShortId>(shortIdJson); dataType = shortIdObject.CollectionName; id = shortIdObject.OriginalId; } // Validate if (string.IsNullOrEmpty(dataType)) { return(BadRequest("Data type not specified")); } if (string.IsNullOrEmpty(id)) { return(BadRequest("ID not specified")); } // Authorize var loggedInUsername = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name); var resourceDescription = new GetDataResourceDescription(dataType); var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername); if (!authorizationResult.IsAuthorized) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized")); } IRdDataStorage rdDataStorage; try { rdDataStorage = await dataRouter.GetSourceSystemAsync(dataType); } catch (KeyNotFoundException e) { return(BadRequest(e.Message)); } if (!await rdDataStorage.ExistsAsync(dataType, id)) { return(NotFound()); } // Provide apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' has accessed binary payload of type '{dataType}' with ID '{id}'"); if (!(rdDataStorage is IBinaryRdDataStorage binaryRdDataStorage)) { var metadata = await rdDataStorage.GetFromIdAsync(dataType, id); var metadataJson = DataEncoder.DecodeToJson(metadata.Data); var filename = GetFilename(dataType, id, metadataJson); return(new FileContentResult(Encoding.UTF8.GetBytes(metadataJson), Conventions.JsonContentType) { FileDownloadName = filename }); }