♻️ Copy session-handling code to FreenetRequest
[Sone.git] / src / main / java / net / pterodactylus / sone / main / DefaultLoaders.java
1 package net.pterodactylus.sone.main;
2
3 import static net.pterodactylus.util.template.TemplateParser.parse;
4
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8 import java.io.UnsupportedEncodingException;
9
10 import net.pterodactylus.sone.web.WebInterface;
11 import net.pterodactylus.util.io.Closer;
12 import net.pterodactylus.util.template.ClassPathTemplateProvider;
13 import net.pterodactylus.util.template.Template;
14 import net.pterodactylus.util.template.TemplateProvider;
15 import net.pterodactylus.util.web.Page;
16 import net.pterodactylus.util.web.Request;
17 import net.pterodactylus.util.web.StaticPage;
18
19 /**
20  * Default {@link Loaders} implementation that loads resources from the classpath.
21  */
22 public class DefaultLoaders implements Loaders {
23
24         @Override
25         public Template loadTemplate(String path) {
26                 InputStream templateInputStream = null;
27                 Reader reader = null;
28                 try {
29                         templateInputStream = getClass().getResourceAsStream(path);
30                         reader = new InputStreamReader(templateInputStream, "UTF-8");
31                         return parse(reader);
32                 } catch (UnsupportedEncodingException uee1) {
33                         throw new RuntimeException("UTF-8 not supported.");
34                 } finally {
35                         Closer.close(reader);
36                         Closer.close(templateInputStream);
37                 }
38         }
39
40         @Override
41         public <REQ extends Request> Page<REQ> loadStaticPage(String pathPrefix, String basePath, String mimeType) {
42                 return new StaticPage<REQ>(pathPrefix, basePath, mimeType) {
43                 };
44         }
45
46         @Override
47         public TemplateProvider getTemplateProvider() {
48                 return new ClassPathTemplateProvider(WebInterface.class, "/templates/");
49         }
50
51 }