public TestIndicesUtilCustomization(
     IRequestConfiguration requestConfig,
     ISettingsProvider settings = null)
 {
     this.settings = settings;
     this.requestConfig = requestConfig;
 }
		public RequestData(HttpMethod method, string path, PostData<object> data, IConnectionConfigurationValues global, IRequestConfiguration local, IMemoryStreamFactory memoryStreamFactory)
		{
			this.ConnectionSettings = global;
			this.MemoryStreamFactory = memoryStreamFactory;
			this.Method = method;
			this.PostData = data;
			this.Path = this.CreatePathWithQueryStrings(path, this.ConnectionSettings, null);

			this.Pipelined = global.HttpPipeliningEnabled || (local?.EnableHttpPipelining).GetValueOrDefault(false);
			this.HttpCompression = global.EnableHttpCompression;
			this.ContentType = local?.ContentType ?? MimeType;
			this.Headers = global.Headers;

			this.RequestTimeout = local?.RequestTimeout ?? global.RequestTimeout;
			this.PingTimeout = 
				local?.PingTimeout
				?? global?.PingTimeout
				?? (global.ConnectionPool.UsingSsl ? ConnectionConfiguration.DefaultPingTimeoutOnSSL : ConnectionConfiguration.DefaultPingTimeout);

			this.KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000);
			this.KeepAliveTime = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000);

			this.ProxyAddress = global.ProxyAddress;
			this.ProxyUsername = global.ProxyUsername;
			this.ProxyPassword = global.ProxyPassword;
			this.DisableAutomaticProxyDetection = global.DisableAutomaticProxyDetection;
			this.BasicAuthorizationCredentials = local?.BasicAuthenticationCredentials ?? global.BasicAuthenticationCredentials;
			this.CancellationToken = local?.CancellationToken ?? CancellationToken.None;
			this.AllowedStatusCodes = local?.AllowedStatusCodes ?? Enumerable.Empty<int>();
		}
