making the images work as well
This commit is contained in:
@ -10,6 +10,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -112,6 +113,21 @@ public class Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
public void rewritePaths(Path pluginTarget) {
|
||||
try {
|
||||
for (Map.Entry<String, File> html : solutionLanguageFiles.entrySet()) {
|
||||
byte[] htmlFileAsBytes = Files.readAllBytes(Paths.get(html.getValue().toURI()));
|
||||
String htmlFile = new String(htmlFileAsBytes);
|
||||
htmlFile = htmlFile.replaceAll(this.lesson.getSimpleName() + "_files", pluginTarget.getFileName().toString() + "/lessons/plugin/SqlStringInjection/lessonSolutions/en/" + this.lesson.getSimpleName() + "_files");
|
||||
Files.write(Paths.get(html.getValue().toURI()), htmlFile.getBytes(), StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new PluginLoadingFailure("Unable to rewrite the paths in the solutions", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Class<AbstractLesson> getLesson() {
|
||||
return lesson;
|
||||
}
|
||||
|
@ -16,8 +16,9 @@ public class PluginBackgroundLoader implements ServletContextListener {
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent event) {
|
||||
String pluginPath = event.getServletContext().getRealPath("plugin_lessons");
|
||||
String targetPath = event.getServletContext().getRealPath("plugin_extracted");
|
||||
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
scheduler.scheduleAtFixedRate(new PluginsLoader(Paths.get(pluginPath)), 0, 5, TimeUnit.MINUTES);
|
||||
scheduler.scheduleAtFixedRate(new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)), 0, 5, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,22 +26,15 @@ import static org.owasp.webgoat.plugins.PluginFileUtils.createDirsIfNotExists;
|
||||
*/
|
||||
public class PluginExtractor {
|
||||
|
||||
private static final String DIRECTORY = "webgoat";
|
||||
private final Path pluginArchive;
|
||||
private final Map<String, byte[]> classes = new HashMap<String, byte[]>();
|
||||
private final Map<String, byte[]> classes = new HashMap<>();
|
||||
private final List<Path> files = new ArrayList<>();
|
||||
private Path baseDirectory;
|
||||
|
||||
public PluginExtractor(Path pluginArchive) {
|
||||
this.pluginArchive = pluginArchive;
|
||||
try {
|
||||
baseDirectory = createDirsIfNotExists(Paths.get(System.getProperty("java.io.tmpdir"), DIRECTORY));
|
||||
} catch (IOException io) {
|
||||
new Plugin.PluginLoadingFailure(format("Unable to create base directory: {}", pluginArchive.getFileName()), io);
|
||||
}
|
||||
}
|
||||
|
||||
public void extract() {
|
||||
public void extract(final Path target) {
|
||||
try (FileSystem zip = createZipFileSystem()) {
|
||||
final Path root = zip.getPath("/");
|
||||
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
|
||||
@ -52,7 +45,7 @@ public class PluginExtractor {
|
||||
Files.copy(file, bos);
|
||||
classes.put(file.toString(), bos.toByteArray());
|
||||
}
|
||||
files.add(Files.copy(file, createDirsIfNotExists(Paths.get(baseDirectory.toString(), file.toString())), REPLACE_EXISTING));
|
||||
files.add(Files.copy(file, createDirsIfNotExists(Paths.get(target.toString(), file.toString())), REPLACE_EXISTING));
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
@ -69,10 +62,6 @@ public class PluginExtractor {
|
||||
return this.files;
|
||||
}
|
||||
|
||||
public Path getBaseDirectory() {
|
||||
return this.baseDirectory;
|
||||
}
|
||||
|
||||
private FileSystem createZipFileSystem() throws IOException {
|
||||
final URI uri = URI.create("jar:file:" + pluginArchive.toUri().getPath());
|
||||
return FileSystems.newFileSystem(uri, new HashMap<String, Object>());
|
||||
|
@ -15,25 +15,29 @@ import java.util.List;
|
||||
public class PluginsLoader implements Runnable {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
private final Path path;
|
||||
private final Path pluginSource;
|
||||
private Path pluginTarget;
|
||||
|
||||
public PluginsLoader(Path path) {
|
||||
this.path = path;
|
||||
public PluginsLoader(Path pluginSource, Path pluginTarget) {
|
||||
this.pluginSource = pluginSource;
|
||||
this.pluginTarget = pluginTarget;
|
||||
}
|
||||
|
||||
public List<Plugin> loadPlugins(final boolean reload) {
|
||||
final List<Plugin> plugins = new ArrayList<Plugin>();
|
||||
try {
|
||||
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
|
||||
Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() {
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
try {
|
||||
PluginFileUtils.createDirsIfNotExists(pluginTarget);
|
||||
PluginExtractor extractor = new PluginExtractor(file);
|
||||
extractor.extract();
|
||||
Plugin plugin = new Plugin(extractor.getBaseDirectory());
|
||||
extractor.extract(pluginTarget);
|
||||
Plugin plugin = new Plugin(pluginTarget);
|
||||
plugin.loadClasses(extractor.getClasses());
|
||||
plugin.loadFiles(extractor.getFiles(), reload);
|
||||
plugin.rewritePaths(pluginTarget);
|
||||
plugins.add(plugin);
|
||||
} catch (Plugin.PluginLoadingFailure e) {
|
||||
logger.error("Unable to load plugin, continue loading others...");
|
||||
|
Reference in New Issue
Block a user