Fix HTML encoding of ampersand.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / page / StaticPage.java
1 /*
2  * Sone - StaticPage.java - Copyright © 2010 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.sone.web.page;
19
20 import java.io.InputStream;
21
22 /**
23  * {@link Page} implementation that delivers static files from the class path.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class StaticPage implements Page {
28
29         /** The prefix for {@link #getPath()}. */
30         private final String pathPrefix;
31
32         /** The path used as prefix when loading resources. */
33         private final String resourcePathPrefix;
34
35         /** The MIME type for the files this path contains. */
36         private final String mimeType;
37
38         /**
39          * Creates a new CSS page.
40          *
41          * @param pathPrefix
42          *            The prefix for {@link #getPath()}
43          * @param resourcePathPrefix
44          *            The path prefix when loading resources
45          * @param mimeType
46          *            The MIME type of the files this path contains
47          */
48         public StaticPage(String pathPrefix, String resourcePathPrefix, String mimeType) {
49                 this.pathPrefix = pathPrefix;
50                 this.resourcePathPrefix = resourcePathPrefix;
51                 this.mimeType = mimeType;
52         }
53
54         /**
55          * {@inheritDoc}
56          */
57         @Override
58         public String getPath() {
59                 return pathPrefix;
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         public Response handleRequest(Request request) {
67                 String path = request.getUri().getPath();
68                 int lastSlash = path.lastIndexOf('/');
69                 String filename = path.substring(lastSlash + 1);
70                 InputStream fileInputStream = getClass().getResourceAsStream(resourcePathPrefix + filename);
71                 if (fileInputStream == null) {
72                         return new Response(404, "Not found.", null, "");
73                 }
74                 return new Response(200, "OK", mimeType, null, fileInputStream);
75         }
76
77 }