示例#3
0
        /// <summary>
        /// Calculates the number of required connections for routing and request messages.
        /// </summary>
        /// <param name="routingConfiguration">Contains the number of routing requests in parallel.</param>
        /// <param name="requestConfiguration">Contains the number of requests for P2P operations in parallel.</param>
        /// <param name="builder">The builder that tells us if we should TCP or UDP.</param>
        /// <returns>The future channel creator.</returns>
        public Task<ChannelCreator> CreateAsync(RoutingConfiguration routingConfiguration,
            IRequestConfiguration requestConfiguration, DefaultConnectionConfiguration builder)
        {
            if (routingConfiguration == null && requestConfiguration == null)
            {
                throw new ArgumentException("Both routing configuration and request configuration must be set.");
            }

            int nrConnectionsUdp = 0;
            int nrConnectionsTcp = 0;
            if (requestConfiguration != null)
            {
                if (builder.IsForceUdp)
                {
                    nrConnectionsUdp = requestConfiguration.Parallel;
                }
                else
                {
                    nrConnectionsTcp = requestConfiguration.Parallel;
                }
            }
            if (routingConfiguration != null)
            {
                if (!builder.IsForceTcp)
                {
                    nrConnectionsUdp = Math.Max(nrConnectionsUdp, routingConfiguration.Parallel);
                }
                else
                {
                    nrConnectionsTcp = Math.Max(nrConnectionsTcp, routingConfiguration.Parallel);
                }
            }

            return CreateAsync(nrConnectionsUdp, nrConnectionsTcp);
        }
		protected override Task<ElasticsearchResponse<Stream>> DoAsyncRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
		{
			return Task.Factory.StartNew(() =>
			{
				var cs = this.ReturnConnectionStatus(request, data, requestSpecificConfig);
				return cs;
			});
		}
		public ElasticsearchResponse<Stream> HeadSync(Uri uri, IRequestConfiguration requestConfiguration = null)
		{
			var restRequest = new RestRequest();
			restRequest.Method = Method.HEAD;
			restRequest.Uri = uri;

			restRequest.Headers = new Dictionary<string, string>();
			restRequest.Headers.Add("Content-Type", "application/json");
			return this.Execute(restRequest, requestConfiguration);
		}
		private ElasticsearchResponse<Stream> ReturnConnectionStatus(HttpWebRequest request, byte[] data, IRequestConfiguration requestSpecificConfig = null)
		{
			var method = request.Method;
			var path = request.RequestUri.ToString();

			var cs = ElasticsearchResponse<Stream>.Create(this.ConnectionSettings, _statusCode, method, path, data);
			cs.Response = new MemoryStream(_fixedResultBytes);
			if (this.ConnectionSettings.ConnectionStatusHandler != null)
				this.ConnectionSettings.ConnectionStatusHandler(cs);
			return cs;
		}
        protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data,
            IRequestConfiguration requestSpecificConfig)
        {
            DateTime tsStart = DateTime.Now;
            HttpWebRequest request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
            TimeSpan timeSpan = DateTime.Now.Subtract(tsStart);

            RequestHandler.Add(tsStart, timeSpan, request.Method, uri, Encoding.UTF8.GetString(data));

            return request;
        }
        protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            if (_authType == AuthType.InstanceProfile)
            {
                RefreshCredentials();
            }

            var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
            SignV4Util.SignRequest(request, data, _accessKey, _secretKey, _token, _region, "es");
            return request;
        }
		public Task<ElasticsearchResponse<Stream>> Head(Uri uri, IRequestConfiguration requestConfiguration = null)
		{
			var restRequest = new RestRequest();
			restRequest.Method = Method.HEAD;
			restRequest.Uri = uri;

			restRequest.Headers = new Dictionary<string, string>();
			restRequest.Headers.Add("Content-Type", "application/json");
			return Task.Factory.StartNew<ElasticsearchResponse<Stream>>(() =>
			{
				return this.Execute(restRequest, requestConfiguration);
			});
		}
        private void CreateIndex(IRequestConfiguration requestConfig)
        {
            var indexName = requestConfig.GenerateIndexIdentifier(this.ctx);
            this.client.Raw.IndicesCreate(indexName, null);

            const int CircuitBreaker = 2;
            int count = 0;
            do
            {
                if (count > 0) // on retries - wait a bit
                    System.Threading.Thread.Sleep(25);

                // ES creates new indices in an async fashion, so you have to check manually when the index is ready
                // across the whole cluster to receive writes.  Waiting for one healthy shard is good enough.
                this.client.ClusterHealth(p => p.Index(indexName).WaitForActiveShards(1).Timeout("5s"));
                count++;
            }
            while (count <= CircuitBreaker);
        }
		/// <summary>
		/// Wraps the DoRequest to run synchronously
		/// </summary>
		/// <param name="method">The method.</param>
		/// <param name="uri">The URI.</param>
		/// <param name="data">The data.</param>
		/// <param name="requestSpecificConfig">The request specific configuration.</param>
		/// <returns>ElasticsearchResponse&lt;Stream&gt;.</returns>
		public ElasticsearchResponse<Stream> DoRequestSync(HttpMethod method, Uri uri, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
		{
			ThrowIfDisposed();

			var requestTask = DoRequest(method, uri, data, requestSpecificConfig);

			try
			{
				requestTask.Wait();
				return requestTask.Result;
			}
			catch (AggregateException ex)
			{
				return ElasticsearchResponse<Stream>.CreateError(_settings, ex.Flatten(), method.ToString().ToLowerInvariant(), uri.ToString(), data);
			}
			catch (Exception ex)
			{
				return ElasticsearchResponse<Stream>.CreateError(_settings, ex, method.ToString().ToLowerInvariant(), uri.ToString(), data);
			}
		}
示例#12
0
		private RequestData(
			HttpMethod method,
			string path,
			PostData<object> data,
			IConnectionConfigurationValues global,
			IRequestConfiguration local,
			IMemoryStreamFactory memoryStreamFactory)
		{
			this.ConnectionSettings = global;
			this.MemoryStreamFactory = memoryStreamFactory;
			this.Method = method;
			this.PostData = data;

			if (data != null)
				data.DisableDirectStreaming = local?.DisableDirectStreaming ?? global.DisableDirectStreaming;

			this.Path = this.CreatePathWithQueryStrings(path, this.ConnectionSettings, null);

			this.Pipelined = local?.EnableHttpPipelining ?? global.HttpPipeliningEnabled;
			this.HttpCompression = global.EnableHttpCompression;
			this.ContentType = local?.ContentType ?? MimeType;
			this.Accept = local?.Accept ?? MimeType;
			this.Headers = global.Headers != null ? new NameValueCollection(global.Headers) : new NameValueCollection();
			this.RunAs = local?.RunAs;

			this.RequestTimeout = local?.RequestTimeout ?? global.RequestTimeout;
			this.PingTimeout =
				local?.PingTimeout
				?? global?.PingTimeout
				?? (global.ConnectionPool.UsingSsl ? ConnectionConfiguration.DefaultPingTimeoutOnSSL : ConnectionConfiguration.DefaultPingTimeout);

			this.KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000);
			this.KeepAliveTime = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000);

			this.ProxyAddress = global.ProxyAddress;
			this.ProxyUsername = global.ProxyUsername;
			this.ProxyPassword = global.ProxyPassword;
			this.DisableAutomaticProxyDetection = global.DisableAutomaticProxyDetection;
			this.BasicAuthorizationCredentials = local?.BasicAuthenticationCredentials ?? global.BasicAuthenticationCredentials;
			this.AllowedStatusCodes = local?.AllowedStatusCodes ?? Enumerable.Empty<int>();
		}
		private ElasticsearchResponse<Stream> BodyRequest(Uri uri, byte[] data, string method, IRequestConfiguration requestSpecificConfig)
		{
			var r = this.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
			return this.DoSynchronousRequest(r, data, requestSpecificConfig);
		}
		private int GetRequestTimeout(IRequestConfiguration requestConfiguration)
		{
			if (requestConfiguration != null && requestConfiguration.ConnectTimeout.HasValue)
				return requestConfiguration.RequestTimeout.Value;

			return this.ConnectionSettings.Timeout;
		}
		public virtual ElasticsearchResponse<Stream> DeleteSync(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
		{
			return this.BodyRequest(uri, data, "DELETE", requestSpecificConfig);
		}
        public virtual Task <ElasticsearchResponse <Stream> > Put(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
        {
            var r = this.CreateHttpWebRequest(uri, "PUT", data, requestSpecificConfig);

            return(this.DoAsyncRequest(r, data, requestSpecificConfig: requestSpecificConfig));
        }
        protected virtual Task <ElasticsearchResponse <Stream> > DoAsyncRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
        {
            var tcs = new TaskCompletionSource <ElasticsearchResponse <Stream> >();

            if (this.ConnectionSettings.MaximumAsyncConnections <= 0 ||
                this._resourceLock == null)
            {
                return(this.CreateIterateTask(request, data, requestSpecificConfig, tcs));
            }

            var timeout = GetRequestTimeout(requestSpecificConfig);
            var path    = request.RequestUri.ToString();
            var method  = request.Method;

            if (!this._resourceLock.WaitOne(timeout))
            {
                var m = "Could not start the operation before the timeout of " + timeout +
                        "ms completed while waiting for the semaphore";
                var cs = ElasticsearchResponse <Stream> .CreateError(this.ConnectionSettings, new TimeoutException(m), method, path, data);

                tcs.SetResult(cs);
                return(tcs.Task);
            }
            try
            {
                return(this.CreateIterateTask(request, data, requestSpecificConfig, tcs));
            }
            finally
            {
                this._resourceLock.Release();
            }
        }
		public virtual Task<ElasticsearchResponse<Stream>> Delete(Uri uri, IRequestConfiguration requestSpecificConfig = null)
		{
			_uriObserver.Observe(uri);
			return Task.FromResult(_responseGenerator.Create());
		}
 protected override Task <ElasticsearchResponse <Stream> > DoAsyncRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
 {
     return(Task.Factory.StartNew(() =>
     {
         var cs = this.ReturnConnectionStatus(request, data, requestSpecificConfig);
         return cs;
     }));
 }
        private ElasticsearchResponse <Stream> BodyRequest(Uri uri, byte[] data, string method, IRequestConfiguration requestSpecificConfig)
        {
            var r = this.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);

            return(this.DoSynchronousRequest(r, data, requestSpecificConfig));
        }
示例#21
0
        private Task <ElasticsearchResponse <Stream> > _doRequestAsync(string method, Uri uri, byte[] postData, IRequestConfiguration requestSpecificConfig)
        {
            switch (method.ToLowerInvariant())
            {
            case "head": return(this._connection.Head(uri, requestSpecificConfig));

            case "get": return(this._connection.Get(uri, requestSpecificConfig));

            case "post": return(this._connection.Post(uri, postData, requestSpecificConfig));

            case "put": return(this._connection.Put(uri, postData, requestSpecificConfig));

            case "delete":
                return(postData == null || postData.Length == 0
                                                ? this._connection.Delete(uri, requestSpecificConfig)
                                                : this._connection.Delete(uri, postData, requestSpecificConfig));
            }
            throw new Exception("Unknown HTTP method " + method);
        }
 public virtual ElasticsearchResponse <Stream> DeleteSync(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
 {
     return(this.BodyRequest(uri, data, "DELETE", requestSpecificConfig));
 }
        private ElasticsearchResponse <Stream> HeaderOnlyRequest(Uri uri, string method, IRequestConfiguration requestSpecificConfig)
        {
            var r = this.CreateHttpWebRequest(uri, method, null, requestSpecificConfig);

            return(this.DoSynchronousRequest(r, requestSpecificConfig: requestSpecificConfig));
        }
 public virtual ElasticsearchResponse <Stream> DeleteSync(Uri uri, IRequestConfiguration requestSpecificConfig = null)
 {
     return(this.HeaderOnlyRequest(uri, "DELETE", requestSpecificConfig));
 }
        private IEnumerable <Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource <ElasticsearchResponse <Stream> > tcs, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            var timeout = GetRequestTimeout(requestSpecificConfig);

            if (data != null)
            {
                var getRequestStream = Task.Factory.FromAsync <Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
                ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                yield return(getRequestStream);

                var requestStream = getRequestStream.Result;
                try
                {
                    var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, data, 0, data.Length, null);
                    yield return(writeToRequestStream);
                }
                finally
                {
                    requestStream.Close();
                }
            }

            // Get the response
            var getResponse = Task.Factory.FromAsync <WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);

            ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
            yield return(getResponse);

            var path   = request.RequestUri.ToString();
            var method = request.Method;

            //http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
            //Either the stream or the response object needs to be closed but not both (although it won't)
            //throw any errors if both are closed atleast one of them has to be Closed.
            //Since we expose the stream we let closing the stream determining when to close the connection
            var response       = (HttpWebResponse)getResponse.Result;
            var responseStream = response.GetResponseStream();
            var cs             = ElasticsearchResponse <Stream> .Create(this.ConnectionSettings, (int)response.StatusCode, method, path, data);

            cs.Response = responseStream;
            tcs.TrySetResult(cs);
        }
 private Task <ElasticsearchResponse <Stream> > CreateIterateTask(HttpWebRequest request, byte[] data, IRequestConfiguration requestSpecificConfig, TaskCompletionSource <ElasticsearchResponse <Stream> > tcs)
 {
     this.Iterate(request, data, this._AsyncSteps(request, tcs, data, requestSpecificConfig), tcs);
     return(tcs.Task);
 }
		public virtual Task<ElasticsearchResponse<Stream>> Post(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
		{
			var r = this.CreateHttpWebRequest(uri, "POST", data, requestSpecificConfig);
			return this.DoAsyncRequest(r, data, requestSpecificConfig: requestSpecificConfig);
		}
示例#28
0
        /// <summary>
        /// Makes an async call to the specified url. Uses the timeout from the IRequestSpecifiConfig is supplied, or the global timeout from settings.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="uri">The URI.</param>
        /// <param name="data">The data.</param>
        /// <param name="requestSpecificConfig">The request specific configuration.</param>
        /// <returns>Task&lt;ElasticsearchResponse&lt;Stream&gt;&gt;.</returns>
        public async Task <ElasticsearchResponse <Stream> > DoRequest(HttpMethod method, Uri uri, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
        {
            ThrowIfDisposed();

            try
            {
                var request = new HttpRequestMessage(method, uri);
                if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(requestSpecificConfig.ContentType));
                }
                else if (!string.IsNullOrWhiteSpace(DefaultContentType))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(DefaultContentType));
                }


                if (!string.IsNullOrEmpty(uri.UserInfo))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(uri.UserInfo)));
                }

                if (method != HttpMethod.Get && method != HttpMethod.Head && data != null && data.Length > 0)
                {
                    request.Content = new ByteArrayContent(data);
                    if (!string.IsNullOrWhiteSpace(DefaultContentType) && request.Content != null && request.Content.Headers != null)
                    {
                        request.Content.Headers.ContentType = new MediaTypeHeaderValue(DefaultContentType);
                    }
                }

                var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

                if (method == HttpMethod.Head || response.Content == null || !response.Content.Headers.ContentLength.HasValue || response.Content.Headers.ContentLength.Value <= 0)
                {
                    return(ElasticsearchResponse <Stream> .Create(_settings, (int)response.StatusCode, method.ToString().ToLowerInvariant(), uri.ToString(), data));
                }

                var responseStream = await response.Content.ReadAsStreamAsync();

                return(ElasticsearchResponse <Stream> .Create(_settings, (int)response.StatusCode, method.ToString().ToLowerInvariant(), uri.ToString(), data, responseStream));
            }
            catch (Exception ex)
            {
                return(ElasticsearchResponse <Stream> .CreateError(_settings, ex, method.ToString().ToLowerInvariant(), uri.ToString(), data));
            }
        }
		public virtual Task<ElasticsearchResponse<Stream>> Get(Uri uri, IRequestConfiguration requestSpecificConfig = null)
		{
			return DoAsyncRequest(uri);
		}
