Site Tools


guides:ws_payloads_retrieve_payload

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:ws_payloads_retrieve_payload [2020/08/24 20:58] brett.zamoraguides:ws_payloads_retrieve_payload [2020/08/24 21:24] (current) brett.zamora
Line 2: Line 2:
  
 The payloads web service is a REST structured service that derives its functionality based on syntax used when calling the web service. To retrieve a payload from a repository, call the payloads web service with a repository name and the payload file name. The payloads web service is a REST structured service that derives its functionality based on syntax used when calling the web service. To retrieve a payload from a repository, call the payloads web service with a repository name and the payload file name.
 +
 +The payload will be returned in the body of the response as a UTF-8 encoded XML string.
  
 **Important:** The payload must be locked prior to retrieving it. **Important:** The payload must be locked prior to retrieving it.
Line 119: Line 121:
   * Retrieve the remote payload.   * Retrieve the remote payload.
   * Validate your retrieved payload.   * Validate your retrieved payload.
-  * If the validation is successful: +  * If the validation is successful: Delete the locked remote payload. 
-  ** Delete the locked remote payload. +  * Otherwise: Unlock the remote payload. (Troubleshoot or re-retrieve.) 
-  * Otherwise: + 
-  ** Unlock the remote payload. (Troubleshoot or re-retrieve.) +
-  *  +
 The following snippets show the process, minus the validation, step... The following snippets show the process, minus the validation, step...
 +
  
 ** Snippet 1: ** ** Snippet 1: **
Line 135: Line 137:
     string strLocalFolder = tbLocalFolderName.Text;     string strLocalFolder = tbLocalFolderName.Text;
     string strRepository = (cbRepository.SelectedItem as ComboBoxItem).Content as string;     string strRepository = (cbRepository.SelectedItem as ComboBoxItem).Content as string;
 +    
 +    // See payloads:get_repository_file_names web servicefor the following
 +    
     string[] strFolderFileNames = getRepositoryFileNames(strRepository);     string[] strFolderFileNames = getRepositoryFileNames(strRepository);
  
