private string requestNewToken()
        {
            string extendUrl = "/api/auth/token?username="******"&password="******"", AUTH_CONTENT_TYPE);

                if (response.isSuccess())
                {
                    log.Debug("Request new Inspur TSDB token");
                    Dictionary <string, string> token = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.getContent());
                    return(token["token"]);
                }
                else
                {
                    log.ErrorFormat("获取TSDB token失败,response error:" + response.getContent());
                    throw new Exception("Response error:" + response.getContent());
                }
            }
            catch (Exception e)
            {
                log.ErrorFormat("获取TSDB token失败:{0},{1}", e.Message, e.StackTrace);
                throw;
            }
        }
        public override SimpleHttpResponse pushLastQueries(String content, ExpectResponse expectResponse)
        {
            if (content == null)
            {
                throw new Exception("content 不能为空");
            }
            int tryTime = 1;
            SimpleHttpResponse response = null;

            while (tryTime <= maxTryTimes)
            {
                response = httpClient.doPost(buildUrl(serviceUrl, GetAuthUrl(QUERY_POST_LAST_API), expectResponse),
                                             content);
                if (response.isSuccess())
                {
                    break;
                }
                else
                {
                    log.ErrorFormat("[InspurTSDB]Get Response Error,Try Times:{0} ,Status Code: {1},Content: {2}", tryTime, response.getStatusCode(), response.getContent());
                    if (response.getStatusCode() == 401)
                    {
                        deleteCurrentToken();
                    }
                    tryTime++;
                }
            }
            return(response);
        }
        public override Response pushMetrics(MetricBuilder builder, ExpectResponse expectResponse)
        {
            if (builder == null)
            {
                throw new Exception("QueryBuilder 不能为空");
            }
            int tryTime = 1;
            SimpleHttpResponse response = null;

            while (tryTime <= maxTryTimes)
            {
                response = httpClient.doPost(buildUrl(serviceUrl, GetAuthUrl(PUT_POST_API), expectResponse),
                                             builder.build());
                if (response.isSuccess())
                {
                    break;
                }
                else
                {
                    log.ErrorFormat("[InspurTSDB]Get Response Error,Try Times:{0} ,Status Code: {1},Content: {2}", tryTime, response.getStatusCode(), response.getContent());
                    if (response.getStatusCode() == 401)
                    {
                        deleteCurrentToken();
                    }
                    tryTime++;
                }
            }


            return(getResponse(response));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Uploads a stream to CloudFoundry via HTTP
        /// </summary>
        /// <param name="uploadUri">Uri to upload to</param>
        /// <param name="zipStream">The compressed stream to upload</param>
        /// <param name="resources">The json payload describing the files of the app</param>
        /// <returns></returns>
        private async Task <SimpleHttpResponse> UploadZip(Uri uploadUri, Stream zipStream, string resources)
        {
            string boundary = DateTime.Now.Ticks.ToString("x");

            using (SimpleHttpClient httpClient = new SimpleHttpClient(this.Client.CancellationToken, AppsEndpoint.DefaultUploadTimeout))
            {
                // http://apidocs.cloudfoundry.org/210/apps/uploads_the_bits_for_an_app.html
                httpClient.HttpProxy = Client.HttpProxy;
                httpClient.SkipCertificateValidation = Client.SkipCertificateValidation;

                httpClient.Headers.Add("Authorization", string.Format("bearer {0}", this.Client.AuthorizationToken));

                httpClient.Uri    = uploadUri;
                httpClient.Method = HttpMethod.Put;

                List <HttpMultipartFormData> mpd = new List <HttpMultipartFormData>();
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resources)))
                {
                    ms.Position = 0;
                    mpd.Add(new HttpMultipartFormData("resources", string.Empty, string.Empty, ms));
                    mpd.Add(new HttpMultipartFormData("application", "application.zip", "application/zip", zipStream));
                    SimpleHttpResponse response = await httpClient.SendAsync(mpd);

                    return(response);
                }
            }
        }
