Add servlet that forwards all requests to pages and templates.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 21:47:21 +0000 (23:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 21:47:21 +0000 (23:47 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java [new file with mode: 0644]

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 (file)
index 0000000..a2ec825
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class TemplateServlet extends HttpServlet {
+
+       private Core core;
+       private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
+
+       private final Map<String, Page<ServletRequest>> pages = new HashMap<String, Page<ServletRequest>>();
+
+       //
+       // 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<ServletRequest> page = (Page<ServletRequest>) 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<ServletRequest> 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);
+               }
+       }
+
+}