Line 141: Line 146:
         for (int i = 0; i < strFolderFileNames.Length; i++)         for (int i = 0; i < strFolderFileNames.Length; i++)
         {         {
-            rc = LockUnlockRemoteFile(strRepository, strFolderFileNames[i], "lock"); // You must lock the file before you can do any operations on it. +            // Lock the payload 
-            DownloadRemoteFile(strRepository, strFolderFileNames[i], strLocalFolder);  // Retrieve the file +            rc = LockUnlockRemoteFile(strRepository, strFolderFileNames[i], "lock"); 
-            rc = DeleteRemoteFile(strRepository, strFolderFileNames[i]); // After successfully retrieving the file it should be removed from the remote repository.+            // Retrieve the payload 
 +            DownloadRemoteFile(strRepository, strFolderFileNames[i], strLocalFolder); 
 +            // Delete the remote payload 
 +            rc = DeleteRemoteFile(strRepository, strFolderFileNames[i]);
         }         }
     }     }
 +}
 +</code>
 +
 +
 +** Snippet 2: Lock The Payload **
 +
 +<code C#>
 +private bool LockUnlockRemoteFile(string strRepositoryType, string strFileName, string strAction)
 +{
 +    bool rc = false;  //Default
 +
 +    if (strAction == "lock")
 +    {
 +        tbMessageLine.Text = "Locking remote file " + strFileName;
 +    }
 +    else if (strAction == "unlock")
 +    {
 +        tbMessageLine.Text = "Unlocking remote file " + strFileName;
 +    }
 +    else
 +    {
 +        tbMessageLine.Text = "";
 +    }
 +
 +    RestClient client = null;
 +    RestRequest request = null;
 +    IRestResponse response = null;
 +
 +    string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
 +
 +    // Password has to be hashed.
 +    // New convention uses a Salt value from the user record
 +    // See the Login web service for more detail.
 +
 +    string strSalt = GetSalt(tbUserName.Text);
 +    string strPassword = tbPassword.Password.ToString();
 +
 +    string strHashedPassword = HashPassword(strPassword, strSalt);
 +
 +    string strParms = "{\n\t\"user_name\" : \"";
 +    strParms += tbUserName.Text;
 +    strParms += "\",\n\t\"password\" : \"";
 +    strParms += strHashedPassword;
 +    strParms += "\"\n}";
 +
 +    client = new RestClient(strURL);
 +    request = new RestRequest(Method.POST);
 +
 +    request.AddHeader("Cache-Control", "no-cache");
 +    request.AddHeader("Content-Type", "application/json");
 +
 +    request.AddParameter("Login", strParms, ParameterType.RequestBody);
 +    response = client.Execute(request);
 +
 +    if (response.IsSuccessful)
 +    {
 +        var jsonResult1 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        string token = jsonResult1.access_token;
 +
 +        strURL = null;
 +        client = null;
 +        request = null;
 +        response = null;
 +
 +        strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName + "?operation=" + strAction;  
 +
 +        client = new RestClient(strURL);
 +        request = new RestRequest(Method.PATCH);
 +
 +        request.AddHeader("Cache-Control", "no-cache");
 +        request.AddHeader("access_token", token);
 +        response = client.Execute(request);
 +
 +        if (response.IsSuccessful)
 +        {
 +            if (strAction == "lock")
 +            {
 +                tbMessageLine.Text = "Remote file " + strFileName + " successfully locked.";
 +            }
 +            else if (strAction == "unlock")
 +            {
 +                tbMessageLine.Text = "Remote file " + strFileName + " successfully unlocked.";
 +            }
 +            else
 +            {
 +                tbMessageLine.Text = "";
 +            }
 +            rc = true;
 +        }
 +        else
 +        {
 +            var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +            string strMessage = jsonResult2.Exception.Message;
 +            tbMessageLine.Clear();
 +            tbMessageLine.Text = strMessage;
 +        }
 +    }
 +    else
 +    {
 +        var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        tbMessageLine.Text = jsonResult2.ToString();
 +    }
 +    return (rc);
 +}
 +</code>
 +
 +** Snippet 3: Retrieve The Payload **
 +
 +<code C#>
 +private void DownloadRemoteFile(string strRepositoryType, string strFileName, string strLocalFolder)
 +{
 +    tbMessageLine.Text = "";
 +
 +    RestClient client = null;
 +    RestRequest request = null;
 +    IRestResponse response = null;
 +
 +    string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
 +
 +    // Password has to be hashed.
 +    // New convention uses a Salt value from the user record
 +    // See the Login web service for more detail
 +
 +    string strSalt = GetSalt(tbUserName.Text);
 +    string strPassword = tbPassword.Password.ToString();
 +
 +    string strHashedPassword = HashPassword(strPassword, strSalt);
 +
 +    string strParms = "{\n\t\"user_name\" : \"";
 +    strParms += tbUserName.Text;
 +    strParms += "\",\n\t\"password\" : \"";
 +    strParms += strHashedPassword;
 +    strParms += "\"\n}";
 +
 +    client = new RestClient(strURL);
 +    request = new RestRequest(Method.POST);
 +
 +    request.AddHeader("Cache-Control", "no-cache");
 +    request.AddHeader("Content-Type", "application/json");
 +
 +    request.AddParameter("Login", strParms, ParameterType.RequestBody);
 +    response = client.Execute(request);
 +
 +    if (response.IsSuccessful)
 +    {
 +        var jsonResult1 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        string token = jsonResult1.access_token;
 +
 +        strURL = null;
 +        client = null;
 +        request = null;
 +        response = null;
 +
 +        strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName;  
 +
 +        client = new RestClient(strURL);
 +        request = new RestRequest(Method.GET);
 +
 +        request.AddHeader("Cache-Control", "no-cache");
 +        request.AddHeader("access_token", token);
 +        request.AddHeader("Content-Type", "text/plain");
 +        response = client.Execute(request);
 +
 +        if (response.IsSuccessful)
 +        {
 +            // Note: Do not try to use the LoadXml API as it does not account for the encoding preamble.
 +            
 +            XmlReader myXmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(response.Content)));
 +            XmlDocument xmlDoc = new XmlDocument();
 +            xmlDoc.Load(myXmlReader);
 +            xmlDoc.Save(strLocalFolder + "\\" + strFileName);
 +        }
 +        else
 +        {
 +            var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +            string strMessage = jsonResult2.Exception.Message;
 +            tbMessageLine.Text = strMessage;
 +        }
 +    }
 +    else
 +    {
 +        var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        tbMessageLine.Text = jsonResult2.ToString();
 +    }
 +}
 +</code>
 +
 +** Snippet 4: Delete The Remote Payload **
 +
 +<code C#>
 +private bool DeleteRemoteFile(string strRepositoryType, string strFileName)
 +{
 +    tbMessageLine.Text = "Deleting remote file " + strFileName;
 +
 +    bool rc = false;
 +
 +    RestClient client = null;
 +    RestRequest request = null;
 +    IRestResponse response = null;
 +
 +    string strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/user/login";
 +
 +    // Password has to be hashed.
 +    // New convention uses a Salt value from the user record
 +    // See the Login web service for more detail
 +
 +    string strSalt = GetSalt(tbUserName.Text);
 +    string strPassword = tbPassword.Password.ToString();
 +
 +    string strHashedPassword = HashPassword(strPassword, strSalt);
 +
 +    string strParms = "{\n\t\"user_name\" : \"";
 +    strParms += tbUserName.Text;
 +    strParms += "\",\n\t\"password\" : \"";
 +    strParms += strHashedPassword;
 +    strParms += "\"\n}";
 +
 +    client = new RestClient(strURL);
 +    request = new RestRequest(Method.POST);
 +
 +    request.AddHeader("Cache-Control", "no-cache");
 +    request.AddHeader("Content-Type", "application/json");
 +
 +    request.AddParameter("Login", strParms, ParameterType.RequestBody);
 +    response = client.Execute(request);
 +
 +    if (response.IsSuccessful)
 +    {
 +        var jsonResult1 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        string token = jsonResult1.access_token;
 +
 +        strURL = null;
 +        client = null;
 +        request = null;
 +        response = null;
 +        
 +        strURL = "https://apex-prd.certna.org/APEX/Service/APEXPublicServer.svc/payloads/" + strRepositoryType + "/" + strFileName; 
 +
 +        client = new RestClient(strURL);
 +        request = new RestRequest(Method.DELETE);
 +
 +        request.AddHeader("Cache-Control", "no-cache");
 +        request.AddHeader("access_token", token);
 +        response = client.Execute(request);
 +
 +        if (response.IsSuccessful)
 +        {
 +            tbMessageLine.Text = "Remote file " + strFileName + " successfully retrieved. Remote file deleted.";
 +            rc = true;
 +        }
 +        else
 +        {
 +            var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +            string strMessage = jsonResult2.Exception.Message;
 +            tbMessageLine.Text = strMessage;
 +        }
 +    }
 +    else
 +    {
 +        var jsonResult2 = JsonConvert.DeserializeObject<dynamic>(response.Content);
 +        tbMessageLine.Text = jsonResult2.ToString();
 +    }
 +    return (rc);
 } }
 </code> </code>
guides/ws_payloads_retrieve_payload.1598302682.txt.gz · Last modified: by brett.zamora