Ejemplo n.º 1
0
        protected internal virtual void ResetIndex(string namedIndexingService, string namedIndex)
        {
            NamedIndexingServiceElement namedIndexingServiceElement = SearchSettings.GetNamedIndexingServiceElement(namedIndexingService);
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary.Add("{namedindex}", namedIndex);
            dictionary.Add("{accesskey}", namedIndexingServiceElement.AccessKey);
            string text = SearchSettings.Config.ResetUriTemplate;

            foreach (string current in dictionary.Keys)
            {
                text = text.Replace(current, HttpUtility.UrlEncode(dictionary[current]));
            }
            text = namedIndexingServiceElement.BaseUri + text;
            OnResetRequestSending(this, new EventArgs());
            try
            {
                MakeHttpRequest(text, namedIndexingServiceElement, SearchSettings.Config.ResetHttpMethod, null, null);
                OnResetRequestSent(this, new EventArgs());
            }
            catch (Exception ex)
            {
                SearchSettings.Log.Error(string.Format("Could not reset index '{0}' for service uri '{1}'. Message: {2}{3}", new object[]
                {
                    namedIndex,
                    text,
                    ex.Message,
                    ex.StackTrace
                }));
            }
        }
Ejemplo n.º 2
0
        //private static void MakeHttpRequest(string url, NamedIndexingServiceElement namedIndexingServiceElement, string method, System.IO.Stream postData, System.Action<WebResponse> responseHandler)
        //{
        //    HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
        //    httpWebRequest.UseDefaultCredentials = false;
        //    if (httpWebRequest != null)
        //    {
        //        HttpWebRequest httpWebRequest2 = httpWebRequest;
        //        X509Certificate2 clientCertificate = namedIndexingServiceElement.GetClientCertificate();
        //        if (clientCertificate != null)
        //        {
        //            httpWebRequest2.ClientCertificates.Add(clientCertificate);
        //        }
        //        if (namedIndexingServiceElement.CertificateAllowUntrusted)
        //        {
        //            ServicePointManager.ServerCertificateValidationCallback = ((object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true);
        //        }
        //    }
        //    httpWebRequest.Method = method;
        //    if (method == "POST" || method == "DELETE" || method == "PUT")
        //    {
        //        httpWebRequest.ContentType = "application/xml";
        //        if (postData != null)
        //        {
        //            httpWebRequest.ContentLength = postData.Length;
        //            System.IO.Stream requestStream = httpWebRequest.GetRequestStream();
        //            RequestHandler.CopyStream(postData, requestStream);
        //            requestStream.Close();
        //        }
        //        else
        //        {
        //            httpWebRequest.ContentLength = 0L;
        //        }
        //        WebResponse response = httpWebRequest.GetResponse();
        //        if (responseHandler != null)
        //        {
        //            responseHandler(response);
        //        }
        //        response.Close();
        //        return;
        //    }
        //    if (method == "GET")
        //    {
        //        httpWebRequest.ContentType = "application/xml";
        //        WebResponse response2 = httpWebRequest.GetResponse();
        //        if (responseHandler != null)
        //        {
        //            responseHandler(response2);
        //        }
        //        response2.Close();
        //    }
        //}

        private static void MakeHttpRequest(string url, NamedIndexingServiceElement namedIndexingServiceElement, string method, Stream postData, Action <WebResponse> responseHandler)
        {
            string res = String.Empty;

            WebRequest request = WebRequest.Create(url) as HttpWebRequest;

            if (request is HttpWebRequest)
            {
                HttpWebRequest   hwr  = (HttpWebRequest)request;
                X509Certificate2 cert = namedIndexingServiceElement.GetClientCertificate();
                if (cert != null)
                {
                    hwr.ClientCertificates.Add(cert);
                }
                if (namedIndexingServiceElement.CertificateAllowUntrusted)
                {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
                    {
                        return(true);
                    };
                }
            }

            request.Method = method;
            if (method == "POST" || method == "DELETE" || method == "PUT")
            {
                request.ContentType = "application/xml";
                if (postData != null)
                {
                    request.ContentLength = postData.Length;

                    Stream dataStream = request.GetRequestStream();
                    CopyStream(postData, dataStream);
                    dataStream.Close();
                }
                else
                {
                    request.ContentLength = 0;
                }

                // Get the response.
                WebResponse response = request.GetResponse();
                if (responseHandler != null)
                {
                    responseHandler(response);
                }
                response.Close();
            }
            else if (method == "GET")
            {
                request.ContentType = "application/xml";

                WebResponse response = request.GetResponse();
                if (responseHandler != null)
                {
                    responseHandler(response);
                }
                response.Close();
            }
        }
Ejemplo n.º 3
0
        protected internal virtual Collection <string> GetNamedIndexes(string namedIndexingService)
        {
            NamedIndexingServiceElement namedIndexingServiceElement = SearchSettings.GetNamedIndexingServiceElement(namedIndexingService);
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary.Add("{accesskey}", namedIndexingServiceElement.AccessKey);
            string text = SearchSettings.Config.NamedIndexesUriTemplate;

            foreach (string current in dictionary.Keys)
            {
                text = text.Replace(current, HttpUtility.UrlEncode(dictionary[current]));
            }
            text = namedIndexingServiceElement.BaseUri + text;
            XmlReader           xmlReader    = null;
            SyndicationFeed     feed         = null;
            Collection <string> namedIndexes = new Collection <string>();

            try
            {
                MakeHttpRequest(text, namedIndexingServiceElement, "GET", null, delegate(WebResponse response)
                {
                    xmlReader = XmlReader.Create(response.GetResponseStream());
                    feed      = SyndicationFeed.Load(xmlReader);
                    foreach (SyndicationItem current2 in feed.Items)
                    {
                        namedIndexes.Add(current2.Title.Text);
                    }
                });
            }
            catch (Exception ex)
            {
                SearchSettings.Log.Error(string.Format("Could not get named indexes for uri '{0}'. Message: {1}{2}", text, ex.Message, ex.StackTrace));
            }
            return(namedIndexes);
        }
Ejemplo n.º 4
0
        protected internal virtual bool SendRequest(SyndicationFeed feed, string namedIndexingService, Collection <Identity> ids)
        {
            NamedIndexingServiceElement namedIndexingServiceElement = SearchSettings.IndexingServices[namedIndexingService];
            bool result;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings
                {
                    Encoding = Encoding.UTF8,
                    CheckCharacters = false,
                    CloseOutput = false
                }))
                {
                    feed.GetAtom10Formatter().WriteTo(xmlWriter);
                    xmlWriter.Flush();
                }
                memoryStream.Position = 0L;
                string text = SearchSettings.Config.UpdateUriTemplate.Replace("{accesskey}", namedIndexingServiceElement.AccessKey);
                text = namedIndexingServiceElement.BaseUri + text;
                OnUpdateRequestSending(this, new EventArgs());
                try
                {
                    MakeHttpRequest(text, namedIndexingServiceElement, "POST", memoryStream, null);
                    OnUpdateRequestSent(this, new EventArgs());
                    result = true;
                    return(result);
                }
                catch (Exception ex)
                {
                    SearchSettings.Log.Error(string.Format("Update batch could not be sent to service uri '{0}'. Message: '{1}{2}'", text, ex.Message, ex.StackTrace));
                }
                result = false;
            }
            return(result);
        }