示例#1
0
        /// <summary>
        /// Dummies the get call for proxy authentication.
        /// Hack to force a first GET on just aceql servlet if we are with a proxy, just to avoid system failure
        /// Because of a bug in C#, if a POST is done first to detect a 407 (proxy auth asked), HttpClient throws an Exception
        /// </summary>
        private async Task DummyGetCallForProxyAuthentication()
        {
            if ((this.httpManager.Proxy != null && proxyCredentials != null) || useCredentialCache)
            {
                String getResult = await httpManager.CallWithGetAsync(server).ConfigureAwait(false);

                ConsoleEmul.WriteLine(server + " - getResult: " + getResult);
            }
        }
示例#2
0
        /// <summary>
        /// Opens this instance.
        /// </summary>
        /// <exception cref="ArgumentNullException"> if a required parameter extracted from connection string is missing.
        /// </exception>
        /// <exception cref="AceQLException"> if any other Exception occurs.
        /// </exception>
        internal async Task OpenAsync()
        {
            string sessionId;

            try
            {
                ConnectionStringDecoder connectionStringDecoder = new ConnectionStringDecoder();
                connectionStringDecoder.Decode(connectionString);
                this.server             = connectionStringDecoder.Server;
                this.database           = connectionStringDecoder.Database;
                this.username           = connectionStringDecoder.Username;
                this.password           = connectionStringDecoder.Password;
                sessionId               = connectionStringDecoder.SessionId;
                this.proxyUri           = connectionStringDecoder.ProxyUri;
                this.proxyCredentials   = connectionStringDecoder.ProxyCredentials;
                this.useCredentialCache = connectionStringDecoder.UseCredentialCache;
                this.timeout            = connectionStringDecoder.Timeout;
                this.enableDefaultSystemAuthentication = connectionStringDecoder.EnableDefaultSystemAuthentication;

                if (connectionStringDecoder.EnableTrace)
                {
                    simpleTracer.SetTraceOn(true);
                }

                await simpleTracer.TraceAsync("connectionString: " + connectionString).ConfigureAwait(false);

                await simpleTracer.TraceAsync("DecodeConnectionString() done!").ConfigureAwait(false);

                if (credential != null)
                {
                    username = credential.Username;

                    if (credential.Password != null)
                    {
                        password = credential.Password;
                    }

                    if (credential.SessionId != null)
                    {
                        sessionId = credential.SessionId;
                    }
                }

                if (server == null)
                {
                    throw new ArgumentNullException("Server keyword not found in connection string.");
                }

                if (password == null && sessionId == null)
                {
                    throw new ArgumentNullException("Password keyword or SessionId keyword not found in connection string or AceQLCredential not set");
                }

                if (database == null)
                {
                    throw new ArgumentNullException("Database keyword not found in connection string.");
                }

                this.username = username ?? throw new ArgumentNullException("Username keyword not found in connection string or AceQLCredential not set.");

                // Create the HttpManager instance
                this.httpManager = new HttpManager(proxyUri, proxyCredentials, timeout, enableDefaultSystemAuthentication);
                this.httpManager.SetSimpleTracer(simpleTracer);

                Debug("httpManager.Proxy: " + httpManager.Proxy);

                UserLoginStore userLoginStore = new UserLoginStore(server, username,
                                                                   database);

                if (sessionId != null)
                {
                    userLoginStore.SetSessionId(sessionId);
                }

                if (userLoginStore.IsAlreadyLogged())
                {
                    await simpleTracer.TraceAsync("Get a new connection with get_connection").ConfigureAwait(false);

                    sessionId = userLoginStore.GetSessionId();

                    String theUrl = server + "/session/" + sessionId + "/get_connection";
                    String result = await httpManager.CallWithGetAsync(theUrl).ConfigureAwait(false);

                    await simpleTracer.TraceAsync("result: " + result).ConfigureAwait(false);

                    ResultAnalyzer resultAnalyzer = new ResultAnalyzer(result,
                                                                       HttpStatusCode);

                    if (!resultAnalyzer.IsStatusOk())
                    {
                        throw new AceQLException(resultAnalyzer.GetErrorMessage(),
                                                 resultAnalyzer.GetErrorId(),
                                                 resultAnalyzer.GetStackTrace(),
                                                 HttpStatusCode);
                    }

                    String connectionId = resultAnalyzer.GetValue("connection_id");
                    await simpleTracer.TraceAsync("Ok. New Connection created: " + connectionId).ConfigureAwait(false);

                    this.url = server + "/session/" + sessionId + "/connection/"
                               + connectionId + "/";
                }
                else
                {
                    await DummyGetCallForProxyAuthentication().ConfigureAwait(false);

                    String theUrl = server + "/database/" + database + "/username/" + username + "/login";
                    ConsoleEmul.WriteLine("theUrl: " + theUrl);

                    Dictionary <string, string> parametersMap = new Dictionary <string, string>
                    {
                        { "password", new String(password) },
                        { "client_version", VersionValues.VERSION }
                    };

                    await simpleTracer.TraceAsync("Before CallWithPostAsyncReturnString: " + theUrl);

                    String result = await httpManager.CallWithPostAsyncReturnString(new Uri(theUrl), parametersMap).ConfigureAwait(false);

                    ConsoleEmul.WriteLine("result: " + result);
                    await simpleTracer.TraceAsync("result: " + result).ConfigureAwait(false);

                    ResultAnalyzer resultAnalyzer = new ResultAnalyzer(result, HttpStatusCode);
                    if (!resultAnalyzer.IsStatusOk())
                    {
                        throw new AceQLException(resultAnalyzer.GetErrorMessage(),
                                                 resultAnalyzer.GetErrorId(),
                                                 resultAnalyzer.GetStackTrace(),
                                                 HttpStatusCode);
                    }

                    String theSessionId    = resultAnalyzer.GetValue("session_id");
                    String theConnectionId = resultAnalyzer.GetValue("connection_id");

                    this.url = server + "/session/" + theSessionId + "/connection/" + theConnectionId + "/";
                    userLoginStore.SetSessionId(theSessionId);
                    await simpleTracer.TraceAsync("OpenAsync url: " + this.url).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                await simpleTracer.TraceAsync(exception.ToString()).ConfigureAwait(false);

                if (exception.GetType() == typeof(AceQLException))
                {
                    throw;
                }
                else
                {
                    throw new AceQLException(exception.Message, 0, exception, HttpStatusCode);
                }
            }
        }