public TypeDefinitionCache(IBindingSession session)
        {
            int repCount = session.GetValue(SessionParameter.CacheSizeRepositories, CacheSizeRepositories);

            if (repCount < 1)
            {
                repCount = CacheSizeRepositories;
            }

            int typeCount = session.GetValue(SessionParameter.CacheSizeTypes, CacheSizeTypes);

            if (typeCount < 1)
            {
                typeCount = CacheSizeTypes;
            }

            cache = new Cache("Type Definition Cache");
            cache.Initialize(new string[] {
                typeof(DictionaryCacheLevel).FullName + " " + DictionaryCacheLevel.Capacity + "=" + repCount.ToString(CultureInfo.InvariantCulture), // repository
                typeof(LruCacheLevel).FullName + " " + LruCacheLevel.MaxEntries + "=" + typeCount.ToString(CultureInfo.InvariantCulture)             // type
            });
        }
        public RepositoryInfoCache(IBindingSession session)
        {
            int repCount = session.GetValue(SessionParameter.CacheSizeRepositories, CacheSizeRepositories);

            if (repCount < 1)
            {
                repCount = CacheSizeRepositories;
            }

            cache = new Cache("Repository Info Cache");
            cache.Initialize(new string[] {
                typeof(DictionaryCacheLevel).FullName + " " + DictionaryCacheLevel.Capacity + "=" + repCount.ToString(CultureInfo.InvariantCulture)
            });
        }
Beispiel #3
0
        private IResponse Invoke(UrlBuilder url, HttpMethod method, System.Net.Http.HttpContent content, IBindingSession session,
                                 long?offset, long?length, IDictionary <string, string> headers)
        {
            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString());
            }

            IWindowsAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IWindowsAuthenticationProvider;

            HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient;

            if (httpClient == null)
            {
                lock (invokerLock)
                {
                    httpClient = session.GetValue(InvokerHttpClient) as HttpClient;
                    if (httpClient == null)
                    {
                        HttpBaseProtocolFilter httpClientFilter = new HttpBaseProtocolFilter();

                        // redirects
                        httpClientFilter.AllowAutoRedirect = false;

                        // compression
                        string compressionFlag = session.GetValue(SessionParameter.Compression) as string;
                        if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true"))
                        {
                            httpClientFilter.AutomaticDecompression = true;
                        }

                        // authentication
                        httpClientFilter.AllowUI = false;

                        // authentication provider
                        if (authProvider != null)
                        {
                            authProvider.PrepareHttpClientFilter(httpClientFilter);
                        }

                        // create HttpClient
                        httpClient = new HttpClient(httpClientFilter);

                        session.PutValue(InvokerHttpClient, httpClient);
                    }
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(method, new Uri(url.ToString()));

            // set additional headers

            string userAgent = session.GetValue(SessionParameter.UserAgent) as string;

            request.Headers.UserAgent.Add(HttpProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent));

            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    request.Headers.TryAppendWithoutValidation(header.Key, header.Value);
                }
            }

            // range
            if (offset != null && length != null)
            {
                long longOffset = offset.Value < 0 ? 0 : offset.Value;
                if (length.Value > 0)
                {
                    request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + longOffset + "-" + (longOffset + length.Value - 1)));
                }
                else
                {
                    request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + longOffset + "-"));
                }
            }
            else if (offset != null && offset.Value > 0)
            {
                request.Headers.Add(new KeyValuePair <string, string>("Range", "bytes=" + offset.Value + "-"));
            }

            // content
            if (content != null)
            {
                request.Content = new ConvertedHttpContent(content);

                if (request.Content.Headers.ContentLength == null)
                {
                    request.Headers.TransferEncoding.TryParseAdd("chunked");
                }
            }

            // authentication provider
            if (authProvider != null)
            {
                authProvider.PrepareHttpRequestMessage(request);
            }

            // timeouts
            int timeout = session.GetValue(SessionParameter.ConnectTimeout, -2);

            WindowsResponse response;

            try
            {
                Task <HttpResponseMessage> task = Send(httpClient, request, timeout);
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }
                else
                {
                    HttpResponseMessage httpResponseMessage = task.Result;

                    if (authProvider != null)
                    {
                        authProvider.HandleResponse(httpResponseMessage);
                    }
                    response = new WindowsResponse(httpResponseMessage);
                }
            }
            catch (Exception e)
            {
                throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e);
            }

            return(response);
        }
