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.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.io.UnsupportedEncodingException;
27 import java.lang.reflect.InvocationTargetException;
28 import java.net.URISyntaxException;
29 import java.util.Enumeration;
30 import java.util.HashMap;
33 import javax.servlet.ServletConfig;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServlet;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
39 import net.pterodactylus.demoscenemusic.page.ServletRequest;
40 import net.pterodactylus.util.io.Closer;
41 import net.pterodactylus.util.io.StreamCopier;
42 import net.pterodactylus.util.template.HtmlFilter;
43 import net.pterodactylus.util.template.ReflectionAccessor;
44 import net.pterodactylus.util.template.Template;
45 import net.pterodactylus.util.template.TemplateContextFactory;
46 import net.pterodactylus.util.template.TemplateParser;
47 import net.pterodactylus.util.web.Header;
48 import net.pterodactylus.util.web.Page;
49 import net.pterodactylus.util.web.Response;
54 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56 public class TemplateServlet extends HttpServlet {
59 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
61 private final Map<String, Page<ServletRequest>> pages = new HashMap<String, Page<ServletRequest>>();
71 public void init(ServletConfig config) throws ServletException {
73 core = (Core) config.getServletContext().getAttribute("core");
75 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
77 templateContextFactory.addFilter("html", new HtmlFilter());
79 templateContextFactory.addTemplateObject("core", core);
80 templateContextFactory.addTemplateObject("dataManager", core.getDataManager());
82 for (@SuppressWarnings("rawtypes")
83 Enumeration parameterNames = config.getInitParameterNames(); parameterNames.hasMoreElements();) {
84 String pageName = (String) parameterNames.nextElement();
85 Template template = getTemplate(pageName);
86 String pageClassName = config.getInitParameter(pageName);
88 Class<?> pageClass = Class.forName(pageClassName);
89 @SuppressWarnings("unchecked")
90 Page<ServletRequest> page = (Page<ServletRequest>) pageClass.getConstructor(Core.class, TemplateContextFactory.class, Template.class).newInstance(core, templateContextFactory, template);
91 pages.put(pageName, page);
92 } catch (ClassNotFoundException cnfe1) {
93 cnfe1.printStackTrace();
94 } catch (IllegalArgumentException iae1) {
95 iae1.printStackTrace();
96 } catch (SecurityException se1) {
97 se1.printStackTrace();
98 } catch (InstantiationException ie1) {
99 ie1.printStackTrace();
100 } catch (IllegalAccessException iae1) {
101 iae1.printStackTrace();
102 } catch (InvocationTargetException ite1) {
103 ite1.printStackTrace();
104 } catch (NoSuchMethodException nsme1) {
105 nsme1.printStackTrace();
114 protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
115 String path = request.getPathInfo();
116 if (path.startsWith("/")) {
117 path = path.substring(1);
119 Page<ServletRequest> page = pages.get(path);
121 ServletRequest servletRequest;
123 servletRequest = new ServletRequest(request);
124 } catch (URISyntaxException use1) {
125 throw new IOException("Could not create URI from " + request.getRequestURI(), use1);
127 ByteArrayOutputStream responseOutputBuffer = new ByteArrayOutputStream();
128 Response response = new Response(responseOutputBuffer);
129 response = page.handleRequest(servletRequest, response);
130 for (Header header : response.getHeaders()) {
131 for (String value : header) {
132 httpServletResponse.addHeader(header.getName(), value);
135 httpServletResponse.setContentType(response.getContentType());
136 httpServletResponse.setStatus(response.getStatusCode());
137 StreamCopier.copy(new ByteArrayInputStream(responseOutputBuffer.toByteArray()), httpServletResponse.getOutputStream());
146 private Template getTemplate(String pageName) {
147 InputStream templateInputStream = getClass().getResourceAsStream("/templates/" + pageName);
148 if (templateInputStream == null) {
151 Reader templateReader = null;
153 templateReader = new InputStreamReader(templateInputStream, "UTF-8");
154 return TemplateParser.parse(templateReader);
155 } catch (UnsupportedEncodingException uee1) {
156 /* can not happen. */
157 throw new RuntimeException("Could not parse templates!", uee1);
159 Closer.close(templateReader);
160 Closer.close(templateInputStream);