<commit_before>package foam.nanos.http;

import com.google.api.client.util.IOUtils;

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.io.PrintWriter;
import java.util.HashMap;

public class FileServlet
    extends HttpServlet
{

  public static final String SERVLET_NAME = "static";

  protected static final HashMap<String, String> extLookup;
  protected static final String                  defaultExt = "application/octet-stream";

  static {
    extLookup = new HashMap<>();

    extLookup.put("js",    "application/javascript");
    extLookup.put("class", "application/java-vm");
    extLookup.put("xml",   "application/xml");

    extLookup.put("gif",   "image/gif");
    extLookup.put("png",   "image/png");

    extLookup.put("java",  "text/x-java-source");
    extLookup.put("csv",   "text/csv");
    extLookup.put("txt",   "text/plain");
    extLookup.put("html",  "text/html");
  }

  private void fileNotFoundError(HttpServletResponse resp, String file) {
    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    resp.setContentType("application/json");

    try {
      PrintWriter pw = resp.getWriter();
      pw.write(
          "{\"error\": \"File not found\"," +
              "\"filename\": \""+file+"\"}");
    } catch (IOException ignore) {}
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    String   pathInfo = req.getPathInfo();
    String   filePath = pathInfo.substring(SERVLET_NAME.length() + 2);

    try {
      File   srcFile = new File(filePath.isEmpty() ? "./" : filePath);
      String path    = srcFile.getAbsolutePath();
      String cwd     = System.getProperty("user.dir");

      if ( srcFile.isDirectory() && srcFile.canRead() && cwd.equals(path.substring(0, cwd.length())) ) {
        resp.setContentType(extLookup.get("html"));
        PrintWriter pw = resp.getWriter();
        pw.write(
            "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<body>\n" +
                "<ul style=\"list-style-type:disc\">");

        File[] files = srcFile.listFiles();
        if ( files != null && files.length > 0 ) {
          for ( File file : files ) {
            pw.write("<li>" + "<a href=\"/static/" + filePath + (filePath.isEmpty() ? "" : "/") + file.getName() + "\"?>"
                + file.getName()
                + "</a></li>");
          }
        }

        pw.write("</ul>  \n" +
            "</body>\n" +
            "</html>");

      } else if ( srcFile.isFile() && srcFile.canRead() && cwd.equals(path.substring(0, cwd.length())) ) {
        String tokens[] = srcFile.getName().split("\\.");

        String extension = tokens.length > 0 ? tokens[tokens.length-1 ] : "unknown";
        String ext = extLookup.get(extension);
        resp.setContentType(ext != null ? ext : defaultExt);
        resp.setHeader("Content-Disposition", "filename=\"" + srcFile.getName() + "\"");
        FileInputStream fis = new FileInputStream(srcFile);
        IOUtils.copy(fis, resp.getOutputStream());
      } else {
        fileNotFoundError(resp, filePath);
      }
    } catch (StringIndexOutOfBoundsException | IOException e) {
      fileNotFoundError(resp, filePath);
    }
  }
}<commit_msg>Formatting.<commit_after>package foam.nanos.http;

import com.google.api.client.util.IOUtils;
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.io.PrintWriter;
import java.util.HashMap;

public class FileServlet
    extends HttpServlet
{
  public    static final String                  SERVLET_NAME = "static";
  protected static final String                  DEFAULT_EXT  = "application/octet-stream";
  protected static final HashMap<String, String> EXTS         = new HashMap();

  static {
    EXTS.put("js",    "application/javascript");
    EXTS.put("class", "application/java-vm");
    EXTS.put("xml",   "application/xml");

    EXTS.put("gif",   "image/gif");
    EXTS.put("png",   "image/png");

    EXTS.put("java",  "text/x-java-source");
    EXTS.put("csv",   "text/csv");
    EXTS.put("txt",   "text/plain");
    EXTS.put("html",  "text/html");
  }

  private void fileNotFoundError(HttpServletResponse resp, String file) {
    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    resp.setContentType("application/json");

    try {
      resp.getWriter().write("{\"error\": \"File not found\"," + "\"filename\": \"" + file + "\"}");
    } catch (IOException ignore) {}
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    String pathInfo = req.getPathInfo();
    String filePath = pathInfo.substring(SERVLET_NAME.length() + 2);

    try {
      File   srcFile = new File(filePath.isEmpty() ? "./" : filePath);
      String path    = srcFile.getAbsolutePath();
      String cwd     = System.getProperty("user.dir");

      if ( srcFile.isDirectory() && srcFile.canRead() && cwd.equals(path.substring(0, cwd.length())) ) {
        resp.setContentType(EXTS.get("html"));
        PrintWriter pw = resp.getWriter();
        pw.write(
            "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<body>\n" +
                "<ul style=\"list-style-type:disc\">");

        File[] files = srcFile.listFiles();

        if ( files != null && files.length > 0 ) {
          for ( File file : files ) {
            pw.write("<li>" + "<a href=\"/static/" + filePath + (filePath.isEmpty() ? "" : "/") + file.getName() + "\"?>"
                + file.getName()
                + "</a></li>");
          }
        }

        pw.write("</ul>  \n" +
            "</body>\n" +
            "</html>");

      } else if ( srcFile.isFile() && srcFile.canRead() && cwd.equals(path.substring(0, cwd.length())) ) {
        String          tokens[]  = srcFile.getName().split("\\.");
        String          extension = tokens.length > 0 ? tokens[tokens.length-1 ] : "unknown";
        String          ext       = EXTS.get(extension);
        FileInputStream fis       = new FileInputStream(srcFile);

        resp.setContentType(ext != null ? ext : DEFAULT_EXT);
        resp.setHeader("Content-Disposition", "filename=\"" + srcFile.getName() + "\"");
        IOUtils.copy(fis, resp.getOutputStream());
      } else {
        fileNotFoundError(resp, filePath);
      }
    } catch (StringIndexOutOfBoundsException | IOException e) {
      fileNotFoundError(resp, filePath);
    }
  }
}
{'fraction_non_alphanumeric': 0.16435081428357987, 'fraction_numerical': 0.0017929179740026895, 'mean_word_length': 2.5606382978723405, 'pattern_counts': {'":': 4, '<': 28, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2348184', 'n_tokens_mistral': 2035, 'n_tokens_neox': 2000, 'n_words': 381}