--- /dev/null
+/*
+ * 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;
+ }
+
+}