diff --git a/src/main/java/it/inaf/ia2/transfer/service/ArchiveService.java b/src/main/java/it/inaf/ia2/transfer/service/ArchiveService.java index 8b07c73a5768ae27f6343f97ec24508bc4bf03a6..3c9e767758a4ebacb1a3b5f745c753f3d09f0f52 100644 --- a/src/main/java/it/inaf/ia2/transfer/service/ArchiveService.java +++ b/src/main/java/it/inaf/ia2/transfer/service/ArchiveService.java @@ -114,7 +114,7 @@ public class ArchiveService { // support directory used to generate folder inside tar files (path is redefined each time by TarEntry class) File supportDir = Files.createTempDirectory("dir").toFile(); - try (ArchiveHandler<O, E> handler = getArchiveHandler(archiveFile, job.getType())) { + try ( ArchiveHandler<O, E> handler = getArchiveHandler(archiveFile, job.getType())) { fillArchive(entryDescriptors, commonParent, supportDir, job.getPrincipal(), servletRequest, handler); } finally { @@ -133,10 +133,10 @@ public class ArchiveService { // it will be initialized only when necessary Map<Integer, String> portalLocationUrls = null; - - List<ArchiveEntryDescriptor> noTargetEntryDescriptors = - entryDescriptors.stream().filter(ed -> !ed.isPointingToAnotherNode()) - .collect(Collectors.toList()); + + List<ArchiveEntryDescriptor> noTargetEntryDescriptors + = entryDescriptors.stream().filter(ed -> !ed.isPointingToAnotherNode()) + .collect(Collectors.toList()); // Start with archive entry descriptors which don't point to another node List<String> vosPaths = noTargetEntryDescriptors.stream() @@ -146,16 +146,16 @@ public class ArchiveService { if (!vosPaths.isEmpty()) { for (FileInfo fileInfo : fileDAO.getArchiveFileInfos(vosPaths)) { - String relPath = fileInfo.getVirtualPath().substring(commonParent.length()); + String relPath = fileInfo.getVirtualPath().substring(commonParent.length()); this.insertEntryIntoArchive(fileInfo, supportDir, relPath, tokenPrincipal, portalLocationUrls, servletRequest, handler); } } - - List<ArchiveEntryDescriptor> pointingEntryDescriptors = - entryDescriptors.stream().filter(ed -> ed.isPointingToAnotherNode()) - .collect(Collectors.toList()); + + List<ArchiveEntryDescriptor> pointingEntryDescriptors + = entryDescriptors.stream().filter(ed -> ed.isPointingToAnotherNode()) + .collect(Collectors.toList()); // Now archive entry descriptors pointing to another node List<String> targetNodesVosPaths = pointingEntryDescriptors.stream() @@ -164,18 +164,18 @@ public class ArchiveService { if (!targetNodesVosPaths.isEmpty()) { for (FileInfo fileInfo : fileDAO.getArchiveFileInfos(targetNodesVosPaths)) { - + // relPaths is calculated from base node String targetNodeVosPath = fileInfo.getVirtualPath(); List<String> linkVosPaths = pointingEntryDescriptors.stream() - .filter(ed->ed.getTargetNodeVosPath().equals(targetNodeVosPath)) - .map(ed->ed.getVosPath()) + .filter(ed -> ed.getTargetNodeVosPath().equals(targetNodeVosPath)) + .map(ed -> ed.getVosPath()) .collect(Collectors.toList()); - - for(String vosPath : linkVosPaths) { - String relPath = vosPath.substring(commonParent.length()); - - this.insertEntryIntoArchive(fileInfo, supportDir, relPath, tokenPrincipal, portalLocationUrls, servletRequest, handler); + + for (String vosPath : linkVosPaths) { + String relPath = vosPath.substring(commonParent.length()); + + this.insertEntryIntoArchive(fileInfo, supportDir, relPath, tokenPrincipal, portalLocationUrls, servletRequest, handler); } } } @@ -191,11 +191,15 @@ public class ArchiveService { return; } - // I expect only external links - // local links have been resolved before calling this endpoint + // I retrieve only external links + // local links have been resolved before calling this endpoint + // TODO: we need to discuss about internal links in container nodes if (fileInfo.isLink()) { - downloadExternalLinkIntoArchive(fileInfo, relPath, - tokenPrincipal, handler, servletRequest); + String target = fileInfo.getTarget(); + if (!target.startsWith("vos://")) { + downloadExternalLinkIntoArchive(fileInfo, relPath, + tokenPrincipal, handler, servletRequest); + } return; } @@ -255,9 +259,9 @@ public class ArchiveService { List<String> vosPaths = entryDescriptors.stream().map(ed -> ed.getVosPath()) .collect(Collectors.toList()); - - if(vosPaths.size() == 1) { - String vosPath = vosPaths.get(0); + + if (vosPaths.size() == 1) { + String vosPath = vosPaths.get(0); return vosPath.substring(0, vosPath.lastIndexOf("/")); } @@ -318,7 +322,7 @@ public class ArchiveService { } @Override - public TarEntry getEntry(File file, String path) { + public TarEntry getEntry(File file, String path) { return new TarEntry(file, path); } @@ -370,10 +374,10 @@ public class ArchiveService { } }, res -> { File tmpFile = Files.createTempFile("download", null).toFile(); - try (FileOutputStream os = new FileOutputStream(tmpFile)) { + try ( FileOutputStream os = new FileOutputStream(tmpFile)) { res.getBody().transferTo(os); handler.putNextEntry(tmpFile, relPath); - try (FileInputStream is = new FileInputStream(tmpFile)) { + try ( FileInputStream is = new FileInputStream(tmpFile)) { is.transferTo(handler.getOutputStream()); } } finally { @@ -427,7 +431,7 @@ public class ArchiveService { File file = new File(fileInfo.getFilePath()); LOG.trace("Adding file " + file.getAbsolutePath() + " to tar archive"); - try (InputStream is = new FileInputStream(file)) { + try ( InputStream is = new FileInputStream(file)) { handler.putNextEntry(file, relPath); is.transferTo(handler.getOutputStream()); }