Add group comparator to sort filter.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / core / StaticServlet.java
1 /*
2  * DemosceneMusic - StaticServlet.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.core;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.net.URISyntaxException;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
35
36 /**
37  * Servlet that delivers static content using {@link StaticPage}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class StaticServlet extends HttpServlet {
42
43         /** The page for delivering static content. */
44         private StaticPage<ServletRequest> staticPage;
45
46         //
47         // GENERICSERVLET METHODS
48         //
49
50         /**
51          * {@inheritDoc}
52          */
53         @Override
54         public void init() throws ServletException {
55                 staticPage = new StaticPage<ServletRequest>("/static/css/", "/static/css/", "text/css");
56         }
57
58         //
59         // HTTPSERVLET METHODS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
67                 ServletRequest servletRequest;
68                 try {
69                         servletRequest = new ServletRequest(httpServletRequest, this);
70                 } catch (URISyntaxException use1) {
71                         throw new IOException("Could not create URI from " + httpServletRequest.getRequestURI(), use1);
72                 }
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);
79                         }
80                 }
81                 httpServletResponse.setContentType(response.getContentType());
82                 httpServletResponse.setStatus(response.getStatusCode());
83                 StreamCopier.copy(new ByteArrayInputStream(responseOutputBuffer.toByteArray()), httpServletResponse.getOutputStream());
84                 return;
85         }
86
87 }