Exemplo n.º 1
0
        public override JObject SupportedVersionsJson(Server server)
        {
            var uri = PanoramaUtil.Call(server.URI, @"targetedms", null, @"getMaxSupportedVersions");

            string supportedVersionsJson;

            using (var webClient = new WebClientWithCredentials(server.URI, server.Username, server.Password))
            {
                try
                {
                    supportedVersionsJson = webClient.DownloadString(uri);
                }
                catch (WebException)
                {
                    // An exception will be thrown if the response code is not 200 (Success).
                    // We may be communicating with an older server that does not understand the request.
                    return(null);
                }
            }
            try
            {
                return(JObject.Parse(supportedVersionsJson));
            }
            catch (Exception)
            {
                // If there was an error in parsing the JSON.
                return(null);
            }
        }
Exemplo n.º 2
0
        public override JObject SupportedVersionsJson(Server server)
        {
            var uri = PanoramaUtil.Call(server.URI, "targetedms", null, "getMaxSupportedVersions"); // Not L10N

            string supportedVersionsJson;

            using (var webClient = new WebClient())
            {
                webClient.Headers.Add(HttpRequestHeader.Authorization, server.AuthHeader);

                try
                {
                    supportedVersionsJson = webClient.DownloadString(uri);
                }
                catch (WebException)
                {
                    // An exception will be thrown if the response code is not 200 (Success).
                    // We may be communicating with an older server that does not understand the request.
                    return(null);
                }
            }
            try
            {
                return(JObject.Parse(supportedVersionsJson));
            }
            catch (Exception)
            {
                // If there was an error in parsing the JSON.
                return(null);
            }
        }
Exemplo n.º 3
0
        public UserState IsValidUser(string username, string password)
        {
            var refServerUri = ServerUri;
            var userState    = PanoramaUtil.ValidateServerAndUser(ref refServerUri, username, password);

            if (userState == UserState.valid)
            {
                ServerUri = refServerUri;
            }
            return(userState);
        }
Exemplo n.º 4
0
        public override JToken GetInfoForFolders(Server server, string folder)
        {
            // Retrieve folders from server.
            Uri uri = PanoramaUtil.GetContainersUri(server.URI, folder, true); // Not L10N

            using (WebClient webClient = new WebClient())
            {
                webClient.Headers.Add(HttpRequestHeader.Authorization, server.AuthHeader);
                string folderInfo = webClient.UploadString(uri, PanoramaUtil.FORM_POST, string.Empty);
                return(JObject.Parse(folderInfo));
            }
        }
Exemplo n.º 5
0
        public override JToken GetInfoForFolders(Server server, string folder)
        {
            EnsureLogin(server);

            // Retrieve folders from server.
            Uri uri = PanoramaUtil.GetContainersUri(server.URI, folder, true);

            using (var webClient = new WebClientWithCredentials(server.URI, server.Username, server.Password))
            {
                return(webClient.Get(uri));
            }
        }
Exemplo n.º 6
0
        public void EnsureLogin(Server server)
        {
            var       refServerUri = server.URI;
            UserState userState    = PanoramaUtil.ValidateServerAndUser(ref refServerUri, server.Username, server.Password);

            if (userState == UserState.valid)
            {
                server.URI = refServerUri;
                return;
            }

            switch (userState)
            {
            case UserState.nonvalid:
                throw new PanoramaServerException(Resources.EditServerDlg_OkDialog_The_username_and_password_could_not_be_authenticated_with_the_panorama_server);

            case UserState.unknown:
                throw new PanoramaServerException(string.Format(Resources.EditServerDlg_OkDialog_Unknown_error_connecting_to_the_server__0__, refServerUri.AbsoluteUri));
            }
        }
Exemplo n.º 7
0
        public FolderState IsValidFolder(string folderPath, string username, string password)
        {
            try
            {
                var uri = PanoramaUtil.GetContainersUri(ServerUri, folderPath, false);

                using (var webClient = new WebClient())
                {
                    webClient.Headers.Add(HttpRequestHeader.Authorization, Server.GetBasicAuthHeader(username, password));
                    var    folderInfo = webClient.UploadString(uri, PanoramaUtil.FORM_POST, string.Empty); // Not L10N
                    JToken response   = JObject.Parse(folderInfo);

                    // User needs write permissions to publish to the folder
                    if (!PanoramaUtil.CheckFolderPermissions(response))
                    {
                        return(FolderState.nopermission);
                    }

                    // User can only upload to a TargetedMS folder type.
                    if (!PanoramaUtil.CheckFolderType(response))
                    {
                        return(FolderState.notpanorama);
                    }
                }
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(FolderState.notfound);
                }
                else
                {
                    throw;
                }
            }
            return(FolderState.valid);
        }
Exemplo n.º 8
0
        public FolderState IsValidFolder(string folderPath, string username, string password)
        {
            try
            {
                var uri = PanoramaUtil.GetContainersUri(ServerUri, folderPath, false);

                using (var webClient = new WebClientWithCredentials(ServerUri, username, password))
                {
                    JToken response = webClient.Get(uri);

                    // User needs write permissions to publish to the folder
                    if (!PanoramaUtil.CheckFolderPermissions(response))
                    {
                        return(FolderState.nopermission);
                    }

                    // User can only upload to a TargetedMS folder type.
                    if (!PanoramaUtil.CheckFolderType(response))
                    {
                        return(FolderState.notpanorama);
                    }
                }
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(FolderState.notfound);
                }
                else
                {
                    throw;
                }
            }
            return(FolderState.valid);
        }
