Esempio n. 1
0
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
        {
            if (disposed)
                throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);

            if (RequestTimeout != null)
                createHttpJsonRequestParams.Timeout = RequestTimeout.Value;

            var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
            {
                ShouldCacheRequest =
                    createHttpJsonRequestParams.AvoidCachingRequest == false &&
                    createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
            };

            if (request.ShouldCacheRequest && !DisableHttpCaching)
            {
                var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.AddHeader);
                request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
                request.SkipServerCheck = cachedRequestDetails.SkipServerCheck;
            }

            ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs { Client = request.httpClient, Credentials = createHttpJsonRequestParams.Credentials });
            return request;
        }
Esempio n. 2
0
		 public void CanDeleteTombstones()
		 {
			 var store1 = (DocumentStore)CreateStore();
			var x= store1.DatabaseCommands.Put("ayende", null, new RavenJObject(), new RavenJObject());
			 store1.DatabaseCommands.Delete("ayende", null);
			 servers[0].Database.TransactionalStorage.Batch(accessor =>
			 {
				 Assert.NotEmpty(accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Etag.Empty, null, 10));
			 });

			 Etag last = Etag.Empty.Setup(UuidType.Documents, 1).IncrementBy(3);
			 var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null,
			                                                                   servers[0].Database.ServerUrl +
																			   "admin/replication/purge-tombstones?docEtag=" + last,
			                                                                   "POST",
																			   new OperationCredentials(null, CredentialCache.DefaultCredentials),
			                                                                   store1.Conventions);
			 store1.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams).ExecuteRequest();


			 servers[0].Database.TransactionalStorage.Batch(accessor =>
			 {
				 Assert.Empty(accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Etag.Empty, null, 10).ToArray());
			 });
		 }
Esempio n. 3
0
		internal HttpJsonRequest(
			CreateHttpJsonRequestParams requestParams,
			HttpJsonRequestFactory factory)
		{
			Url = requestParams.Url;
			this.factory = factory;
			owner = requestParams.Owner;
			conventions = requestParams.Convention;
			Method = requestParams.Method;
			webRequest = (HttpWebRequest)WebRequest.Create(requestParams.Url);
			webRequest.UseDefaultCredentials = true;
			webRequest.Credentials = requestParams.Credentials;
			webRequest.Method = requestParams.Method;
			if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
			{
				if (requestParams.Method == "POST" || requestParams.Method == "PUT" ||
					requestParams.Method == "PATCH" || requestParams.Method == "EVAL")
					webRequest.Headers["Content-Encoding"] = "gzip";

				webRequest.Headers["Accept-Encoding"] = "gzip";
			}
			webRequest.ContentType = "application/json; charset=utf-8";
			webRequest.Headers.Add("Raven-Client-Version", ClientVersion);
			WriteMetadata(requestParams.Metadata);
			requestParams.UpdateHeaders(webRequest);
		}
Esempio n. 4
0
        public async Task Dictionary_with_empty_string_as_key_should_fail_bulk_insert_request()
        {
            var jsonRequestFactory = new HttpJsonRequestFactory(25);
            using (var store = NewRemoteDocumentStore())
            {
                var url = String.Format("{0}/databases/{1}", store.Url, store.DefaultDatabase);
                using (ConnectionOptions.Expect100Continue(url))
                {
                    var operationUrl = "/bulkInsert?&operationId=" + Guid.NewGuid();
                    var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null, url + operationUrl, "POST", new OperationCredentials(String.Empty, CredentialCache.DefaultNetworkCredentials), new DocumentConvention());
                    var request = jsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams);
                    
                    var response = await request.ExecuteRawRequestAsync((requestStream, tcs) => 
                    {
                        using (var bufferedStream = new MemoryStream())
                        {
                            long bytesWritten;
                            WriteToBuffer(bufferedStream, out bytesWritten);

                            var requestBinaryWriter = new BinaryWriter(requestStream);
                            requestBinaryWriter.Write((int)bufferedStream.Position);

                            bufferedStream.WriteTo(requestStream);
                            requestStream.Flush();
                            tcs.TrySetResult(null);
                        }
                    });

                    Assert.Equal(422, (int)response.StatusCode);
                }
            }
        }
