Lessons which load JavaScript etc now works
This commit is contained in:
@ -150,7 +150,7 @@ public class LegacyLoader {
|
||||
public void loadFiles(ServletContext context, String path) {
|
||||
logger.debug("Loading files into cache, path: " + path);
|
||||
Resource resource = new ClassPathResource("/");
|
||||
|
||||
//resource.get
|
||||
Set resourcePaths = null;
|
||||
if (resourcePaths == null) {
|
||||
logger.error("Unable to load file cache for courses, this is probably a bug or configuration issue");
|
||||
@ -216,7 +216,7 @@ public class LegacyLoader {
|
||||
// logger.error("Error in loadLessons: ", e);
|
||||
}
|
||||
}
|
||||
// loadResources(lessons);
|
||||
loadResources(lessons);
|
||||
return lessons;
|
||||
}
|
||||
|
||||
|
@ -52,8 +52,6 @@ public class Plugin {
|
||||
|
||||
private void findLesson(String name) {
|
||||
String realClassName = StringUtils.trimLeadingCharacter(name, '/').replaceAll("/", ".").replaceAll(".class", "");
|
||||
//TODO should be passed in (refactor)
|
||||
//TomcatEmbeddedWebappClassLoader cl = (TomcatEmbeddedWebappClassLoader) Thread.currentThread().getContextClassLoader();
|
||||
|
||||
try {
|
||||
Class clazz = classLoader.loadClass(realClassName);
|
||||
@ -95,19 +93,18 @@ public class Plugin {
|
||||
public void rewritePaths(Path pluginTarget) {
|
||||
try {
|
||||
replaceInFiles(this.lesson.getSimpleName() + "_files",
|
||||
pluginTarget.getFileName().toString() + "/plugin/" + this.lesson
|
||||
"plugin_lessons/plugin/" + this.lesson
|
||||
.getSimpleName() + "/lessonSolutions/en/" + this.lesson.getSimpleName() + "_files",
|
||||
solutionLanguageFiles.values());
|
||||
replaceInFiles(this.lesson.getSimpleName() + "_files",
|
||||
pluginTarget.getFileName().toString() + "/plugin/" + this.lesson
|
||||
"plugin_lessons/plugin/" + this.lesson
|
||||
.getSimpleName() + "/lessonPlans/en/" + this.lesson.getSimpleName() + "_files",
|
||||
lessonPlansLanguageFiles.values());
|
||||
|
||||
String[] replacements = {"jsp", "js"};
|
||||
for (String replacement : replacements) {
|
||||
String s = String.format("plugin/%s/%s/", this.lesson.getSimpleName(), replacement);
|
||||
String r = String.format("%s/plugin/%s/%s/", pluginTarget.getFileName().toString(),
|
||||
this.lesson.getSimpleName(), replacement);
|
||||
String r = String.format("plugin_lessons/plugin/s/%s/", this.lesson.getSimpleName(), replacement);
|
||||
replaceInFiles(s, r, pluginFiles);
|
||||
replaceInFiles(s, r, Arrays.asList(lessonSourceFile));
|
||||
}
|
||||
@ -115,7 +112,7 @@ public class Plugin {
|
||||
//CSS with url('/plugin/images') should not begin with / otherwise image cannot be found
|
||||
String s = String.format("/plugin/%s/images/", this.lesson.getSimpleName());
|
||||
String r = String
|
||||
.format("%s/plugin/%s/images/", pluginTarget.getFileName().toString(), this.lesson.getSimpleName());
|
||||
.format("plugin_lessons/plugin/%s/images/", this.lesson.getSimpleName());
|
||||
replaceInFiles(s, r, pluginFiles);
|
||||
replaceInFiles(s, r, Arrays.asList(lessonSourceFile));
|
||||
} catch (IOException e) {
|
||||
|
@ -5,82 +5,66 @@ import org.apache.commons.io.FileUtils;
|
||||
import org.owasp.webgoat.util.LabelProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletionService;
|
||||
import java.util.concurrent.ExecutorCompletionService;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
/**
|
||||
* <p>PluginsLoader class.</p>
|
||||
*
|
||||
* @version $Id: $Id
|
||||
* @author dm
|
||||
* @version $Id: $Id
|
||||
*/
|
||||
@Component
|
||||
public class PluginsLoader {
|
||||
|
||||
private static final String WEBGOAT_PLUGIN_EXTENSION = "jar";
|
||||
private static boolean alreadyLoaded = false;
|
||||
private static final int BUFFER_SIZE = 32 * 1024;
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
private final Path pluginSource;
|
||||
private Path pluginTarget;
|
||||
private final File pluginTargetDirectory;
|
||||
|
||||
/**
|
||||
* <p>Constructor for PluginsLoader.</p>
|
||||
*
|
||||
* @param pluginSource a {@link java.nio.file.Path} object.
|
||||
* @param pluginTarget a {@link java.nio.file.Path} object.
|
||||
*/
|
||||
public PluginsLoader(Path pluginSource, Path pluginTarget) {
|
||||
this.pluginSource = Objects.requireNonNull(pluginSource, "plugin source cannot be null");
|
||||
this.pluginTarget = Objects.requireNonNull(pluginTarget, "plugin target cannot be null");
|
||||
@Autowired
|
||||
public PluginsLoader(File pluginTargetDirectory) {
|
||||
this.pluginTargetDirectory = pluginTargetDirectory;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Copy jars to the lib directory
|
||||
// */
|
||||
// public void copyJars() {
|
||||
// try {
|
||||
// if (!alreadyLoaded) {
|
||||
// WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
|
||||
// // cl.setAntiJARLocking(true);
|
||||
// List<URL> jars = listJars();
|
||||
// for (URL jar : jars) {
|
||||
// // cl.setResources();
|
||||
// // cl.addRepository(jar.toString());
|
||||
// }
|
||||
// alreadyLoaded = true;
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Copying plugins failed", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* <p>loadPlugins.</p>
|
||||
*
|
||||
* @return a {@link java.util.List} object.
|
||||
*/
|
||||
public List<Plugin> loadPlugins() {
|
||||
// copyJars();
|
||||
List<Plugin> plugins = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
PluginFileUtils.createDirsIfNotExists(pluginTarget);
|
||||
cleanupExtractedPluginsDirectory();
|
||||
File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
|
||||
if (jarFile.isDirectory()) {
|
||||
extractToTempDirectoryFromExplodedDirectory(jarFile);
|
||||
} else {
|
||||
extractToTempDirectoryFromJarFile(jarFile);
|
||||
}
|
||||
List<URL> jars = listJars();
|
||||
|
||||
plugins = processPlugins(jars);
|
||||
} catch (Exception e) {
|
||||
logger.error("Loading plugins failed", e);
|
||||
@ -88,14 +72,49 @@ public class PluginsLoader {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
private void cleanupExtractedPluginsDirectory() {
|
||||
Path i18nDirectory = pluginTarget.resolve("plugin/i18n/");
|
||||
FileUtils.deleteQuietly(i18nDirectory.toFile());
|
||||
private void extractToTempDirectoryFromJarFile(File jarFile) throws IOException {
|
||||
JarFile jar = new JarFile(jarFile);
|
||||
Enumeration<? extends ZipEntry> entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry zipEntry = entries.nextElement();
|
||||
if (zipEntry.getName().contains("plugin_lessons") && zipEntry.getName().endsWith(".jar")) {
|
||||
unpack(jar, zipEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<URL> listJars() throws IOException {
|
||||
private void unpack(JarFile jar, ZipEntry zipEntry) throws IOException {
|
||||
try (InputStream inputStream = jar.getInputStream(zipEntry)) {
|
||||
String name = zipEntry.getName();
|
||||
if (name.lastIndexOf("/") != -1) {
|
||||
name = name.substring(name.lastIndexOf("/") + 1);
|
||||
}
|
||||
try (OutputStream outputStream = new FileOutputStream(new File(pluginTargetDirectory, name))) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void extractToTempDirectoryFromExplodedDirectory(File directory) throws IOException {
|
||||
Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
if (dir.endsWith("plugin_lessons")) {
|
||||
FileUtils.copyDirectory(dir.toFile(), pluginTargetDirectory);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<URL> listJars() throws Exception {
|
||||
final List<URL> jars = Lists.newArrayList();
|
||||
Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() {
|
||||
Files.walkFileTree(Paths.get(pluginTargetDirectory.toURI()), new SimpleFileVisitor<Path>() {
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
@ -115,17 +134,17 @@ public class PluginsLoader {
|
||||
final CompletionService<Plugin> completionService = new ExecutorCompletionService<>(executorService);
|
||||
final List<Callable<Plugin>> callables = extractJars(jars);
|
||||
|
||||
for (Callable<Plugin> s : callables) {
|
||||
completionService.submit(s);
|
||||
}
|
||||
callables.forEach(s -> completionService.submit(s));
|
||||
int n = callables.size();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
Plugin plugin = completionService.take().get();
|
||||
if (plugin.getLesson().isPresent()) {
|
||||
plugins.add(plugin);
|
||||
}
|
||||
}
|
||||
LabelProvider.updatePluginResources(pluginTarget.resolve("plugin/i18n/WebGoatLabels.properties"));
|
||||
LabelProvider.updatePluginResources(
|
||||
pluginTargetDirectory.toPath().resolve("plugin/i18n/WebGoatLabels.properties"));
|
||||
return plugins;
|
||||
} finally {
|
||||
executorService.shutdown();
|
||||
@ -141,7 +160,7 @@ public class PluginsLoader {
|
||||
classLoader.addURL(jar);
|
||||
extractorCallables.add(() -> {
|
||||
PluginExtractor extractor = new PluginExtractor();
|
||||
return extractor.extractJarFile(ResourceUtils.getFile(jar), pluginTarget.toFile(), classLoader);
|
||||
return extractor.extractJarFile(ResourceUtils.getFile(jar), pluginTargetDirectory, classLoader);
|
||||
});
|
||||
}
|
||||
return extractorCallables;
|
||||
|
Reference in New Issue
Block a user