<commit_before>package com.timgroup.tucker.info;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.timgroup.tucker.info.servlet.WebResponse;
import com.timgroup.tucker.info.status.StatusPage;
import com.timgroup.tucker.info.status.StatusPageGenerator;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;

public class ApplicationInformationHandler {

    private static final String UTF_8 = "UTF-8";

    private final Map<String, Handler> dispatch = new HashMap<String, Handler>();

    public ApplicationInformationHandler(StatusPageGenerator statusPage) {
        dispatch.put(null, new RedirectTo("/status"));
        dispatch.put("", new RedirectTo("/status"));
        dispatch.put("/health", new TextWriter("healthy")); // or "unwell"
        dispatch.put("/stoppable", new TextWriter("safe")); // or "ill"
        dispatch.put("/version", new ComponentWriter(statusPage.getVersionComponent()));
        dispatch.put("/status", new StatusPageWriter(statusPage));
        dispatch.put("/status-page.dtd", new ResourceWriter(StatusPageGenerator.DTD_FILENAME, "application/xml-dtd"));
        dispatch.put("/status-page.css", new ResourceWriter(StatusPageGenerator.CSS_FILENAME, "text/css"));
    }

    public void handle(String path, WebResponse response) throws IOException {
        if (dispatch.containsKey(path)) {
            dispatch.get(path).handle(response);
        } else {
            response.reject(HTTP_NOT_FOUND, "try asking for .../status");
        }
    }

    private interface Handler {
        void handle(WebResponse response) throws IOException;
    }

    private static final class RedirectTo implements Handler {
        private final String targetPath;

        public RedirectTo(String targetPath) {
            this.targetPath = targetPath;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            response.redirect(targetPath);
        }
    }

    private static final class StatusPageWriter implements Handler {
        private final StatusPageGenerator statusPage;

        public StatusPageWriter(StatusPageGenerator statusPage) {
            this.statusPage = statusPage;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            OutputStream out = response.respond("text/xml", UTF_8);
            StatusPage report = statusPage.getApplicationReport();
            report.render(new OutputStreamWriter(out, UTF_8));
        }
    }
    
    private static final class TextWriter implements Handler {
        private String text;

        public TextWriter(String text) {
            this.text = text;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            OutputStream out = response.respond("text/plain", UTF_8);
            out.write(text.getBytes(Charset.forName(UTF_8)));
            out.close();
        }
    }
    
    private static final class ComponentWriter implements Handler {
        private final Component component;

        public ComponentWriter(Component component) {
            this.component = component;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            final OutputStream out = response.respond("text/plain", UTF_8);
            final Report versionReport = component.getReport();
            final String versionString = versionReport.hasValue() ? versionReport.getValue().toString() : "";
            out.write(versionString.getBytes(Charset.forName(UTF_8)));
            out.close();
        }
    }
    
    private static final class ResourceWriter implements Handler {
        private final String resourceName;
        private final String contentType;

        public ResourceWriter(String resourceName, String contentType) {
            this.resourceName = resourceName;
            this.contentType = contentType;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            InputStream resource = StatusPageGenerator.class.getResourceAsStream(resourceName);
            if (resource == null) {
                response.reject(HTTP_NOT_FOUND, "could not find resource with name " + resourceName);
                return;
            }
            OutputStream output = response.respond(contentType, UTF_8);
            IOUtils.copy(resource, output);
        }
    }
}<commit_msg>close the stream<commit_after>package com.timgroup.tucker.info;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.timgroup.tucker.info.servlet.WebResponse;
import com.timgroup.tucker.info.status.StatusPage;
import com.timgroup.tucker.info.status.StatusPageGenerator;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;

public class ApplicationInformationHandler {

    private static final String UTF_8 = "UTF-8";

    private final Map<String, Handler> dispatch = new HashMap<String, Handler>();

    public ApplicationInformationHandler(StatusPageGenerator statusPage) {
        dispatch.put(null, new RedirectTo("/status"));
        dispatch.put("", new RedirectTo("/status"));
        dispatch.put("/health", new TextWriter("healthy")); // or "unwell"
        dispatch.put("/stoppable", new TextWriter("safe")); // or "ill"
        dispatch.put("/version", new ComponentWriter(statusPage.getVersionComponent()));
        dispatch.put("/status", new StatusPageWriter(statusPage));
        dispatch.put("/status-page.dtd", new ResourceWriter(StatusPageGenerator.DTD_FILENAME, "application/xml-dtd"));
        dispatch.put("/status-page.css", new ResourceWriter(StatusPageGenerator.CSS_FILENAME, "text/css"));
    }

    public void handle(String path, WebResponse response) throws IOException {
        if (dispatch.containsKey(path)) {
            dispatch.get(path).handle(response);
        } else {
            response.reject(HTTP_NOT_FOUND, "try asking for .../status");
        }
    }

    private interface Handler {
        void handle(WebResponse response) throws IOException;
    }

    private static final class RedirectTo implements Handler {
        private final String targetPath;

        public RedirectTo(String targetPath) {
            this.targetPath = targetPath;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            response.redirect(targetPath);
        }
    }

    private static final class StatusPageWriter implements Handler {
        private final StatusPageGenerator statusPage;

        public StatusPageWriter(StatusPageGenerator statusPage) {
            this.statusPage = statusPage;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            OutputStream out = response.respond("text/xml", UTF_8);
            StatusPage report = statusPage.getApplicationReport();
            report.render(new OutputStreamWriter(out, UTF_8));
        }
    }
    
    private static final class TextWriter implements Handler {
        private String text;

        public TextWriter(String text) {
            this.text = text;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            OutputStream out = response.respond("text/plain", UTF_8);
            out.write(text.getBytes(Charset.forName(UTF_8)));
            out.close();
        }
    }
    
    private static final class ComponentWriter implements Handler {
        private final Component component;

        public ComponentWriter(Component component) {
            this.component = component;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            final OutputStream out = response.respond("text/plain", UTF_8);
            final Report versionReport = component.getReport();
            final String versionString = versionReport.hasValue() ? versionReport.getValue().toString() : "";
            out.write(versionString.getBytes(Charset.forName(UTF_8)));
            out.close();
        }
    }
    
    private static final class ResourceWriter implements Handler {
        private final String resourceName;
        private final String contentType;

        public ResourceWriter(String resourceName, String contentType) {
            this.resourceName = resourceName;
            this.contentType = contentType;
        }
        
        @Override public void handle(WebResponse response) throws IOException {
            InputStream resource = StatusPageGenerator.class.getResourceAsStream(resourceName);
            if (resource == null) {
                response.reject(HTTP_NOT_FOUND, "could not find resource with name " + resourceName);
                return;
            }
            OutputStream out = response.respond(contentType, UTF_8);
            IOUtils.copy(resource, out);
            out.close();
        }
    }
}
{'fraction_non_alphanumeric': 0.0926682433885186, 'fraction_numerical': 0.0019350677273704579, 'mean_word_length': 2.722689075630252, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5219365', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2243, 'n_words': 559}