Esempio n. 5
0
        private async Task CheckIfServerIsUpNow(OperationMetadata operationMetadata, OperationMetadata primaryOperation, CancellationToken token)
        {
            for (int i = 0; i < 5; i++)
            {
                token.ThrowCancellationIfNotDefault();
                try
                {
                    var r = await TryOperationAsync <object>(async metadata =>
                    {
                        var requestParams = new CreateHttpJsonRequestParams(null, GetServerCheckUrl(metadata.Url), "GET", metadata.Credentials, Conventions);
                        using (var request = requestFactory.CreateHttpJsonRequest(requestParams))
                        {
                            await request.ReadResponseJsonAsync().WithCancellation(token).ConfigureAwait(false);
                        }
                        return(null);
                    }, operationMetadata, primaryOperation, true, token).ConfigureAwait(false);

                    if (r.Success)
                    {
                        ResetFailureCount(operationMetadata.Url);
                        return;
                    }
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                await Task.Delay(DelayTimeInMiliSec, token).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
            }

            var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
            {
                ShouldCacheRequest =
                    createHttpJsonRequestParams.AvoidCachingRequest == false &&
                    createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
            };

            if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
            {
                var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.AddHeader);
                request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
                request.SkipServerCheck      = cachedRequestDetails.SkipServerCheck;
            }

            if (RequestTimeout != null)
            {
                request.Timeout = RequestTimeout.Value;
            }

            ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {
                Client = request.httpClient, Credentials = createHttpJsonRequestParams.Credentials
            });
            return(request);
        }
Esempio n. 7
0
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            Url          = requestParams.Url;
            this.factory = factory;
            owner        = requestParams.Owner;
            conventions  = requestParams.Convention;
            Method       = requestParams.Method;
            webRequest   = (HttpWebRequest)WebRequest.Create(requestParams.Url);
            webRequest.UseDefaultCredentials = true;
            webRequest.Credentials           = requestParams.Credentials;
            webRequest.Method = requestParams.Method;
            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (requestParams.Method == "POST" || requestParams.Method == "PUT" ||
                    requestParams.Method == "PATCH" || requestParams.Method == "EVAL")
                {
                    webRequest.Headers["Content-Encoding"] = "gzip";
                }

                webRequest.Headers["Accept-Encoding"] = "gzip";
            }
            webRequest.ContentType = "application/json; charset=utf-8";
            webRequest.Headers.Add("Raven-Client-Version", ClientVersion);
            WriteMetadata(requestParams.Metadata);
            requestParams.UpdateHeaders(webRequest);
        }
Esempio n. 8
0
        public void CanDeleteTombstonesButNotAfterTheSpecifiedEtag(string requestedStorage)
        {
            var store1 = (DocumentStore)CreateStore(databaseName: Constants.SystemDatabase, requestedStorageType: requestedStorage);
            store1.DatabaseCommands.Put("ayende", null, new RavenJObject(), new RavenJObject());
            store1.DatabaseCommands.Delete("ayende", null);
            store1.DatabaseCommands.Put("rahien", null, new RavenJObject(), new RavenJObject());
            store1.DatabaseCommands.Delete("rahien", null);
            servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
            {
                var count = accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Etag.Empty, null, 10).Count();
                Assert.Equal(2, count);
            });

            Etag last = Etag.Empty.Setup(UuidType.Documents, 1).IncrementBy(3);

            var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null,
                                                                              servers[0].SystemDatabase.ServerUrl +
                                                                              "admin/replication/purge-tombstones?docEtag=" + last,
                                                                              "POST",
                                                                              new OperationCredentials(null, CredentialCache.DefaultCredentials),
                                                                              store1.Conventions);
            store1.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams).ExecuteRequest();


            servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
            {
                Assert.Equal(1, accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Etag.Empty, null, 10).Count());
            });
        }
