Example #1
0
        private static void UploadFile(
            vCloudClient client,
            string fileName,
            string fileURL,
            Stream inputStreams,
            long size,
            long startByte,
            long endByte)
        {
            HttpClient httpClient = client.HttpClient;

            Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.UPLOAD_FILE_SIZE_MSG) + " - " + fileName + "\t Total Size " + (object)size);
            try
            {
                long        num         = size;
                HttpContent httpContent = (HttpContent) new StreamContent(inputStreams);
                if (endByte > startByte)
                {
                    num = endByte - startByte + 1L;
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.UPLOAD_FILE_SIZE_MSG) + " - " + fileName + "\t" + (object)size + "\t Upload \t" + (object)startByte + " to \t" + (object)endByte);
                    ContentRangeHeaderValue rangeHeaderValue = new ContentRangeHeaderValue(startByte, endByte, size);
                    httpContent.Headers.ContentRange = rangeHeaderValue;
                }
                httpContent.Headers.ContentLength = new long?(num);
                if (!httpClient.DefaultRequestHeaders.Contains(client.VcloudTokenHeader))
                {
                    httpClient.DefaultRequestHeaders.Add(client.VcloudTokenHeader, client.VcloudToken);
                }
                HttpContent content = (HttpContent) new StreamContent(inputStreams);
                using (HttpResponseMessage result = httpClient.PutAsync(new System.Uri(fileURL), content).Result)
                {
                    if (result == null)
                    {
                        return;
                    }
                    string message = SerializationUtil.UTF8ByteArrayToString(result.Content.ReadAsByteArrayAsync().Result);
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.UPLOAD_COMPLETE_MSG) + " - " + fileName);
                    }
                    else
                    {
                        Logger.Log(TraceLevel.Critical, SdkUtil.GetI18nString(SdkMessage.UPLOAD_FAILED_MSG) + " - " + fileURL);
                        Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.STATUS_MSG) + " - " + result.StatusCode.ToString());
                        Logger.Log(TraceLevel.Information, message);
                        Logger.Log(TraceLevel.Critical, SdkUtil.GetI18nString(SdkMessage.FILE_UPLOAD_FAILED_MSG) + " - " + fileURL);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                Logger.Log(TraceLevel.Critical, SdkUtil.GetI18nString(SdkMessage.HTTP_EXCEPTION__UPLOADING_MSG) + " - " + fileURL);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (IOException ex)
            {
                Logger.Log(TraceLevel.Critical, SdkUtil.GetI18nString(SdkMessage.IO_EXCEPTION_UPLOADING_MSG) + " - " + fileURL);
                throw new VCloudRuntimeException((Exception)ex);
            }
            finally
            {
                inputStreams.Dispose();
                httpClient.DefaultRequestHeaders.Clear();
            }
        }
Example #2
0
        public static Response Put(
            vCloudClient client,
            string url,
            string content,
            string contentType,
            string charset)
        {
            HttpClient httpClient = client.HttpClient;
            Response   response   = new Response();
            string     str        = (string)null;
            int        num        = 0;

            Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.PUT_URL_MSG) + " - " + url);
            try
            {
                httpClient.DefaultRequestHeaders.Add("Accept", vCloudClient.SUPPORTED_SDK_VERSIONS[client.VcloudClientVersion]);
                httpClient.DefaultRequestHeaders.Add(client.VcloudTokenHeader, client.VcloudToken);
                HttpContent content1 = (HttpContent)null;
                if (content != null && content.Length > 0 && (contentType != null && contentType.Length > 0))
                {
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.PUT_REQUEST_BODY) + " - " + content);
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.PUT_CONTENT_TYPE) + " - " + contentType);
                    content1 = (HttpContent) new StringContent(content, Encoding.UTF8, contentType);
                }
                HttpRequestMessage  request = new HttpRequestMessage(HttpMethod.Put, url);
                HttpResponseMessage httpResponseMessage1;
                HttpResponseMessage httpResponseMessage2;
                if (content1 != null)
                {
                    httpResponseMessage2 = httpResponseMessage1 = httpClient.PutAsync(new System.Uri(url), content1).Result;
                }
                else
                {
                    httpResponseMessage1 = httpResponseMessage2 = httpClient.SendAsync(request).Result;
                }
                using (httpResponseMessage2)
                {
                    if (httpResponseMessage1 != null)
                    {
                        num = (int)httpResponseMessage1.StatusCode;
                        str = SerializationUtil.UTF8ByteArrayToString(httpResponseMessage1.Content.ReadAsByteArrayAsync().Result);
                    }
                    response.ResponseStatusCode = num;
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.STATUS_CODE_MSG) + " - " + (object)num);
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.STATUS_MSG) + " - " + httpResponseMessage1.StatusCode.ToString());
                    response.ResponseXml = str;
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.RESPONSE_MSG) + " - " + str);
                }
            }
            catch (HttpRequestException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (ApplicationException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (IOException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            finally
            {
                httpClient.DefaultRequestHeaders.Clear();
            }
            return(response);
        }
Example #3
0
        internal static Response Login(
            vCloudClient client,
            string url,
            HttpRequestHeaders authHeader)
        {
            HttpClient httpClient = client.HttpClient;
            Response   response   = new Response();
            string     str        = (string)null;
            int        num        = 0;

            Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.LOGIN_URL_MSG) + " - " + url);
            try
            {
                httpClient.DefaultRequestHeaders.Add("Accept", vCloudClient.SUPPORTED_SDK_VERSIONS[client.VcloudClientVersion]);
                HttpRequestMessage  request1 = new HttpRequestMessage(HttpMethod.Post, url);
                HttpResponseMessage result;
                using (result = httpClient.SendAsync(request1).Result)
                {
                    if (result != null)
                    {
                        if (result.StatusCode == HttpStatusCode.MovedPermanently)
                        {
                            if (!httpClient.DefaultRequestHeaders.Contains("Accept"))
                            {
                                httpClient.DefaultRequestHeaders.Add("Accept", vCloudClient.SUPPORTED_SDK_VERSIONS[client.VcloudClientVersion]);
                            }
                            System.Uri         uri      = new System.Uri(RestUtil.RedirectURL(result));
                            HttpRequestMessage request2 = (HttpRequestMessage)null;
                            request2.RequestUri.MakeRelativeUri(uri);
                            result = httpClient.SendAsync(request2).Result;
                        }
                        if (result.Headers.GetValues("x-vcloud-authorization") != null)
                        {
                            client.VcloudTokenHeader = "x-vcloud-authorization";
                            client.VcloudToken       = result.Headers.GetValues("x-vcloud-authorization").FirstOrDefault <string>();
                        }
                        num = (int)result.StatusCode;
                        str = SerializationUtil.UTF8ByteArrayToString(result.Content.ReadAsByteArrayAsync().Result);
                    }
                    response.ResponseStatusCode = num;
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.STATUS_CODE_MSG) + " - " + (object)num);
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.STATUS_MSG) + " - " + result.StatusCode.ToString());
                    response.ResponseXml = str;
                    Logger.Log(TraceLevel.Information, SdkUtil.GetI18nString(SdkMessage.RESPONSE_MSG) + " - " + str);
                }
            }
            catch (HttpRequestException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (NotSupportedException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (IOException ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException((Exception)ex);
            }
            catch (Exception ex)
            {
                Logger.Log(TraceLevel.Critical, ex.Message);
                throw new VCloudRuntimeException(ex);
            }
            finally
            {
                httpClient.DefaultRequestHeaders.Clear();
            }
            return(response);
        }