Search in sources :

Example 1 with FileSystemBinding

use of ratpack.file.FileSystemBinding in project ratpack by ratpack.

the class AbstractPropertiesConfigSource method loadConfigData.

@Override
public ObjectNode loadConfigData(ObjectMapper objectMapper, FileSystemBinding fileSystemBinding) throws Exception {
    ObjectNode rootNode = objectMapper.createObjectNode();
    Properties properties = loadProperties();
    Stream<Pair<String, String>> pairs = properties.stringPropertyNames().stream().map(key -> Pair.of(key, properties.getProperty(key)));
    if (prefix.isPresent()) {
        pairs = pairs.filter(p -> p.left.startsWith(prefix.get())).map(((Function<Pair<String, String>, Pair<String, String>>) p -> p.mapLeft(s -> s.substring(prefix.get().length()))).toFunction());
    }
    pairs.forEach(p -> populate(rootNode, p.left, p.right));
    return rootNode;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Function(ratpack.func.Function) Properties(java.util.Properties) Stream(java.util.stream.Stream) FileSystemBinding(ratpack.file.FileSystemBinding) ConfigSource(ratpack.config.ConfigSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Optional(java.util.Optional) Pair(ratpack.func.Pair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) Function(ratpack.func.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Properties(java.util.Properties) Pair(ratpack.func.Pair)

Example 2 with FileSystemBinding

use of ratpack.file.FileSystemBinding in project ratpack by ratpack.

the class TextTemplateModule method provideGroovyTemplateRenderingEngine.

@Provides
@Singleton
TextTemplateRenderingEngine provideGroovyTemplateRenderingEngine(ServerConfig serverConfig, ExecController execController, ByteBufAllocator bufferAllocator, Config config) {
    String templatesPath = config.getTemplatesPath();
    FileSystemBinding templateDir = serverConfig.getBaseDir().binding(templatesPath);
    if (templateDir == null) {
        throw new IllegalStateException("templatesPath '" + templatesPath + "' is outside the file system binding");
    }
    return new TextTemplateRenderingEngine(bufferAllocator, templateDir, serverConfig.isDevelopment(), config.staticallyCompile);
}
Also used : FileSystemBinding(ratpack.file.FileSystemBinding) TextTemplateRenderingEngine(ratpack.groovy.template.internal.TextTemplateRenderingEngine) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 3 with FileSystemBinding

use of ratpack.file.FileSystemBinding in project ratpack by ratpack.

the class FileSystemChecksumServices method service.

/**
 *  Get checksum service for additional path related to server's base dir and calculation method as checksummer function.
 *  Apply file filtering by extension: <i>js</i>, <i>css</i>, <i>png</i> filter files in target path.
 *  If checksummer function is not provided then noop method is used (no checksum calculation).
 *  If additional path is not given then server's base dir is used.
 *  If additional path is not correct path definition (contains illegal characters) or is not existing directory, IllegalArgumentException is thrown.
 *  If fileEndsWith variable length argument is not given then checksum may be calculated for file with any extension.
 *  If fileEndsWith is given and file's extension no match NoSuchFileException is thrown.
 *
 *  @param serverConfig server configuration. The most important parameter is baseDir. File path taken as parameter to checksummer is calculated relative to baseDir and additional path.
 *  @param checksummerFunc checksum calculation function
 *  @param path additional path calculated relative to server's base dir. Becomes root for checksummer function.
 *  @param fileEndsWith variable length array of extenstions filtering files in target path
 *  @return file system checksummer service
 */
public static FileSystemChecksumService service(ServerConfig serverConfig, Function<? super InputStream, ? extends String> checksummerFunc, String path, String... fileEndsWith) {
    Function<? super InputStream, ? extends String> checksummer = checksummerFunc != null ? checksummerFunc : noopChecksummer();
    FileSystemBinding fsb = path != null ? serverConfig.getBaseDir().binding(path) : serverConfig.getBaseDir();
    List<String> exts = Arrays.asList(fileEndsWith);
    if (fsb == null || !Files.isDirectory(fsb.getFile())) {
        throw new IllegalArgumentException("Non existing path related to server's base dir.");
    }
    DefaultFileSystemChecksumService service = new DefaultFileSystemChecksumService(fsb, checksummer, exts);
    if (serverConfig.isDevelopment()) {
        return service;
    } else {
        CachingFileSystemChecksumService cachingService = new CachingFileSystemChecksumService(service);
        new FileSystemChecksumServicePopulater(fsb.getFile(), exts, cachingService, Executors.newFixedThreadPool(5), 4).start();
        return cachingService;
    }
}
Also used : FileSystemBinding(ratpack.file.FileSystemBinding)

Example 4 with FileSystemBinding

use of ratpack.file.FileSystemBinding in project ratpack by ratpack.

the class FileSystemBindingHandler method handle.

public void handle(Context context) throws ExecutionException {
    FileSystemBinding parentBinding = context.get(FileSystemBinding.class);
    FileSystemBinding binding = parentBinding.binding(path);
    if (binding == null) {
        context.clientError(404);
    } else {
        context.insert(CACHE.get(binding, FileSystemBindingHandler::registry), handler);
    }
}
Also used : FileSystemBinding(ratpack.file.FileSystemBinding)

Aggregations

FileSystemBinding (ratpack.file.FileSystemBinding)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Stream (java.util.stream.Stream)1 ConfigSource (ratpack.config.ConfigSource)1 Function (ratpack.func.Function)1 Pair (ratpack.func.Pair)1 TextTemplateRenderingEngine (ratpack.groovy.template.internal.TextTemplateRenderingEngine)1