Esempio n. 9
0
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            _credentials = requestParams.Credentials;
            Url          = requestParams.Url;
            Method       = requestParams.Method;

            this.factory = factory;
            owner        = requestParams.Owner;
            conventions  = requestParams.Convention;

            if (factory.httpMessageHandler != null)
            {
                handler         = factory.httpMessageHandler;
                recreateHandler = () => factory.httpMessageHandler;
            }
            else
            {
                var webRequestHandler = new WebRequestHandler
                {
                    UseDefaultCredentials = _credentials.HasCredentials() == false,
                    Credentials           = requestParams.Credentials.Credentials,
                };
                recreateHandler = () => new WebRequestHandler
                {
                    Credentials = webRequestHandler.Credentials
                };
                disableAuthentication = () =>
                {
                    webRequestHandler.Credentials           = null;
                    webRequestHandler.UseDefaultCredentials = false;
                };
                handler = webRequestHandler;
            }

            httpClient = new HttpClient(handler);

            if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
            {
                if (Method == "POST" || Method == "PUT" || Method == "PATCH" || Method == "EVAL")
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                }

                httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            }

#if DEBUG
            if (Debugger.IsAttached)
            {
                Timeout = TimeSpan.FromMinutes(5);
            }
#endif

            headers.Add("Raven-Client-Version", ClientVersion);
            WriteMetadata(requestParams.Metadata);
            requestParams.UpdateHeaders(headers);
        }
