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.io.IOException; | ||||||
| import java.nio.file.Files; | import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||||
|  | import java.nio.file.Paths; | ||||||
| import java.nio.file.StandardOpenOption; | import java.nio.file.StandardOpenOption; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.List; | 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() { |     public Class<AbstractLesson> getLesson() { | ||||||
|         return lesson; |         return lesson; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -16,8 +16,9 @@ public class PluginBackgroundLoader implements ServletContextListener { | |||||||
|     @Override |     @Override | ||||||
|     public void contextInitialized(ServletContextEvent event) { |     public void contextInitialized(ServletContextEvent event) { | ||||||
|         String pluginPath = event.getServletContext().getRealPath("plugin_lessons"); |         String pluginPath = event.getServletContext().getRealPath("plugin_lessons"); | ||||||
|  |         String targetPath = event.getServletContext().getRealPath("plugin_extracted"); | ||||||
|         scheduler = Executors.newSingleThreadScheduledExecutor(); |         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 |     @Override | ||||||
|  | |||||||
| @ -26,22 +26,15 @@ import static org.owasp.webgoat.plugins.PluginFileUtils.createDirsIfNotExists; | |||||||
|  */ |  */ | ||||||
| public class PluginExtractor { | public class PluginExtractor { | ||||||
|  |  | ||||||
|     private static final String DIRECTORY = "webgoat"; |  | ||||||
|     private final Path pluginArchive; |     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 final List<Path> files = new ArrayList<>(); | ||||||
|     private Path baseDirectory; |  | ||||||
|  |  | ||||||
|     public PluginExtractor(Path pluginArchive) { |     public PluginExtractor(Path pluginArchive) { | ||||||
|         this.pluginArchive = 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()) { |         try (FileSystem zip = createZipFileSystem()) { | ||||||
|             final Path root = zip.getPath("/"); |             final Path root = zip.getPath("/"); | ||||||
|             Files.walkFileTree(root, new SimpleFileVisitor<Path>() { |             Files.walkFileTree(root, new SimpleFileVisitor<Path>() { | ||||||
| @ -52,7 +45,7 @@ public class PluginExtractor { | |||||||
|                         Files.copy(file, bos); |                         Files.copy(file, bos); | ||||||
|                         classes.put(file.toString(), bos.toByteArray()); |                         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; |                     return FileVisitResult.CONTINUE; | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
| @ -69,10 +62,6 @@ public class PluginExtractor { | |||||||
|         return this.files; |         return this.files; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public Path getBaseDirectory() { |  | ||||||
|         return this.baseDirectory; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private FileSystem createZipFileSystem() throws IOException { |     private FileSystem createZipFileSystem() throws IOException { | ||||||
|         final URI uri = URI.create("jar:file:" + pluginArchive.toUri().getPath()); |         final URI uri = URI.create("jar:file:" + pluginArchive.toUri().getPath()); | ||||||
|         return FileSystems.newFileSystem(uri, new HashMap<String, Object>()); |         return FileSystems.newFileSystem(uri, new HashMap<String, Object>()); | ||||||
|  | |||||||
| @ -15,25 +15,29 @@ import java.util.List; | |||||||
| public class PluginsLoader implements Runnable { | public class PluginsLoader implements Runnable { | ||||||
|  |  | ||||||
|     private final Logger logger = LoggerFactory.getLogger(this.getClass()); |     private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||||
|     private final Path path; |     private final Path pluginSource; | ||||||
|  |     private Path pluginTarget; | ||||||
|  |  | ||||||
|     public PluginsLoader(Path path) { |     public PluginsLoader(Path pluginSource, Path pluginTarget) { | ||||||
|         this.path = path; |         this.pluginSource = pluginSource; | ||||||
|  |         this.pluginTarget = pluginTarget; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public List<Plugin> loadPlugins(final boolean reload) { |     public List<Plugin> loadPlugins(final boolean reload) { | ||||||
|         final List<Plugin> plugins = new ArrayList<Plugin>(); |         final List<Plugin> plugins = new ArrayList<Plugin>(); | ||||||
|         try { |         try { | ||||||
|             Files.walkFileTree(path, new SimpleFileVisitor<Path>() { |             Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() { | ||||||
|  |  | ||||||
|                 @Override |                 @Override | ||||||
|                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||||||
|                     try { |                     try { | ||||||
|  |                         PluginFileUtils.createDirsIfNotExists(pluginTarget); | ||||||
|                         PluginExtractor extractor = new PluginExtractor(file); |                         PluginExtractor extractor = new PluginExtractor(file); | ||||||
|                         extractor.extract(); |                         extractor.extract(pluginTarget); | ||||||
|                         Plugin plugin = new Plugin(extractor.getBaseDirectory()); |                         Plugin plugin = new Plugin(pluginTarget); | ||||||
|                         plugin.loadClasses(extractor.getClasses()); |                         plugin.loadClasses(extractor.getClasses()); | ||||||
|                         plugin.loadFiles(extractor.getFiles(), reload); |                         plugin.loadFiles(extractor.getFiles(), reload); | ||||||
|  |                         plugin.rewritePaths(pluginTarget); | ||||||
|                         plugins.add(plugin); |                         plugins.add(plugin); | ||||||
|                     } catch (Plugin.PluginLoadingFailure e) { |                     } catch (Plugin.PluginLoadingFailure e) { | ||||||
|                        logger.error("Unable to load plugin, continue loading others..."); |                        logger.error("Unable to load plugin, continue loading others..."); | ||||||
|  | |||||||
| @ -281,42 +281,17 @@ public class Course { | |||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Load all of the filenames into a temporary cache |  | ||||||
|      * |  | ||||||
|      * @param context |  | ||||||
|      * @param path |  | ||||||
|      */ |  | ||||||
|     private void loadFiles(ServletContext context, String path) { |  | ||||||
|         logger.debug("Loading files into cache, path: " + path); |  | ||||||
|         Set resourcePaths = context.getResourcePaths(path); |  | ||||||
|         if (resourcePaths == null) { |  | ||||||
|             logger.error("Unable to load file cache for courses, this is probably a bug or configuration issue"); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         Iterator itr = resourcePaths.iterator(); |  | ||||||
|  |  | ||||||
|         while (itr.hasNext()) { |  | ||||||
|             String file = (String) itr.next(); |  | ||||||
|  |  | ||||||
|             if (file.length() != 1 && file.endsWith("/")) { |  | ||||||
|                 loadFiles(context, file); |  | ||||||
|             } else { |  | ||||||
|                 files.add(file); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void loadLessionFromPlugin(ServletContext context) { |     private void loadLessionFromPlugin(ServletContext context) { | ||||||
|  |         context.getContextPath(); | ||||||
|         logger.debug("Loading plugins into cache"); |         logger.debug("Loading plugins into cache"); | ||||||
|         String path = context.getRealPath("plugin_lessons"); |         String pluginPath = context.getRealPath("plugin_lessons"); | ||||||
|         if (path == null) { |         String targetPath = context.getRealPath("plugin_extracted"); | ||||||
|             logger.error("Plugins directory {} not found", path); |         if (pluginPath == null) { | ||||||
|  |             logger.error("Plugins directory {} not found", pluginPath); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         Path pluginDirectory = Paths.get(path); |         Path pluginDirectory = Paths.get(pluginPath); | ||||||
|         webgoatContext.setPluginDirectory(pluginDirectory); |         List<Plugin> plugins = new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).loadPlugins(false); | ||||||
|         List<Plugin> plugins = new PluginsLoader(pluginDirectory).loadPlugins(false); |  | ||||||
|         for (Plugin plugin : plugins) { |         for (Plugin plugin : plugins) { | ||||||
|             try { |             try { | ||||||
|                 Class<AbstractLesson> c = plugin.getLesson(); |                 Class<AbstractLesson> c = plugin.getLesson(); | ||||||
| @ -341,85 +316,6 @@ public class Course { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Instantiate all the lesson objects into a cache |  | ||||||
|      * |  | ||||||
|      * @deprecated should be removed if everything is loaded with plugins |  | ||||||
|      * @param path |  | ||||||
|      */ |  | ||||||
|     private void loadLessons(String path) { |  | ||||||
|         for (String file : files) { |  | ||||||
|             String className = getClassFile(file, path); |  | ||||||
|  |  | ||||||
|             if (className != null && !className.endsWith("_i")) { |  | ||||||
|                 try { |  | ||||||
|                     Class c = Class.forName(className); |  | ||||||
|                     Object o = c.newInstance(); |  | ||||||
|  |  | ||||||
|                     if (o instanceof AbstractLesson) { |  | ||||||
|                         AbstractLesson lesson = (AbstractLesson) o; |  | ||||||
|                         lesson.setWebgoatContext(webgoatContext); |  | ||||||
|  |  | ||||||
|                         lesson.update(properties); |  | ||||||
|  |  | ||||||
|                         if (lesson.getHidden() == false) { |  | ||||||
|                             lessons.add(lesson); |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } catch (Exception e) { |  | ||||||
|                     logger.error("Error in loadLessons: ", e); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private String getLanguageFromFileName(String first, String absoluteFile) { |  | ||||||
|         int p1 = absoluteFile.indexOf("/", absoluteFile.indexOf(first) + 1); |  | ||||||
|         int p2 = absoluteFile.indexOf("/", p1 + 1); |  | ||||||
|         String langStr = absoluteFile.substring(p1 + 1, p2); |  | ||||||
|  |  | ||||||
|         return langStr; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * For each lesson, set the source file and lesson file |  | ||||||
|      */ |  | ||||||
|     private void loadResources() { |  | ||||||
|         for (AbstractLesson lesson : lessons) { |  | ||||||
|             logger.info("Loading resources for lesson -> " + lesson.getName()); |  | ||||||
|             String className = lesson.getClass().getName(); |  | ||||||
|             String classFile = getSourceFile(className); |  | ||||||
|             logger.info("Lesson classname: " + className); |  | ||||||
|             logger.info("Lesson java file: " + classFile); |  | ||||||
|  |  | ||||||
|             for (String absoluteFile : files) { |  | ||||||
|                 String fileName = getFileName(absoluteFile); |  | ||||||
|                 //logger.debug("Course: looking at file: " + absoluteFile); |  | ||||||
|  |  | ||||||
|                 if (absoluteFile.endsWith(classFile)) { |  | ||||||
|                     logger.info("Set source file for " + classFile); |  | ||||||
|                     lesson.setSourceFileName(absoluteFile); |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 if (absoluteFile.startsWith("/lesson_plans") && absoluteFile.endsWith(".html") && className |  | ||||||
|                     .endsWith(fileName)) { |  | ||||||
|                     logger.info( |  | ||||||
|                         "setting lesson plan file " + absoluteFile + " for lesson " + lesson.getClass().getName()); |  | ||||||
|                     logger.info("fileName: " + fileName + " == className: " + className); |  | ||||||
|                     String language = getLanguageFromFileName("/lesson_plans", absoluteFile); |  | ||||||
|                     lesson.setLessonPlanFileName(language, absoluteFile); |  | ||||||
|                 } |  | ||||||
|                 if (absoluteFile.startsWith("/lesson_solutions") && absoluteFile.endsWith(".html") && className |  | ||||||
|                     .endsWith(fileName)) { |  | ||||||
|                     logger.info( |  | ||||||
|                         "setting lesson solution file " + absoluteFile + " for lesson " + lesson.getClass().getName()); |  | ||||||
|                     logger.info("fileName: " + fileName + " == className: " + className); |  | ||||||
|                     lesson.setLessonSolutionFileName(absoluteFile); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Description of the Method |      * Description of the Method | ||||||
|      * |      * | ||||||
| @ -431,10 +327,6 @@ public class Course { | |||||||
|         logger.info("Loading courses: " + path); |         logger.info("Loading courses: " + path); | ||||||
|         this.webgoatContext = webgoatContext; |         this.webgoatContext = webgoatContext; | ||||||
|         loadLessionFromPlugin(context); |         loadLessionFromPlugin(context); | ||||||
|         loadFiles(context, path); |  | ||||||
|         //loadLessons(path); |  | ||||||
|         loadResources(); |  | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -4,7 +4,6 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| import javax.servlet.http.HttpServlet; | import javax.servlet.http.HttpServlet; | ||||||
| import java.nio.file.Path; |  | ||||||
|  |  | ||||||
| public class WebgoatContext { | public class WebgoatContext { | ||||||
|  |  | ||||||
| @ -216,13 +215,4 @@ public class WebgoatContext { | |||||||
|     public String getDefaultLanguage() { |     public String getDefaultLanguage() { | ||||||
|         return defaultLanguage; |         return defaultLanguage; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public Path getPluginDirectory() { |  | ||||||
|         return pluginDirectory; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public void setPluginDirectory(Path pluginDirectory) { |  | ||||||
|         this.pluginDirectory = pluginDirectory; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -55,10 +55,6 @@ public class LabelProvider | |||||||
| 		return labels.get(locale).getString(strName); | 		return labels.get(locale).getString(strName); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public void addLabels() { |  | ||||||
|  |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	private class WebGoatResourceBundleController extends ResourceBundle.Control | 	private class WebGoatResourceBundleController extends ResourceBundle.Control | ||||||
| 	{ | 	{ | ||||||
| 		private final Locale fallbackLocale = new Locale(DEFAULT_LANGUAGE); | 		private final Locale fallbackLocale = new Locale(DEFAULT_LANGUAGE); | ||||||
|  | |||||||
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user