Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke.middleware;
import com.jetdrone.vertx.yoke.Middleware;
import com.jetdrone.vertx.yoke.MimeType;
import com.jetdrone.vertx.yoke.util.Utils;
import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.*;
import org.vertx.java.core.file.FileProps;
import org.vertx.java.core.file.FileSystem;
import org.vertx.java.core.json.JsonArray;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* # Static
*
* Static file server with the given ```root``` path. Optionaly will also generate index pages for directory listings.
*/
public class Static extends Middleware {
/**
* SimpleDateFormat to format date objects into ISO format.
*/
private final SimpleDateFormat ISODATE;
/**
* Cache for the HTML template of the directory listing page
*/
private final String directoryTemplate;
/**
* Root directory where to look files from
*/
private final String root;
/**
* Max age allowed for cache of resources
*/
private final long maxAge;
/**
* Allow directory listing
*/
private final boolean directoryListing;
/**
* Include hidden files (Hiden files are files start start with dot (.).
*/
private final boolean includeHidden;
/**
* Create a new Static File Server Middleware
*
*
* new Yoke(...)
* .use(new Static("webroot", 0, true, false));
*
*
* @param root the root location of the static files in the file system (relative to the main Verticle).
* @param maxAge cache-control max-age directive
* @param directoryListing generate index pages for directories
* @param includeHidden in the directory listing show dot files
*/
public Static(@NotNull String root, final long maxAge, final boolean directoryListing, final boolean includeHidden) {
// if the root is not empty it should end with / for convenience
if (!"".equals(root)) {
if (!root.endsWith("/")) {
root = root + "/";
}
}
this.root = root;
this.maxAge = maxAge;
this.includeHidden = includeHidden;
this.directoryListing = directoryListing;
this.directoryTemplate = Utils.readResourceToBuffer(getClass(), "directory.html").toString();
ISODATE = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
ISODATE.setTimeZone(TimeZone.getTimeZone("UTC"));
}
/**
* Create a new Static File Server Middleware that does not generate directory listings or hidden files
*
*
* new Yoke(...)
* .use(new Static("webroot", 0));
*
*
* @param root the root location of the static files in the file system (relative to the main Verticle).
* @param maxAge cache-control max-age directive
*/
public Static(@NotNull final String root, final long maxAge) {
this(root, maxAge, false, false);
}
/**
* Create a new Static File Server Middleware that does not generate directory listings or hidden files and files
* are cache for 1 full day
*
*
* new Yoke(...)
* .use(new Static("webroot"));
*
*
* @param root the root location of the static files in the file system (relative to the main Verticle).
*/
public Static(@NotNull final String root) {
this(root, 86400000, false, false);
}
/**
* Create all required header so content can be cache by Caching servers or Browsers
*
* @param request
* @param props
*/
private void writeHeaders(final YokeRequest request, final FileProps props) {
MultiMap headers = request.response().headers();
if (!headers.contains("etag")) {
headers.set("etag", "\"" + props.size() + "-" + props.lastModifiedTime().getTime() + "\"");
}
if (!headers.contains("date")) {
headers.set("date", ISODATE.format(new Date()));
}
if (!headers.contains("cache-control")) {
headers.set("cache-control", "public, max-age=" + maxAge / 1000);
}
if (!headers.contains("last-modified")) {
headers.set("last-modified", ISODATE.format(props.lastModifiedTime()));
}
}
/**
* Write a file into the response body
*
* @param request
* @param file
* @param props
*/
private void sendFile(final YokeRequest request, final String file, final FileProps props) {
// write content type
String contentType = MimeType.getMime(file);
String charset = MimeType.getCharset(contentType);
request.response().setContentType(contentType, charset);
request.response().putHeader("Content-Length", Long.toString(props.size()));
// head support
if ("HEAD".equals(request.method())) {
request.response().end();
} else {
request.response().sendFile(file);
}
}
/**
* Generate Directory listing
*
* @param request
* @param dir
* @param next
*/
private void sendDirectory(final YokeRequest request, final String dir, final Handler