Esempio n. 10
0
		 public void CanDeleteTombstonesButNotAfterTheSpecifiedEtag()
		 {
			 var store1 = (DocumentStore)CreateStore();
			 store1.DatabaseCommands.Put("ayende", null, new RavenJObject(), new RavenJObject());
			 store1.DatabaseCommands.Delete("ayende", null);
			 store1.DatabaseCommands.Put("rahien", null, new RavenJObject(), new RavenJObject());
			 store1.DatabaseCommands.Delete("rahien", null);
			 servers[0].Database.TransactionalStorage.Batch(accessor =>
			 {
				 Assert.Equal(2, accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Guid.Empty, 10).Count());
			 });

			 var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null,
																			   servers[0].Database.ServerUrl +
																			   "admin/replication/purge-tombstones?docEtag=00000001-0000-0100-0000-000000000003",
																			   "POST",
																			   CredentialCache.DefaultCredentials,
																			   store1.Conventions);
			 store1.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams).ExecuteRequest();


			 servers[0].Database.TransactionalStorage.Batch(accessor =>
			 {
				 Assert.Equal(1, accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, Guid.Empty, 10).Count());
			 });
		 }
        /// <summary>
        /// Should execute the operation using the specified operation URL
        /// </summary>
        public virtual bool ShouldExecuteUsing(OperationMetadata operationMetadata, OperationMetadata primaryOperation, int currentRequest, string method, bool primary)
        {
            if (primary == false)
            {
                AssertValidOperation(method);
            }

            var failureCounter = GetHolder(operationMetadata.Url);

            if (failureCounter.Value == 0)
            {
                return(true);
            }

            if (failureCounter.ForceCheck)
            {
                return(true);
            }

            var currentTask = failureCounter.CheckDestination;

            if (currentTask.Status != TaskStatus.Running && DelayTimeInMiliSec > 0)
            {
                var checkDestination = new Task(async delegate
                {
                    for (int i = 0; i < 3; i++)
                    {
                        try
                        {
                            var r = await TryOperationAsync <object>(async metadata =>
                            {
                                var requestParams = new CreateHttpJsonRequestParams(null, GetServerCheckUrl(metadata.Url), "GET", metadata.Credentials, conventions);
                                await requestFactory.CreateHttpJsonRequest(requestParams).ReadResponseJsonAsync().ConfigureAwait(false);
                                return(null);
                            }, operationMetadata, primaryOperation, avoidThrowing: true).ConfigureAwait(false);
                            if (r.Success)
                            {
                                ResetFailureCount(operationMetadata.Url);
                                return;
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            return;     // disposed, nothing to do here
                        }
                        await Task.Delay(DelayTimeInMiliSec).ConfigureAwait(false);
                    }
                });

                var old = Interlocked.CompareExchange(ref failureCounter.CheckDestination, checkDestination, currentTask);
                if (old == currentTask)
                {
                    checkDestination.Start(TaskScheduler.Default);
                }
            }

            return(false);
        }
Esempio n. 12
0
        private HttpJsonRequest CreateRequestParams(OperationMetadata operationMetadata, string requestUrl, string method, bool disableRequestCompression = false, bool disableAuthentication = false, TimeSpan? timeout = null)
        {
            var metadata = new RavenJObject();
            var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(profilingInfo, operationMetadata.Url + requestUrl, method, metadata, operationMetadata.Credentials, convention, timeout)
                .AddOperationHeaders(operationHeaders);

            createHttpJsonRequestParams.DisableRequestCompression = disableRequestCompression;
            createHttpJsonRequestParams.DisableAuthentication = disableAuthentication;

            return factory.CreateHttpJsonRequest(createHttpJsonRequestParams);
        }
        public async Task<SynchronizationReport> PushChangesAsync(CancellationToken token)
        {
            token.Register(() => { });//request.Abort() TODO: check this

            token.ThrowIfCancellationRequested();

            if (sourceStream.CanRead == false)
                throw new Exception("Stream does not support reading");

            var baseUrl = synchronizationServerClient.BaseUrl;
            var credentials = synchronizationServerClient.Credentials;
            var conventions = synchronizationServerClient.Conventions;

            var requestParams = new CreateHttpJsonRequestParams(this, baseUrl + "/synchronization/MultipartProceed", "POST", credentials, conventions, timeout: TimeSpan.FromHours(12))
            {
                DisableRequestCompression = true
            };

            using (var request = synchronizationServerClient.RequestFactory.CreateHttpJsonRequest(requestParams))
            {
                request.AddHeaders(sourceMetadata);
                request.AddHeader("Content-Type", "multipart/form-data; boundary=" + syncingBoundary);
                request.AddHeader("If-None-Match", "\"" + sourceMetadata.Value<string>(Constants.MetadataEtagField) + "\"");

                request.AddHeader(SyncingMultipartConstants.FileName, fileName);
                request.AddHeader(SyncingMultipartConstants.SourceFileSystemInfo, fileSystemInfo.AsJson());

                try
                {
                    await request.WriteAsync(PrepareMultipartContent(token)).ConfigureAwait(false);

                    var response = await request.ReadResponseJsonAsync().ConfigureAwait(false);
                    return JsonExtensions.CreateDefaultJsonSerializer().Deserialize<SynchronizationReport>(new RavenJTokenReader(response));
                }
                catch (Exception exception)
                {
                    if (token.IsCancellationRequested)
                    {
                        throw new OperationCanceledException(token);
                    }

                    var webException = exception as ErrorResponseException;

                    if (webException != null)
                    {
                        webException.SimplifyException();
                    }

                    throw;
                }
            }
        }
Esempio n. 14
0
		/// <summary>
		/// Creates the HTTP json request.
		/// </summary>
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			if (disposed)
				throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
			var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
			{
				ShouldCacheRequest =
					createHttpJsonRequestParams.AvoidCachingRequest == false && 
					createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
			};

			if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
			{
				var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.webRequest.Headers.Set);
				request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
				request.SkipServerCheck = cachedRequestDetails.SkipServerCheck;
			}
			ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {Request = request.webRequest});
			return request;
		}
Esempio n. 15
0
 internal HttpJsonRequest(
     CreateHttpJsonRequestParams requestParams,
     HttpJsonRequestFactory factory)
 {
     Url                    = requestParams.Url;
     this.factory           = factory;
     owner                  = requestParams.Owner;
     conventions            = requestParams.Convention;
     Method                 = requestParams.Method;
     webRequest             = (HttpWebRequest)WebRequest.Create(requestParams.Url);
     webRequest.Credentials = requestParams.Credentials;
     webRequest.Method      = requestParams.Method;
     if (factory.DisableRequestCompression == false &&
         (requestParams.Method == "POST" || requestParams.Method == "PUT" || requestParams.Method == "PATCH"))
     {
         webRequest.Headers["Content-Encoding"] = "gzip";
     }
     webRequest.ContentType = "application/json; charset=utf-8";
     WriteMetadata(requestParams.Metadata);
     requestParams.UpdateHeaders(webRequest);
 }