示例#30
0
 ElasticsearchResponse <Stream> IConnection.HeadSync(Uri uri, IRequestConfiguration requestSpecificConfig)
 {
     return(DoRequestSync(HttpMethod.Head, uri, null, requestSpecificConfig));
 }
 protected override ElasticsearchResponse <Stream> DoSynchronousRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
 {
     return(this.ReturnConnectionStatus(request, data, requestSpecificConfig));
 }
示例#32
0
 Task <ElasticsearchResponse <Stream> > IConnection.Put(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig)
 {
     return(DoRequest(HttpMethod.Put, uri, data, requestSpecificConfig));
 }
        protected virtual HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            var myReq = this.CreateWebRequest(uri, method, data, requestSpecificConfig);

            this.SetBasicAuthorizationIfNeeded(myReq);
            this.SetProxyIfNeeded(myReq);
            return(myReq);
        }
示例#34
0
 Task <ElasticsearchResponse <Stream> > IConnection.Delete(Uri uri, IRequestConfiguration requestSpecificConfig)
 {
     return(DoRequest(HttpMethod.Delete, uri, null, requestSpecificConfig));
 }
 protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig) {
     var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
     request.Headers["Authorization"] = this.authorizationHeader;
     return request;
 }
示例#36
0
 ElasticsearchResponse <Stream> IConnection.DeleteSync(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig)
 {
     return(DoRequestSync(HttpMethod.Delete, uri, data, requestSpecificConfig));
 }
		private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ElasticsearchResponse<Stream>> tcs, byte[] data, IRequestConfiguration requestSpecificConfig)
		{
			var timeout = GetRequestTimeout(requestSpecificConfig);

			if (data != null)
			{
				var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
				ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
				yield return getRequestStream;

				var requestStream = getRequestStream.Result;
				try
				{
					var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, data, 0, data.Length, null);
					yield return writeToRequestStream;
				}
				finally
				{
					requestStream.Close();
				}
			}

			// Get the response
			var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
			ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
			yield return getResponse;

			var path = request.RequestUri.ToString();
			var method = request.Method;

			//http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
			//Either the stream or the response object needs to be closed but not both (although it won't)
			//throw any errors if both are closed atleast one of them has to be Closed.
			//Since we expose the stream we let closing the stream determining when to close the connection
			var response = (HttpWebResponse)getResponse.Result;
			var responseStream = response.GetResponseStream();
			var cs = ElasticsearchResponse<Stream>.Create(this.ConnectionSettings, (int)response.StatusCode, method, path, data);
			cs.Response = responseStream;
			tcs.TrySetResult(cs);
		}
