Exemplo n.º 1
0
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            if (!_option.RestOption.EnableJwt)
            {
                await _next.Invoke(context);

                return;
            }

            try
            {
                //开启Jwt认证,需要在发起了AssignFileKey请求之后,将数据保存下来
                if (context.Response.GetType() == typeof(AssignFileKeyResponse))
                {
                    var fid            = ((AssignFileKeyResponse)context.Response).Fid;
                    var authentication = context.Response.Headers.FirstOrDefault(x => x.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase));
                    //添加到token管理器
                    _jwtManager.AddAssignJwt(fid, authentication.Value.ToString());
                }
            }
            catch (Exception ex)
            {
                SetPipelineError(context, new AssignJwtError($"AssignJwt出错,{ex.Message}"));
                return;
            }

            await _next.Invoke(context);
        }
Exemplo n.º 2
0
 /// <summary>设置错误信息
 /// </summary>
 public void SetPipelineError(RestExecuteContext context, List <Error> errors)
 {
     foreach (var error in errors)
     {
         SetPipelineError(context, error);
     }
 }
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            if (!_option.RestOption.EnableJwt)
            {
                await _next.Invoke(context);

                return;
            }

            try
            {
                //上传,删除文件之前获取Jwt
                if (IsPreAuthRequestAuth(context.Request.GetType()))
                {
                    //Assign Jwt
                    var jwt = _jwtManager.GetAssignJwt(context.Request.Fid);

                    //添加认证头部
                    context.Builder.AddParameter("Authorization", jwt, ParameterType.HttpHeader);
                }
            }
            catch (Exception ex)
            {
                SetPipelineError(context, new AssignJwtError($"UploadAuthentication出错,{ex.Message}"));
                return;
            }

            await _next.Invoke(context);
        }
Exemplo n.º 4
0
 /// <summary>设置错误信息
 /// </summary>
 public void SetPipelineError(RestExecuteContext context, Error error)
 {
     if (context.Request == null)
     {
         Logger.LogError("执行Seaweedfs请求出错,RestExecuteContext中的Request为空.{0}", error.Message);
     }
     context?.Errors.Add(error);
 }
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            try
            {
                var httpBuilder = context.Request.CreateBuilder();
                context.Builder = httpBuilder;
            }
            catch (Exception ex)
            {
                SetPipelineError(context, new BuildHttpError($"构建HttpBuilder出错,{ex.Message}"));
                return;
            }

            await _next.Invoke(context);
        }
Exemplo n.º 6
0
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            try
            {
                //获取连接
                var connection = GetConnection(context);
                if (connection == null)
                {
                    Logger.LogError("无法根据请求的相关数据获取连接Connection!");
                    SetPipelineError(context, new ExecuteError("无法根据请求的相关数据获取连接Connection!"));
                    return;
                }
                IRestRequest restRequest  = context.Builder.BuildRequest();
                var          restResponse = await connection.ExecuteAsync(restRequest);

                Logger.LogDebug("Execute执行返回结果为:{0}", restResponse.Content);
                //设置response返回的状态,头部Header等
                if (!restResponse.IsSuccessful)
                {
                    SetPipelineError(context, new ExecuteError("RestResponse返回数据不正确!"));
                    return;
                }
                context.Response = _jsonSerializer.Deserialize(restResponse.Content, context.Response.GetType()) as SeaweedfsResponse;
                context.Response.IsSuccessful   = restResponse.IsSuccessful;
                context.Response.ErrorException = restResponse.ErrorException;
                context.Response.ErrorMessage   = restResponse.ErrorMessage;

                foreach (var header in restResponse.Headers)
                {
                    context.Response.Headers.Add(header.TransferToParameter());
                }
            }
            catch (Exception ex)
            {
                SetPipelineError(context, new ExecuteError($"调用Execute出错,{ex.Message}"));
                return;
            }

            await _next.Invoke(context);
        }
Exemplo n.º 7
0
        private Connection GetConnection(RestExecuteContext context)
        {
            Connection connection = null;

            if (context.ServerType == ServerType.Master)
            {
                connection = _connectionManager.GetMasterConnection();
            }
            else if (context.ServerType == ServerType.Volume)
            {
                //根据指定的服务器地址获取连接
                if (!context.Request.AssignServer.IsNullOrWhiteSpace())
                {
                    connection = _connectionManager.GetVolumeConnectionByUrl(context.AssignServer);
                }
                else if (!context.Request.Fid.IsNullOrWhiteSpace())
                {
                    connection = _connectionManager.GetVolumeConnectionByVolumeIdOrFid(context.Request.Fid);
                }
            }
            return(connection);
        }
Exemplo n.º 8
0
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            if (!_option.RestOption.EnableJwt)
            {
                await _next.Invoke(context);

                return;
            }

            try
            {
                if (context.Response.GetType() == typeof(LookupResponse))
                {
                    var lookupRequest = (LookupRequest)context.Request;

                    var authentication = context.Response.Headers.FirstOrDefault(x => x.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase));
                    if (lookupRequest.IsRead.HasValue && lookupRequest.IsRead.Value)
                    {
                        if (_option.RestOption.EnableReadJwt)
                        {
                            //添加到Read Jwt管理器
                            _jwtManager.AddReadJwt(context.Request.Fid, authentication.Value.ToString());
                        }
                    }
                    else
                    {
                        //添加到token管理器
                        _jwtManager.AddLookupJwt(context.Request.Fid, authentication.Value.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                SetPipelineError(context, new LookupJwtError($"Lookup Jwt出错,{ex.Message}"));
                return;
            }
            await _next.Invoke(context);
        }
Exemplo n.º 9
0
        /// <summary>Invoke
        /// </summary>
        public async Task InvokeAsync(RestExecuteContext context)
        {
            await Task.CompletedTask;

            return;
        }