Esempio n. 16
0
        public void PassingInvalidDocEtagDoesNotIgnoreAttachmentEtagWhenPurgingTombstones()
        {
            var store1 = CreateStore(databaseName: Constants.SystemDatabase);

            store1.DatabaseCommands.PutAttachment("attachment/1", null, new MemoryStream(), new RavenJObject());
            store1.DatabaseCommands.DeleteAttachment("attachment/1", null);

            servers[0].SystemDatabase.TransactionalStorage.Batch(accessor => Assert.NotEmpty(accessor.Lists.Read(Constants.RavenReplicationAttachmentsTombstones, Etag.Empty, null, 10)));

            Etag lastAttachmentEtag = Etag.Empty.Setup(UuidType.Attachments, 1).IncrementBy(3);

            var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null,
                                                                              servers[0].SystemDatabase.ServerUrl +
                                                                              string.Format("admin/replication/purge-tombstones?docEtag={0}&attachmentEtag={1}", null, lastAttachmentEtag),
                                                                              "POST",
                                                                              new OperationCredentials(null, CredentialCache.DefaultCredentials),
                                                                              store1.Conventions);
            store1.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams).ExecuteRequest();


            servers[0].SystemDatabase.TransactionalStorage.Batch(accessor => Assert.Empty(accessor.Lists.Read(Constants.RavenReplicationAttachmentsTombstones, Etag.Empty, null, 10)));
        }
Esempio n. 17
0
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
            }
            var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
            {
                ShouldCacheRequest = createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
            };

            if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
            {
                var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.webRequest.Headers.Set);
                request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
                request.SkipServerCheck      = cachedRequestDetails.SkipServerCheck;
            }
            ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {
                Request = request.webRequest
            });
            return(request);
        }
Esempio n. 18
0
		private static Etag GetReplicatedEtag(IDocumentStore source, IDocumentStore dest)
		{
			var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(
				null,
				source.Url.LastReplicatedEtagFor(dest.Url),
				"GET",
				new OperationCredentials(((DocumentStore) source).ApiKey, ((DocumentStore) source).Credentials),
				source.Conventions);
			var httpJsonRequest = source.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams);

			var json = httpJsonRequest.ReadResponseJson();

			return Etag.Parse(json.Value<string>("LastDocumentEtag"));
		}
Esempio n. 19
0
	    private async Task<ReplicatedEtagInfo> GetReplicatedEtagsFor(string destinationUrl, string database)
		{
			var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(
				null,
				destinationUrl.LastReplicatedEtagFor(documentStore.Url.ForDatabase(database ?? documentStore.DefaultDatabase)),
				"GET",
				new OperationCredentials(documentStore.ApiKey, documentStore.Credentials), 
				documentStore.Conventions);
			var httpJsonRequest = documentStore.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams);
			var json = await httpJsonRequest.ReadResponseJsonAsync();

			return new ReplicatedEtagInfo
			{
				DestinationUrl = destinationUrl,
				DocumentEtag = Etag.Parse(json.Value<string>("LastDocumentEtag")),
				AttachmentEtag = Etag.Parse(json.Value<string>("LastAttachmentEtag"))
			};
		}
