internal IAsyncResult BeginDoRequest <T>(T request, DoValidateDelegate doValidateDelegate,
                                                 AsyncCallback callback, object state) where T : ObsWebServiceRequest
        {
            HttpContext context = this.BeforeRequest(request, doValidateDelegate, true);

            try
            {
                HttpObsAsyncResult result = this.httpClient.BeginPerformRequest(this.PrepareHttpRequest(request, context), context, callback, state);

                result.AdditionalState = new object[] { request, context };
                return(result);
            }
            catch (ObsException ex)
            {
                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(string.Format("{0} exception code: {1}, with message: {2}", request.GetAction(), ex.ErrorCode, ex.Message));
                }
                throw ex;
            }
            catch (Exception ex)
            {
                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(string.Format("{0} exception with message: {1}", request.GetAction(), ex.Message));
                }
                throw new ObsException(ex.Message, ex);
            }
        }
Example #2
0
        internal K DoRequest <T, K>(T request, DoValidateDelegate doValidateDelegate)
            where T : ObsWebServiceRequest
            where K : ObsWebServiceResponse
        {
            HttpContext context     = this.BeforeRequest(request, doValidateDelegate, false);
            DateTime    reqTime     = DateTime.Now;
            HttpRequest httpRequest = null;

            try
            {
                httpRequest = this.PrepareHttpRequest(request, context);
                HttpResponse httpResponse = this.httpClient.PerformRequest(httpRequest, context);
                return(PrepareResponse <T, K>(request, context, httpRequest, httpResponse));
            }
            catch (ObsException ex)
            {
                if ("CreateBucket".Equals(request.GetAction()) &&
                    ex.StatusCode == HttpStatusCode.BadRequest &&
                    "Unsupported Authorization Type".Equals(ex.ErrorMessage) &&
                    this.ObsConfig.AuthTypeNegotiation &&
                    context.AuthType == AuthTypeEnum.OBS)
                {
                    try
                    {
                        if (httpRequest.Content != null && httpRequest.Content.CanSeek)
                        {
                            httpRequest.Content.Seek(0, SeekOrigin.Begin);
                        }
                        context.AuthType = AuthTypeEnum.V2;
                        return(PrepareResponse <T, K>(request, context, httpRequest, this.httpClient.PerformRequest(httpRequest, context)));
                    }catch (ObsException _ex)
                    {
                        if (LoggerMgr.IsErrorEnabled)
                        {
                            LoggerMgr.Error(string.Format("{0} exception code: {1}, with message: {2}", request.GetAction(), _ex.ErrorCode, _ex.Message));
                        }
                        throw _ex;
                    }
                    catch (Exception _ex)
                    {
                        if (LoggerMgr.IsErrorEnabled)
                        {
                            LoggerMgr.Error(string.Format("{0} exception with message: {1}", request.GetAction(), _ex.Message));
                        }
                        throw new ObsException(_ex.Message, _ex);
                    }
                }

                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(string.Format("{0} exception code: {1}, with message: {2}", request.GetAction(), ex.ErrorCode, ex.Message));
                }
                throw ex;
            }
            catch (Exception ex)
            {
                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(string.Format("{0} exception with message: {1}", request.GetAction(), ex.Message));
                }
                throw new ObsException(ex.Message, ex);
            }
            finally
            {
                if (request != null)
                {
                    request.Sender = null;
                }

                CommonUtil.CloseIDisposable(httpRequest);

                if (LoggerMgr.IsInfoEnabled)
                {
                    LoggerMgr.Info(string.Format("{0} end, cost {1} ms", request.GetAction(), (DateTime.Now - reqTime).TotalMilliseconds));
                }
            }
        }
Example #3
0
        private HttpContext BeforeRequest <T>(T request, DoValidateDelegate doValidateDelegate, bool async) where T : ObsWebServiceRequest
        {
            if (request == null)
            {
                throw new ObsException(Constants.NullRequestMessage, ErrorType.Sender, Constants.NullRequest, "");
            }

            HttpContext context = new HttpContext(this.sp, this.ObsConfig);

            if (request is GetApiVersionRequest)
            {
                context.SkipAuth = true;
            }
            else
            {
                ObsBucketWebServiceRequest _request = request as ObsBucketWebServiceRequest;
                if (_request != null && string.IsNullOrEmpty(_request.BucketName))
                {
                    throw new ObsException(Constants.InvalidBucketNameMessage, ErrorType.Sender, Constants.InvalidBucketName, "");
                }
                if (this.ObsConfig.AuthTypeNegotiation)
                {
                    AuthTypeEnum?authType;
                    if (_request == null)
                    {
                        //list buckets
                        authType = this.NegotiateAuthType(null, async);
                    }
                    else
                    {
                        if ((authType = this.authTypeCache.GetAuthType(_request.BucketName)) == null)
                        {
                            lock (this.locksHolder.GetLock(_request.BucketName))
                            {
                                if ((authType = this.authTypeCache.GetAuthType(_request.BucketName)) == null)
                                {
                                    if (request is CreateBucketRequest)
                                    {
                                        authType = this.NegotiateAuthType(null, async);
                                    }
                                    else
                                    {
                                        authType = this.NegotiateAuthType(_request.BucketName, async);
                                        this.authTypeCache.RefreshAuthType(_request.BucketName, authType.Value);
                                        if (LoggerMgr.IsInfoEnabled)
                                        {
                                            LoggerMgr.Info(string.Format("Refresh auth type {0} for bucket {1}", authType, _request.BucketName));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (LoggerMgr.IsDebugEnabled)
                    {
                        LoggerMgr.Debug(string.Format("Get auth type {0}", authType));
                    }
                    context.AuthType = authType;
                }
            }

            request.Sender = this;
            doValidateDelegate?.Invoke();

            if (LoggerMgr.IsInfoEnabled)
            {
                LoggerMgr.Info(request.GetAction() + " begin.");
            }
            return(context);
        }