示例#1
0
        private void EnsureGraphClientConstructed()
        {
            if (_graphClient != null)
            {
                return;
            }
            Guard.Null(ConnectionConfig);
            Guard.NullOrEmpty(ConnectionConfig.GraphUri, "Connection string cannot be null or empty");

            var graphUri = new Uri(ConnectionConfig.GraphUri);

            var httpClientHandler = new HttpClientHandler();

            // We want the staging config for proxyUri  to be overridden when running in TeamCity
            // Or you can do something like this C:\>set Data:Neo4j:ProxyUri=http://teamcity.internal.dev:3128
            var disableProxyHint = "\"<";

            if (ConnectionConfig.HasProxyUri() && !ConnectionConfig.ProxyUri.StartsWith(disableProxyHint))
            {
                httpClientHandler.Proxy = new WebProxy(ConnectionConfig.ProxyUri, true);
                httpClientHandler.UseProxy = true;
            };

            var httpClient = new HttpClient(httpClientHandler);
            
            var httpClientWrapper = new HttpClientWrapper(ConnectionConfig.Username, ConnectionConfig.Password, httpClient);

            _graphClient = new GraphClient(graphUri, httpClientWrapper);
            
            _graphClient.JsonContractResolver = new CamelCasePropertyNamesContractResolver();
        }
        /// <summary>
        /// Get HTTP client wrapper for access to authenticated Neo4j graph database using the Neo4jClient library.
        /// </summary>
        /// <returns></returns>
        public static Neo4jClient.HttpClientWrapper GetNeo4jAuthenticatedClient()
        {
            // Get authentication header from configuration
            var authentication = Configuration.GetAuthorizationHeader();

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", Convert.ToBase64String(Encoding.ASCII.GetBytes(authentication)));
            Neo4jClient.HttpClientWrapper clientWrapper = new Neo4jClient.HttpClientWrapper(httpClient);
            return(clientWrapper);
        }
        internal static async Task <NeoServerConfiguration> GetConfigurationAsync(Uri rootUri, string username, string password, ExecutionConfiguration executionConfiguration)
        {
            if (executionConfiguration == null)
            {
                var httpClient = new HttpClientWrapper(username, password);

                executionConfiguration = new ExecutionConfiguration
                {
                    HttpClient = httpClient,
                    UserAgent  =
                        string.Format("Neo4jClient/{0}", typeof(NeoServerConfiguration).Assembly.GetName().Version),
                    UseJsonStreaming = true,
                    JsonConverters   = GraphClient.DefaultJsonConverters,
                    Username         = username,
                    Password         = password
                };
            }

            if (!rootUri.AbsoluteUri.EndsWith("/"))
            {
                rootUri = new Uri(rootUri.AbsoluteUri + "/");
            }

            rootUri = new Uri(rootUri, "");

            var result = await Request.With(executionConfiguration)
                         .Get(rootUri)
                         .WithExpectedStatusCodes(HttpStatusCode.OK)
                         .ParseAs <RootApiResponse>()
                         .ExecuteAsync().ConfigureAwait(false);

            if (result == null)
            {
                throw new ApplicationException("Couldn't obtain server Root API configuration.");
            }

            var rootUriWithoutUserInfo = rootUri;

            if (!string.IsNullOrEmpty(rootUriWithoutUserInfo.UserInfo))
            {
                rootUriWithoutUserInfo = new UriBuilder(rootUri.AbsoluteUri)
                {
                    UserName = "",
                    Password = ""
                }.Uri;
            }

            var baseUriLengthToTrim = rootUriWithoutUserInfo.AbsoluteUri.Length - 1;

            result.Batch             = result.Batch.Substring(baseUriLengthToTrim);
            result.Node              = result.Node.Substring(baseUriLengthToTrim);
            result.NodeIndex         = result.NodeIndex.Substring(baseUriLengthToTrim);
            result.Relationship      = "/relationship"; //Doesn't come in on the Service Root
            result.RelationshipIndex = result.RelationshipIndex.Substring(baseUriLengthToTrim);
            result.ExtensionsInfo    = result.ExtensionsInfo.Substring(baseUriLengthToTrim);

            if (!string.IsNullOrEmpty(result.Transaction))
            {
                result.Transaction = result.Transaction.Substring(baseUriLengthToTrim);
            }

            if (result.Extensions != null && result.Extensions.GremlinPlugin != null)
            {
                result.Extensions.GremlinPlugin.ExecuteScript =
                    result.Extensions.GremlinPlugin.ExecuteScript.Substring(baseUriLengthToTrim);
            }

            if (result.Cypher != null)
            {
                result.Cypher = result.Cypher.Substring(baseUriLengthToTrim);
            }

            return(new NeoServerConfiguration(result)
            {
                RootUri = rootUri,
                Username = username,
                Password = password
            });
        }
        internal static NeoServerConfiguration GetConfiguration(Uri rootUri, string username, string password, ExecutionConfiguration executionConfiguration)
        {
            if (executionConfiguration == null)
            {
                var httpClient = new HttpClientWrapper(username, password);

                executionConfiguration = new ExecutionConfiguration
                {
                    HttpClient = httpClient,
                    UserAgent =
                        string.Format("Neo4jClient/{0}", typeof (NeoServerConfiguration).Assembly.GetName().Version),
                    UseJsonStreaming = true,
                    JsonConverters = GraphClient.DefaultJsonConverters,
                    Username = username,
                    Password = password
                };
            }

            if (!rootUri.AbsoluteUri.EndsWith("/"))
                rootUri = new Uri(rootUri.AbsoluteUri + "/");

            rootUri = new Uri(rootUri, "");

            var result = Request.With(executionConfiguration)
                .Get(rootUri)
                .WithExpectedStatusCodes(HttpStatusCode.OK)
                .ParseAs<RootApiResponse>()
                .Execute();

            if (result == null)
            {
                throw new ApplicationException("Couldn't obtain server Root API configuration.");
            }

            var rootUriWithoutUserInfo = rootUri;
            if (!string.IsNullOrEmpty(rootUriWithoutUserInfo.UserInfo))
            {
                rootUriWithoutUserInfo = new UriBuilder(rootUri.AbsoluteUri)
                {
                    UserName = "",
                    Password = ""
                }.Uri;
            }

            var baseUriLengthToTrim = rootUriWithoutUserInfo.AbsoluteUri.Length - 1;

            result.Batch = result.Batch.Substring(baseUriLengthToTrim);
            result.Node = result.Node.Substring(baseUriLengthToTrim);
            result.NodeIndex = result.NodeIndex.Substring(baseUriLengthToTrim);
            result.Relationship = "/relationship"; //Doesn't come in on the Service Root
            result.RelationshipIndex = result.RelationshipIndex.Substring(baseUriLengthToTrim);
            result.ExtensionsInfo = result.ExtensionsInfo.Substring(baseUriLengthToTrim);

            if (!string.IsNullOrEmpty(result.Transaction))
            {
                result.Transaction = result.Transaction.Substring(baseUriLengthToTrim);
            }

            if (result.Extensions != null && result.Extensions.GremlinPlugin != null)
            {
                result.Extensions.GremlinPlugin.ExecuteScript =
                    result.Extensions.GremlinPlugin.ExecuteScript.Substring(baseUriLengthToTrim);
            }

            if (result.Cypher != null)
            {
                result.Cypher = result.Cypher.Substring(baseUriLengthToTrim);
            }

            return new NeoServerConfiguration(result)
            {
                RootUri = rootUri,
                Username = username,
                Password = password
            };
        }
 /// <summary>
 /// Retrieve the GraphClient object for the Neo4j graph database configured in application settings.
 /// </summary>
 /// <returns>Returns Neo4jClient .NET wrapper for data store management of a graph database instance managed over HTTP.</returns>
 public static GraphClient GetNeo4jGraphClient()
 {
     Neo4jClient.HttpClientWrapper clientWrapper = Processor.GetNeo4jAuthenticatedClient();
     Neo4jClient.GraphClient       graphClient   = new Neo4jClient.GraphClient(new Uri(Configuration.GetDatabaseUri()), clientWrapper);
     return(graphClient);
 }
        /// <summary>
        /// Get HTTP client wrapper for access to authenticated Neo4j graph database using the Neo4jClient library.
        /// </summary>
        /// <returns></returns>
        public static Neo4jClient.HttpClientWrapper GetNeo4jAuthenticatedClient()
        {
            // Get authentication header from configuration
            var authentication = Configuration.GetAuthorizationHeader();

            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", Convert.ToBase64String(Encoding.ASCII.GetBytes(authentication)));
            Neo4jClient.HttpClientWrapper clientWrapper = new Neo4jClient.HttpClientWrapper(httpClient);
            return clientWrapper;
        }