From: David ‘Bombe’ Roden Date: Wed, 18 Apr 2012 21:47:21 +0000 (+0200) Subject: Add servlet that forwards all requests to pages and templates. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6cbe089ff6c0ea477e8c8a2f4fe1d20c51f72e69;p=demoscenemusic.git Add servlet that forwards all requests to pages and templates. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java new file mode 100644 index 0000000..a2ec825 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java @@ -0,0 +1,153 @@ +/* + * DemosceneMusic - TemplateServlet.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.demoscenemusic.core; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.net.URISyntaxException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.pterodactylus.demoscenemusic.page.ServletRequest; +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContextFactory; +import net.pterodactylus.util.template.TemplateParser; +import net.pterodactylus.util.web.Header; +import net.pterodactylus.util.web.Page; +import net.pterodactylus.util.web.Response; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +public class TemplateServlet extends HttpServlet { + + private Core core; + private final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); + + private final Map> pages = new HashMap>(); + + // + // SERVLET METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + core = (Core) config.getServletContext().getAttribute("core"); + + templateContextFactory.addTemplateObject("core", core); + templateContextFactory.addTemplateObject("dataManager", core.getDataManager()); + + for (@SuppressWarnings("rawtypes") + Enumeration parameterNames = config.getInitParameterNames(); parameterNames.hasMoreElements();) { + String pageName = (String) parameterNames.nextElement(); + Template template = getTemplate(pageName); + String pageClassName = config.getInitParameter(pageName); + try { + Class pageClass = Class.forName(pageClassName); + @SuppressWarnings("unchecked") + Page page = (Page) pageClass.getConstructor(Core.class, TemplateContextFactory.class, Template.class).newInstance(core, templateContextFactory, template); + pages.put(pageName, page); + } catch (ClassNotFoundException cnfe1) { + cnfe1.printStackTrace(); + } catch (IllegalArgumentException iae1) { + iae1.printStackTrace(); + } catch (SecurityException se1) { + se1.printStackTrace(); + } catch (InstantiationException ie1) { + ie1.printStackTrace(); + } catch (IllegalAccessException iae1) { + iae1.printStackTrace(); + } catch (InvocationTargetException ite1) { + ite1.printStackTrace(); + } catch (NoSuchMethodException nsme1) { + nsme1.printStackTrace(); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { + String path = request.getPathInfo(); + if (path.startsWith("/")) { + path = path.substring(1); + } + Page page = pages.get(path); + if (page != null) { + ServletRequest servletRequest; + try { + servletRequest = new ServletRequest(request); + } catch (URISyntaxException use1) { + throw new IOException("Could not create URI from " + request.getRequestURI(), use1); + } + Response response = new Response(httpServletResponse.getOutputStream()); + response = page.handleRequest(servletRequest, response); + for (Header header : response.getHeaders()) { + for (String value : header) { + httpServletResponse.addHeader(header.getName(), value); + } + } + httpServletResponse.setContentType(response.getContentType()); + httpServletResponse.setStatus(response.getStatusCode()); + return; + } + } + + // + // PRIVATE METHODS + // + + private Template getTemplate(String pageName) { + InputStream templateInputStream = getClass().getResourceAsStream("/templates/" + pageName); + if (templateInputStream == null) { + return null; + } + Reader templateReader = null; + try { + templateReader = new InputStreamReader(templateInputStream, "UTF-8"); + return TemplateParser.parse(templateReader); + } catch (UnsupportedEncodingException uee1) { + /* can not happen. */ + throw new RuntimeException("Could not parse templates!", uee1); + } finally { + Closer.close(templateReader); + Closer.close(templateInputStream); + } + } + +}