Skip to content
This repository
Browse code

Update to return file names with the asset file info.

Add method to wrapper to get streams for asset files
  • Loading branch information...
commit ef728d073cfbcb9c2359968ffce060db4b3fb36e 1 parent 509942b
Jason Cooke [Microsoft] authored January 10, 2013
4  ...soft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceScenarioTest.java
@@ -111,7 +111,7 @@ public void downloadFiles() throws Exception {
111 111
         wrapper.uploadFilesToAsset(asset, 10, getTestAssetFiles());
112 112
         signalSetupFinished();
113 113
 
114  
-        List<URL> fileUrls = wrapper.createFileURLsFromAsset(asset, 10);
  114
+        Hashtable<String, URL> fileUrls = wrapper.createFileURLsFromAsset(asset, 10);
115 115
         validator.validateAssetFileUrls(fileUrls, getTestAssetFiles());
116 116
     }
117 117
 
@@ -166,7 +166,7 @@ public void transformEncryptedAsset() throws Exception {
166 166
         validator.validateAssetFiles(outputAsset, getTestAssetFiles());
167 167
 
168 168
         // Verify assets were decoded.
169  
-        List<URL> fileUrls = wrapper.createFileURLsFromAsset(outputAsset, 10);
  169
+        Hashtable<String, URL> fileUrls = wrapper.createFileURLsFromAsset(outputAsset, 10);
170 170
         validator.validateAssetFileUrls(fileUrls, getTestAssetFiles());
171 171
     }
172 172
 
10  microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java
@@ -139,12 +139,12 @@ public void validateAssetFiles(AssetInfo asset, Hashtable<String, InputStream> i
139 139
         }
140 140
     }
141 141
 
142  
-    public void validateAssetFileUrls(List<URL> fileUrls, Hashtable<String, InputStream> inputFiles)
  142
+    public void validateAssetFileUrls(Hashtable<String, URL> fileUrls, Hashtable<String, InputStream> inputFiles)
143 143
             throws IOException, InterruptedException {
144  
-        assertEquals("fileUrls count", inputFiles.size(), fileUrls.size());
145  
-        for (URL file : fileUrls) {
146  
-            InputStream expected = inputFiles.get(inputFiles.keySet().toArray()[0]);
147  
-            InputStream actual = getInputStreamWithRetry(file);
  144
+        assertEquals("fileUrls count", inputFiles.size(), fileUrls.keySet().size());
  145
+        for (String fileName : fileUrls.keySet()) {
  146
+            InputStream expected = inputFiles.get(fileName);
  147
+            InputStream actual = getInputStreamWithRetry(fileUrls.get(fileName));
148 148
             assertStreamsEqual(expected, actual);
149 149
         }
150 150
     }
52  microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java
@@ -162,9 +162,8 @@ public void uploadFilesToAsset(AssetInfo asset, int uploadWindowInMinutes,
162 162
 
163 163
         String contentKeyId = null;
164 164
         if (aesKey != null) {
165  
-            String protectionKeyId = (String) service.action(ProtectionKey
166  
-                    .getProtectionKeyId(ContentKeyType.StorageEncryption));
167  
-            String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
  165
+            String protectionKeyId = service.action(ProtectionKey.getProtectionKeyId(ContentKeyType.StorageEncryption));
  166
+            String protectionKey = service.action(ProtectionKey.getProtectionKey(protectionKeyId));
168 167
 
169 168
             String contentKeyIdUuid = UUID.randomUUID().toString();
170 169
             contentKeyId = "nb:kid:UUID:" + contentKeyIdUuid;
@@ -355,9 +354,9 @@ public void cancelJob(JobInfo job) throws ServiceException {
355 354
     }
356 355
 
357 356
     // Deliver
358  
-    public List<URL> createFileURLsFromAsset(AssetInfo asset, int availabilityWindowInMinutes) throws ServiceException,
359  
-            MalformedURLException {
360  
-        List<URL> ret = new ArrayList<URL>();
  357
+    public Hashtable<String, URL> createFileURLsFromAsset(AssetInfo asset, int availabilityWindowInMinutes)
  358
+            throws ServiceException, MalformedURLException {
  359
+        Hashtable<String, URL> ret = new Hashtable<String, URL>();
361 360
 
362 361
         AccessPolicyInfo readAP = service.create(AccessPolicy.create(accessPolicyPrefix + "tempAccessPolicy",
363 362
                 availabilityWindowInMinutes, EnumSet.of(AccessPolicyPermission.READ)));
@@ -366,12 +365,51 @@ public void cancelJob(JobInfo job) throws ServiceException {
366 365
         List<AssetFileInfo> publishedFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
367 366
         for (AssetFileInfo fi : publishedFiles) {
368 367
             URL file = constructUrlFromLocatorAndFileName(readLocator, fi.getName());
369  
-            ret.add(file);
  368
+            ret.put(fi.getName(), file);
370 369
         }
371 370
 
372 371
         return ret;
373 372
     }
374 373
 
  374
+    // Ingest
  375
+    public Hashtable<String, InputStream> downloadFilesFromAsset(AssetInfo asset, int downloadWindowInMinutes)
  376
+            throws Exception {
  377
+        Hashtable<String, URL> urls = createFileURLsFromAsset(asset, downloadWindowInMinutes);
  378
+        Hashtable<String, InputStream> ret = new Hashtable<String, InputStream>();
  379
+
  380
+        for (String fileName : urls.keySet()) {
  381
+            URL url = urls.get(fileName);
  382
+            InputStream stream = getInputStreamWithRetry(url);
  383
+            ret.put(fileName, stream);
  384
+        }
  385
+
  386
+        return ret;
  387
+    }
  388
+
  389
+    // This method is needed because there can be a delay before a new read locator
  390
+    // is applied for the asset files.
  391
+    private InputStream getInputStreamWithRetry(URL file) throws IOException, InterruptedException {
  392
+        InputStream reader = null;
  393
+        for (int counter = 0; true; counter++) {
  394
+            try {
  395
+                reader = file.openConnection().getInputStream();
  396
+                break;
  397
+            }
  398
+            catch (IOException e) {
  399
+                System.out.println("Got error, wait a bit and try again");
  400
+                if (counter < 6) {
  401
+                    Thread.sleep(10000);
  402
+                }
  403
+                else {
  404
+                    // No more retries.
  405
+                    throw e;
  406
+                }
  407
+            }
  408
+        }
  409
+
  410
+        return reader;
  411
+    }
  412
+
375 413
     private URL constructUrlFromLocatorAndFileName(LocatorInfo locator, String fileName) throws MalformedURLException {
376 414
         String locatorPath = locator.getPath();
377 415
         int startOfSas = locatorPath.indexOf("?");

0 notes on commit ef728d0

Please sign in to comment.
Something went wrong with that request. Please try again.