Add servlet that delivers static content.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 04:44:59 +0000 (06:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 05:28:49 +0000 (07:28 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/StaticServlet.java [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml.template

diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/StaticServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/StaticServlet.java
new file mode 100644 (file)
index 0000000..83fba08
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * DemosceneMusic - StaticServlet.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.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+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.StreamCopier;
+import net.pterodactylus.util.web.Header;
+import net.pterodactylus.util.web.Response;
+import net.pterodactylus.util.web.StaticPage;
+
+/**
+ * Servlet that delivers static content using {@link StaticPage}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class StaticServlet extends HttpServlet {
+
+       /** The page for delivering static content. */
+       private StaticPage<ServletRequest> staticPage;
+
+       //
+       // GENERICSERVLET METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void init() throws ServletException {
+               staticPage = new StaticPage<ServletRequest>("/static/css/", "/static/css/", "text/css");
+       }
+
+       //
+       // HTTPSERVLET METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
+               ServletRequest servletRequest;
+               try {
+                       servletRequest = new ServletRequest(httpServletRequest, this);
+               } catch (URISyntaxException use1) {
+                       throw new IOException("Could not create URI from " + httpServletRequest.getRequestURI(), use1);
+               }
+               ByteArrayOutputStream responseOutputBuffer = new ByteArrayOutputStream();
+               Response response = new Response(responseOutputBuffer);
+               response = staticPage.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());
+               StreamCopier.copy(new ByteArrayInputStream(responseOutputBuffer.toByteArray()), httpServletResponse.getOutputStream());
+               return;
+       }
+
+}
index 98e0d4d..258819c 100644 (file)
        </resource-ref>
 
        <servlet>
+               <servlet-name>StaticServlet</servlet-name>
+               <servlet-class>net.pterodactylus.demoscenemusic.core.StaticServlet</servlet-class>
+       </servlet>
+
+       <servlet>
                <servlet-name>TemplateServlet</servlet-name>
                <servlet-class>net.pterodactylus.demoscenemusic.core.TemplateServlet</servlet-class>
                <init-param>
        </servlet>
 
        <servlet-mapping>
+               <servlet-name>StaticServlet</servlet-name>
+               <url-pattern>/static/*</url-pattern>
+       </servlet-mapping>
+
+       <servlet-mapping>
                <servlet-name>TemplateServlet</servlet-name>
                <url-pattern>/*</url-pattern>
        </servlet-mapping>