<commit_before>package org.hcjf.io.net.http;

import org.hcjf.encoding.MimeType;
import org.hcjf.properties.SystemProperties;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * Created by javaito on 8/6/2016.
 */
public class FolderContext extends Context {

    private static final String START_CONTEXT = "^";
    private static final String END_CONTEXT = ".*";
    private static final String URI_FOLDER_SEPARATOR = "/";
    private static final String[] FORBIDDEN_CHARACTERS = {"..", "~"};
    private static final String FILE_EXTENSION_REGEX = "\\.(?=[^\\.]+$)";
    private static final String FOLDER_DEFAULT_HTML_DOCUMENT = "<!DOCTYPE html><html><head><title>%s</title><body>%s</body></html></head>";
    private static final String FOLDER_DEFAULT_HTML_BODY = "<table>%s</table>";
    private static final String FOLDER_DEFAULT_HTML_ROW = "<tr><th><a href=\"%s\">%s</a></th></tr>";

    private final Path baseFolder;
    private final String name;
    private final String[] names;

    public FolderContext(String name, Path baseFolder) {
        super(START_CONTEXT + URI_FOLDER_SEPARATOR + name + END_CONTEXT);

        if(baseFolder == null) {
            throw new NullPointerException("Folder location can't be null");
        }

        if(!baseFolder.toFile().exists()) {
            throw new IllegalArgumentException("The base folder doesn't exist");
        }

        this.name = name;
        this.baseFolder = baseFolder;
        this.names = name.split(URI_FOLDER_SEPARATOR);
    }

    /**
     * This method is called when there comes a http package addressed to this
     * context.
     *
     * @param request All the request information.
     * @return Return an object with all the response information.
     */
    @Override
    public HttpResponse onContext(HttpRequest request) {
        String[] elements = request.getContext().split(URI_FOLDER_SEPARATOR);
        for(String forbidden : FORBIDDEN_CHARACTERS) {
            for(String element : elements) {
                if (element.contains(forbidden)) {
                    throw new IllegalArgumentException("Forbidden path (" + forbidden + "):" + request.getContext());
                }
            }
        }

        //The first value is a empty value, and the second value is the base public context.
        Path path = baseFolder.toAbsolutePath();
        for(String element : elements) {
            if(!element.isEmpty() && Arrays.binarySearch(names, element) < 0) {
                path = path.resolve(element);
            }
        }
<commit_msg>Added default file path for the folder context<commit_after>package org.hcjf.io.net.http;

import org.hcjf.encoding.MimeType;
import org.hcjf.properties.SystemProperties;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * Created by javaito on 8/6/2016.
 */
public class FolderContext extends Context {

    private static final String START_CONTEXT = "^";
    private static final String END_CONTEXT = ".*";
    private static final String URI_FOLDER_SEPARATOR = "/";
    private static final String[] FORBIDDEN_CHARACTERS = {"..", "~"};
    private static final String FILE_EXTENSION_REGEX = "\\.(?=[^\\.]+$)";
    private static final String FOLDER_DEFAULT_HTML_DOCUMENT = "<!DOCTYPE html><html><head><title>%s</title><body>%s</body></html></head>";
    private static final String FOLDER_DEFAULT_HTML_BODY = "<table>%s</table>";
    private static final String FOLDER_DEFAULT_HTML_ROW = "<tr><th><a href=\"%s\">%s</a></th></tr>";

    private final Path baseFolder;
    private final String name;
    private final String defaultFile;
    private final String[] names;

    public FolderContext(String name, Path baseFolder, String defaultFile) {
        super(START_CONTEXT + URI_FOLDER_SEPARATOR + name + END_CONTEXT);

        if(baseFolder == null) {
            throw new NullPointerException("Folder location can't be null");
        }

        if(!baseFolder.toFile().exists()) {
            throw new IllegalArgumentException("The base folder doesn't exist");
        }

        this.name = name;
        this.baseFolder = baseFolder;
        this.defaultFile = defaultFile;
        this.names = name.split(URI_FOLDER_SEPARATOR);
    }

    public FolderContext(String name, Path baseFolder) {
        this(name, baseFolder, null);
    }

    /**
     * This method is called when there comes a http package addressed to this
     * context.
     *
     * @param request All the request information.
     * @return Return an object with all the response information.
     */
    @Override
    public HttpResponse onContext(HttpRequest request) {
        String[] elements = request.getContext().split(URI_FOLDER_SEPARATOR);
        for(String forbidden : FORBIDDEN_CHARACTERS) {
            for(String element : elements) {
                if (element.contains(forbidden)) {
                    throw new IllegalArgumentException("Forbidden path (" + forbidden + "):" + request.getContext());
                }
            }
        }

        //The first value is a empty value, and the second value is the base public context.
        Path path = baseFolder.toAbsolutePath();
        boolean emptyElement = true;
        for(String element : elements) {
            if(!element.isEmpty() && Arrays.binarySearch(names, element) < 0) {
                path = path.resolve(element);
                emptyElement = false;
            }
        }
        if(emptyElement && defaultFile != null) {
            path = path.resolve(defaultFile);
        }

{'fraction_non_alphanumeric': 0.11498257839721254, 'fraction_numerical': 0.0024390243902439024, 'mean_word_length': 2.774490466798159, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, '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': '2356749', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1588, 'n_words': 425}