Permalink
Browse files

Update to return file names with the asset file info.

Add method to wrapper to get streams for asset files
  • Loading branch information...
1 parent 509942b commit ef728d073cfbcb9c2359968ffce060db4b3fb36e @jcookems jcookems committed Jan 10, 2013
View
4 ...src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceScenarioTest.java
@@ -111,7 +111,7 @@ public void downloadFiles() throws Exception {
wrapper.uploadFilesToAsset(asset, 10, getTestAssetFiles());
signalSetupFinished();
- List<URL> fileUrls = wrapper.createFileURLsFromAsset(asset, 10);
+ Hashtable<String, URL> fileUrls = wrapper.createFileURLsFromAsset(asset, 10);
validator.validateAssetFileUrls(fileUrls, getTestAssetFiles());
}
@@ -166,7 +166,7 @@ public void transformEncryptedAsset() throws Exception {
validator.validateAssetFiles(outputAsset, getTestAssetFiles());
// Verify assets were decoded.
- List<URL> fileUrls = wrapper.createFileURLsFromAsset(outputAsset, 10);
+ Hashtable<String, URL> fileUrls = wrapper.createFileURLsFromAsset(outputAsset, 10);
validator.validateAssetFileUrls(fileUrls, getTestAssetFiles());
}
View
10 ...i/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceValidation.java
@@ -139,12 +139,12 @@ public void validateAssetFiles(AssetInfo asset, Hashtable<String, InputStream> i
}
}
- public void validateAssetFileUrls(List<URL> fileUrls, Hashtable<String, InputStream> inputFiles)
+ public void validateAssetFileUrls(Hashtable<String, URL> fileUrls, Hashtable<String, InputStream> inputFiles)
throws IOException, InterruptedException {
- assertEquals("fileUrls count", inputFiles.size(), fileUrls.size());
- for (URL file : fileUrls) {
- InputStream expected = inputFiles.get(inputFiles.keySet().toArray()[0]);
- InputStream actual = getInputStreamWithRetry(file);
+ assertEquals("fileUrls count", inputFiles.size(), fileUrls.keySet().size());
+ for (String fileName : fileUrls.keySet()) {
+ InputStream expected = inputFiles.get(fileName);
+ InputStream actual = getInputStreamWithRetry(fileUrls.get(fileName));
assertStreamsEqual(expected, actual);
}
}
View
52 ...-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceWrapper.java
@@ -162,9 +162,8 @@ public void uploadFilesToAsset(AssetInfo asset, int uploadWindowInMinutes,
String contentKeyId = null;
if (aesKey != null) {
- String protectionKeyId = (String) service.action(ProtectionKey
- .getProtectionKeyId(ContentKeyType.StorageEncryption));
- String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
+ String protectionKeyId = service.action(ProtectionKey.getProtectionKeyId(ContentKeyType.StorageEncryption));
+ String protectionKey = service.action(ProtectionKey.getProtectionKey(protectionKeyId));
String contentKeyIdUuid = UUID.randomUUID().toString();
contentKeyId = "nb:kid:UUID:" + contentKeyIdUuid;
@@ -355,9 +354,9 @@ public void cancelJob(JobInfo job) throws ServiceException {
}
// Deliver
- public List<URL> createFileURLsFromAsset(AssetInfo asset, int availabilityWindowInMinutes) throws ServiceException,
- MalformedURLException {
- List<URL> ret = new ArrayList<URL>();
+ public Hashtable<String, URL> createFileURLsFromAsset(AssetInfo asset, int availabilityWindowInMinutes)
+ throws ServiceException, MalformedURLException {
+ Hashtable<String, URL> ret = new Hashtable<String, URL>();
AccessPolicyInfo readAP = service.create(AccessPolicy.create(accessPolicyPrefix + "tempAccessPolicy",
availabilityWindowInMinutes, EnumSet.of(AccessPolicyPermission.READ)));
@@ -366,12 +365,51 @@ public void cancelJob(JobInfo job) throws ServiceException {
List<AssetFileInfo> publishedFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
for (AssetFileInfo fi : publishedFiles) {
URL file = constructUrlFromLocatorAndFileName(readLocator, fi.getName());
- ret.add(file);
+ ret.put(fi.getName(), file);
}
return ret;
}
+ // Ingest
+ public Hashtable<String, InputStream> downloadFilesFromAsset(AssetInfo asset, int downloadWindowInMinutes)
+ throws Exception {
+ Hashtable<String, URL> urls = createFileURLsFromAsset(asset, downloadWindowInMinutes);
+ Hashtable<String, InputStream> ret = new Hashtable<String, InputStream>();
+
+ for (String fileName : urls.keySet()) {
+ URL url = urls.get(fileName);
+ InputStream stream = getInputStreamWithRetry(url);
+ ret.put(fileName, stream);
+ }
+
+ return ret;
+ }
+
+ // This method is needed because there can be a delay before a new read locator
+ // is applied for the asset files.
+ private InputStream getInputStreamWithRetry(URL file) throws IOException, InterruptedException {
+ InputStream reader = null;
+ for (int counter = 0; true; counter++) {
+ try {
+ reader = file.openConnection().getInputStream();
+ break;
+ }
+ catch (IOException e) {
+ System.out.println("Got error, wait a bit and try again");
+ if (counter < 6) {
+ Thread.sleep(10000);
+ }
+ else {
+ // No more retries.
+ throw e;
+ }
+ }
+ }
+
+ return reader;
+ }
+
private URL constructUrlFromLocatorAndFileName(LocatorInfo locator, String fileName) throws MalformedURLException {
String locatorPath = locator.getPath();
int startOfSas = locatorPath.indexOf("?");

0 comments on commit ef728d0

Please sign in to comment.