2 * DemosceneMusic - TemplateServlet.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.core;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.net.URISyntaxException;
27 import java.util.Enumeration;
28 import java.util.HashMap;
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServlet;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
37 import net.pterodactylus.demoscenemusic.page.ServletRequest;
38 import net.pterodactylus.util.io.Closer;
39 import net.pterodactylus.util.template.HtmlFilter;
40 import net.pterodactylus.util.template.ReflectionAccessor;
41 import net.pterodactylus.util.template.Template;
42 import net.pterodactylus.util.template.TemplateContextFactory;
43 import net.pterodactylus.util.template.TemplateParser;
44 import net.pterodactylus.util.web.Header;
45 import net.pterodactylus.util.web.Page;
46 import net.pterodactylus.util.web.Response;
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class TemplateServlet extends HttpServlet {
56 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
58 private final Map<String, Page<ServletRequest>> pages = new HashMap<String, Page<ServletRequest>>();
68 public void init(ServletConfig config) throws ServletException {
70 core = (Core) config.getServletContext().getAttribute("core");
72 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
74 templateContextFactory.addFilter("html", new HtmlFilter());
76 templateContextFactory.addTemplateObject("core", core);
77 templateContextFactory.addTemplateObject("dataManager", core.getDataManager());
79 for (@SuppressWarnings("rawtypes")
80 Enumeration parameterNames = config.getInitParameterNames(); parameterNames.hasMoreElements();) {
81 String pageName = (String) parameterNames.nextElement();
82 Template template = getTemplate(pageName);
83 String pageClassName = config.getInitParameter(pageName);
85 Class<?> pageClass = Class.forName(pageClassName);
86 @SuppressWarnings("unchecked")
87 Page<ServletRequest> page = (Page<ServletRequest>) pageClass.getConstructor(Core.class, TemplateContextFactory.class, Template.class).newInstance(core, templateContextFactory, template);
88 pages.put(pageName, page);
89 } catch (ClassNotFoundException cnfe1) {
90 cnfe1.printStackTrace();
91 } catch (IllegalArgumentException iae1) {
92 iae1.printStackTrace();
93 } catch (SecurityException se1) {
94 se1.printStackTrace();
95 } catch (InstantiationException ie1) {
96 ie1.printStackTrace();
97 } catch (IllegalAccessException iae1) {
98 iae1.printStackTrace();
99 } catch (InvocationTargetException ite1) {
100 ite1.printStackTrace();
101 } catch (NoSuchMethodException nsme1) {
102 nsme1.printStackTrace();
111 protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
112 String path = request.getPathInfo();
113 if (path.startsWith("/")) {
114 path = path.substring(1);
116 Page<ServletRequest> page = pages.get(path);
118 ServletRequest servletRequest;
120 servletRequest = new ServletRequest(request);
121 } catch (URISyntaxException use1) {
122 throw new IOException("Could not create URI from " + request.getRequestURI(), use1);
124 Response response = new Response(httpServletResponse.getOutputStream());
125 response = page.handleRequest(servletRequest, response);
126 for (Header header : response.getHeaders()) {
127 for (String value : header) {
128 httpServletResponse.addHeader(header.getName(), value);
131 httpServletResponse.setContentType(response.getContentType());
132 httpServletResponse.setStatus(response.getStatusCode());
141 private Template getTemplate(String pageName) {
142 InputStream templateInputStream = getClass().getResourceAsStream("/templates/" + pageName);
143 if (templateInputStream == null) {
146 Reader templateReader = null;
148 templateReader = new InputStreamReader(templateInputStream, "UTF-8");
149 return TemplateParser.parse(templateReader);
150 } catch (UnsupportedEncodingException uee1) {
151 /* can not happen. */
152 throw new RuntimeException("Could not parse templates!", uee1);
154 Closer.close(templateReader);
155 Closer.close(templateInputStream);