<commit_before>/**
 * (c) 2012 uchicom
 */
package com.uchicom.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.SocketAddress;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.time.OffsetDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 *
 */
public class DefaultRouter implements Router {

	protected static Map<String, WebFile> map = new ConcurrentHashMap<String, WebFile>();
	protected static Map<String, File> errorMap = new ConcurrentHashMap<String, File>();

	protected static int htmlFileLength;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.uchicom.server.http.Router#init(java.io.File)
	 */
	@Override
	public void init(File baseFile) {
		File htmlFile = new File(baseFile, "html");
		htmlFileLength = htmlFile.toURI().getPath().length();
		createMap(htmlFile);
		createErrorMap(new File(baseFile, "error"));
		watch(baseFile);
	}

	@Override
	public boolean isModified(String filePath, TemporalAccessor ifModified) {
		WebFile file = map.get(filePath);
		return file.getLastModified().equals(ifModified);
	}

	/* (non-Javadoc)
	 * @see com.uchicom.server.http.Router#exists(java.lang.String)
	 */
	@Override
	public boolean exists(String filePath) {
		return map.containsKey(filePath);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.uchicom.server.http.Router#request(java.lang.String, java.util.Map,
	 * java.io.OutputStream)
	 */
	@Override
	public void request(String fileName,
			SocketAddress address,
			Map<String, String[]> paramMap,
			OutputStream outputStream) throws IOException {

		try (PrintStream ps = new PrintStream(outputStream);) {
			WebFile file = map.get(fileName);
			long len = file.length();
			ps.print("HTTP/1.1 200 OK \r\n");
			ps.print("Date: ");
			ps.print(Constants.formatter.format(OffsetDateTime.now()));
			ps.print("\r\n");
			ps.print("Server: uchicom-http\r\n");
			ps.print("Accept-Ranges: bytes\r\n");
			ps.print("Connection: close\r\n");
			if (len != 0) {
				ps.print("Content-Length: ");
				ps.print(String.valueOf(len));
				ps.print("\r\n");
			}
			ps.print("Last-Modified: ");
			ps.print(Constants.formatter.format(file.getLastModified()));
			ps.print("\r\n");
			ps.print("Content-Type: ");
			ps.print(file.getContentType());
			ps.print("\r\n");
			ps.print("Expires: 43200");
			ps.print("\r\n\r\n");
			byte[] bytes = null;
			if ("text/html".equals(file.getContentType())) {<commit_msg>キャッシュの日付、コミット漏れ<commit_after>/**
 * (c) 2012 uchicom
 */
package com.uchicom.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.SocketAddress;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.time.OffsetDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 *
 */
public class DefaultRouter implements Router {

	protected static Map<String, WebFile> map = new ConcurrentHashMap<String, WebFile>();
	protected static Map<String, File> errorMap = new ConcurrentHashMap<String, File>();

	protected static int htmlFileLength;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.uchicom.server.http.Router#init(java.io.File)
	 */
	@Override
	public void init(File baseFile) {
		File htmlFile = new File(baseFile, "html");
		htmlFileLength = htmlFile.toURI().getPath().length();
		createMap(htmlFile);
		createErrorMap(new File(baseFile, "error"));
		watch(baseFile);
	}

	@Override
	public boolean isModified(String filePath, TemporalAccessor ifModified) {
		WebFile file = map.get(filePath);
		return file.getLastModified().equals(ifModified);
	}

	/* (non-Javadoc)
	 * @see com.uchicom.server.http.Router#exists(java.lang.String)
	 */
	@Override
	public boolean exists(String filePath) {
		return map.containsKey(filePath);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.uchicom.server.http.Router#request(java.lang.String, java.util.Map,
	 * java.io.OutputStream)
	 */
	@Override
	public void request(String fileName,
			SocketAddress address,
			Map<String, String[]> paramMap,
			OutputStream outputStream) throws IOException {

		try (PrintStream ps = new PrintStream(outputStream);) {
			WebFile file = map.get(fileName);
			long len = file.length();
			ps.print("HTTP/1.1 200 OK \r\n");
			ps.print("Date: ");
			ps.print(Constants.formatter.format(OffsetDateTime.now()));
			ps.print("\r\n");
			ps.print("Server: uchicom-http\r\n");
			ps.print("Accept-Ranges: bytes\r\n");
			ps.print("Connection: close\r\n");
			if (len != 0) {
				ps.print("Content-Length: ");
				ps.print(String.valueOf(len));
				ps.print("\r\n");
			}
			ps.print("Last-Modified: ");
			ps.print(Constants.formatter.format(file.getLastModified()));
			ps.print("\r\n");
			ps.print("Content-Type: ");
			ps.print(file.getContentType());
			ps.print("\r\n");
			ps.print("Expires: ");
			ps.print(Constants.formatter.format(file.getLastModified().plusDays(1)));
			ps.print("\r\n\r\n");
			byte[] bytes = null;
			if ("text/html".equals(file.getContentType())) {
{'fraction_non_alphanumeric': 0.15931202223766505, 'fraction_numerical': 0.004517025712300208, 'mean_word_length': 4.390449438202247, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3132121', 'n_tokens_mistral': 2142, 'n_tokens_neox': 1942, 'n_words': 352}