Ejemplo n.º 5
0
        public void ValidateStatusCodes(int statusCode, bool isSuccessful, bool isRetryable)
        {
            var response = new SimpleHttpResponse(statusCode, null, new Dictionary <string, string>());

            Assert.True(response.StatusCode == statusCode);
            Assert.True(response.WasSuccessful == isSuccessful);
            Assert.True(response.IsStatusCodeRetryable == isRetryable);
        }
Ejemplo n.º 6
0
        public void Redirect301Request()
        {
            byte[] input = Encoding.ASCII.GetBytes(SampleRedirectResponse);
            int    statusCode;
            var    headers  = HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF), out statusCode);
            var    response = new SimpleHttpResponse(statusCode, null, headers);

            Assert.AreEqual(statusCode, 301);
            Assert.AreEqual(response.Location, @"https://1000genomes.s3.amazonaws.com/release/20110521/ALL.chr9.phase1_release_v3.20101123.snps_indels_svs.genotypes.vcf.gz?AWSAccessKeyId=AKIAIYDIF27GS5AAXHQQ&Expires=1427000627&Signature=PFrSu5ZXoUl17mCRg3HwDORfkg4%3D");
        }
Ejemplo n.º 7
0
        public void CanWeParseHttpStatusAndConvertToInt()
        {
            byte[] input = Encoding.ASCII.GetBytes(SampleHttpResponse);
            int    statusCode;
            var    result   = HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF), out statusCode);
            var    response = new SimpleHttpResponse(statusCode, input, result);

            Assert.True(response.StatusCode == 200);
            Assert.True(response.WasSuccessful);
            Assert.True(response.IsStatusCodeRetryable);
        }
Ejemplo n.º 8
0
        public void MultipleHeadersWithTheSameKey()
        {
            byte[] input = Encoding.ASCII.GetBytes(SampleRedirectWithMultipleCookiesResponse);
            int    statusCode;
            var    result = HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF),
                                                      out statusCode);
            var response = new SimpleHttpResponse(statusCode, input, result);

            Assert.True(response.StatusCode == 301);

            Assert.True(response.Headers["Set-Cookie"].Contains("blah"));
        }
