2 * shortener - CSSPage.java - Copyright © 2010 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.sone.web.page;
20 import java.io.InputStream;
23 * {@link Page} implementation that delivers static files from the class path.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class StaticPage implements Page {
29 /** The prefix for {@link #getPath()}. */
30 private final String pathPrefix;
32 /** The path used as prefix when loading resources. */
33 private final String resourcePathPrefix;
35 /** The MIME type for the files this path contains. */
36 private final String mimeType;
39 * Creates a new CSS page.
42 * The prefix for {@link #getPath()}
43 * @param resourcePathPrefix
44 * The path prefix when loading resources
46 * The MIME type of the files this path contains
48 public StaticPage(String pathPrefix, String resourcePathPrefix, String mimeType) {
49 this.pathPrefix = pathPrefix;
50 this.resourcePathPrefix = resourcePathPrefix;
51 this.mimeType = mimeType;
58 public String getPath() {
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, "");
74 return new Response(200, "OK", mimeType, null, fileInputStream);