Fetch data from Azure storage

This article shows you how to populate an adaptive form with the data stored in Azure storage.
It is assumed that you have stored the adaptive form data in Azure storage and now want to prefill your adaptive form with that data.

NOTE
The code in this article does not work with core components based adaptive form.The equivalent article for core component based adaptive form is available here

Create GET request

The next step is to write the code to fetch the data from the Azure Storage using the blobID. The following code was written to fetch the data. The url was constructed using the sasToken and storageURI values from the OSGi configuration and the blobID passed to the getBlobData function

 @Override
public String getBlobData(String blobID) {
    String sasToken = azurePortalConfigurationService.getSASToken();
    String storageURI = azurePortalConfigurationService.getStorageURI();
    log.debug("The SAS Token is " + sasToken);
    log.debug("The Storage URL is " + storageURI);
    String httpGetURL = storageURI + blobID;
    httpGetURL = httpGetURL + sasToken;
    HttpGet httpGet = new HttpGet(httpGetURL);

    org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CloseableHttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        String blobData = EntityUtils.toString(httpEntity);
        log.debug("The blob data I got was " + blobData);
        return blobData;

    } catch (ClientProtocolException e) {

        log.debug("Got Client Protocol Exception " + e.getMessage());
    } catch (IOException e) {

        log.debug("Got IOEXception " + e.getMessage());
    }

    return null;
}

When an adaptive form is rendered with a guid parameter in the url, the custom page component associated with the template fetches and populates the adaptive form with the data from Azure storage.
The following is the code in the jsp of the page component asscoiated with the template

com.aemforms.saveandfetchfromazure.StoreAndFetchDataFromAzureStorage azureStorage = sling.getService(com.aemforms.saveandfetchfromazure.StoreAndFetchDataFromAzureStorage.class);


String guid = request.getParameter("guid");

if(guid!=null&&!guid.isEmpty())
{
    String dataXml = azureStorage.getBlobData(guid);
    slingRequest.setAttribute("data",dataXml);

}

Test the solution

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e