Skip to content

Commit e8317e4

Browse files
committed
make sure symlinks are included in dispatcher config ZIP file
1 parent cca702e commit e8317e4

1 file changed

Lines changed: 54 additions & 4 deletions

File tree

tooling/conga-aem-maven-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/maven/CloudManagerDispatcherConfigMojo.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121

2222
import java.io.File;
2323
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.util.Collections;
2427
import java.util.List;
28+
import java.util.Set;
2529

30+
import org.apache.commons.lang3.StringUtils;
2631
import org.apache.maven.plugin.MojoExecutionException;
2732
import org.apache.maven.plugin.MojoFailureException;
2833
import org.apache.maven.plugins.annotations.Component;
@@ -32,6 +37,7 @@
3237
import org.codehaus.plexus.archiver.ArchiverException;
3338
import org.codehaus.plexus.archiver.zip.ZipArchiver;
3439

40+
import io.wcm.devops.conga.generator.util.FileUtil;
3541
import io.wcm.devops.conga.plugins.aem.maven.model.ModelParser;
3642

3743
/**
@@ -72,16 +78,60 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7278
private void buildDispatcherConfig(File nodeDir) throws MojoFailureException {
7379
File targetFile = new File(getTargetDir(), nodeDir.getName() + ".dispatcher-config.zip");
7480

75-
zipArchiver.setDestFile(targetFile);
76-
String[] includes = new String[] { "**/*" };
77-
String[] excludes = new String[] { ModelParser.MODEL_FILE };
78-
zipArchiver.addDirectory(nodeDir, includes, excludes);
7981
try {
82+
String basePath = toZipDirectoryPath(nodeDir);
83+
addZipDirectory(basePath, nodeDir, Collections.singleton(ModelParser.MODEL_FILE));
84+
zipArchiver.setDestFile(targetFile);
85+
8086
zipArchiver.createArchive();
8187
}
8288
catch (ArchiverException | IOException ex) {
8389
throw new MojoFailureException("Unable to build file " + targetFile.getPath() + ": " + ex.getMessage(), ex);
8490
}
8591
}
8692

93+
/**
94+
* Recursive through all directory and add file to zipArchiver.
95+
* This method has special support for symlinks which are required for dispatcher configuration.
96+
* @param basePath Base path
97+
* @param directory Directory to include
98+
* @param excludeFiles Exclude filenames
99+
* @throws IOException I/O exception
100+
*/
101+
private void addZipDirectory(String basePath, File directory, Set<String> excludeFiles) throws IOException {
102+
String directoryPath = toZipDirectoryPath(directory);
103+
if (StringUtils.startsWith(directoryPath, basePath)) {
104+
String relativeDirectoryPath = StringUtils.substring(directoryPath, basePath.length());
105+
File[] files = directory.listFiles();
106+
if (files != null) {
107+
for (File file : files) {
108+
if (excludeFiles.contains(file.getName())) {
109+
continue;
110+
}
111+
if (file.isDirectory()) {
112+
addZipDirectory(basePath, file, Collections.emptySet());
113+
}
114+
else if (Files.isSymbolicLink(file.toPath())) {
115+
Path linkPath = file.toPath();
116+
Path targetPath = linkPath.toRealPath();
117+
Path symlinkPath = file.getParentFile().toPath().relativize(targetPath);
118+
zipArchiver.addSymlink(relativeDirectoryPath + file.getName(), sanitizePathSeparators(symlinkPath.toString()));
119+
}
120+
else {
121+
zipArchiver.addFile(file, relativeDirectoryPath + file.getName());
122+
}
123+
}
124+
}
125+
}
126+
}
127+
128+
private String toZipDirectoryPath(File directory) {
129+
String canoncialPath = FileUtil.getCanonicalPath(directory);
130+
return sanitizePathSeparators(canoncialPath) + "/";
131+
}
132+
133+
private String sanitizePathSeparators(String path) {
134+
return StringUtils.replace(path, "\\", "/");
135+
}
136+
87137
}

0 commit comments

Comments
 (0)