2 * DemosceneMusic - StaticServlet.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.core;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.net.URISyntaxException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import net.pterodactylus.demoscenemusic.page.ServletRequest;
31 import net.pterodactylus.util.io.StreamCopier;
32 import net.pterodactylus.util.web.Header;
33 import net.pterodactylus.util.web.Response;
34 import net.pterodactylus.util.web.StaticPage;
37 * Servlet that delivers static content using {@link StaticPage}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class StaticServlet extends HttpServlet {
43 /** The page for delivering static content. */
44 private StaticPage<ServletRequest> staticPage;
47 // GENERICSERVLET METHODS
54 public void init() throws ServletException {
55 staticPage = new StaticPage<ServletRequest>("/static/css/", "/static/css/", "text/css");
59 // HTTPSERVLET METHODS
66 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
67 ServletRequest servletRequest;
69 servletRequest = new ServletRequest(httpServletRequest, this);
70 } catch (URISyntaxException use1) {
71 throw new IOException("Could not create URI from " + httpServletRequest.getRequestURI(), use1);
73 ByteArrayOutputStream responseOutputBuffer = new ByteArrayOutputStream();
74 Response response = new Response(responseOutputBuffer);
75 response = staticPage.handleRequest(servletRequest, response);
76 for (Header header : response.getHeaders()) {
77 for (String value : header) {
78 httpServletResponse.addHeader(header.getName(), value);
81 httpServletResponse.setContentType(response.getContentType());
82 httpServletResponse.setStatus(response.getStatusCode());
83 StreamCopier.copy(new ByteArrayInputStream(responseOutputBuffer.toByteArray()), httpServletResponse.getOutputStream());