Пример #1
0
        public OperationExecuteResponse Execute(Dictionary <string, string> requestParameters = null)
        {
            ICustomOperation operation = CreateInstanceForOperation();

            this.SetInputParameters(operation, requestParameters);

            OperationResult result = operation.Execute();

            return(new OperationExecuteResponse()
            {
                Outputs = this.GetOutputParameters(operation),
                Result = result
            });
        }
Пример #2
0
        public void SetInputParameters(ICustomOperation customOperation, Dictionary <string, string> requestParameters)
        {
            if (requestParameters == null || requestParameters.Count <= 0)
            {
                return;
            }

            Type objectType = customOperation.GetType();

            foreach (var parameter in requestParameters)
            {
                PropertyInfo propertyInfo = objectType.GetProperty(parameter.Key);
                if (IsInputParameters(propertyInfo))
                {
                    propertyInfo.SetValue(customOperation, Convert.ChangeType(parameter.Value, propertyInfo.PropertyType));
                }
            }
        }
Пример #3
0
        public Dictionary <string, string> GetOutputParameters(ICustomOperation customOperation)
        {
            Dictionary <string, string> outputs = new Dictionary <string, string>();

            foreach (PropertyInfo prop in customOperation.GetType().GetProperties())
            {
                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    if (attr is VariableMappingAttribute)
                    {
                        object retObj = prop.GetValue(customOperation);
                        string retStr = retObj != null?Convert.ToString(retObj) : string.Empty;

                        outputs.Add(prop.Name, retStr);
                    }
                }
            }
            return(outputs);
        }
        public static void RegisterCustomOperation(ICustomOperation customOperation, string servicePath)
        {
            if (customOperation == null || string.IsNullOrWhiteSpace(customOperation.OperationRestPath))
            {
                return;
            }

            servicePath = string.IsNullOrWhiteSpace(servicePath) ? ServiceMetadata.DefaultServicePath : servicePath.Trim().ToLower();
            if (!EndpointHost.Config.MetadataMap.ContainsKey(servicePath))
            {
                throw new Exception(string.Format("Make sure the service path '{0}' is existing in the apphost.", servicePath));
            }

            string restPath = customOperation.OperationRestPath.Trim().ToLower();

            if (!CustomOperations.ContainsKey(servicePath))
            {
                CustomOperations[servicePath] = new Dictionary <string, ICustomOperation>();
            }
            CustomOperations[servicePath][restPath] = customOperation;

            WhiteListPlugin.ExcludePathController(restPath, servicePath);
        }
Пример #5
0
        public OperationExecuteResponse Execute(Dictionary <string, string> requestParameters = null, bool useProxy = true)
        {
            ICustomOperation operation = null;

            if (useProxy)
            {
                operation = CreateProxyInstanceForOperation();
            }
            else
            {
                operation = CreateInstanceForOperation();
            }

            this.SetInputParameters(operation, requestParameters);

            OperationResult result = operation.Execute();

            return(new OperationExecuteResponse()
            {
                Outputs = this.GetOutputParameters(operation),
                Result = result
            });
        }
        public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            DataFormat format = httpReq.ContentType.ToDataFormat();

            if (format == DataFormat.NotSupported)
            {
                format = httpReq.QueryString["format"].ToDataFormat();
            }
            HttpMethodEnum   method          = httpReq.HttpMethod.ToHttpMethod();
            ICustomOperation customOperation = CustomOperations[httpReq.ServicePath][RestPath];
            AckCodeType?     ack             = AckCodeType.Success;

            try
            {
                if (method == HttpMethodEnum.NotSupported)
                {
                    throw new NotSupportedException("HTTP Method " + method + " is not supported.");
                }
                if (format == DataFormat.NotSupported)
                {
                    throw new NotSupportedException("Data Transfer Format is not supported.");
                }

                if (EndpointHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    ack = null;
                    return;
                }

                if (customOperation.RequestDTOType != null)
                {
                    if (method == HttpMethodEnum.POST || method == HttpMethodEnum.PUT)
                    {
                        if (httpReq.InputStream.Length > 0)
                        {
                            using (Stream stream = httpReq.InputStream)
                            {
                                httpReq.RequestObject = GeneralSerializer.Deserialize(customOperation.RequestDTOType, stream, format);
                            }
                        }
                    }
                    else
                    {
                        bool hasOnlyFormatParam = httpReq.QueryString.Count == 1 && httpReq.QueryString["format"] != null;
                        if (httpReq.QueryString.Count > 0 && !hasOnlyFormatParam)
                        {
                            httpReq.RequestObject = GeneralSerializer.DeserializeFromQueryString(customOperation.RequestDTOType, httpReq.QueryString);
                        }
                    }
                }

                if (!customOperation.IsValidRequest(httpReq, httpReq.RequestObject))
                {
                    throw new UnauthorizedAccessException("Not allowed.");
                }

                httpRes.ContentType = format.ToContentType();
                object responseObject = customOperation.ExecuteOperation(httpReq, httpReq.RequestObject);
                if (responseObject == null)
                {
                    return;
                }

                if (EndpointHost.Config.MetadataMap[ServicePath].UseChunkedTransferEncoding)
                {
                    httpRes.AddHeader(ServiceUtils.ResponseStatusHttpHeaderKey, ack.Value.ToString());
                    EndpointsExtensions.HttpResponseExtensions.UseChunkedTransferEncoding(httpRes);
                }

                using (Stream stream = httpRes.OutputStream)
                {
                    GeneralSerializer.Serialize(responseObject, stream, format);
                }
            }
            catch (Exception ex)
            {
                ack = AckCodeType.Failure;
                ErrorUtils.LogError("Custom Operation Error", httpReq, ex, true, "FXD300008");
                httpRes.StatusCode = EndpointsExtensions.HttpResponseExtensions.ToStatusCode(ex);
            }
            finally
            {
                if (ack.HasValue)
                {
                    if (!EndpointsExtensions.HttpResponseExtensions.UsedChunkedTransferEncoding(httpRes))
                    {
                        httpRes.AddHeader(ServiceUtils.ResponseStatusHttpHeaderKey, ack.Value.ToString());
                    }

                    httpRes.LogRequest(httpReq);
                }

                HostContext.Instance.EndRequest();
            }
        }
 public static void RegisterCustomOperation(ICustomOperation customOperation)
 {
     RegisterCustomOperation(customOperation, null);
 }