Beispiel #4
0
        private IResponse Invoke(UrlBuilder url, HttpMethod method, HttpContent content, IBindingSession session,
                                 long?offset, long?length, IDictionary <string, string> headers)
        {
            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString());
            }

            IPortableAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IPortableAuthenticationProvider;

            HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient;

            if (httpClient == null)
            {
                lock (invokerLock)
                {
                    httpClient = session.GetValue(InvokerHttpClient) as HttpClient;
                    if (httpClient == null)
                    {
                        // create a HTTP client handler
                        HttpClientHandler httpClientHandler = null;

                        if (authProvider != null)
                        {
                            httpClientHandler = authProvider.CreateHttpClientHandler();
                        }
                        if (httpClientHandler == null)
                        {
                            httpClientHandler = new HttpClientHandler();
                        }

                        // redirects
                        if (httpClientHandler.SupportsRedirectConfiguration)
                        {
                            httpClientHandler.AllowAutoRedirect = false;
                        }

                        // compression
                        if (httpClientHandler.SupportsAutomaticDecompression)
                        {
                            string compressionFlag = session.GetValue(SessionParameter.Compression) as string;
                            if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true"))
                            {
                                httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                            }
                        }

                        // authentication
                        httpClientHandler.PreAuthenticate       = true;
                        httpClientHandler.UseDefaultCredentials = false;

                        // authentication provider
                        if (authProvider != null)
                        {
                            authProvider.PrepareHttpClientHandler(httpClientHandler);
                        }

                        // create HttpClient
                        httpClient = new HttpClient(httpClientHandler, true);

                        // timeouts
                        int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                        if (connectTimeout >= -1)
                        {
                            httpClient.Timeout = TimeSpan.FromMilliseconds(connectTimeout);
                        }

                        session.PutValue(InvokerHttpClient, httpClient);
                    }
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(method, url.ToString());

            // set additional headers

            string userAgent = session.GetValue(SessionParameter.UserAgent) as string;

            request.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent));

            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
            }

            // range
            if (offset != null && length != null)
            {
                long longOffset = offset.Value < 0 ? 0 : offset.Value;
                if (length.Value > 0)
                {
                    request.Headers.Range = new RangeHeaderValue(longOffset, longOffset + length.Value - 1);
                }
                else
                {
                    request.Headers.Range = new RangeHeaderValue(longOffset, null);
                }
            }
            else if (offset != null && offset.Value > 0)
            {
                request.Headers.Range = new RangeHeaderValue(offset, null);
            }

            // content
            if (content != null)
            {
                request.Headers.TransferEncodingChunked = true;
                request.Content = content;
            }

            // authentication provider
            if (authProvider != null)
            {
                authProvider.PrepareHttpRequestMessage(request);
            }

            Response response;

            try
            {
                Task <HttpResponseMessage> task = Send(httpClient, request);
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }
                else
                {
                    HttpResponseMessage httpResponseMessage = task.Result;

                    if (authProvider != null)
                    {
                        authProvider.HandleResponse(httpResponseMessage);
                    }
                    response = new Response(httpResponseMessage);
                }
            }
            catch (Exception e)
            {
                throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e);
            }


            return(response);
        }
