/** * 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; }
/** * Returns the caller class of the calling method.<br> For example: <br> A.calling() -> B.called() B.called() * -> * getCallerClass(): A <br> If a thread context classloader is defined, it will be used for loading the class, * otherwise the default class loader is used. * * @return the class of the calling method's class */ public static Class<?> getCallerClass() { final StackTraceElement[] stElements = Thread.currentThread().getStackTrace(); try { final StackTraceElement caller = findCaller(stElements); return loadClass(caller.getClassName()); } catch (ClassNotFoundException e) { throw new RuntimeException("Could not determine caller class", e); } }
/** * 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; }