示例#38
0
        /// <summary>
        /// Wraps the DoRequest to run synchronously
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="uri">The URI.</param>
        /// <param name="data">The data.</param>
        /// <param name="requestSpecificConfig">The request specific configuration.</param>
        /// <returns>ElasticsearchResponse&lt;Stream&gt;.</returns>
        public ElasticsearchResponse <Stream> DoRequestSync(HttpMethod method, Uri uri, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
        {
            ThrowIfDisposed();

            var requestTask = DoRequest(method, uri, data, requestSpecificConfig);

            try
            {
                requestTask.Wait();
                return(requestTask.Result);
            }
            catch (AggregateException ex)
            {
                return(ElasticsearchResponse <Stream> .CreateError(_settings, ex.Flatten(), method.ToString().ToLowerInvariant(), uri.ToString(), data));
            }
            catch (Exception ex)
            {
                return(ElasticsearchResponse <Stream> .CreateError(_settings, ex, method.ToString().ToLowerInvariant(), uri.ToString(), data));
            }
        }
		public virtual ElasticsearchResponse<Stream> DeleteSync(Uri uri, IRequestConfiguration requestSpecificConfig = null)
		{
			return this.HeaderOnlyRequest(uri, "DELETE", requestSpecificConfig);
		}
 public virtual Task <ElasticsearchResponse <Stream> > Get(Uri uri, IRequestConfiguration requestSpecificConfig = null)
 {
     return(DoAsyncRequest(uri));
 }
		private ElasticsearchResponse<Stream> HeaderOnlyRequest(Uri uri, string method, IRequestConfiguration requestSpecificConfig)
		{
			var r = this.CreateHttpWebRequest(uri, method, null, requestSpecificConfig);
			return this.DoSynchronousRequest(r, requestSpecificConfig: requestSpecificConfig);
		}
 public virtual Task <ElasticsearchResponse <Stream> > Post(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
 {
     _uriObserver.Observe(uri);
     return(Task.FromResult(_responseGenerator.Create()));
 }
		public virtual Task<ElasticsearchResponse<Stream>> Head(Uri uri, IRequestConfiguration requestSpecificConfig = null)
		{
			var r = this.CreateHttpWebRequest(uri, "HEAD", null, requestSpecificConfig);
			return this.DoAsyncRequest(r, requestSpecificConfig: requestSpecificConfig);
		}
 public virtual ElasticsearchResponse <Stream> PutSync(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
 {
     _uriObserver.Observe(uri);
     return(_responseGenerator.Create());
 }
 protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig) {
     var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
     request.ServicePoint.SetTcpKeepAlive(true, 30 * 1000, 2000);
     return request;
 }
        protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            //TODO append global querystring
            //var url = this._CreateUriString(path);

            var myReq = (HttpWebRequest)WebRequest.Create(uri);

            myReq.Accept      = "application/json";
            myReq.ContentType = "application/json";
            myReq.MaximumResponseHeadersLength = -1;
            myReq.Pipelined = false;
            //myReq.AllowWriteStreamBuffering = false;
            if (this.ConnectionSettings.EnableCompressedResponses)
            {
                myReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                myReq.Headers.Add("Accept-Encoding", "gzip,deflate");
            }
            if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
            {
                myReq.Accept      = requestSpecificConfig.ContentType;
                myReq.ContentType = requestSpecificConfig.ContentType;
            }
            var timeout = GetRequestTimeout(requestSpecificConfig);

            myReq.Timeout          = timeout;
            myReq.ReadWriteTimeout = timeout;
            myReq.Method           = method;

            //WebRequest won't send Content-Length: 0 for empty bodies
            //which goes against RFC's and might break i.e IIS when used as a proxy.
            //see: https://github.com/elasticsearch/elasticsearch-net/issues/562
            var m = method.ToLowerInvariant();

            if (m != "head" && m != "get" && (data == null || data.Length == 0))
            {
                myReq.ContentLength = 0;
            }

            return(myReq);
        }
		public virtual ElasticsearchResponse<Stream> PutSync(Uri uri, byte[] data, IRequestConfiguration requestSpecificConfig = null)
		{
			_uriObserver.Observe(uri);
			return _responseGenerator.Create();
		}
 /// <inheritdoc cref="IProductRegistration.CreateSniffRequestData"/>
 public RequestData CreateSniffRequestData(Node node, IRequestConfiguration requestConfiguration, ITransportConfigurationValues settings, IMemoryStreamFactory memoryStreamFactory) =>
 throw new NotImplementedException();
示例#49
0
        protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);

            request.ServicePoint.SetTcpKeepAlive(true, 30 * 1000, 2000);
            return(request);
        }
        protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
        {
            var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);

            request.Headers["Authorization"] = this.authorizationHeader;
            return(request);
        }
        private ElasticsearchResponse <Stream> ReturnConnectionStatus(HttpWebRequest request, byte[] data, IRequestConfiguration requestSpecificConfig = null)
        {
            var method = request.Method;
            var path   = request.RequestUri.ToString();

            var cs = ElasticsearchResponse <Stream> .Create(this.ConnectionSettings, _statusCode, method, path, data);

            cs.Response = new MemoryStream(_fixedResultBytes);
            if (this.ConnectionSettings.ConnectionStatusHandler != null)
            {
                this.ConnectionSettings.ConnectionStatusHandler(cs);
            }

            if (this.RecordRequests)
            {
                this.Requests.Add(Tuple.Create(method, request.RequestUri, data));
            }

            return(cs);
        }
		protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
		{
			//TODO append global querystring
			//var url = this._CreateUriString(path);

			var myReq = (HttpWebRequest)WebRequest.Create(uri);
			myReq.Accept = "application/json";
			myReq.ContentType = "application/json";
			myReq.MaximumResponseHeadersLength = -1;
			myReq.Pipelined = false;
			//myReq.AllowWriteStreamBuffering = false;
			if (this.ConnectionSettings.EnableCompressedResponses)
			{
				myReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
				myReq.Headers.Add("Accept-Encoding", "gzip,deflate");
			}
			if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
			{
				myReq.Accept = requestSpecificConfig.ContentType;
				myReq.ContentType = requestSpecificConfig.ContentType;
			}
			var timeout = GetRequestTimeout(requestSpecificConfig);
			myReq.Timeout = timeout;
			myReq.ReadWriteTimeout = timeout;
			myReq.Method = method;

			//WebRequest won't send Content-Length: 0 for empty bodies
			//which goes against RFC's and might break i.e IIS when used as a proxy.
			//see: https://github.com/elasticsearch/elasticsearch-net/issues/562
			var m = method.ToLowerInvariant();
			if (m != "head" && m != "get" && (data == null || data.Length == 0))
				myReq.ContentLength = 0;

			return myReq;
		}
		protected override ElasticsearchResponse<Stream> DoSynchronousRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
		{
			return this.ReturnConnectionStatus(request, data, requestSpecificConfig);
		}
		protected virtual ElasticsearchResponse<Stream> DoSynchronousRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
		{
			var path = request.RequestUri.ToString();
			var method = request.Method;

			if (data != null)
			{
				using (var r = request.GetRequestStream())
				{
					r.Write(data, 0, data.Length);
				}
			}
			try
			{
				//http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
				//Either the stream or the response object needs to be closed but not both although it won't
				//throw any errors if both are closed atleast one of them has to be Closed.
				//Since we expose the stream we let closing the stream determining when to close the connection
				var response = (HttpWebResponse)request.GetResponse();
				var responseStream = response.GetResponseStream();
				return WebToElasticsearchResponse(data, responseStream, response, method, path);
			}
			catch (WebException webException)
			{
				return HandleWebException(data, webException, method, path);
			}
		}
        public virtual Task <ElasticsearchResponse <Stream> > Delete(Uri uri, IRequestConfiguration requestSpecificConfig = null)
        {
            var r = this.CreateHttpWebRequest(uri, "DELETE", null, requestSpecificConfig);

            return(this.DoAsyncRequest(r, requestSpecificConfig: requestSpecificConfig));
        }
		protected virtual Task<ElasticsearchResponse<Stream>> DoAsyncRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
		{
			var tcs = new TaskCompletionSource<ElasticsearchResponse<Stream>>();
			if (this.ConnectionSettings.MaximumAsyncConnections <= 0
			  || this._resourceLock == null)
				return this.CreateIterateTask(request, data, requestSpecificConfig, tcs);

			var timeout = GetRequestTimeout(requestSpecificConfig);
			var path = request.RequestUri.ToString();
			var method = request.Method;
			if (!this._resourceLock.WaitOne(timeout))
			{
				var m = "Could not start the operation before the timeout of " + timeout +
				  "ms completed while waiting for the semaphore";
				var cs = ElasticsearchResponse<Stream>.CreateError(this.ConnectionSettings, new TimeoutException(m), method, path, data);
				tcs.SetResult(cs);
				return tcs.Task;
			}
			try
			{
				return this.CreateIterateTask(request, data, requestSpecificConfig, tcs);
			}
			finally
			{
				this._resourceLock.Release();
			}
		}
