1 package com.github.searls.jasmine.server; 2 3 import com.github.searls.jasmine.config.JasmineConfiguration; 4 import com.github.searls.jasmine.io.RelativizesFilePaths; 5 import com.github.searls.jasmine.mojo.Context; 6 import com.github.searls.jasmine.runner.CreatesRunner; 7 import com.github.searls.jasmine.thirdpartylibs.ClassPathResourceHandler; 8 import com.github.searls.jasmine.thirdpartylibs.WebJarResourceHandler; 9 import org.apache.commons.lang3.StringUtils; 10 import org.eclipse.jetty.server.Handler; 11 import org.eclipse.jetty.server.handler.ContextHandler; 12 import org.eclipse.jetty.server.handler.ContextHandlerCollection; 13 import org.eclipse.jetty.server.handler.ResourceHandler; 14 15 import java.io.File; 16 import java.io.IOException; 17 18 public class ResourceHandlerConfigurator { 19 20 private final JasmineConfiguration configuration; 21 private final RelativizesFilePaths relativizesFilePaths; 22 private final CreatesRunner createsRunner; 23 24 public ResourceHandlerConfigurator(JasmineConfiguration configuration, 25 RelativizesFilePaths relativizesFilePaths, 26 CreatesRunner createsRunner) { 27 this.configuration = configuration; 28 this.relativizesFilePaths = relativizesFilePaths; 29 this.createsRunner = createsRunner; 30 } 31 32 public Handler createHandler() throws IOException { 33 ContextHandlerCollection contexts = new ContextHandlerCollection(); 34 35 for (Context context : this.configuration.getContexts()) { 36 String contextRoot = StringUtils.prependIfMissing(context.getContextRoot(), "/"); 37 ContextHandler handler = contexts.addContext(contextRoot, ""); 38 handler.setAliases(true); 39 handler.setHandler(this.createResourceHandler(true, context.getDirectory().getCanonicalPath(), null)); 40 } 41 42 ContextHandler rootContextHandler = contexts.addContext("/", ""); 43 rootContextHandler.setHandler(this.createResourceHandler(false, this.configuration.getBasedir().getCanonicalPath(), new String[]{this.getWelcomeFilePath()})); 44 rootContextHandler.setAliases(true); 45 46 ContextHandler classPathContextHandler = contexts.addContext("/classpath", ""); 47 classPathContextHandler.setHandler(new ClassPathResourceHandler(configuration.getProjectClassLoader())); 48 classPathContextHandler.setAliases(true); 49 50 ContextHandler webJarsContextHandler = contexts.addContext("/webjars", ""); 51 webJarsContextHandler.setHandler(new WebJarResourceHandler(configuration.getProjectClassLoader())); 52 webJarsContextHandler.setAliases(true); 53 54 return contexts; 55 } 56 57 private ResourceHandler createResourceHandler(boolean directory, String absolutePath, String[] welcomeFiles) { 58 ResourceHandler resourceHandler = new JasmineResourceHandler(this.createsRunner, this.configuration); 59 resourceHandler.setDirectoriesListed(directory); 60 if (welcomeFiles != null) { 61 resourceHandler.setWelcomeFiles(welcomeFiles); 62 } 63 resourceHandler.setResourceBase(absolutePath); 64 return resourceHandler; 65 } 66 67 private String getWelcomeFilePath() throws IOException { 68 return this.relativizesFilePaths.relativize(this.configuration.getBasedir(), this.configuration.getJasmineTargetDir()) + File.separator + createsRunner.getRunnerFile(); 69 } 70 }