Example #1
0
		private ReplicationInfoStatus[] CheckDestinations(ReplicationDocument replicationDocument)
		{
			var results = new ReplicationInfoStatus[replicationDocument.Destinations.Count];

            Parallel.ForEach(replicationDocument.Destinations, (replicationDestination,state,i) =>
			{
				var url = replicationDestination.Url;

				if (!url.ToLower().Contains("/databases/"))
				{
					url += "/databases/" + replicationDestination.Database;
				}

				var result = new ReplicationInfoStatus
				{
					Url = url,
					Status = "Valid",
					Code = (int)HttpStatusCode.OK
				};

				results[i] = result;

			    var ravenConnectionStringOptions = new RavenConnectionStringOptions
			    {
			        ApiKey = replicationDestination.ApiKey, 
                    DefaultDatabase = replicationDestination.Database,
			    };
                if (string.IsNullOrEmpty(replicationDestination.Username) == false)
                {
                    ravenConnectionStringOptions.Credentials = new NetworkCredential(replicationDestination.Username,
                                                                                     replicationDestination.Password,
                                                                                     replicationDestination.Domain ?? string.Empty);
                }
			    var request = requestFactory.Create(url + "/replication/info", "POST", ravenConnectionStringOptions);
				try
				{	
					request.ExecuteRequest();
				}
				catch (WebException e)
				{
					FillStatus(result, e);
				}
			});

			return results;
		}
		private void FillStatus(ReplicationInfoStatus replicationInfoStatus, WebException e)
		{
			if (e.GetBaseException() is WebException)
				e = (WebException) e.GetBaseException();

			var response = e.Response as HttpWebResponse;
			if (response == null)
			{
				replicationInfoStatus.Status = e.Message;
				replicationInfoStatus.Code = -1 * (int)e.Status;
				return;
			}

			switch (response.StatusCode)
			{
				case HttpStatusCode.BadRequest:
					string error = GetErrorStringFromException(e, response);
					replicationInfoStatus.Status = error.Contains("Could not figure out what to do")
														   ? "Replication Bundle not activated."
														   : error;
					replicationInfoStatus.Code = (int)response.StatusCode;
					break;
				case HttpStatusCode.PreconditionFailed:
					replicationInfoStatus.Status = "Could not authenticate using OAuth's API Key";
					replicationInfoStatus.Code = (int)response.StatusCode;
					break;
				case HttpStatusCode.Forbidden:
				case HttpStatusCode.Unauthorized:
					replicationInfoStatus.Status = "Could not authenticate using Windows Auth";
					replicationInfoStatus.Code = (int)response.StatusCode;
					break;
				default:
					replicationInfoStatus.Status = response.StatusDescription;
					replicationInfoStatus.Code = (int)response.StatusCode;
					break;
			}
		}