From a2abb97fd8854208489d73e49bb8a5fc85735c05 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Jul 2012 06:44:59 +0200 Subject: [PATCH] Add servlet that delivers static content. --- .../demoscenemusic/core/StaticServlet.java | 87 ++++++++++++++++++++++ src/main/webapp/WEB-INF/web.xml.template | 10 +++ 2 files changed, 97 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/core/StaticServlet.java 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 index 0000000..83fba08 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/StaticServlet.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class StaticServlet extends HttpServlet { + + /** The page for delivering static content. */ + private StaticPage staticPage; + + // + // GENERICSERVLET METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public void init() throws ServletException { + staticPage = new StaticPage("/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; + } + +} diff --git a/src/main/webapp/WEB-INF/web.xml.template b/src/main/webapp/WEB-INF/web.xml.template index 98e0d4d..258819c 100644 --- a/src/main/webapp/WEB-INF/web.xml.template +++ b/src/main/webapp/WEB-INF/web.xml.template @@ -29,6 +29,11 @@ + StaticServlet + net.pterodactylus.demoscenemusic.core.StaticServlet + + + TemplateServlet net.pterodactylus.demoscenemusic.core.TemplateServlet @@ -71,6 +76,11 @@ + StaticServlet + /static/* + + + TemplateServlet /* -- 2.7.4