コード例 #1
0
        public async Task <IActionResult> GetSubmissionMetadata([FromQuery] string dataType, [FromQuery] string id)
        {
            // Validate
            if (dataType == null)
            {
                return(BadRequest("Invalid dataType"));
            }
            if (id == null)
            {
                return(BadRequest("Invalid id"));
            }

            // 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)
            {
                return(BadRequest($"No data storage backend for data type '{dataType}'"));
            }

            if (!(rdDataStorage is IBinaryRdDataStorage binaryRdDataStorage))
            {
                return(await Get(dataType, id));
            }

            if (!await binaryRdDataStorage.ExistsAsync(dataType, id))
            {
                return(NotFound());
            }

            apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' has accessed metadata of type '{dataType}' with ID '{id}'");
            var container = await binaryRdDataStorage.GetMetadataFromId(dataType, id);

            var json = DataEncoder.DecodeToJson(container.Data);

            return(new ContentResult
            {
                ContentType = Conventions.JsonContentType,
                Content = json,
                StatusCode = (int)HttpStatusCode.OK
            });
        }
コード例 #2
0
        public async Task <IActionResult> Get([FromQuery] string dataType, [FromQuery] string id)
        {
            // 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));
            }

            // Provide
            if (!await rdDataStorage.ExistsAsync(dataType, id))
            {
                return(NotFound());
            }
            apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' has accessed data of type '{dataType}' with ID '{id}'");
            var matchingContainer = await rdDataStorage.GetFromIdAsync(dataType, id);

            var json = DataEncoder.DecodeToJson(matchingContainer.Data);

            return(new ContentResult
            {
                ContentType = Conventions.JsonContentType,
                Content = json,
                StatusCode = (int)HttpStatusCode.OK
            });
        }
コード例 #3
0
        public async Task <IActionResult> GetMany([FromQuery] string dataType, [FromQuery] string whereArguments, [FromQuery] string orderByArguments, [FromQuery] uint?limit = null)
        {
            // Validate
            if (string.IsNullOrEmpty(dataType))
            {
                return(BadRequest("Data type 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));
            }

            apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' requested objects of type '{dataType}' matching '{whereArguments?.RemoveLineBreaks()}' ordered by '{orderByArguments?.RemoveLineBreaks()}'");
            try
            {
                var getManyResult = rdDataStorage.GetManyAsync(dataType, whereArguments, orderByArguments, limit);
                var stream        = new SearchResultStream(getManyResult.Select(x => DataEncoder.DecodeToJson(x.Data)));
                return(new FileStreamResult(stream, Conventions.JsonContentType));
            }
            catch (FormatException formatException)
            {
                return(BadRequest(formatException.Message));
            }
            catch (Exception e)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, e.InnermostException().Message));
            }
        }
コード例 #4
0
        public async Task <IActionResult> Exists([FromQuery] string dataType, [FromQuery] string id)
        {
            // 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));
            }

            // Provide
            var exists = await rdDataStorage.ExistsAsync(dataType, id);

            if (exists)
            {
                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }
コード例 #5
0
        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
                });
            }