Exemplo n.º 9
0
        public override Uri  SendZipFile(Server server, string folderPath, string zipFilePath, IProgressMonitor progressMonitor)
        {
            _progressMonitor = progressMonitor;
            _progressStatus  = new ProgressStatus(String.Empty);
            var zipFileName = Path.GetFileName(zipFilePath) ?? string.Empty;

            // Upload zip file to pipeline folder.
            using (_webClient = new NonStreamBufferingWebClient())
            {
                _webClient.UploadProgressChanged += webClient_UploadProgressChanged;
                _webClient.UploadFileCompleted   += webClient_UploadFileCompleted;

                _webClient.Headers.Add(HttpRequestHeader.Authorization, server.AuthHeader);
                var     webDav         = PanoramaUtil.Call(server.URI, "pipeline", folderPath, "getPipelineContainer", true); // Not L10N
                var     webDavInfo     = _webClient.UploadString(webDav, PanoramaUtil.FORM_POST, string.Empty);
                JObject jsonWebDavInfo = JObject.Parse(webDavInfo);

                string webDavUrl = (string)jsonWebDavInfo["webDavURL"]; // Not L10N

                // Upload Url minus the name of the zip file.
                var baseUploadUri = new Uri(server.URI, webDavUrl);
                // Use Uri.EscapeDataString instead of Uri.EscapleUriString.
                // The latter will not escape characters such as '+' or '#'
                var escapedZipFileName = Uri.EscapeDataString(zipFileName);
                var tmpUploadUri       = new Uri(baseUploadUri, escapedZipFileName + ".part"); // Not L10N
                var uploadUri          = new Uri(baseUploadUri, escapedZipFileName);

                lock (this)
                {
                    // Write to a temp file first. This will be renamed after a successful upload or deleted if the upload is canceled.
                    // Add a "Temporary" header so that LabKey marks this as a temporary file.
                    // https://www.labkey.org/issues/home/Developer/issues/details.view?issueId=19220
                    _webClient.Headers.Add("Temporary", "T");                     // Not L10N
                    _webClient.UploadFileAsync(tmpUploadUri, "PUT", zipFilePath); // Not L10N

                    // Wait for the upload to complete
                    Monitor.Wait(this);
                }

                if (progressMonitor.IsCanceled)
                {
                    // Delete the temporary file on the server
                    progressMonitor.UpdateProgress(
                        _progressStatus =
                            _progressStatus.ChangeMessage(
                                Resources.WebPanoramaPublishClient_SendZipFile_Deleting_temporary_file_on_server));
                    DeleteTempZipFile(tmpUploadUri, server.AuthHeader);
                    return(null);
                }
                // Make sure the temporary file was uploaded to the server
                ConfirmFileOnServer(tmpUploadUri, server.AuthHeader);

                // Rename the temporary file
                _progressStatus = _progressStatus.ChangeMessage(
                    Resources.WebPanoramaPublishClient_SendZipFile_Renaming_temporary_file_on_server);
                progressMonitor.UpdateProgress(_progressStatus);

                RenameTempZipFile(tmpUploadUri, uploadUri, server.AuthHeader);

                _progressStatus = _progressStatus.ChangeMessage(Resources.WebPanoramaPublishClient_SendZipFile_Waiting_for_data_import_completion___);
                progressMonitor.UpdateProgress(_progressStatus = _progressStatus.ChangePercentComplete(-1));

                // Data must be completely uploaded before we can import.
                Uri importUrl = PanoramaUtil.Call(server.URI, "targetedms", folderPath, "skylineDocUploadApi"); // Not L10N

                // Need to tell server which uploaded file to import.
                var dataImportInformation = new NameValueCollection
                {
                    // For now, we only have one root that user can upload to
                    { "path", "./" },                                                                                      // Not L10N
                    { "file", zipFileName }                                                                                // Not L10N
                };
                byte[] responseBytes  = _webClient.UploadValues(importUrl, PanoramaUtil.FORM_POST, dataImportInformation); // Not L10N
                string response       = Encoding.UTF8.GetString(responseBytes);
                JToken importResponse = JObject.Parse(response);

                // ID to check import status.
                var details   = importResponse["UploadedJobDetails"];                                                 // Not L10N
                int rowId     = (int)details[0]["RowId"];                                                             // Not L10N
                Uri statusUri = PanoramaUtil.Call(server.URI, "query", folderPath, "selectRows",                      // Not L10N
                                                  "query.queryName=job&schemaName=pipeline&query.rowId~eq=" + rowId); // Not L10N
                bool complete = false;
                // Wait for import to finish before returning.
                Uri result = null;
                while (!complete)
                {
                    if (progressMonitor.IsCanceled)
                    {
                        return(null);
                    }

                    string statusResponse  = _webClient.UploadString(statusUri, PanoramaUtil.FORM_POST, string.Empty);
                    JToken jStatusResponse = JObject.Parse(statusResponse);
                    JToken rows            = jStatusResponse["rows"];                            // Not L10N
                    var    row             = rows.FirstOrDefault(r => (int)r["RowId"] == rowId); // Not L10N
                    if (row == null)
                    {
                        continue;
                    }

                    string status = (string)row["Status"];                                 // Not L10N
                    result = new Uri(server.URI, (string)row["_labkeyurl_Description"]);   // Not L10N
                    if (string.Equals(status, "ERROR"))                                    // Not L10N
                    {
                        var jobUrl = new Uri(server.URI, (string)row["_labkeyurl_RowId"]); // Not L10N
                        var e      = new PanoramaImportErrorException(server.URI, jobUrl);
                        progressMonitor.UpdateProgress(
                            _progressStatus = _progressStatus.ChangeErrorException(e));
                        throw e;
                    }

                    complete = string.Equals(status, "COMPLETE"); // Not L10N
                }

                progressMonitor.UpdateProgress(_progressStatus.Complete());
                return(result);
            }
        }