示例#57
0
 public ElasticsearchResponse <Stream> GetSync(Uri uri, IRequestConfiguration requestConfiguration = null)
 {
     throw new NotImplementedException();
 }
		private Task<ElasticsearchResponse<Stream>> CreateIterateTask(HttpWebRequest request, byte[] data, IRequestConfiguration requestSpecificConfig, TaskCompletionSource<ElasticsearchResponse<Stream>> tcs)
		{
			this.Iterate(request, data, this._AsyncSteps(request, tcs, data, requestSpecificConfig), tcs);
			return tcs.Task;
		}
示例#59
0
        private RequestData(HttpMethod method,
                            PostData data,
                            ITransportConfigurationValues global,
                            IRequestConfiguration local,
                            IMemoryStreamFactory memoryStreamFactory
                            )
        {
            ConnectionSettings  = global;
            MemoryStreamFactory = memoryStreamFactory;
            Method   = method;
            PostData = data;

            if (data != null)
            {
                data.DisableDirectStreaming = local?.DisableDirectStreaming ?? global.DisableDirectStreaming;
            }

            Pipelined       = local?.EnableHttpPipelining ?? global.HttpPipeliningEnabled;
            HttpCompression = global.EnableHttpCompression;
            RequestMimeType = local?.ContentType ?? MimeType;
            Accept          = local?.Accept ?? MimeType;

            if (global.Headers != null)
            {
                Headers = new NameValueCollection(global.Headers);
            }

            if (local?.Headers != null)
            {
                Headers ??= new NameValueCollection();
                foreach (var key in local.Headers.AllKeys)
                {
                    Headers[key] = local.Headers[key];
                }
            }

            if (!string.IsNullOrEmpty(local?.OpaqueId))
            {
                Headers ??= new NameValueCollection();
                Headers.Add(OpaqueIdHeader, local.OpaqueId);
            }

            RunAs = local?.RunAs;
            SkipDeserializationForStatusCodes = global?.SkipDeserializationForStatusCodes;
            ThrowExceptions = local?.ThrowExceptions ?? global.ThrowExceptions;

            RequestTimeout = local?.RequestTimeout ?? global.RequestTimeout;
            PingTimeout    =
                local?.PingTimeout
                ?? global.PingTimeout
                ?? (global.ConnectionPool.UsingSsl ? TransportConfiguration.DefaultPingTimeoutOnSSL : TransportConfiguration.DefaultPingTimeout);

            KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000);
            KeepAliveTime     = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000);
            DnsRefreshTimeout = global.DnsRefreshTimeout;

            ProxyAddress  = global.ProxyAddress;
            ProxyUsername = global.ProxyUsername;
            ProxyPassword = global.ProxyPassword;
            DisableAutomaticProxyDetection  = global.DisableAutomaticProxyDetection;
            BasicAuthorizationCredentials   = local?.BasicAuthenticationCredentials ?? global.BasicAuthenticationCredentials;
            ApiKeyAuthenticationCredentials = local?.ApiKeyAuthenticationCredentials ?? global.ApiKeyAuthenticationCredentials;
            AllowedStatusCodes      = local?.AllowedStatusCodes ?? EmptyReadOnly <int> .Collection;
            ClientCertificates      = local?.ClientCertificates ?? global.ClientCertificates;
            UserAgent               = global.UserAgent;
            TransferEncodingChunked = local?.TransferEncodingChunked ?? global.TransferEncodingChunked;
            TcpStats        = local?.EnableTcpStats ?? global.EnableTcpStats;
            ThreadPoolStats = local?.EnableThreadPoolStats ?? global.EnableThreadPoolStats;
        }
        protected virtual ElasticsearchResponse <Stream> DoSynchronousRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
        {
            var path   = request.RequestUri.ToString();
            var method = request.Method;

            if (data != null)
            {
                using (var r = request.GetRequestStream())
                {
                    r.Write(data, 0, data.Length);
                }
            }
            try
            {
                //http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
                //Either the stream or the response object needs to be closed but not both although it won't
                //throw any errors if both are closed atleast one of them has to be Closed.
                //Since we expose the stream we let closing the stream determining when to close the connection
                var response       = (HttpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();
                return(WebToElasticsearchResponse(data, responseStream, response, method, path));
            }
            catch (WebException webException)
            {
                return(HandleWebException(data, webException, method, path));
            }
        }