Esempio n. 20
0
        internal HttpJsonRequest(
            CreateHttpJsonRequestParams requestParams,
            HttpJsonRequestFactory factory)
        {
            try
            {
                _credentials        = requestParams.DisableAuthentication == false ? requestParams.Credentials : null;
                disabledAuthRetries = requestParams.DisableAuthentication;

                Url    = requestParams.Url;
                Method = requestParams.Method;

                if (requestParams.Timeout.HasValue)
                {
                    Timeout = requestParams.Timeout.Value;
                }
                else
                {
                    Timeout = TimeSpan.FromSeconds(100);                     // default HttpClient timeout
#if DEBUG
                    if (Debugger.IsAttached)
                    {
                        Timeout = TimeSpan.FromMinutes(5);
                    }
#endif
                }

                this.factory = factory;
                owner        = requestParams.Owner;
                conventions  = requestParams.Convention;

                if (factory.httpMessageHandler != null)
                {
                    recreateHandler = () => factory.httpMessageHandler;
                }
                else
                {
                    recreateHandler = () => new WebRequestHandler
                    {
                        UseDefaultCredentials = _credentials != null && _credentials.HasCredentials() == false,
                        Credentials           = _credentials != null ? _credentials.Credentials : null,
                    };
                }

                httpClient = factory.httpClientCache.GetClient(Timeout, _credentials, recreateHandler);

                if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
                {
                    if (Method == "POST" || Method == "PUT" || Method == "PATCH" || Method == "EVAL")
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Encoding", "gzip");
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                    }

                    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                }

                headers.Add("Raven-Client-Version", ClientVersion);
                WriteMetadata(requestParams.Metadata);
                requestParams.UpdateHeaders(headers);
            }
            catch (Exception)
            {
                throw;
            }
        }
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			throw new NotImplementedException();
		}
Esempio n. 22
0
        private async Task<ReplicatedEtagInfo> GetReplicatedEtagsFor(string destinationUrl, string sourceUrl, string sourceDbId)
        {
            var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(
                null,
                destinationUrl.LastReplicatedEtagFor(sourceUrl, sourceDbId),
                "GET",
                new OperationCredentials(documentStore.ApiKey, documentStore.Credentials), 
                documentStore.Conventions);

            using (var request = documentStore.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams))
            {
                var json = await request.ReadResponseJsonAsync();
                var etag = Etag.Parse(json.Value<string>("LastDocumentEtag"));

                if ( log.IsDebugEnabled )
                {
                    log.Debug("Received last replicated document Etag {0} from server {1}", etag, destinationUrl);
                }
                                
                return new ReplicatedEtagInfo
                {
                    DestinationUrl = destinationUrl,
                    DocumentEtag = etag 
                };
            }
        }
