public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            if (reader.TokenType != JsonToken.StartArray)
                throw new Exception("Expecting StartArray, received " + reader.TokenType);

            reader.Read();
            string username = (string)reader.Value;
            reader.Read();
            string passwordBase64 = (string)reader.Value;
            reader.Read();

            string password = Encoding.UTF8.GetString(Convert.FromBase64String(passwordBase64));
            Credentials res = new Credentials(username, password);
            return res;
        }
Пример #2
0
        private String UploadString(Credentials credentials, string url, bool ignoreUntrustedCertificate)
        {
            string res;

            if (logger.IsTraceEnabled)
                logger.Trace("Uploading: " + url);

            // set the thread-static field
            JenkinsService.ignoreUntrustedCertificate = ignoreUntrustedCertificate;

            WebClient webClient = GetWebClient(credentials);
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
              res = webClient.UploadString(url,"");

            if (logger.IsTraceEnabled)
                logger.Trace("Uploaded: " + res);

            return res;
        }
Пример #3
0
        private WebClient GetWebClient(Credentials credentials)
        {
            if (threadWebClient == null)
            {
                logger.Info("Creating web client in thread " + Thread.CurrentThread.ManagedThreadId
                    + " (" + Thread.CurrentThread.Name + ")");
                threadWebClient = new CookieAwareWebClient();
                threadWebClient.Encoding = Encoding.UTF8;
            }

            // reinitialize HTTP headers
            threadWebClient.Headers = new WebHeaderCollection();

            // credentials
            if (credentials != null)
            {
                string authentication = "Basic " + Convert.ToBase64String(
                    Encoding.ASCII.GetBytes(credentials.Username + ":" + credentials.Password));
                threadWebClient.Headers.Add("Authorization", authentication);
            }

            return threadWebClient;
        }
Пример #4
0
        private BuildDetails GetBuildDetails(Credentials credentials, string buildUrl, bool ignoreUntrustedCertificate)
        {
            if (buildUrl == null)
                return null;

            String url = NetUtils.ConcatUrls(buildUrl, "/api/xml");

            if (logger.IsDebugEnabled)
                logger.Debug("Getting build details from " + url);

            String xmlStr = DownloadString(credentials, url, true, ignoreUntrustedCertificate);

            if (logger.IsTraceEnabled)
                logger.Trace("XML: " + xmlStr);

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlStr);

            string number = xml.SelectSingleNode("/*/number").InnerText;
            string fullDisplayName = xml.SelectSingleNode("/*/fullDisplayName").InnerText;
            string timestamp = xml.SelectSingleNode("/*/timestamp").InnerText;
            string estimatedDuration = xml.SelectSingleNode("/*/estimatedDuration").InnerText;
            string duration = xml.SelectSingleNode("/*/duration").InnerText;
            XmlNode xmlResult = xml.SelectSingleNode("/*/result");
            string result = xmlResult == null ? string.Empty : xmlResult.InnerText;
            XmlNodeList userNodes = xml.SelectNodes("/*/culprit/fullName");

            TimeSpan ts = TimeSpan.FromSeconds(long.Parse(timestamp) / 1000);
            DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            date = date.Add(ts);
            TimeSpan estimatedts = TimeSpan.FromSeconds(long.Parse(estimatedDuration) / 1000);
            TimeSpan durationts = TimeSpan.FromSeconds(long.Parse(estimatedDuration) / 1000);

            ISet<string> users = new HashedSet<string>();
            foreach (XmlNode userNode in userNodes)
            {
                string userName = StringUtils.ExtractUserName(userNode.InnerText);
                users.Add(userName);
            }

            BuildDetails res = new BuildDetails();
            BuildCauses.FillInBuildCauses(res, xml);
            res.Number = int.Parse(number);
            res.DisplayName = fullDisplayName;
            res.Time = date;
            res.EstimatedDuration = estimatedts;
            res.Duration = durationts;
            res.Result = BuildStatus.StringToBuildStatus(result);
            res.Users = users;

            ClaimService.FillInBuildDetails(res, xml);

            if (logger.IsDebugEnabled)
                logger.Debug("Done getting build details");

            return res;
        }
Пример #5
0
        private String DownloadString(Credentials credentials, string url, bool cacheable,
            bool ignoreUntrustedCertificate)
        {
            string res;

            if (logger.IsTraceEnabled)
                logger.Trace("Downloading: " + url);

            if (cacheable)
            {
                lock (this)
                {
                    // mark the URL as visited
                    visitedURLs.Add(url);
                    // perform a lookup in the cache
                    if (cache.TryGetValue(url, out res))
                    {
                        if (logger.IsTraceEnabled)
                            logger.Trace("Cache hit: " + url);
                        return res;
                    }
                }

                if (logger.IsTraceEnabled)
                    logger.Trace("Cache miss: " + url);
            }

            // set the thread-static field
            JenkinsService.ignoreUntrustedCertificate = ignoreUntrustedCertificate;

            WebClient webClient = GetWebClient(credentials);
            res = webClient.DownloadString(url);

            if (logger.IsTraceEnabled)
                logger.Trace("Downloaded: " + res);

            if (cacheable)
            {
                lock (this)
                {
                    // store in cache
                    cache[url] = res;
                }
            }

            return res;
        }