Beispiel #5
0
        private IResponse Invoke(UrlBuilder url, HttpMethod method, HttpContent content, IBindingSession session,
                                 long?offset, long?length, IDictionary <string, string> headers)
        {
            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("HTTP: " + method.ToString() + " " + url.ToString());
            }

            IPortableAuthenticationProvider authProvider = session.GetAuthenticationProvider() as IPortableAuthenticationProvider;

            HttpClient httpClient = session.GetValue(InvokerHttpClient) as HttpClient;

            if (httpClient == null)
            {
                lock (invokerLock)
                {
                    httpClient = session.GetValue(InvokerHttpClient) as HttpClient;
                    if (httpClient == null)
                    {
                        // create a HTTP client handler
                        HttpClientHandler httpClientHandler = null;
                        if (authProvider != null)
                        {
                            httpClientHandler = authProvider.CreateHttpClientHandler();
                        }
                        if (httpClientHandler == null)
                        {
                            httpClientHandler = new HttpClientHandler();
                        }


                        // redirects
                        if (httpClientHandler.SupportsRedirectConfiguration)
                        {
                            httpClientHandler.AllowAutoRedirect = false;
                        }

                        // compression
                        if (httpClientHandler.SupportsAutomaticDecompression)
                        {
                            string compressionFlag = session.GetValue(SessionParameter.Compression) as string;
                            if (compressionFlag != null && compressionFlag.ToLowerInvariant().Equals("true"))
                            {
                                httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                            }
                        }

                        // authentication
                        httpClientHandler.PreAuthenticate       = true;
                        httpClientHandler.UseDefaultCredentials = false;


                        // authentication provider
                        if (authProvider != null)
                        {
                            authProvider.PrepareHttpClientHandler(httpClientHandler);
                        }
                        string                  sslProtocols            = SessionParameterDefaults.SslProtocol;
                        string                  clientCertificateOption = SessionParameterDefaults.ClientCertificateOption;
                        SslProtocols            prot                       = SslProtocols.None;
                        ClientCertificateOption clientOption               = ClientCertificateOption.Manual;
                        object                  sslProtocolsobj            = session.GetValue(SessionParameter.SslProtocols, SessionParameterDefaults.SslProtocol);
                        object                  clientCertificateOptionObj = session.GetValue(SessionParameter.ClientCertificateOption, SessionParameterDefaults.ClientCertificateOption);
                        if (sslProtocolsobj != null)
                        {
                            sslProtocols = $"{sslProtocolsobj}";
                            if (!string.IsNullOrWhiteSpace(sslProtocols))
                            {
                                if (Enum.TryParse <SslProtocols>(sslProtocols, out SslProtocols result))
                                {
                                    prot = result;
                                }
                            }
                        }
                        if (clientCertificateOptionObj != null)
                        {
                            clientCertificateOption = $"{clientCertificateOptionObj}";
                            if (!string.IsNullOrWhiteSpace(clientCertificateOption))
                            {
                                if (Enum.TryParse <ClientCertificateOption>(clientCertificateOption, out ClientCertificateOption result))
                                {
                                    clientOption = result;
                                }
                            }
                        }
                        httpClientHandler.ClientCertificateOptions = clientOption;
                        httpClientHandler.SslProtocols             = prot;
                        httpClientHandler.ServerCertificateCustomValidationCallback =
                            (httpRequestMessage, cert, cetChain, policyErrors) =>
                        {
                            return(true);
                        };
                        // create HttpClient
                        httpClient = new HttpClient(httpClientHandler, true);

                        // timeouts
                        int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                        if (connectTimeout >= -1)
                        {
                            httpClient.Timeout = TimeSpan.FromMilliseconds(connectTimeout);
                        }
                        session.PutValue(InvokerHttpClient, httpClient);
                    }
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(method, url.ToString());

            try
            {
                // set additional headers
                string userAgent = session.GetValue(SessionParameter.UserAgent) as string;
                request.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(userAgent ?? ClientVersion.UserAgent));

                if (headers != null)
                {
                    foreach (KeyValuePair <string, string> header in headers)
                    {
                        request.Headers.TryAddWithoutValidation(header.Key, header.Value);
                    }
                }

                // range
                if (offset != null && length != null)
                {
                    long longOffset = offset.Value < 0 ? 0 : offset.Value;
                    if (length.Value > 0)
                    {
                        request.Headers.Range = new RangeHeaderValue(longOffset, longOffset + length.Value - 1);
                    }
                    else
                    {
                        request.Headers.Range = new RangeHeaderValue(longOffset, null);
                    }
                }
                else if (offset != null && offset.Value > 0)
                {
                    request.Headers.Range = new RangeHeaderValue(offset, null);
                }

                // content
                if (content != null)
                {
                    request.Headers.TransferEncodingChunked = true;
                    request.Content = content;
                }

                // authentication provider
                if (authProvider != null)
                {
                    authProvider.PrepareHttpRequestMessage(request);
                }

                Response response;
                try
                {
                    Task <HttpResponseMessage> task = Send(httpClient, request);
                    if (task.IsFaulted)
                    {
                        throw task.Exception;
                    }
                    else
                    {
                        HttpResponseMessage httpResponseMessage = task.Result;

                        if (authProvider != null)
                        {
                            authProvider.HandleResponse(httpResponseMessage);
                        }
                        response = new Response(httpResponseMessage);
                    }
                }
                catch (Exception e)
                {
                    throw new CmisConnectionException("Cannot access " + url + ": " + e.Message, e);
                }


                return(response);
            }
            finally
            {
                request.Dispose();
            }
        }