Ejemplo n.º 9
0
 private static bool ProcessException(Exception ex, bool ignoreNotFound, SimpleHttpResponse response)
 {
     if (ex is StorageException)
     {
         var storeEx = (StorageException)ex;
         if (!ignoreNotFound || storeEx.RequestInformation.HttpStatusCode != (int)HttpStatusCode.NotFound)
         {
             response.StatusCode   = (HttpStatusCode)storeEx.RequestInformation.HttpStatusCode;
             response.ReasonPhrase = storeEx.RequestInformation.HttpStatusMessage;
         }
         return(true);
     }
     return(false);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Defines and uploads the package bits.
        /// <remarks>
        /// This method is only available on the .NET 4.5 framework.
        /// Calling this method from a Windows Phone App or a Windows App will throw a <see cref="NotImplementedException" />.
        /// </remarks>
        /// </summary>
        /// <param name="packageGuid">Paclage guid</param>
        /// <param name="zipStream">The zip stream.</param>
        /// <exception cref="CloudFoundryException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UploadBits(Guid packageGuid, Stream zipStream)
        {
            UriBuilder uploadEndpoint = new UriBuilder(this.Client.CloudTarget.AbsoluteUri);

            uploadEndpoint.Path = string.Format(CultureInfo.InvariantCulture, "/v3/packages/{0}/upload", packageGuid.ToString());

            SimpleHttpResponse uploadResult = await this.UploadZip(uploadEndpoint.Uri, zipStream);

            if (uploadResult.StatusCode != System.Net.HttpStatusCode.OK)
            {
                var statusCode = uploadResult.StatusCode.ToString("D");

                throw new CloudFoundryException(string.Format(CultureInfo.InvariantCulture, "An error occurred while uploading bits to the server. Status code: {0}", statusCode));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Defines and uploads the application bits.
        /// <remarks>
        /// This method is only available on the .NET 4.5 framework.
        /// Calling this method from a Windows Phone App or a Windows App will throw a <see cref="NotImplementedException" />.
        /// </remarks>
        /// </summary>
        /// <param name="appGuid">Application guid</param>
        /// <param name="zipStream">The zip stream.</param>
        /// <param name="fingerPrintList">The finger print list.</param>
        /// <exception cref="CloudFoundryException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UploadBits(Guid appGuid, Stream zipStream, List <FileFingerprint> fingerPrintList)
        {
            string serializedFingerprints = JsonConvert.SerializeObject(fingerPrintList);

            UriBuilder uploadEndpoint = new UriBuilder(this.Client.CloudTarget.AbsoluteUri);

            uploadEndpoint.Path = string.Format(CultureInfo.InvariantCulture, "/v2/apps/{0}/bits", appGuid.ToString());

            SimpleHttpResponse uploadResult = await this.UploadZip(uploadEndpoint.Uri, zipStream, serializedFingerprints);

            if (uploadResult.StatusCode != System.Net.HttpStatusCode.Created)
            {
                var statusCode = uploadResult.StatusCode.ToString("D");

                throw new CloudFoundryException(string.Format(CultureInfo.InvariantCulture, "An error occurred while uploading bits to the server. Status code: {0}", statusCode));
            }
        }
        private Response getResponse(SimpleHttpResponse httpResponse)
        {
            Response response = new Response(httpResponse.getStatusCode());
            string   content  = httpResponse.getContent();

            if (!string.IsNullOrEmpty(content))
            {
                if (response.isSuccess())
                {
                    ErrorDetail errorDetail = JsonConvert.DeserializeObject <ErrorDetail>(content);
                    response.setErrorDetail(errorDetail);
                }
                else
                {
                    //logger.error("request failed!" + httpResponse);
                    //获取数据失败了,可能是auth 过期,把缓存清掉
                    deleteCurrentToken();
                }
            }
            return(response);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Uploads a stream to CloudFoundry via HTTP
        /// </summary>
        /// <param name="uploadUri">Uri to upload to</param>
        /// <param name="zipStream">The compressed stream to upload</param>
        /// <returns></returns>
        private async Task <SimpleHttpResponse> UploadZip(Uri uploadUri, Stream zipStream)
        {
            string boundary = DateTime.Now.Ticks.ToString("x");

            using (SimpleHttpClient httpClient = new SimpleHttpClient(this.Client.CancellationToken, PackagesExperimentalEndpoint.DefaultUploadTimeout))
            {
                // http://apidocs.cloudfoundry.org/212/packages_(experimental)/upload_bits_for_a_package_of_type_bits.html
                httpClient.HttpProxy = Client.HttpProxy;
                httpClient.SkipCertificateValidation = Client.SkipCertificateValidation;

                httpClient.Headers.Add("Authorization", string.Format("bearer {0}", this.Client.AuthorizationToken));

                httpClient.Uri    = uploadUri;
                httpClient.Method = HttpMethod.Post;

                List <HttpMultipartFormData> mpd = new List <HttpMultipartFormData>();
                mpd.Add(new HttpMultipartFormData("bits", "application.zip", "application/zip", zipStream));
                SimpleHttpResponse response = await httpClient.SendAsync(mpd);

                return(response);
            }
        }
Ejemplo n.º 14
0
        private static async Task <SimpleHttpResponse> DoForContainersAsync(string container,
                                                                            HttpStatusCode successStatus,
                                                                            Func <CloudBlobContainer, Task> action,
                                                                            IEnumerable <CloudStorageAccount> accounts,
                                                                            bool ignoreNotFound,
                                                                            IEnumerable <CloudStorageAccount> excludeAccounts)
        {
            SimpleHttpResponse retval = new SimpleHttpResponse
            {
                StatusCode = successStatus,
            };

            if (excludeAccounts != null)
            {
                accounts = accounts.Except(excludeAccounts);
            }
            var actionTasks = accounts
                              .Select(account => action(NamespaceHandler.GetContainerByName(account, container)));

            try
            {
                await Task.WhenAll(actionTasks.ToArray());
            }
            catch (AggregateException aggEx)
            {
                aggEx.Handle(ex => ProcessException(ex, ignoreNotFound, retval));
            }
            catch (Exception ex)
            {
                if (!ProcessException(ex, ignoreNotFound, retval))
                {
                    throw;
                }
            }
            return(retval);
        }
Ejemplo n.º 15
0
 public void ValidateStatusCodes(  int statusCode, bool isSuccessful, bool isRetryable)
 {
     var response = new SimpleHttpResponse(statusCode, null, new Dictionary<string,string>());
      Assert.True(response.StatusCode == statusCode);
     Assert.True(response.WasSuccessful == isSuccessful);
     Assert.True(response.IsStatusCodeRetryable == isRetryable);
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Pushes an application to the cloud.
        /// <remarks>
        /// This method is only available on the .NET 4.5 framework.
        /// Calling this method from a Windows Phone App or a Windows App will throw a <see cref="NotImplementedException"/>.
        /// </remarks>
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        /// <param name="appGuid">Application guid</param>
        /// <param name="appPath">Path of origin from which the application will be deployed</param>
        /// <param name="startApplication">True if the app should be started after upload is complete, false otherwise</param>
        public async Task Push(Guid appGuid, string appPath, bool startApplication)
        {
            if (appPath == null)
            {
                throw new ArgumentNullException("appPath");
            }

            IAppPushTools pushTools = new AppPushTools();
            int           usedSteps = 1;

            // Step 1 - Check if application exists
            this.TriggerPushProgressEvent(usedSteps, "Checking if application exists");
            RetrieveAppResponse app = await this.Client.Apps.RetrieveApp(appGuid);

            usedSteps += 1;

            // Step 2 - Compute fingerprints for local files
            this.TriggerPushProgressEvent(usedSteps, "Calculating file fingerprints ...");
            Dictionary <string, List <FileFingerprint> > fingerprints = await pushTools.GetFileFingerprints(appPath, this.Client.CancellationToken);

            if (this.CheckCancellation())
            {
                return;
            }

            usedSteps += 1;

            // Step 3 - Compare fingerprints of local files with what the server has
            this.TriggerPushProgressEvent(usedSteps, "Comparing file fingerprints ...");
            string[] neededFiles = await this.FilterExistingFiles(fingerprints);

            if (this.CheckCancellation())
            {
                return;
            }

            usedSteps += 1;

            // Step 4 - Zip all needed files and get a stream back from the PushTools
            this.TriggerPushProgressEvent(usedSteps, "Creating zip package ...");
            using (Stream zippedPayload = await pushTools.GetZippedPayload(appPath, neededFiles, this.Client.CancellationToken))
            {
                if (this.CheckCancellation())
                {
                    return;
                }

                usedSteps += 1;

                // Step 5 - Upload zip to CloudFoundry ...
                this.TriggerPushProgressEvent(usedSteps, "Uploading zip package ...");
                UriBuilder uploadEndpoint = new UriBuilder(this.Client.CloudTarget.AbsoluteUri);
                uploadEndpoint.Path = string.Format(CultureInfo.InvariantCulture, "/v2/apps/{0}/bits", appGuid.ToString());

                List <FileFingerprint> fingerPrintList = fingerprints.Values.SelectMany(list => list).ToList();

                string             serializedFingerprints = JsonConvert.SerializeObject(fingerPrintList);
                SimpleHttpResponse uploadResult           = await this.UploadZip(uploadEndpoint.Uri, zippedPayload, serializedFingerprints);

                if (this.CheckCancellation())
                {
                    return;
                }

                usedSteps += 1;
            }

            if (startApplication)
            {
                // Step 6 - Start Application
                UpdateAppRequest updateApp = new UpdateAppRequest()
                {
                    State = "STARTED"
                };
                UpdateAppResponse response = await this.UpdateApp(appGuid, updateApp);

                if (this.CheckCancellation())
                {
                    return;
                }

                usedSteps += 1;
            }

            // Step 7 - Done
            this.TriggerPushProgressEvent(usedSteps, "Application {0} pushed successfully", app.Name);
        }
Ejemplo n.º 17
0
 public void Redirect301Request()
 {
     byte[] input = Encoding.ASCII.GetBytes(SampleRedirectResponse);
     int statusCode;
     var headers =HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF), out statusCode);
     var response = new SimpleHttpResponse(statusCode, null, headers);
     Assert.AreEqual(statusCode, 301);
     Assert.AreEqual(response.Location, @"https://1000genomes.s3.amazonaws.com/release/20110521/ALL.chr9.phase1_release_v3.20101123.snps_indels_svs.genotypes.vcf.gz?AWSAccessKeyId=AKIAIYDIF27GS5AAXHQQ&Expires=1427000627&Signature=PFrSu5ZXoUl17mCRg3HwDORfkg4%3D");
 }
Ejemplo n.º 18
0
        public void MultipleHeadersWithTheSameKey()
        {
            byte[] input = Encoding.ASCII.GetBytes(SampleRedirectWithMultipleCookiesResponse);
            int statusCode;
            var result = HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF),
                                                   out statusCode);
            var response = new SimpleHttpResponse(statusCode, input, result);
            Assert.True(response.StatusCode == 301);

            Assert.True(response.Headers["Set-Cookie"].Contains("blah"));
        }
        /// <summary>
        /// Returns a LogMessage containing recent logs
        /// </summary>
        /// <param name="appGuid">The Cloud Foundry app unique identifier.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">appGuid</exception>
        /// <returns></returns>
        public async Task <ApplicationLog[]> Recent(string appGuid, CancellationToken cancellationToken)
        {
            if (appGuid == null)
            {
                throw new ArgumentNullException("appGuid");
            }

            UriBuilder appLogUri = new UriBuilder(this.LoggregatorEndpoint);

            if (appLogUri.Scheme == "ws")
            {
                appLogUri.Scheme = "http";
            }
            else
            {
                appLogUri.Scheme = "https";
            }

            appLogUri.Path  = "recent";
            appLogUri.Query = string.Format(CultureInfo.InvariantCulture, "app={0}", appGuid);

            SimpleHttpClient client;

            if (httpClient != null)
            {
                client = httpClient as SimpleHttpClient;
                client.cancellationToken = cancellationToken;
            }
            else
            {
                client = new SimpleHttpClient(cancellationToken);
            }
            client.Uri    = appLogUri.Uri;
            client.Method = HttpMethod.Get;
            client.Headers.Add("AUTHORIZATION", this.AuthenticationToken);
            client.HttpProxy = this.HttpProxy;
            client.SkipCertificateValidation = this.SkipCertificateValidation;

            SimpleHttpResponse response = await client.SendAsync();

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                string errorMessage = await response.ReadContentAsStringAsync();

                throw new LoggregatorException(string.Format(CultureInfo.InvariantCulture, "Server returned error code {0} with message: '{1}'", response.StatusCode, errorMessage));
            }

            MultipartMemoryStreamProvider multipart = null;

            try
            {
                multipart = await response.Content.ReadAsMultipartAsync();
            }
            catch (IOException multipartException)
            {
                // There are no recent Logs. We need to investigate a better way for handling this
                if (multipartException.Message.Contains("MIME multipart message is not complete"))
                {
                    return(new ApplicationLog[] { new ApplicationLog()
                                                  {
                                                      Message = "(Server did not return any recent logs)"
                                                  } });
                }
                else
                {
                    throw;
                }
            }

            List <ApplicationLog> messages = new List <ApplicationLog>();

            foreach (var msg in multipart.Contents)
            {
                messages.Add(this.protobufSerializer.DeserializeApplicationLog(await msg.ReadAsByteArrayAsync()));
            }

            return(messages.ToArray());
        }
Ejemplo n.º 20
0
 public DelegatedResponse(SimpleHttpResponse src) : base(src)
 {
 }
Ejemplo n.º 21
0
 public void CanWeParseHttpStatusAndConvertToInt()
 {
     byte[] input = Encoding.ASCII.GetBytes(SampleHttpResponse);
     int statusCode;
     var result = HttpParser.GetHttpHeaders(input, input.IndexOf(ByteArrayExtensionsTests.BODY_CRLF), out statusCode);
     var response = new SimpleHttpResponse(statusCode, input, result);
     Assert.True(response.StatusCode == 200);
     Assert.True(response.WasSuccessful);
     Assert.True(response.IsStatusCodeRetryable);
 }