コード例 #1
0
        // --------------------------------------------------------------------------------------------------------------- operations

        /// <summary>
        /// Handles GET operations. Parameters come from the URL or the request stream.
        /// </summary>
        internal async Task WriteGetOperationResultAsync(HttpContext httpContext, ODataRequest odataReq, IConfiguration appConfig)
        {
            var content = ODataMiddleware.LoadContentByVersionRequest(odataReq.RepositoryPath, httpContext);

            if (content == null)
            {
                throw new ContentNotFoundException(string.Format(SNSR.GetString("$Action,ErrorContentNotFound"), odataReq.RepositoryPath));
            }

            var action = ODataMiddleware.ActionResolver.GetAction(content, odataReq.Scenario, odataReq.PropertyName, null, null, httpContext, appConfig);

            if (action == null)
            {
                // check if this is a versioning action (e.g. a checkout)
                SavingAction.AssertVersioningAction(content, odataReq.PropertyName, true);

                SnTrace.System.WriteError($"OData: {odataReq.PropertyName} operation not found " +
                                          $"for content {content.Path} and user {User.Current.Username}.");

                throw new InvalidContentActionException(InvalidContentActionReason.UnknownAction, content.Path, null, odataReq.PropertyName);
            }

            if (!action.IsODataOperation)
            {
                throw new ODataException("Not an OData operation.", ODataExceptionCode.IllegalInvoke);
            }
            if (action.CausesStateChange)
            {
                throw new ODataException("OData action cannot be invoked with HTTP GET.", ODataExceptionCode.IllegalInvoke);
            }

            if (action.Forbidden || (action.GetApplication() != null && !action.GetApplication().Security.HasPermission(PermissionType.RunApplication)))
            {
                throw new InvalidContentActionException("Forbidden action: " + odataReq.PropertyName);
            }

            var response = action is ODataOperationMethodExecutor odataAction
                ? (odataAction.IsAsync ? await odataAction.ExecuteAsync(content) : action.Execute(content))
                : action.Execute(content, GetOperationParameters(action, httpContext.Request));

            if (response is Content responseAsContent)
            {
                await WriteSingleContentAsync(responseAsContent, httpContext)
                .ConfigureAwait(false);

                return;
            }

            response = ProcessOperationResponse(response, odataReq, httpContext, out var count);
            await WriteOperationResultAsync(response, httpContext, odataReq, count)
            .ConfigureAwait(false);
        }
コード例 #2
0
        internal async Task WriteMetadataAsync(HttpContext httpContext, ODataRequest req)
        {
            var content = ODataMiddleware.LoadContentByVersionRequest(req.RepositoryPath, httpContext);

            //var isRoot = content?.ContentType.IsInstaceOfOrDerivedFrom("Site") ?? true;
            var isRoot   = content == null;
            var metadata = isRoot
                ? MetaGenerator.GetMetadata()
                : MetaGenerator.GetMetadata(content, req.IsCollection);

            var mimeType = this.MimeType;

            if (mimeType != null)
            {
                httpContext.Response.ContentType = mimeType;
            }

            await WriteMetadataAsync(httpContext, metadata);
        }
コード例 #3
0
        /// <summary>
        /// Handles POST operations. Parameters come from request stream.
        /// </summary>
        internal async Task WritePostOperationResultAsync(HttpContext httpContext, ODataRequest odataReq, IConfiguration appConfig)
        {
            var content = ODataMiddleware.LoadContentByVersionRequest(odataReq.RepositoryPath, httpContext);

            if (content == null)
            {
                throw new ContentNotFoundException(string.Format(SNSR.GetString("$Action,ErrorContentNotFound"), odataReq.RepositoryPath));
            }

            var action = ODataMiddleware.ActionResolver.GetAction(content, odataReq.Scenario, odataReq.PropertyName, null, null, httpContext, appConfig);

            if (action == null)
            {
                // check if this is a versioning action (e.g. a checkout)
                SavingAction.AssertVersioningAction(content, odataReq.PropertyName, true);

                throw new InvalidContentActionException(InvalidContentActionReason.UnknownAction, content.Path, null, odataReq.PropertyName);
            }

            if (action.Forbidden || (action.GetApplication() != null && !action.GetApplication().Security.HasPermission(PermissionType.RunApplication)))
            {
                throw new InvalidContentActionException("Forbidden action: " + odataReq.PropertyName);
            }

            var response = action is ODataOperationMethodExecutor odataAction
            ? (odataAction.IsAsync ? await odataAction.ExecuteAsync(content) : action.Execute(content))
            : action.Execute(content, await GetOperationParametersAsync(action, httpContext, odataReq));

            if (response is Content responseAsContent)
            {
                await WriteSingleContentAsync(responseAsContent, httpContext)
                .ConfigureAwait(false);

                return;
            }

            response = ProcessOperationResponse(response, odataReq, httpContext, out var count);
            await WriteOperationResultAsync(response, httpContext, odataReq, count)
            .ConfigureAwait(false);
        }
コード例 #4
0
        internal async Task WriteContentPropertyAsync(string path, string propertyName, bool rawValue, HttpContext httpContext, ODataRequest req, IConfiguration appConfig)
        {
            var content = ODataMiddleware.LoadContentByVersionRequest(path, httpContext);

            if (content == null)
            {
                ODataMiddleware.ContentNotFound(httpContext);
                return;
            }

            if (propertyName == ODataMiddleware.ActionsPropertyName)
            {
                var actionItems = ODataTools.GetActionItems(content, req, httpContext).ToArray();
                await WriteActionsAsync(actionItems, httpContext, req);

                return;
            }
            if (propertyName == ODataMiddleware.ChildrenPropertyName)
            {
                await WriteChildrenCollectionAsync(path, httpContext, req)
                .ConfigureAwait(false);
            }

            if (content.Fields.TryGetValue(propertyName, out var field))
            {
                if (field is ReferenceField refField)
                {
                    var refFieldSetting = refField.FieldSetting as ReferenceFieldSetting;
                    var isMultiRef      = true;
                    if (refFieldSetting != null)
                    {
                        isMultiRef = refFieldSetting.AllowMultiple == true;
                    }
                    if (isMultiRef)
                    {
                        await WriteMultiRefContentsAsync(refField.GetData(), httpContext, req)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await WriteSingleRefContentAsync(refField.GetData(), httpContext)
                        .ConfigureAwait(false);
                    }
                }
                else if (field is AllowedChildTypesField actField)
                {
                    await WriteMultiRefContentsAsync(actField.GetData(), httpContext, req)
                    .ConfigureAwait(false);
                }
                else if (!rawValue)
                {
                    await WriteSingleContentAsync(httpContext, new ODataEntity { { propertyName, field.GetData() } })
                    .ConfigureAwait(false);
                }
                else
                {
                    await WriteRawAsync(field.GetData(), httpContext)
                    .ConfigureAwait(false);
                }
            }
            else
            {
                await WriteGetOperationResultAsync(httpContext, req, appConfig)
                .ConfigureAwait(false);
            }
        }