Esempio n. 23
0
        /// <summary>
        /// Should execute the operation using the specified operation URL
        /// </summary>
        private bool ShouldExecuteUsing(OperationMetadata operationMetadata, OperationMetadata primaryOperation, HttpMethod method, bool primary, Exception error, CancellationToken token)
        {
            if (primary == false)
            {
                AssertValidOperation(method, error);
            }

            var failureCounter = FailureCounters.GetHolder(operationMetadata.Url);

            if (failureCounter.Value == 0)
            {
                return(true);
            }

            if (failureCounter.ForceCheck)
            {
                return(true);
            }

            var currentTask = failureCounter.CheckDestination;

            if (currentTask.Status != TaskStatus.Running && DelayTimeInMiliSec > 0)
            {
                var checkDestination = new Task(async delegate
                {
                    for (int i = 0; i < 3; i++)
                    {
                        token.ThrowCancellationIfNotDefault();
                        try
                        {
                            var r = await TryOperationAsync <object>(async metadata =>
                            {
                                var requestParams = new CreateHttpJsonRequestParams(null, GetServerCheckUrl(metadata.Url), HttpMethods.Get, metadata.Credentials, Conventions);
                                using (var request = requestFactory.CreateHttpJsonRequest(requestParams))
                                {
                                    await request.ReadResponseJsonAsync().WithCancellation(token).ConfigureAwait(false);
                                }
                                return(null);
                            }, operationMetadata, primaryOperation, true, token).ConfigureAwait(false);
                            if (r.Success)
                            {
                                FailureCounters.ResetFailureCount(operationMetadata.Url);
                                return;
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            return; // disposed, nothing to do here
                        }
                        await Task.Delay(DelayTimeInMiliSec, token).ConfigureAwait(false);
                    }
                });

                var old = Interlocked.CompareExchange(ref failureCounter.CheckDestination, checkDestination, currentTask);
                if (old == currentTask)
                {
                    checkDestination.Start(TaskScheduler.Default);
                }
            }

            return(false);
        }
        public void CanDeleteTombStones()
        {
            using (var store = NewRemoteDocumentStore(databaseName: Constants.SystemDatabase))
            {
                string userId;
                using (var session = store.OpenSession())
                {
                    var user = new User { Name = "oren" };
                    session.Store(user);
                    userId = user.Id;
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    session.Delete(userId);
                    session.SaveChanges();
                }

                store.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }), new RavenJObject());
                
                store.DatabaseCommands.DeleteAttachment("attach/1", null);
                
                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                                                                     Assert.Equal(1,
                                                                                  accessor.Lists.Read(
                                                                                      Constants
                                                                                          .RavenPeriodicExportsDocsTombstones,
                                                                                      Etag.Empty, null, 10).Count()));

                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                    Assert.Equal(1, accessor.Lists.Read(Constants.RavenPeriodicExportsAttachmentsTombstones, Etag.Empty, null, 10).Count()));

                using (var session = store.OpenSession())
                {
                    var user = new User { Name = "oren" };
                    session.Store(user);
                    userId = user.Id;
                    session.SaveChanges();
                }
                store.DatabaseCommands.PutAttachment("attach/2", null, new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }), new RavenJObject());

                var documentEtagAfterFirstDelete = Etag.Empty;
                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor => documentEtagAfterFirstDelete = accessor.Staleness.GetMostRecentDocumentEtag());

                var attachmentEtagAfterFirstDelete = Etag.Empty;
                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor => attachmentEtagAfterFirstDelete = accessor.Staleness.GetMostRecentAttachmentEtag());

                using (var session = store.OpenSession())
                {
                    session.Delete(userId);
                    session.SaveChanges();
                }

                store.DatabaseCommands.DeleteAttachment("attach/2", null);


                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                    Assert.Equal(2, accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, Etag.Empty, null, 10).Count()));

                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                    Assert.Equal(2, accessor.Lists.Read(Constants.RavenPeriodicExportsAttachmentsTombstones, Etag.Empty, null, 10).Count()));

                var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(null,
                                                                    servers[0].SystemDatabase.ServerUrl +
                                                                    "admin/periodicExport/purge-tombstones?docEtag=" + documentEtagAfterFirstDelete + "&attachmentEtag=" + attachmentEtagAfterFirstDelete,
                                                                    "POST",
                                                                    new OperationCredentials(null, CredentialCache.DefaultCredentials),
                                                                    store.Conventions);

                store.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams).ReadResponseJson();

                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                    Assert.Equal(1, accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, Etag.Empty, null, 10).Count()));
                servers[0].SystemDatabase.TransactionalStorage.Batch(accessor =>
                    Assert.Equal(1, accessor.Lists.Read(Constants.RavenPeriodicExportsAttachmentsTombstones, Etag.Empty, null, 10).Count()));

            }
        }
Esempio n. 25
0
        private static Etag GetReplicatedEtag(DocumentStore source, DocumentStore dest)
        {
            var destinationStatistics = dest.DatabaseCommands.GetStatistics();

            var sourceUrl = source.Url + "/databases/" + source.DefaultDatabase;
            var destinationUrl = dest.Url + "/databases/" + dest.DefaultDatabase;

            var createHttpJsonRequestParams = new CreateHttpJsonRequestParams(
                null,
                sourceUrl.LastReplicatedEtagFor(destinationUrl, destinationStatistics.DatabaseId.ToString()),
                "GET",
                new OperationCredentials(source.ApiKey, source.Credentials),
                source.Conventions);
            var httpJsonRequest = source.JsonRequestFactory.CreateHttpJsonRequest(createHttpJsonRequestParams);

            var json = httpJsonRequest.ReadResponseJson();

            return Etag.Parse(json.Value<string>("LastDocumentEtag"));
        }
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			var request = new HttpJsonRequest(createHttpJsonRequestParams.Url, createHttpJsonRequestParams.Method, createHttpJsonRequestParams.Metadata, createHttpJsonRequestParams.Convention, this);
			ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {Client = request.httpClient});
			return request;
		}