Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to Data Packaging Service #119

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,36 @@ public void getData(ZipOutputStream zout) throws IOException, DistributionExcept
* This section is added to improve logs for each dowload request through bundles.
*/
String recordid ="", filedir ="";
if(filepath.contains("/") && filepath.contains("ark")) {
int in = filepath.indexOf('/', 1 + filepath.indexOf('/', 1 + filepath.indexOf('/')));

recordid = filepath.substring(0, in);
filedir = filepath.substring(in+1);
}
else if(filepath.contains("/")) {
int in = filepath.indexOf('/');

recordid = filepath.substring(0, in);
filedir = filepath.substring(in+1);
if (filepath.contains("/") && filepath.contains("ark")) {
// Process `ark/...` format
int in = filepath.indexOf('/', 1 + filepath.indexOf('/', 1 + filepath.indexOf('/')));
recordid = filepath.substring(filepath.indexOf('/', filepath.indexOf('/') + 1) + 1, in);
filedir = filepath.substring(in + 1);
} else if (filepath.contains("/")) {
// Process 32/34-character `resId` format
int in = filepath.indexOf('/');
recordid = filepath.substring(0, in);
filedir = filepath.substring(in + 1);

if (recordid.matches("[a-zA-Z0-9]{32,34}")) {
String lastPart = recordid.substring(30); // Strip of the first 30 chars and keep the last part
recordid = "mds-" + lastPart; // Prepend "mds-"
}
}

writeLog +=recordid+","+filepath+","+jobject.getFileSize()+","+downloadurl+",";

/*
*End of part for logs
*/

logger.info("Original filepath: {}", filepath);
logger.info("Extracted record ID: {}", recordid);
logger.info("Extracted filename: {}", filedir);

// The modified file path
String modifiedFilePath = recordid + "/" + filedir;
logger.info("Modified file path for zip entry: {}", modifiedFilePath);

if (this.validateUrl(downloadurl)) {

Expand All @@ -141,7 +154,7 @@ else if(filepath.contains("/")) {
fstream = con.getInputStream();
int len;
byte[] buf = new byte[100000];
zout.putNextEntry(new ZipEntry(filepath));
zout.putNextEntry(new ZipEntry(modifiedFilePath));
while ((len = fstream.read(buf)) != -1) {
zout.write(buf, 0, len);
}
Expand Down Expand Up @@ -260,22 +273,22 @@ private void writeLogMessages(ZipOutputStream zout) throws IOException {
if (bundlelogfile.length() != 0) {
bundleInfo.append("\n Following files are not included in the bundle for the reasons given: \n");
bundleInfo.append(this.bundlelogfile);
filename = "/PackagingErrors.txt";
filename = "/DownloadErrors.txt";
bundlerequestStatus += ": partial";
// logger.info(bundlerequestStatus + "This bundle request is not completely successful. ");
}

if (bundlelogError.length() != 0) {
bundleInfo.append(
"\n Following files are not included in the bundle because of errors: \n" + bundlelogError);
filename = "/PackagingErrors.txt";
filename = "/DownloadErrors.txt";
bundlerequestStatus += ": errors";
// logger.info(bundlerequestStatus + "There are errors getting data in this bundle request. ");
}

if ((bundlelogfile.length() == 0 && bundlelogError.length() == 0) && !listUrlsStatusSize.isEmpty()) {
bundleInfo.append("\n All requested files are successfully added to this bundle.");
filename = "/PackagingSuccessful.txt";
filename = "/DownloadSuccessful.txt";
bundlerequestStatus += ": complete";
// logger.info(bundlerequestStatus +" This bundle request is successful.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package gov.nist.oar.distrib.datapackage;public class ResIdProcessorTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,38 @@ public void testValidateBundleRequest() throws DistributionException, IOExceptio

@Test
public void testGetData() throws DistributionException, MalformedURLException, IOException, InputLimitException {
val1 = "{\"filePath\":\"/1894/license.pdf\",\"downloadUrl\":\"https://s3.amazonaws.com/nist-midas/1894/license.pdf\"}";
val2 = "{\"filePath\":\"/testfile2.txt\",\"downloadUrl\":\"https://httpstat.us/404\"}";
createBundleRequest();
int countBefore = 2;
this.createBundleStream();
dp.getData(zos);
zos.close();
int countAfter = this.checkFilesinZip(path);
assertNotEquals(countBefore, countAfter);
val1 = "{\"filePath\":\"/1894/license.pdf\",\"downloadUrl\":\"https://s3.amazonaws.com/nist-midas/1894/license.pdf\"}";
val2 = "{\"filePath\":\"/file.txt\",\"downloadUrl\":\"https://httpstat.us/404\"}";
createBundleRequest();
int expectedCount = 1;
this.createBundleStream();
dp.getData(zos);
zos.close();
// Initialize file count and process ZIP entries
int actualCount = 0;
try (ZipFile zipFile = new ZipFile(path.toString())) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
System.out.println("entryname: " + entry.getName());

// Validate the transformed file paths
if (entry.getName().contains("license.pdf")) {
assertEquals("/1894/license.pdf", entry.getName()); // No transformation for short recordid
} else if (entry.getName().equalsIgnoreCase("/DownloadErrors.txt")) {
continue; // Skip error file, if present
}

actualCount++; // Increment the count for valid entries
}
} catch (IOException ixp) {
logger.error(
"There is an error while reading zip file contents in the getBundleZip test." + ixp.getMessage());
throw ixp;
}

// Assert the number of valid entries in the ZIP file
assertEquals(expectedCount, actualCount); // Expect exactly 2 valid files
}

@Rule
Expand Down Expand Up @@ -199,7 +221,7 @@ public int checkFilesinZip(Path filepath) throws IOException {

ZipEntry entry = entries.nextElement();
System.out.println("entryname:" + entry.getName());
if (!entry.getName().equalsIgnoreCase("/PackagingErrors.txt"))
if (!entry.getName().equalsIgnoreCase("/DownloadErrors.txt"))
count++;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testDownloadAllFiles()

assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.getHeaders().getFirst("Content-Type").startsWith("application/zip"));
assertEquals(59918, response.getBody().length());
assertEquals(59916, response.getBody().length());

}

Expand Down
Loading