/** * Resolves a URL of the resource specified. The resource path should be absolute, as there is no hint provided, * where to start the search. The resource is looked up using either the context classloader or the classloader * of the resolver itself. * @param resource * the resource to be resolved * @return * the URL of the resource or null if it wasnt found. * @throws java.lang.AssertionError * If the resolver is configured to fail on missing resource */ public URL resolve(String resource){ return resolve(resource, getClass()); }
public TemporaryFileBuilder(final TemporaryFolder folder, final String fileName) { this.folder = folder; this.filename = fileName; this.resolver = new ResourceResolver(true); }
/** * Resolves a URL of the resource specified using the provided class as hint to start the search. For searching * the resource the consumer's classloader is used. * @param resource * the path the resource. The path can be either absolute or relative to the consumers location. * @param consumer * the consumer class of the resource. It's classloader is used for the lookup and it's location is used * as reference point for resolving relative path names * @return */ public URL resolve(String resource, Class consumer){ final ClassLoader ccl = Thread.currentThread().getContextClassLoader(); final String resourcePath = resolvePath(resource, consumer); URL contentUrl = null; if (ccl != null) { contentUrl = ccl.getResource(resourcePath); } if (contentUrl == null) { contentUrl = consumer.getResource(resourcePath); } if(failOnMissingResource && contentUrl == null){ throw new AssertionError("Resource " + resource + " not found"); } return contentUrl; }
/** * Defines a ZIP resource on the classpath that provides the static content the server should host. * @param contextRoot * the root path to the content * @param contentResource * the name of the classpath resource denoting a file that should be hosted. If the file denotes a zip file, its * content is hosted instead of the file itself. * The path may be absolute or relative to the caller of the method. * @return * this builder */ public HttpServerBuilder contentFrom(String contextRoot, String contentResource){ URL resource = resolver.resolve(contentResource,CallStack.getCallerClass()); resources.put(contextRoot, resource); return this; }
public ZipFileBuilder(final TemporaryFolder folder, String filename) { this.folder = folder; this.filename = filename; this.entryMap = new HashMap<>(); this.resolver = new ResourceResolver(true); }
/** * Defines the classpath resource from where the content of the file should be retrieved * * @param pathToResource * the path to the classpath resource * @return the builder */ public TemporaryFileBuilder fromClasspathResource(final String pathToResource) { final Class<?> callerClass = CallStack.getCallerClass(); this.content = getResolver().resolve(pathToResource, callerClass); return this; }
/** * Adds an entry to the zip file from a classpath resource. * * @param zipEntryPath * the path of the entry in the zip file. If the path denotes a path (ends with '/') the resource is put * under its own name on that location. If it denotes a file, it will be put as this file into the zip. Note * that even if the path ist defined absolute, starting with a '/', the created entry in the zip file won't * start with a '/' * @param pathToResource * the path to the resource in the classpath * * @return this builder */ public ZipFileBuilder addClasspathResource(final String zipEntryPath, final String pathToResource) { final Class<?> callerClass = getCallerClass(); final URL resource = resolver.resolve(pathToResource, callerClass); addResource(zipEntryPath, resource); return this; }