com.qwazr.webapps.StaticFileServlet Maven / Gradle / Ivy
Show all versions of qwazr-webapps Show documentation
/*
* Copyright 2015-2017 Emmanuel Keller / QWAZR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qwazr.webapps;
import com.qwazr.server.ServerException;
import org.apache.commons.io.IOUtils;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class StaticFileServlet extends HttpServlet {
private final MimetypesFileTypeMap mimeTypeMap;
private final Path staticPath;
public StaticFileServlet(final MimetypesFileTypeMap mimeTypeMap, final Path staticPath) {
this.mimeTypeMap = mimeTypeMap;
if (staticPath == null)
throw new ServerException("The path is empty");
if (!Files.exists(staticPath))
throw new ServerException("Cannot initialize the static path: " + staticPath.toAbsolutePath() +
" - The path does not exists.");
this.staticPath = staticPath;
}
private File handleFile(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
final String contextPath = request.getContextPath();
final String servletPath = request.getServletPath();
final String pathInfo = request.getPathInfo();
final Path staticFile;
final String fullPath;
if (pathInfo == null) {
staticFile = staticPath;
fullPath = contextPath + servletPath;
} else {
staticFile = staticPath.resolve(pathInfo.startsWith("/") ? pathInfo.substring(1) : pathInfo);
fullPath = contextPath + servletPath + pathInfo;
}
if (Files.isDirectory(staticFile)) {
if (Files.exists(staticFile.resolve("index.html"))) {
final boolean slashEnd = fullPath.endsWith("/");
response.sendRedirect(fullPath + (slashEnd ? "index.html" : "/index.html"));
}
return null;
}
if (!Files.exists(staticFile) || !Files.isRegularFile(staticFile)) {
response.sendError(404, "File not found: " + fullPath);
return null;
}
return staticFile.toFile();
}
final static void head(final Long length, final String type, final Long lastModified,
final HttpServletResponse response) {
if (type != null)
response.setContentType(type);
if (length != null)
response.setContentLengthLong(length);
if (lastModified != null)
response.setDateHeader("Last-Modified", lastModified);
response.setHeader("Cache-Control", "max-age=86400");
response.setDateHeader("Expires", System.currentTimeMillis() + 86400 * 1000);
}
@Override
protected void doHead(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
final File staticFile = handleFile(request, response);
if (staticFile == null)
return;
final String type = mimeTypeMap.getContentType(staticFile);
head(staticFile.length(), type, staticFile.lastModified(), response);
}
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
final File staticFile = handleFile(request, response);
if (staticFile == null)
return;
final String type = mimeTypeMap.getContentType(staticFile);
head(staticFile.length(), type, staticFile.lastModified(), response);
try (final FileInputStream fis = new FileInputStream(staticFile)) {
final ServletOutputStream out = response.getOutputStream();
IOUtils.copy(fis, out);
out.flush();
}
}
}