| 1 | package fr.sii.ogham.template.freemarker.adapter; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.io.IOException; | |
| 5 | import java.security.AccessController; | |
| 6 | import java.security.PrivilegedActionException; | |
| 7 | import java.security.PrivilegedExceptionAction; | |
| 8 | ||
| 9 | import fr.sii.ogham.core.resource.resolver.DelegateResourceResolver; | |
| 10 | import fr.sii.ogham.core.resource.resolver.FileResolver; | |
| 11 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 12 | import fr.sii.ogham.template.exception.ResolverAdapterConfigurationException; | |
| 13 | import freemarker.cache.FileTemplateLoader; | |
| 14 | import freemarker.cache.TemplateLoader; | |
| 15 | ||
| 16 | /** | |
| 17 | * Adapter that converts general {@link FileResolver} into FreeMarker specific | |
| 18 | * {@link FileTemplateLoader}. | |
| 19 | * | |
| 20 | * @author Cyril Dejonghe | |
| 21 | * | |
| 22 | */ | |
| 23 | public class FileResolverAdapter extends AbstractFreeMarkerTemplateLoaderOptionsAdapter { | |
| 24 | private final File baseDir; | |
| 25 | | |
| 26 | public FileResolverAdapter() { | |
| 27 | this(new File("/")); | |
| 28 | } | |
| 29 | ||
| 30 | public FileResolverAdapter(File baseDir) { | |
| 31 | super(); | |
| 32 | this.baseDir = baseDir; | |
| 33 | } | |
| 34 | ||
| 35 | @Override | |
| 36 | public boolean supports(ResourceResolver resolver) { | |
| 37 |
2
1. supports : negated conditional → NO_COVERAGE 2. supports : negated conditional → KILLED |
ResourceResolver actualResolver = resolver instanceof DelegateResourceResolver ? ((DelegateResourceResolver) resolver).getActualResourceResolver() : resolver; |
| 38 |
4
1. supports : replaced boolean return with false for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::supports → NO_COVERAGE 2. supports : replaced boolean return with true for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::supports → NO_COVERAGE 3. supports : replaced boolean return with false for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::supports → KILLED 4. supports : replaced boolean return with true for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::supports → KILLED |
return actualResolver instanceof FileResolver; |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public TemplateLoader adapt(ResourceResolver resolver) throws ResolverAdapterConfigurationException { | |
| 43 | try { | |
| 44 |
2
1. adapt : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::adapt → NO_COVERAGE 2. adapt : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter::adapt → KILLED |
return new FileTemplateLoaderAllowingAbsolutePaths(baseDir); |
| 45 | } catch (IOException e) { | |
| 46 | throw new ResolverAdapterConfigurationException("Invalid configuration for " + FileTemplateLoader.class.getSimpleName(), resolver, e); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | private static class FileTemplateLoaderAllowingAbsolutePaths extends FileTemplateLoader { | |
| 51 | private static final boolean SEP_IS_SLASH = File.separatorChar == '/'; | |
| 52 | | |
| 53 | public FileTemplateLoaderAllowingAbsolutePaths(File baseDir) throws IOException { | |
| 54 | super(baseDir, true); | |
| 55 | } | |
| 56 | ||
| 57 | @Override | |
| 58 | public Object findTemplateSource(String name) throws IOException { | |
| 59 | // TODO: add security option to enable/disable absolute paths outside of baseDir | |
| 60 | try { | |
| 61 |
2
1. findTemplateSource : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter$FileTemplateLoaderAllowingAbsolutePaths::findTemplateSource → NO_COVERAGE 2. findTemplateSource : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter$FileTemplateLoaderAllowingAbsolutePaths::findTemplateSource → KILLED |
return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() { |
| 62 | @Override | |
| 63 | public File run() throws IOException { | |
| 64 | File source = new File(name); | |
| 65 |
4
1. run : negated conditional → SURVIVED 2. run : negated conditional → NO_COVERAGE 3. run : negated conditional → NO_COVERAGE 4. run : negated conditional → KILLED |
if (source.isAbsolute() && source.isFile()) { |
| 66 |
2
1. run : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter$FileTemplateLoaderAllowingAbsolutePaths$1::run → NO_COVERAGE 2. run : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter$FileTemplateLoaderAllowingAbsolutePaths$1::run → KILLED |
return source; |
| 67 | } | |
| 68 |
2
1. run : negated conditional → SURVIVED 2. run : negated conditional → NO_COVERAGE |
source = new File(baseDir, SEP_IS_SLASH ? name : name.replace('/', File.separatorChar)); |
| 69 |
2
1. run : negated conditional → NO_COVERAGE 2. run : negated conditional → KILLED |
if (!source.isFile()) { |
| 70 | return null; | |
| 71 | } | |
| 72 |
1
1. run : replaced return value with null for fr/sii/ogham/template/freemarker/adapter/FileResolverAdapter$FileTemplateLoaderAllowingAbsolutePaths$1::run → NO_COVERAGE |
return source; |
| 73 | } | |
| 74 | }); | |
| 75 | } catch (PrivilegedActionException e) { | |
| 76 | throw (IOException) e.getException(); | |
| 77 | } | |
| 78 | } | |
| 79 | } | |
| 80 | } | |
Mutations | ||
| 37 |
1.1 2.2 |
|
| 38 |
1.1 2.2 3.3 4.4 |
|
| 44 |
1.1 2.2 |
|
| 61 |
1.1 2.2 |
|
| 65 |
1.1 2.2 3.3 4.4 |
|
| 66 |
1.1 2.2 |
|
| 68 |
1.1 2.2 |
|
| 69 |
1.1 2.2 |
|
| 72 |
1.1 |