Also extract html files

(cherry picked from commit 2933b79)
This commit is contained in:
nbaars 2015-01-02 15:06:03 +01:00
parent 720040d1f8
commit 0d48b83e82
4 changed files with 91 additions and 31 deletions

View File

@ -2,13 +2,18 @@ package org.owasp.webgoat.plugins;
import org.owasp.webgoat.lessons.AbstractLesson; import org.owasp.webgoat.lessons.AbstractLesson;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
public class Plugin { public class Plugin {
private final Class<AbstractLesson> lesson; private final Class<AbstractLesson> lesson;
private final String lessonPlanHtml; private final Path pluginDirectory;
private final String lessonSolutionHtml;
public static class Builder { public static class Builder {
@ -36,19 +41,23 @@ public class Plugin {
} }
public Plugin build() { public Plugin build() {
return new Plugin(this.lesson, null, null); return new Plugin(this.lesson, pluginDirectory);
} }
} }
public Plugin(Class<AbstractLesson> lesson, String lessonPlanHtml, String lessonSolutionHtml) { public Plugin(Class<AbstractLesson> lesson, Path pluginDirectory) {
this.lesson = lesson; this.lesson = lesson;
this.lessonPlanHtml = lessonPlanHtml; this.pluginDirectory = pluginDirectory;
this.lessonSolutionHtml = lessonSolutionHtml;
} }
public String getLessonPlanHtml() { public String getLessonPlanHtml() {
return lessonPlanHtml; Path lesson_plans = this.pluginDirectory.resolve("lesson_plans");
try {
Files.readAllLines(lesson_plans.resolve(this.lesson.getSimpleName() + ".html"), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
return lesson_plans.resolve(this.lesson.getSimpleName() + ".html").toFile().toString();
} }
public Class<AbstractLesson> getLesson() { public Class<AbstractLesson> getLesson() {
@ -56,6 +65,17 @@ public class Plugin {
} }
public String getLessonSolutionHtml() { public String getLessonSolutionHtml() {
return lessonSolutionHtml; return null;
//return lessonSolutionHtml;
} }
public static void main(String[] args) throws Exception {
Path tempDir = Files.createTempDirectory("tempfiles");
Path tempFile = Files.createTempFile(tempDir, "tempfiles", ".tmp");
List<String> lines = Arrays.asList("Line1", "Line2");
Files.write(tempFile, lines, Charset.defaultCharset(), StandardOpenOption.WRITE);
System.out.printf("Wrote text to temporary file %s%n", tempFile.toString());
}
} }

View File

@ -12,12 +12,15 @@ import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.HashMap;
/** /**
* Extract the zip file and place the files in a temp directory * Extract the zip file and place the files in a temp directory
*
* TODO: should only do the extraction of the zip file return should be the base directory of the extracted
* plugin. The PluginLoader should take care of the loading
*
*/ */
public class PluginExtractor { public class PluginExtractor {
@ -46,7 +49,7 @@ public class PluginExtractor {
Files.copy(file, bos); Files.copy(file, bos);
pluginBuilder.loadClass(file.toString(), bos.toByteArray()); pluginBuilder.loadClass(file.toString(), bos.toByteArray());
} }
Files.copy(file, tempDirectory, StandardCopyOption.REPLACE_EXISTING); // Files.copy(file, tempDirectory.resolve(file.getFileName()), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });

View File

@ -1,32 +1,24 @@
package org.owasp.webgoat.session; package org.owasp.webgoat.session;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.LinkedList;
import javax.servlet.ServletContext;
import org.owasp.webgoat.HammerHead; import org.owasp.webgoat.HammerHead;
import org.owasp.webgoat.lessons.AbstractLesson; import org.owasp.webgoat.lessons.AbstractLesson;
import org.owasp.webgoat.lessons.Category; import org.owasp.webgoat.lessons.Category;
import org.owasp.webgoat.plugins.Plugin; import org.owasp.webgoat.plugins.Plugin;
import org.owasp.webgoat.plugins.PluginExtractor;
import org.owasp.webgoat.plugins.PluginsLoader; import org.owasp.webgoat.plugins.PluginsLoader;
import org.owasp.webgoat.util.WebGoatI18N;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/** /**
* ************************************************************************************************* * *************************************************************************************************
* <p/> * <p/>
@ -334,16 +326,56 @@ public class Course {
if (lesson.getHidden() == false) { if (lesson.getHidden() == false) {
lessons.add(lesson); lessons.add(lesson);
} }
lesson.setLessonSolutionFileName(plugin.getLessonPlanHtml());
} catch (Exception e) { } catch (Exception e) {
logger.error("Error in loadLessons: ", e); logger.error("Error in loadLessons: ", e);
} }
} }
} }
/**
* For each lesson, set the source file and lesson file
*/
private void loadResourcesFromPlugin() {
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);
}
}
}
}
/** /**
* Instantiate all the lesson objects into a cache * Instantiate all the lesson objects into a cache
* *
* @deprecated should be removed if everything is loaded with plugins
* @param path * @param path
*/ */
private void loadLessons(String path) { private void loadLessons(String path) {

View File

@ -13,6 +13,11 @@ log4j.appender.MAIN_LOG.append=true
# a little less spring output # a little less spring output
log4j.category.org.springframework = INFO log4j.category.org.springframework = INFO
log4j.appender.default.out=org.apache.log4j.ConsoleAppender
log4j.appender.default.out.threeshold=DEBUG
log4j.appender.default.out.layout=org.apache.log4j.PatternLayout
log4j.appender.default.out.layout.ConversionPattern=%-5p %c: %m%n
# ERROR # ERROR
log4j.appender.ERROR_LOG=org.apache.log4j.RollingFileAppender log4j.appender.ERROR_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR_LOG.File=${catalina.home}/logs/webgoat_error.log log4j.appender.ERROR_LOG.File=${catalina.home}/logs/webgoat_error.log